Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

168
Vistas
Matching relative to the queried node with esquery

I want to be able to query an element that is the direct descendant of the node I am querying against, and can't figure out if it is possible - it's not in the examples, and the peg.js grammar is just impenetrable enough to foil my attempts at reading it.

For example, given this Javascript snippet:

const assignmentOne = 'foo'
const assignmentTwo = 'baz'

And thus this (abbreviated) AST:

[{
  type: 'VariableDeclaration',
  declarations: [{
    type: 'VariableDeclarator',
    id: { type: 'Identifier', name: 'assignmentOne' },
    init: { type: 'Literal', value: 'foo' }
  }],
  kind: 'const'
}, {
  type: 'VariableDeclaration',
  declarations: [{
    type: 'VariableDeclarator',
    id: { type: 'Identifier', name: 'assignmentTwo' },
    init: { type: 'Literal', value: 'baz' }
  }],
  kind: 'const'
}]

Given node as the first VariableDeclaration node, I want to be able to do something like:

esquery.query(node, '~ VariableDeclaration')

To find siblings of node, but that gives me an error and I couldn't find another way to accomplish this. Here's a minimal test case, I tried to make it a snippet but I couldn't get esparse/esprima working in the browser:

const esprima = require('esprima')
const esquery = require('esquery')

var ast = esprima.parse(`
const example1 = 'foo'
const example2 = 'bar'
`)

const node = esquery.query(ast, 'VariableDeclaration')
const sibling = esquery.query(node, '~ VariableDeclaration')

Note my actual use case is much more complicated than this, so I'm not looking for ways to accomplish this specific query - I want to be able to match siblings, descendants, etc of the node I am querying against in general.

I've tried ., $, #, and & to refer to the current node, to no avail.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda