Skip to content

Commit

Permalink
Merge pull request tree-sitter#108 from joaomoreno/joao/fix-optional-…
Browse files Browse the repository at this point in the history
…nodes-with-eq-predicate

Fix: Query fails to match optional node when using #eq? predicate
  • Loading branch information
maxbrunsfeld committed Apr 29, 2022
2 parents aefa0dd + e3ab26c commit 65fb733
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ Query.prototype._init = function() {
if (c.name === captureName1) node1 = c.node;
if (c.name === captureName2) node2 = c.node;
}
if (node1 === undefined || node2 === undefined) return true;
return (node1.text === node2.text) === isPositive;
});
} else {
Expand All @@ -449,7 +450,7 @@ Query.prototype._init = function() {
return (c.node.text === stringValue) === isPositive;
};
}
return false;
return true;
});
}
break;
Expand All @@ -470,7 +471,7 @@ Query.prototype._init = function() {
for (const c of captures) {
if (c.name === captureName) return regex.test(c.node.text);
}
return false;
return true;
});
break;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"mocha": "^8.3.1",
"prebuild": "^10.0.1",
"superstring": "^2.4.2",
"tree-sitter-javascript": "git://github.com/tree-sitter/tree-sitter-javascript.git#master"
"tree-sitter-javascript": "https://github.com/tree-sitter/tree-sitter-javascript.git#master"
},
"scripts": {
"install": "prebuild-install || node-gyp rebuild",
Expand Down
19 changes: 19 additions & 0 deletions test/query_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,25 @@ describe("Query", () => {
{ pattern: 0, captures: [{ name: "element", text: "g" }] },
]);
});

it("finds optional nodes even when using #eq? predicate", () => {
const tree = parser.parse(`
{ one: true };
{ one: true, two: true };
`);
const query = new Query(JavaScript, `
(
(object (pair key: (property_identifier) @a) (pair key: (property_identifier) @b)?)
(#eq? @a one)
(#eq? @b two)
)
`);
const matches = query.matches(tree.rootNode);
assert.deepEqual(formatMatches(tree, matches), [
{ pattern: 0, captures: [{ name: 'a', text: 'one' }] },
{ pattern: 0, captures: [{ name: 'a', text: 'one' }, { name: 'b', text: 'two' }] },
]);
});
});

describe(".captures", () => {
Expand Down

0 comments on commit 65fb733

Please sign in to comment.