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

Fix: no-useless-escape wrong loc and fix with CRLF in template elements #13953

Merged
merged 1 commit into from Dec 30, 2020

Conversation

mdjermanovic
Copy link
Member

Prerequisites checklist

What is the purpose of this pull request? (put an "X" next to an item)

[ ] Documentation update
[x] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

Tell us about your environment

  • ESLint Version: v7.16.0
  • Node Version: v12.18.4
  • npm Version: v6.14.6

What parser (default, @babel/eslint-parser, @typescript-eslint/parser, etc.) are you using?

default

Please show your full configuration:

Configuration
module.exports = {
  parserOptions: {
    ecmaVersion: 2015
  }
};

What did you do? Please include the actual source code causing the issue.

With CRLF linebreaks:

/* eslint no-useless-escape: error */

`multiline template
literal with useless \escape`

What did you expect to happen?

1 error with the location of \.

Suggestions should remove \, or replace it with \\.

What actually happened? Please include the actual, raw output from ESLint.

  4:21  error  Unnecessary escape character: \e  no-useless-escape

Location is wrong, editor underlines space before \.

Suggestion fixes are also wrong. The "remove" suggestion fixes the code to:

/* eslint no-useless-escape: error */

`multiline template
literal with useless\escape`

The "replace" suggestion fixes the code to:

/* eslint no-useless-escape: error */

`multiline template
literal with useless\ \escape`

What changes did you make? (Give an overview)

Replaced node.value.raw with sourceCode.getText(node) for TemplateElement nodes.

The rule assumed that TemplateElement#value.raw represents source code of the TemplateElement, similar to Literal#raw, but it's actually the Template Raw Value (TRV in the spec), the value that tag functions are getting as "raw".

It's usually the same as the source code, but there's at least one difference: CR and CRLF are normalized to LF.

The TRV of LineTerminatorSequence :: <CR> is the String value consisting of the code unit 0x000A (LINE FEED).
The TRV of LineTerminatorSequence :: <CR> <LF> is the String value consisting of the code unit 0x000A (LINE FEED).

Is there anything you'd like reviewers to focus on?

The regex will now run on the full source code of strings and template elements, incl. quotes, backticks, ${ and }. I think that slicing the inner content is unnecessary and probably less efficient.

@mdjermanovic mdjermanovic added bug ESLint is working incorrectly rule Relates to ESLint's core rules accepted There is consensus among the team that this change meets the criteria for inclusion autofix This change is related to ESLint's autofixing capabilities labels Dec 23, 2020
Copy link
Member

@aladdin-add aladdin-add left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 💯

@mdjermanovic mdjermanovic merged commit f6e7e32 into master Dec 30, 2020
@mdjermanovic mdjermanovic deleted the nouselessescape-crlf branch December 30, 2020 23:51
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Jun 29, 2021
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Jun 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion autofix This change is related to ESLint's autofixing capabilities bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants