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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: use regexpp's default ecmaVersion in no-invalid-regexp #13968

Merged
merged 1 commit into from Jan 1, 2021

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: 2020
  }
};

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

Online Demo

/* eslint no-invalid-regexp: error */

/(?<\ud835\udc9c>.)/g;
new RegExp("(?<\\ud835\\udc9c>.)", "g");

/(?<\u{1d49c}>.)/g;
new RegExp("(?<\\u{1d49c}>.)", "g");

/(?<饾挏>.)/g;
new RegExp("(?<饾挏>.)", "g");

/\p{Script=Nandinagari}/u;
new RegExp("\\p{Script=Nandinagari}", "u");

What did you expect to happen?

No errors. These are valid regular expressions per the ES2020 spec, ref tc39/ecma262#1869 (comment) and tc39/ecma262#1468.

Espree with ecmaVersion: 2020 doesn't report parsing errors in the equivalent regex literals.

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

All four RegExp calls are reported:

   4:1  error  Invalid regular expression: /(?<\ud835\udc9c>.)/: Invalid capture group name  no-invalid-regexp
   7:1  error  Invalid regular expression: /(?<\u{1d49c}>.)/: Invalid capture group name     no-invalid-regexp
  10:1  error  Invalid regular expression: /(?<饾挏>.)/: Invalid capture group name            no-invalid-regexp
  13:1  error  Invalid regular expression: /\p{Script=Nandinagari}/u: Invalid property name  no-invalid-regexp

What changes did you make? (Give an overview)

In the no-invalid-regexp rule I removed the hard-coded ecmaVersion: 2018 passed into RegExpValidator so that the default regexpp's ecmaVersion is used, like in other rules that use regexpp. I think it's always the latest official version (not counting the draft?). In #10062 it was decided to use the latest version in this rule.

This currently bumps ecmaVersion from 2018 to 2020 in no-invalid-regexp.

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

  • I believe this change can produce only fewer warnings.
  • Should we use regexpp's default ecmaVersion, or set it to the max supported by regexpp if they will be different? The actual published regexpp doesn't support 2021, so it wouldn't make a difference at the moment.
  • The previous valid tests are misleading because this rule doesn't use ecmaVersion from the configuration. Most of the allowConstructorFlags tests are thus useless. I'll submit a PR to modify the tests.
  • "g" flag in the examples and the tests doesn't have a particular meaning, but I had to add some flags because the logic on when should the rule try to validate the regex both with and without the "u" flag is currently wrong. For example, new RegExp("\\u{0}*"); is a false negative (it's valid with the "u" flag, but that flag certainly isn't there in this case). I'll submit a PR to fix that.

@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 labels Dec 30, 2020
Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

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

I think using the default is probably fine.

@mdjermanovic mdjermanovic merged commit 83e98cd into master Jan 1, 2021
@mdjermanovic mdjermanovic deleted the noinvalidregexp-ecmaversion branch January 1, 2021 17:59
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Jul 1, 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 Jul 1, 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 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