Skip to content
Merged
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
16 changes: 16 additions & 0 deletions esquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@
}
return true;

case 'has':
var a, collector = [];
for (i = 0, l = selector.selectors.length; i < l; ++i) {
a = [];
estraverse.traverse(node, {
enter: function (node, parent) {
if (parent != null) { a.unshift(parent); }
if (matches(node, selector.selectors[i], a)) {
collector.push(node);
}
},
leave: function () { a.shift(); }
});
}
return collector.length !== 0;

case 'child':
if (matches(node, selector.right, ancestry)) {
return matches(ancestry[0], selector.left, ancestry.slice(1));
Expand Down
4 changes: 3 additions & 1 deletion grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ sequence

atom
= wildcard / identifier / attr / field / negation / matches
/ firstChild / lastChild / nthChild / nthLastChild / class
/ has / firstChild / lastChild / nthChild / nthLastChild / class

wildcard = a:"*" { return { type: 'wildcard', value: a }; }
identifier = "#"? i:identifierName { return { type: 'identifier', value: i }; }
Expand Down Expand Up @@ -88,12 +88,14 @@ 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:selectors _ ")" { return { type: 'has', selectors: ss }; }

firstChild = ":first-child" { return nth(1); }
lastChild = ":last-child" { return nthLast(1); }
nthChild = ":nth-child(" _ n:[0-9]+ _ ")" { return nth(parseInt(n.join(''), 10)); }
nthLastChild = ":nth-last-child(" _ n:[0-9]+ _ ")" { return nthLast(parseInt(n.join(''), 10)); }


class = ":" c:("statement"i / "expression"i / "declaration"i / "function"i / "pattern"i) {
return { type: 'class', name: c };
}
88 changes: 83 additions & 5 deletions parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions tests/queryHas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

define([
"esquery",
"jstestr/assert",
"jstestr/test",
"./fixtures/conditional"
], function (esquery, assert, test, conditional) {

test.defineSuite("Parent selector query", {

"conditional": function () {
var matches = esquery(conditional, 'ExpressionStatement:has([name="foo"][type="Identifier"])');
assert.isEqual(1, matches.length);
},

"one of": function () {
var matches = esquery(conditional, 'IfStatement:has(LogicalExpression [name="foo"], LogicalExpression [name="x"])');
assert.isEqual(1, matches.length);
},

"chaining": function () {
var matches = esquery(conditional, 'BinaryExpression:has(Identifier[name="x"]):has(Literal[value="test"])');
assert.isEqual(1, matches.length);
},

"nesting": function () {
var matches = esquery(conditional, 'Program:has(IfStatement:has(Literal[value=true], Literal[value=false]))');
assert.isEqual(1, matches.length);
},

"non-matching": function () {
var matches = esquery(conditional, ':has([value="impossible"])');
assert.isEqual(0, matches.length);
}

});
});