esquery icon indicating copy to clipboard operation
esquery copied to clipboard

adjacent returns false if keys are undefined

Open nsfmc opened this issue 8 years ago • 2 comments

this is a weird bug that i noticed while writing an eslint rule with the following selector

ImportDeclaration + :not(ImportDeclaration)

for certain types of file using babel-eslint, this would throw Cannot read property 'length' of undefined

``` Cannot read property 'length' of undefined TypeError: Cannot read property 'length' of undefined at adjacent (/Users/marcos/SpacedOut/sense/ui_server/node_modules/esquery/esquery.js:214:33) at Function.matches (/Users/marcos/SpacedOut/sense/ui_server/node_modules/esquery/esquery.js:135:25) at NodeEventGenerator.applySelector (/Users/marcos/SpacedOut/sense/ui_server/node_modules/eslint/lib/util/node-event-generator.js:264:21) at NodeEventGenerator.applySelectors (/Users/marcos/SpacedOut/sense/ui_server/node_modules/eslint/lib/util/node-event-generator.js:292:22) at NodeEventGenerator.enterNode (/Users/marcos/SpacedOut/sense/ui_server/node_modules/eslint/lib/util/node-event-generator.js:308:14) at CodePathAnalyzer.enterNode (/Users/marcos/SpacedOut/sense/ui_server/node_modules/eslint/lib/code-path-analysis/code-path-analyzer.js:602:23) at CommentEventGenerator.enterNode (/Users/marcos/SpacedOut/sense/ui_server/node_modules/eslint/lib/util/comment-event-generator.js:98:23) at Traverser.enter (/Users/marcos/SpacedOut/sense/ui_server/node_modules/eslint/lib/eslint.js:929:36) at Traverser.__execute (/Users/marcos/SpacedOut/sense/ui_server/node_modules/estraverse/estraverse.js:397:31) at Traverser.traverse (/Users/marcos/SpacedOut/sense/ui_server/node_modules/estraverse/estraverse.js:501:28) ```

adding this fixes my specific bug but i have to suspect that it's possible that other selectors are vulnerable to this problem. i can amend this pr to catch those cases as well if you think it's a worthwhile change.

nsfmc avatar May 12 '17 02:05 nsfmc

It seems the version of estraverse we're using just doesn't know about one of the nodes you're running esquery against. The proper solution here would be to add that node type to estraverse's VisitorKeys or possibly even just bump the dependency version if it's already been added.

michaelficarra avatar May 12 '17 06:05 michaelficarra

I got the similar issue for this selector

FunctionDeclaration > BlockStatement > :first-child

TypeError: Cannot read property 'length' of undefined at nthChild (...esquery.js:236:34) `

Is there any update?

StrongBearCeo avatar Nov 07 '17 05:11 StrongBearCeo