Skip to content

Re-implement :has(> by marking first selector isRoot #148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions esquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function inPath(node, ancestor, path, fromPathIndex) {
* @param {?SelectorAST} selector
* @param {external:AST[]} [ancestry=[]]
* @param {ESQueryOptions} [options]
* @returns {void}
* @returns {boolean}
*/

/**
Expand All @@ -91,7 +91,7 @@ const MATCHER_CACHE = typeof WeakMap === 'function' ? new WeakMap : null;
* @param {?SelectorAST} selector
* @returns {SelectorMatcher}
*/
function getMatcher(selector) {
function getMatcherOrig(selector) {
if (selector == null) {
return () => true;
}
Expand All @@ -108,6 +108,18 @@ function getMatcher(selector) {

return generateMatcher(selector);
}
/**
* @param {?SelectorAST} selector
* @returns {SelectorMatcher}
*/
function getMatcher(selector) {
const matcher = getMatcherOrig(selector);
return (node, ancestry, options) => {
// `has` traversal pushes its parent node into ancestry; so expect 1 ancestor even at the root
const satisfiesIsRoot = selector && selector.isRoot ? ancestry.length < 2 : true;
return matcher(node, ancestry, options) && satisfiesIsRoot;
};
}

/**
* Create a matcher function for `selector`,
Expand All @@ -127,11 +139,6 @@ function generateMatcher(selector) {
};
}

case 'exactNode':
return (node, ancestry) => {
return ancestry.length === 0;
};

case 'field': {
const path = selector.name.split('.');
return (node, ancestry) => {
Expand Down Expand Up @@ -553,7 +560,7 @@ function subjects(selector, ancestor) {
* @param {?SelectorAST} selector
* @param {TraverseVisitor} visitor
* @param {ESQueryOptions} [options]
* @returns {external:AST[]}
* @returns {void}
*/
function traverse(ast, selector, visitor, options) {
if (!selector) { return; }
Expand Down
16 changes: 3 additions & 13 deletions grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,10 @@ binaryOp
/ _ "+" _ { return 'adjacent'; }
/ " " _ { return 'descendant'; }

hasSelectors = s:hasSelector ss:(_ "," _ hasSelector)* {
return [s].concat(ss.map(function (s) { return s[3]; }));
}

selectors = s:selector ss:(_ "," _ selector)* {
return [s].concat(ss.map(function (s) { return s[3]; }));
}


hasSelector
= op:binaryOp? s:selector {
if (!op) return s;
return { type: op, left: { type: 'exactNode' }, right: s };
}

selector
= a:sequence ops:(binaryOp sequence)* {
return ops.reduce(function (memo, rhs) {
Expand All @@ -53,9 +42,10 @@ selector
}

sequence
= subject:"!"? as:atom+ {
= subject:"!"? isRoot:(_">"_)? as:atom+ {
const b = as.length === 1 ? as[0] : { type: 'compound', selectors: as };
if(subject) b.subject = true;
if(isRoot) as[0].isRoot = true;
return b;
}

Expand Down Expand Up @@ -107,7 +97,7 @@ field = "." i:identifierName is:("." identifierName)* {

negation = ":not(" _ ss:selectors _ ")" { return { type: 'not', selectors: ss }; }
matches = ":matches(" _ ss:selectors _ ")" { return { type: 'matches', selectors: ss }; }
has = ":has(" _ ss:hasSelectors _ ")" { return { type: 'has', selectors: ss }; }
has = ":has(" _ ss:selectors _ ")" { return { type: 'has', selectors: ss }; }

firstChild = ":first-child" { return nth(1); }
lastChild = ":last-child" { return nthLast(1); }
Expand Down
Loading