Skip to content
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

adjacent returns false if keys are undefined #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nsfmc
Copy link

@nsfmc nsfmc commented May 12, 2017

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.

it's possible for `VisitorKeys` for a parent type to return `undefined` causing the subsequent iteration to fail. this bails early when that happens.
@michaelficarra
Copy link
Member

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.

@StrongBearCeo
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants