question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule Change: Suggestions for prefer-regex-literals disallowRedundantWrapping option

See original GitHub issue

What rule do you want to change?

prefer-regex-literals

What change to do you want to make?

Implement suggestions

How do you think the change should be implemented?

Other

Example code

/* eslint prefer-regex-literals: ["error", {"disallowRedundantWrapping": true}] */
const reg0 = new RegExp('a'); 
const reg1 = new RegExp(/a/);   // no suggestion
const reg2 = new RegExp(/a/, 'g'); // no suggestion

What does the rule currently do for this code?

The rule didn’t suggest for unnecessarily wrapped patterns which disallowed by "disallowRedundantWrapping": true. demo

What will the rule do after it’s changed?

It will make suggestions for the patterns also.

/* eslint prefer-regex-literals: ["error", {"disallowRedundantWrapping": true}] */
const reg1 = new RegExp(/a/);   //  =>  /a/
const reg2 = new RegExp(/a/, 'g'); // => /a/g
const reg3 = new RegExp(/a/g, 'i') // => /a/i

Participation

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

Additional comments

No response

Issue Analytics

  • State:open
  • Created 10 months ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mdjermanoviccommented, Nov 10, 2022
const reg1 = new RegExp(/a/);   //  =>  /a/
const reg2 = new RegExp(/a/, 'g'); // => /a/g

As for these two cases, perhaps the rule could autofix them instead of providing suggestions?

1reaction
mdjermanoviccommented, Nov 10, 2022

Yes, it would be the same behavior, but new RegExp(/a/g, 'i') looks like redundant wrapping + possible error. This is somewhat similar to the cases that no-useless-escape reports, where the rule always provides two suggestions. For example, for "\a" there’s a suggestion to fix to "a" (the same behavior) and a suggestion to fix to "\\a" (not the same behavior, but it’s very possible that this was the actual intent).

Read more comments on GitHub >

github_iconTop Results From Across the Web

prefer-regex-literals - ESLint - Pluggable JavaScript Linter
This rule has an object option: disallowRedundantWrapping set to true additionally checks for unnecessarily wrapped regex literals (Default false ).
Read more >
Eslint error: Value [{"disallowRedundantWrapping":true}] ...
I'm getting this: Value [{"disallowRedundantWrapping":true}] should NOT ... Configuration for rule "prefer-regex-literals" is invalid: Value ...
Read more >
Rule Change: Suggestions for prefer-regex-literals ...
Yes, it would be the same behavior, but new RegExp(/a/g, 'i') looks like redundant wrapping + possible error. This is somewhat similar to...
Read more >
ESLint
Edit project-eslint.json and remove the none json content at the top. Organize the Rules. Create create-sorted-eslint.js const fs = require ...
Read more >
eslint-config-eslint
... 57089b1 docs: add a property assignment example for camelcase rule (#16605) (Milos Djermanovic); b6ab030 docs: add docs codeowners (#16601) (Strek) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found