-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
fix: Cannot read property 'range' of null error with indent #11858
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know we're still evaluating whether we should land this or not, but we are unable to accept PRs that change behavior without corresponding tests. Do you mind adding some?
Using @typescript-eslint/parser
in ASTExplorer, it looks like this should be parsed as a TSAsExpression
node. Do you know why this code would affect that?
Edit: To clarify, looking at the spec, it doesn't look like a BinaryExpression
without an operator is a valid node, and I want to make sure we're making changes in the right place!
@@ -1075,7 +1075,9 @@ module.exports = { | |||
* var foo = bar && | |||
* baz; | |||
*/ | |||
|
|||
if (operator === null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is a bit confusing now because it looks like it's referring to this line.
Hello @kaicataldo , Thanks for the time you took reviewing this.
I completely understand, I tried adding some, but didn't how to configure the parser to be
I tried configuring the live version of ASTExplorer, but couldn't manage to configure prettier to use typescript as a parser, although it seems to be an accepted value. So I dug a bit more: // prettier + eslint
{ type: 'BinaryExpression',
operator: 'as',
left:
{ type: 'Literal',
raw: '\'hello\'',
value: 'hello',
range: [ 17, 24 ],
loc: { start: [Object], end: [Object] },
parent:
{ type: 'TSAsExpression',
expression: [Circular],
typeAnnotation: [Object],
range: [Array],
loc: [Object],
parent: [Object] } },
right:
{ type: 'TSStringKeyword',
range: [ 28, 34 ],
loc: { start: [Object], end: [Object] },
parent:
{ type: 'TSAsExpression',
expression: [Object],
typeAnnotation: [Circular],
range: [Array],
loc: [Object],
parent: [Object] } },
parent:
{ type: 'VariableDeclarator',
id:
{ type: 'Identifier',
name: 'a',
range: [Array],
loc: [Object],
parent: [Circular] },
init:
{ type: 'TSAsExpression',
expression: [Object],
typeAnnotation: [Object],
range: [Array],
loc: [Object],
parent: [Circular] },
range: [ 13, 34 ],
loc: { start: [Object], end: [Object] },
parent:
{ type: 'VariableDeclaration',
declarations: [Array],
kind: 'const',
range: [Array],
loc: [Object],
parent: [Object] } },
range: [ 17, 34 ],
loc:
{ start: { line: 1, column: 17 }, end: { line: 1, column: 34 } } } Here's the AST I get when running prettier-eslint // prettier-eslint
{ type: 'BinaryExpression',
operator: 'as',
left:
{ type: 'Literal',
range: [ 17, 24 ],
loc: { start: [Object], end: [Object] },
raw: '\'hello\'',
value: 'hello',
parent:
{ type: 'TSAsExpression',
range: [Array],
loc: [Object],
transformFlags: undefined,
expression: [Circular],
typeAnnotation: [Object],
parent: [Object] } },
right:
{ type: 'TSTypeAnnotation',
loc: { start: [Object], end: [Object] },
range: [ 26, 34 ],
typeAnnotation:
{ type: 'TSStringKeyword',
range: [Array],
loc: [Object],
parent: [Circular] },
parent:
{ type: 'TSAsExpression',
range: [Array],
loc: [Object],
transformFlags: undefined,
expression: [Object],
typeAnnotation: [Circular],
parent: [Object] } },
parent:
{ type: 'VariableDeclarator',
range: [ 13, 34 ],
loc: { start: [Object], end: [Object] },
id:
{ type: 'Identifier',
range: [Array],
loc: [Object],
name: 'a',
parent: [Circular] },
init:
{ type: 'TSAsExpression',
range: [Array],
loc: [Object],
transformFlags: undefined,
expression: [Object],
typeAnnotation: [Object],
parent: [Circular] },
parent:
{ type: 'VariableDeclaration',
range: [Array],
loc: [Object],
declarations: [Array],
kind: 'const',
parent: [Object] } },
range: [ 17, 34 ],
loc:
{ start: { line: 1, column: 17 }, end: { line: 1, column: 34 } } } The difference seems to be how the right part is interpreted. I couldn't figure out exactly why, or what was wrong. But while I was adding It intrigued me since: So it seems In addition, they apparently fixed the brutal parser overriding in I tried it, and 🎉 : no more errors! So I guess the problem came from |
Thanks for the detailed response. All that information is super helpful! Since ESLint allows for custom parsers (and those parsers can do anything), we can’t guarantee that all core rules will work with them. I believe we have made changes in the past to prevent a rule from crashing with Thanks for digging into this! |
Alright, I'm closing this now :) Thanks again @kaicataldo |
What is the purpose of this pull request? (put an "X" next to item)
Tell us about your environment
What parser (default, Babel-ESLint, etc.) are you using?
@typescript-eslint/parser
Please show your full configuration:
Configuration
What did you do? Please include the actual source code causing the issue.
Run
prettier-eslint --fix
on a file containing a TypeScript casting using theas
keyword:Very interestingly, manually running Prettier and then ESLint does not trigger the bug, I couldn't understand exactly why.
What did you expect to happen?
I expected ESLint to fix any errors I might have, after the file has been prettified.
What actually happened? Please include the actual, raw output from ESLint.
ESLint crashed with the following error:
The issue has already been reported in different places, but it's hard to fully understand what causes the error. For example, babel/babel-eslint#530 (comment)
This apparently has already been fixed for some other rules (eg. #10938)
What changes did you make? (Give an overview)
When parsing an expression containing
as
, it's identifed as aBinaryExpression
, but theoperator
isnull
. If that's the case, shortcut the process as it will lead to errors.Is there anything you'd like reviewers to focus on?