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

@typescript-eslint/prefer-regexp-exec does not respect flags #3477

Closed
3 tasks done
gabrielecirulli opened this issue Jun 2, 2021 · 5 comments · Fixed by #3855
Closed
3 tasks done

@typescript-eslint/prefer-regexp-exec does not respect flags #3477

gabrielecirulli opened this issue Jun 2, 2021 · 5 comments · Fixed by #3855
Labels
bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@gabrielecirulli
Copy link

gabrielecirulli commented Jun 2, 2021

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

When used within a function whose arguments are passed to the new RegExp constructor, the @typescript-eslint/prefer-regexp-exec misfires.

function repro(pattern: string) {
  "hello hello".match(new RegExp(pattern, "gi"))?.reduce(() => []);
}

Gets replaced to (when the eslint --fix option is set):

function repro(pattern: string) {
  new RegExp(pattern, "gi").exec("hello hello")?.reduce(() => []);
}

This is an incorrect replacement because they are not equivalent due to the presence of the "g" flag.

Expected Result

The replacement shouldn't take place, the rule shouldn't error.

Actual Result

The rule returns an error and replaces the match if allowed to.

Additional Info

Versions

typescript 4.3.2
@typescript-eslint/eslint-plugin 4.25.0
@typescript-eslint/parser 4.25.0
eslint 7.27.0
@bradzacher
Copy link
Member

bradzacher commented Jun 2, 2021

Please ensure you use the issue templates when you file an issue by clicking the new issue template. Don't bypass the issue wizard by entering or changing the URL.

Bypassing means you get the wrong issue template and your issue won't be automatically tagged.

@bradzacher bradzacher added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin labels Jun 2, 2021
@gabrielecirulli
Copy link
Author

Please ensure you use the issue templates when you file an issue by clicking the new issue template. Don't bypass the issue wizard by entering or changing the URL.

Bypassing means you get the wrong issue template and your issue won't be automatically tagged.

I used the template but removed some parts that didn't seem applicable. Is there any information you'd like me to add to the issue?

@bradzacher
Copy link
Member

I used the template

Sorry, to clarify. If you create a new issue by clicking the "new issue" button on the Github UI it will take you to https://github.com/typescript-eslint/typescript-eslint/issues/new/choose, which is the issue wizard - through which you can choose an issue template workflow.

But if you manually go to https://github.com/typescript-eslint/typescript-eslint/issues/new, then you will not go via the wizard, and you will get a catch-all template. Your issue will also not get automatically tagged.

The lack of tags is why I know you did not go via the wizard.

Always click the new issue button and follow the wizard to ensure you get the correct template and correct tags.

@DanKaplanSES
Copy link
Contributor

DanKaplanSES commented Sep 18, 2021

I'd like to piggyback off of this because it's the closest thing to the issue I'm experiencing. It may actually be the same issue:

  const matchCount = (str: string, re: RegExp) => {
    return (str.match(re) || []).length;
  };

This triggers the error, but I don't think there's any way it could know if the re has a /g at the end or not; it's a variable. I happen to be calling it like this expect(actual.matchAll(/<\\w+>.*<\\w+>/g), actual).toEqual(0);, and this fix changes behavior.

@rafaelss95
Copy link
Contributor

If #3855 gets in, this will be fixed as well.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
4 participants