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

Update regex-shorthand to ignore regex with u flag #451

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion rules/regex-shorthand.js
Expand Up @@ -9,12 +9,18 @@ const message = 'Use regex shorthands to improve readability.';
const create = context => {
return {
'Literal[regex]': node => {
const {type, value} = context.getSourceCode().getFirstToken(node);
const {type, value, regex} = context.getSourceCode().getFirstToken(node);

if (type !== 'RegularExpression') {
return;
}

// Regex with `u` flag is not well handled by `regexp-tree`
// https://github.com/DmitrySoshnikov/regexp-tree/issues/162
if (regex.flags.includes('u')) {
return;
}

let parsedSource;
try {
parsedSource = parse(value);
Expand Down
15 changes: 10 additions & 5 deletions test/regex-shorthand.js
Expand Up @@ -3,11 +3,8 @@ import avaRuleTester from 'eslint-ava-rule-tester';
import rule from '../rules/regex-shorthand';

const ruleTester = avaRuleTester(test, {
env: {
es6: true
},
parserOptions: {
sourceType: 'module'
ecmaVersion: 2020
}
});

Expand All @@ -31,7 +28,15 @@ ruleTester.run('regex-shorthand', rule, {
'const foo = new RegExp(/\\d/ig)',
'const foo = new RegExp(/\\d/, \'ig\')',
'const foo = new RegExp(/\\d*?/)',
'const foo = new RegExp(/[a-z]/, \'i\')'
'const foo = new RegExp(/[a-z]/, \'i\')',

// Should not crash ESLint (#446 and #448)
'/\\{\\{verificationUrl\\}\\}/gu',
'/^test-(?<name>[a-zA-Z-\\d]+)$/u',

// Should not suggest wrong regex (#447)
'/(\\s|\\.|@|_|-)/u',
'/[\\s.@_-]/u'
],
invalid: [
{
Expand Down