MCPcopy Create free account
hub / github.com/estools/esquery / adjacent

Function adjacent

esquery.js:475–493  ·  view source on GitHub ↗

* Determines if the given node has an adjacent sibling that matches * the given selector matcher. * @param {external:AST} node * @param {SelectorMatcher} matcher * @param {external:AST[]} ancestry * @param {Side} side * @param {ESQueryOptions|undefined} options * @returns {boolean}

(node, matcher, ancestry, side, options)

Source from the content-addressed store, hash-verified

473 * @returns {boolean}
474 */
475function adjacent(node, matcher, ancestry, side, options) {
476 const [parent] = ancestry;
477 if (!parent) { return false; }
478 const keys = getVisitorKeys(parent, options);
479 for (let i = 0; i < keys.length; ++i) {
480 const listProp = parent[keys[i]];
481 if (Array.isArray(listProp)) {
482 const idx = listProp.indexOf(node);
483 if (idx < 0) { continue; }
484 if (side === LEFT_SIDE && idx > 0 && isNode(listProp[idx - 1], options) && matcher(listProp[idx - 1], ancestry, options)) {
485 return true;
486 }
487 if (side === RIGHT_SIDE && idx < listProp.length - 1 && isNode(listProp[idx + 1], options) && matcher(listProp[idx + 1], ancestry, options)) {
488 return true;
489 }
490 }
491 }
492 return false;
493}
494
495/**
496 * Determines if the given node is the `nth` child.

Callers 1

generateMatcherFunction · 0.85

Calls 2

getVisitorKeysFunction · 0.85
isNodeFunction · 0.85

Tested by

no test coverage detected