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

Rule Change: Suggestions for require-unicode-regexp #15089

Closed
1 task
Yash-Singh1 opened this issue Sep 21, 2021 · 17 comments · Fixed by #17007
Closed
1 task

Rule Change: Suggestions for require-unicode-regexp #15089

Yash-Singh1 opened this issue Sep 21, 2021 · 17 comments · Fixed by #17007
Assignees
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 enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects

Comments

@Yash-Singh1
Copy link
Contributor

Yash-Singh1 commented Sep 21, 2021

What rule do you want to change?

require-unicode-regexp

What change to do you want to make?

Implement suggestions

How do you think the change should be implemented?

A new default behavior

Example code

/abc/

What does the rule currently do for this code?

Reports error

What will the rule do after it's changed?

Give user suggestion prompt to convert into:

/abc/u

Participation

  • I am willing to submit a pull request to implement this change.

Additional comments

N/A

@Yash-Singh1 Yash-Singh1 added enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules labels Sep 21, 2021
@eslint-github-bot eslint-github-bot bot added this to Needs Triage in Triage Sep 21, 2021
@ljharb
Copy link
Sponsor Contributor

ljharb commented Sep 21, 2021

This seems very dangerous, unless it only does the autofix when the u flag would not change semantics. Are you envisioning it being able to detect the "same semantic" cases?

@Yash-Singh1
Copy link
Contributor Author

Are you envisioning it being able to detect the "same semantic" cases?

I didn’t take in the fact that this flag has side effects when I was writing this issue, but yes, as long as adding the u flag won’t change the functionality of the regular expression.

@mdjermanovic
Copy link
Member

This is doable, but seems very complicated.

Pattern should not contain anything that matches surrogate code units. That includes ., \D, \S, \W, negated character classes, etc. Ranges should be checked, too.

The i flag has a different mapping when u flag is set.

Also, some patterns would have completely different semantics with the u flag, e.g. /\u{1}/.

@mdjermanovic mdjermanovic moved this from Needs Triage to Feedback Needed in Triage Sep 21, 2021
@Yash-Singh1
Copy link
Contributor Author

So, for the fact that it disables Annex B extensions, that should mean that we have to check the regular expression and make sure it isn't something like /\w{1, 2/u, and that could be done by running regexpp in strict mode. Also, about the surrogate code units, we can probably traverse through the AST generated by regexpp and look for built-in character sets with the negate boolean set to true and validate character classes.

@nzakas
Copy link
Member

nzakas commented Sep 29, 2021

This seems like a risky thing to autofix. Maybe a suggestion would be better?

@Yash-Singh1 Yash-Singh1 changed the title Rule Change: Autofix for require-unicode-regexp Rule Change: Suggestions for require-unicode-regexp Sep 29, 2021
@nzakas
Copy link
Member

nzakas commented Oct 22, 2021

@eslint/eslint-tsc do we want to make a change here?

@btmills
Copy link
Member

btmills commented Oct 25, 2021

I’m 👍 to suggestion instead of fix.

@mdjermanovic
Copy link
Member

Ideally, the rule would autofix regular expressions that will have the same behavior with the u flag. For those that will have different behavior, the rule could provide suggestions with a note that the behavior will change after the suggestion is applied.

If we're not confident that we can determine whether or not the behavior will change, then providing suggestions in both cases might be safer. However, I think that the suggestion message (or the error message itself) should state whether the behavior will change. Otherwise, user may just apply it without thinking about if there's anything that should be additionally changed in the pattern or in the way the regex is used. But, that brings basically the same question as with the autofix - can we reliably know if the behavior will change when the u flag is added?

@nzakas
Copy link
Member

nzakas commented Dec 3, 2021

However, I think that the suggestion message (or the error message itself) should state whether the behavior will change.

I think this is asking too much. The point of suggestions is to provide potential fixes that might change behavior, but I think it’s beyond our scope to indicate how behavior might change. At some point we need to trust that developers have tests that will help them identify runtime errors.

@mdjermanovic
Copy link
Member

However, I think that the suggestion message (or the error message itself) should state whether the behavior will change.

I think this is asking too much. The point of suggestions is to provide potential fixes that might change behavior, but I think it’s beyond our scope to indicate how behavior might change. At some point we need to trust that developers have tests that will help them identify runtime errors.

In that case, the suggestion message would always warn that the suggested fix may change behavior?

@ljharb
Copy link
Sponsor Contributor

ljharb commented Dec 3, 2021

Isn’t that implied?

@nzakas
Copy link
Member

nzakas commented Dec 4, 2021

In that case, the suggestion message would always warn that the suggested fix may change behavior?

No. This is always implied with suggestions. If they didn’t change behavior, then they would be an autofix. The implication of suggestions is that it might change behavior and you should only apply it if you’re sure of what you’re doing. There isn’t any need to add this into every suggestion.

@mdjermanovic
Copy link
Member

It isn't implied. Suggestion can retain the same behavior, but it isn't autofix when it doesn't seem that the behavior was intended. In core rules, we have 3 suggestions that don't change behavior:

suggestNegatedExpression: "Negate '{{operator}}' expression instead of its left operand. This changes the current behavior.",
suggestParenthesisedNegation: "Wrap negation in '()' to make the intention explicit. This preserves the current behavior."

refactor: "Replace '{{original}}' with '{{replacement}}'. This maintains the current functionality.",
escapeBackslash: "Replace '{{original}}' with '{{replacement}}' to include the actual backslash character."

removeEscape: "Remove the `\\`. This maintains the current functionality.",
escapeBackslash: "Replace the `\\` with `\\\\` to include the actual backslash character."

@nzakas
Copy link
Member

nzakas commented Dec 15, 2021

Sorry, I’m not quite sure what you’re arguing here. Suggestions don’t need to change behavior, we just know that they might. This is from our website (https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions):

In some cases fixes aren't appropriate to be automatically applied, for example, if a fix potentially changes functionality or if there are multiple valid ways to fix a rule depending on the implementation intent (see the best practices for applying fixes listed above). In these cases, there is an alternative suggest option on context.report() that allows other tools, such as editors, to expose helpers for users to manually apply a suggestion.

My overall point is that many suggestions change behavior. We can’t start adding this message into one suggestion because then it implies all other suggestions without that message won’t change behavior, which is clearly wrong. So then we would have to update all suggestions to clearly state whether they might change behavior, which we could do for core rules but not for plugin rules. I’d prefer we just not put it in the message and just make sure our documentation about what suggestions are is accurate.

@github-actions
Copy link

Oops! It looks like we lost track of this issue. What do we want to do here? This issue will auto-close in 7 days without an update.

@github-actions github-actions bot added the Stale label Feb 13, 2022
@mdjermanovic
Copy link
Member

Since we have accepted the same change in the no-misleading-character-class rule (#15279), it makes the most sense to accept this change as well.

@github-actions github-actions bot removed the Stale label Feb 15, 2022
@nzakas
Copy link
Member

nzakas commented Feb 17, 2022

Sounds good.

@nzakas nzakas added the accepted There is consensus among the team that this change meets the criteria for inclusion label Feb 17, 2022
@nzakas nzakas moved this from Feedback Needed to Ready to Implement in Triage Feb 17, 2022
@snitin315 snitin315 self-assigned this Nov 20, 2022
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Sep 25, 2023
@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 Sep 25, 2023
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 enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
Archived in project
Triage
Ready to Implement
Development

Successfully merging a pull request may close this issue.

6 participants