diff --git a/rules/regex-shorthand.js b/rules/regex-shorthand.js index 63e3b2f426..1494ef49c0 100644 --- a/rules/regex-shorthand.js +++ b/rules/regex-shorthand.js @@ -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); diff --git a/test/regex-shorthand.js b/test/regex-shorthand.js index d1a6fd6f20..a8265f6c15 100644 --- a/test/regex-shorthand.js +++ b/test/regex-shorthand.js @@ -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 } }); @@ -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-(?[a-zA-Z-\\d]+)$/u', + + // Should not suggest wrong regex (#447) + '/(\\s|\\.|@|_|-)/u', + '/[\\s.@_-]/u' ], invalid: [ {