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
node.range in TemplateElement doesn't match code #13360
Comments
Thank you for your report. Like the range of string literal nodes contain quotes, the range of |
Sorry, I missed your point.
The The range of the If you want to get the whole text of the node, use |
@mysticatea thanks for your answer. That makes lots of sense, looks like I misunderstood the purpose of I'll close this issue now. |
Tell us about your environment
Node version: v12.16.1
npm version: v6.13.4
Local ESLint version: v7.1.0 (Currently used)
Global ESLint version: Not found
What parser (default, Babel-ESLint, etc.) are you using?
Default
Please show your full code:
Repo with reproduction in https://github.com/scinos/eslint-v7-template-literal-range/blob/master/index.js
Example of a broken rule:
What did you expect to happen?
When running the above rule with
node index.js
, I'd expect to not throw, and the auto-fixer to not modify the code (fixer.replaceTextRange(q.range, q.value.raw)
should be equivalent to a noop).What actually happened?
It removes the backticks from the output, so
console.log(`test`);
becomesconsole.log(test);
which is not valid. Before Eslint v7 I was using[q.start, q.end]
and it did work.A few examples form AST Explorer to understand what is going on (https://astexplorer.net/#/gist/0da4480b5208cd870986ea4d35555537/aae58b9c2e76361ed021265d3c04140edbb793f4)
espree
theTemplateElement
hasrange:[12,18], start:13, end: 17
esprima
theTemplateElement
hasrange:[12,18]
acorn
theTemplateElement
hasstart:13, end:17
The difference in the numbers seems to come from https://github.com/eslint/espree/blob/master/lib/espree.js#L263
The problem is that
range
computation includes the backticks (`test`
) butvalue.raw
does not include them, so there is a mismatch.start/end
does not include the backticks, therefore it matchesvalue.raw
.So I think
esprima
has the wrong(*) range,acorn
has the correctstart/end
andespree
has both. The change introduced in eslint/rfcs#25 forbid custom rules to usestart/end
, so the only option is to userange
which doesn't matchvalue.raw
anymore. Any rule doing text substitution on aTemplateElement
can potentially break because of this change.(* I don't actually know if the range is wrong, or the value in
value.raw
is wrong)Are you willing to submit a pull request to fix this bug?
Yes, but not sure where the bug actually is :) I don't know if eslint should bring back
start/end
, ifespree
should leaverange
unmodified, ifesprima
should change range to exclude the backticks, if the node raw value should include them...The text was updated successfully, but these errors were encountered: