Skip to content

Commit

Permalink
Revert "Fix codebase"
Browse files Browse the repository at this point in the history
This reverts commit d8a4978.
  • Loading branch information
fisker committed May 8, 2024
1 parent d8a4978 commit 9bc7b93
Show file tree
Hide file tree
Showing 13 changed files with 301 additions and 301 deletions.
4 changes: 2 additions & 2 deletions rules/no-unnecessary-polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ const additionalPolyfillPatterns = {

const prefixes = '(mdn-polyfills/|polyfill-)';
const suffixes = '(-polyfill)';
const delimiter = String.raw`(\.|-|\.prototype\.|/)?`;
const delimiter = '(\\.|-|\\.prototype\\.|/)?';

Check failure on line 26 in rules/no-unnecessary-polyfills.js

View workflow job for this annotation

GitHub Actions / run-rules-on-codebase

`String.raw` should be used to avoid escaping `\`

const polyfills = Object.keys(compatData).map(feature => {
let [ecmaVersion, constructorName, methodName = ''] = feature.split('.');

if (ecmaVersion === 'es') {
ecmaVersion = String.raw`(es\d*)`;
ecmaVersion = '(es\\d*)';

Check failure on line 32 in rules/no-unnecessary-polyfills.js

View workflow job for this annotation

GitHub Actions / run-rules-on-codebase

`String.raw` should be used to avoid escaping `\`
}

constructorName = `(${constructorName}|${camelCase(constructorName)})`;
Expand Down
2 changes: 1 addition & 1 deletion rules/utils/escape-template-element-raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

const escapeTemplateElementRaw = string => string.replaceAll(
/(?<=(?:^|[^\\])(?:\\\\)*)(?<symbol>(?:`|\$(?={)))/g,
String.raw`\$<symbol>`,
'\\$<symbol>',

Check failure on line 5 in rules/utils/escape-template-element-raw.js

View workflow job for this annotation

GitHub Actions / run-rules-on-codebase

`String.raw` should be used to avoid escaping `\`
);
module.exports = escapeTemplateElementRaw;
210 changes: 105 additions & 105 deletions test/better-regex.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,38 @@ const testCase = (original, optimized) => ({
test({
valid: [
// Literal regex
String.raw`const foo = /\d/`,
String.raw`const foo = /\W/i`,
String.raw`const foo = /\w/gi`,
'const foo = /\\d/',

Check failure on line 32 in test/better-regex.mjs

View workflow job for this annotation

GitHub Actions / run-rules-on-codebase

`String.raw` should be used to avoid escaping `\`
'const foo = /\\W/i',

Check failure on line 33 in test/better-regex.mjs

View workflow job for this annotation

GitHub Actions / run-rules-on-codebase

`String.raw` should be used to avoid escaping `\`
'const foo = /\\w/gi',

Check failure on line 34 in test/better-regex.mjs

View workflow job for this annotation

GitHub Actions / run-rules-on-codebase

`String.raw` should be used to avoid escaping `\`
'const foo = /[a-z]/gi',
String.raw`const foo = /\d*?/gi`,
'const foo = /\\d*?/gi',

Check failure on line 36 in test/better-regex.mjs

View workflow job for this annotation

GitHub Actions / run-rules-on-codebase

`String.raw` should be used to avoid escaping `\`

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

Check failure on line 39 in test/better-regex.mjs

View workflow job for this annotation

GitHub Actions / run-rules-on-codebase

`String.raw` should be used to avoid escaping `\`
'/^test-(?<name>[a-zA-Z-\\d]+)$/u',

Check failure on line 40 in test/better-regex.mjs

View workflow job for this annotation

GitHub Actions / run-rules-on-codebase

`String.raw` should be used to avoid escaping `\`
String.raw`/[\p{Script_Extensions=Greek}--π]/v`,

// Should not suggest wrong regex (#447)
String.raw`/(\s|\.|@|_|-)/u`,
String.raw`/[\s.@_-]/u`,
'/(\\s|\\.|@|_|-)/u',

Check failure on line 44 in test/better-regex.mjs

View workflow job for this annotation

GitHub Actions / run-rules-on-codebase

`String.raw` should be used to avoid escaping `\`
'/[\\s.@_-]/u',

// Should not remove repeating patterns too easily (#769)
String.raw`/http:\/\/[^/]+\/pull\/commits/gi`,
'/http:\\/\\/[^/]+\\/pull\\/commits/gi',

{
code: '/[GgHhIiå.Z:a-f"0-8%A*ä]/',
options: disableSortCharacterClassesOptions,
},

// `RegExp()` constructor
String.raw`new RegExp('\d')`,
String.raw`new RegExp('\d', 'ig')`,
String.raw`new RegExp('\d*?')`,
'new RegExp(\'\\d\')',
'new RegExp(\'\\d\', \'ig\')',
'new RegExp(\'\\d*?\')',
'new RegExp(\'[a-z]\', \'i\')',
String.raw`new RegExp(/\d/)`,
String.raw`new RegExp(/\d/gi)`,
String.raw`new RegExp(/\d/, 'ig')`,
String.raw`new RegExp(/\d*?/)`,
'new RegExp(/\\d/)',
'new RegExp(/\\d/gi)',
'new RegExp(/\\d/, \'ig\')',
'new RegExp(/\\d*?/)',
'new RegExp(/[a-z]/, \'i\')',
// Not `new`
'RegExp("[0-9]")',
Expand All @@ -79,105 +79,105 @@ test({
'/[ ;-]/g',

// #994
String.raw`/\s?\s?/`, // https://github.com/DmitrySoshnikov/regexp-tree/issues/216#issuecomment-762073297
String.raw`/\s{0,2}/`,
'/\\s?\\s?/', // https://github.com/DmitrySoshnikov/regexp-tree/issues/216#issuecomment-762073297
'/\\s{0,2}/',
],
invalid: [
// Literal regex
{
code: String.raw`const foo = /\w/ig`,
errors: createError(String.raw`/\w/ig`, String.raw`/\w/gi`),
output: String.raw`const foo = /\w/gi`,
code: 'const foo = /\\w/ig',
errors: createError('/\\w/ig', '/\\w/gi'),
output: 'const foo = /\\w/gi',
},
{
code: 'const foo = /[0-9]/',
errors: createError('/[0-9]/', String.raw`/\d/`),
output: String.raw`const foo = /\d/`,
errors: createError('/[0-9]/', '/\\d/'),
output: 'const foo = /\\d/',
},
{
code: 'const foo = /[0-9]/ig',
errors: createError('/[0-9]/ig', String.raw`/\d/gi`),
output: String.raw`const foo = /\d/gi`,
errors: createError('/[0-9]/ig', '/\\d/gi'),
output: 'const foo = /\\d/gi',
},
{
code: 'const foo = /[^0-9]/',
errors: createError('/[^0-9]/', String.raw`/\D/`),
output: String.raw`const foo = /\D/`,
errors: createError('/[^0-9]/', '/\\D/'),
output: 'const foo = /\\D/',
},
{
code: 'const foo = /[A-Za-z0-9_]/',
errors: createError('/[A-Za-z0-9_]/', String.raw`/\w/`),
output: String.raw`const foo = /\w/`,
errors: createError('/[A-Za-z0-9_]/', '/\\w/'),
output: 'const foo = /\\w/',
},
{
code: String.raw`const foo = /[A-Za-z\d_]/`,
errors: createError(String.raw`/[A-Za-z\d_]/`, String.raw`/\w/`),
output: String.raw`const foo = /\w/`,
code: 'const foo = /[A-Za-z\\d_]/',
errors: createError('/[A-Za-z\\d_]/', '/\\w/'),
output: 'const foo = /\\w/',
},
{
code: 'const foo = /[a-zA-Z0-9_]/',
errors: createError('/[a-zA-Z0-9_]/', String.raw`/\w/`),
output: String.raw`const foo = /\w/`,
errors: createError('/[a-zA-Z0-9_]/', '/\\w/'),
output: 'const foo = /\\w/',
},
{
code: String.raw`const foo = /[a-zA-Z\d_]/`,
errors: createError(String.raw`/[a-zA-Z\d_]/`, String.raw`/\w/`),
output: String.raw`const foo = /\w/`,
code: 'const foo = /[a-zA-Z\\d_]/',
errors: createError('/[a-zA-Z\\d_]/', '/\\w/'),
output: 'const foo = /\\w/',
},
{
code: String.raw`const foo = /[A-Za-z0-9_]+[0-9]?\.[A-Za-z0-9_]*/`,
errors: createError(String.raw`/[A-Za-z0-9_]+[0-9]?\.[A-Za-z0-9_]*/`, String.raw`/\w+\d?\.\w*/`),
output: String.raw`const foo = /\w+\d?\.\w*/`,
code: 'const foo = /[A-Za-z0-9_]+[0-9]?\\.[A-Za-z0-9_]*/',
errors: createError('/[A-Za-z0-9_]+[0-9]?\\.[A-Za-z0-9_]*/', '/\\w+\\d?\\.\\w*/'),
output: 'const foo = /\\w+\\d?\\.\\w*/',
},
{
code: 'const foo = /[a-z0-9_]/i',
errors: createError('/[a-z0-9_]/i', String.raw`/\w/i`),
output: String.raw`const foo = /\w/i`,
errors: createError('/[a-z0-9_]/i', '/\\w/i'),
output: 'const foo = /\\w/i',
},
{
code: String.raw`const foo = /[a-z\d_]/i`,
errors: createError(String.raw`/[a-z\d_]/i`, String.raw`/\w/i`),
output: String.raw`const foo = /\w/i`,
code: 'const foo = /[a-z\\d_]/i',
errors: createError('/[a-z\\d_]/i', '/\\w/i'),
output: 'const foo = /\\w/i',
},
{
code: 'const foo = /[^A-Za-z0-9_]/',
errors: createError('/[^A-Za-z0-9_]/', String.raw`/\W/`),
output: String.raw`const foo = /\W/`,
errors: createError('/[^A-Za-z0-9_]/', '/\\W/'),
output: 'const foo = /\\W/',
},
{
code: String.raw`const foo = /[^A-Za-z\d_]/`,
errors: createError(String.raw`/[^A-Za-z\d_]/`, String.raw`/\W/`),
output: String.raw`const foo = /\W/`,
code: 'const foo = /[^A-Za-z\\d_]/',
errors: createError('/[^A-Za-z\\d_]/', '/\\W/'),
output: 'const foo = /\\W/',
},
{
code: 'const foo = /[^a-z0-9_]/i',
errors: createError('/[^a-z0-9_]/i', String.raw`/\W/i`),
output: String.raw`const foo = /\W/i`,
errors: createError('/[^a-z0-9_]/i', '/\\W/i'),
output: 'const foo = /\\W/i',
},
{
code: String.raw`const foo = /[^a-z\d_]/i`,
errors: createError(String.raw`/[^a-z\d_]/i`, String.raw`/\W/i`),
output: String.raw`const foo = /\W/i`,
code: 'const foo = /[^a-z\\d_]/i',
errors: createError('/[^a-z\\d_]/i', '/\\W/i'),
output: 'const foo = /\\W/i',
},
{
code: String.raw`const foo = /[^a-z\d_]/ig`,
errors: createError(String.raw`/[^a-z\d_]/ig`, String.raw`/\W/gi`),
output: String.raw`const foo = /\W/gi`,
code: 'const foo = /[^a-z\\d_]/ig',
errors: createError('/[^a-z\\d_]/ig', '/\\W/gi'),
output: 'const foo = /\\W/gi',
},
{
code: String.raw`const foo = /[^\d_a-z]/ig`,
errors: createError(String.raw`/[^\d_a-z]/ig`, String.raw`/\W/gi`),
output: String.raw`const foo = /\W/gi`,
code: 'const foo = /[^\\d_a-z]/ig',
errors: createError('/[^\\d_a-z]/ig', '/\\W/gi'),
output: 'const foo = /\\W/gi',
},
{
code: 'const foo = /[a-z0-9_]/',
errors: createError('/[a-z0-9_]/', String.raw`/[\d_a-z]/`),
output: String.raw`const foo = /[\d_a-z]/`,
errors: createError('/[a-z0-9_]/', '/[\\d_a-z]/'),
output: 'const foo = /[\\d_a-z]/',
},
{
code: 'const foo = /^by @([a-zA-Z0-9-]+)/',
errors: createError('/^by @([a-zA-Z0-9-]+)/', String.raw`/^by @([\dA-Za-z-]+)/`),
output: String.raw`const foo = /^by @([\dA-Za-z-]+)/`,
errors: createError('/^by @([a-zA-Z0-9-]+)/', '/^by @([\\dA-Za-z-]+)/'),
output: 'const foo = /^by @([\\dA-Za-z-]+)/',
},
{
code: '/[GgHhIiå.Z:a-f"0-8%A*ä]/',
Expand All @@ -188,100 +188,100 @@ test({
{
code: '/[a0-9b]/',
options: disableSortCharacterClassesOptions,
errors: createError('/[a0-9b]/', String.raw`/[a\db]/`),
output: String.raw`/[a\db]/`,
errors: createError('/[a0-9b]/', '/[a\\db]/'),
output: '/[a\\db]/',
},

// `RegExp()` constructor
{
code: 'const foo = new RegExp(\'[0-9]\')',
errors: createError('[0-9]', String.raw`\d`),
output: String.raw`const foo = new RegExp('\\d')`,
errors: createError('[0-9]', '\\d'),
output: 'const foo = new RegExp(\'\\\\d\')',
},
{
code: 'const foo = new RegExp("[0-9]")',
errors: createError('[0-9]', String.raw`\d`),
output: String.raw`const foo = new RegExp("\\d")`,
errors: createError('[0-9]', '\\d'),
output: 'const foo = new RegExp("\\\\d")',
},
{
code: String.raw`const foo = new RegExp('\'[0-9]\'')`,
errors: createError('\'[0-9]\'', String.raw`'\d'`),
output: String.raw`const foo = new RegExp('\'\\d\'')`,
code: 'const foo = new RegExp(\'\\\'[0-9]\\\'\')',
errors: createError('\'[0-9]\'', '\'\\d\''),
output: 'const foo = new RegExp(\'\\\'\\\\d\\\'\')',
},
{
code: 'const foo = new RegExp("\'[0-9]\'")',
errors: createError('\'[0-9]\'', String.raw`'\d'`),
output: String.raw`const foo = new RegExp("'\\d'")`,
errors: createError('\'[0-9]\'', '\'\\d\''),
output: 'const foo = new RegExp("\'\\\\d\'")',
},
{
code: 'const foo = new RegExp(\'[0-9]\', \'ig\')',
errors: createError('[0-9]', String.raw`\d`),
output: String.raw`const foo = new RegExp('\\d', 'ig')`,
errors: createError('[0-9]', '\\d'),
output: 'const foo = new RegExp(\'\\\\d\', \'ig\')',
},
{
code: 'const foo = new RegExp(/[0-9]/, \'ig\')',
errors: createError('/[0-9]/', String.raw`/\d/`),
output: String.raw`const foo = new RegExp(/\d/, 'ig')`,
errors: createError('/[0-9]/', '/\\d/'),
output: 'const foo = new RegExp(/\\d/, \'ig\')',
},
{
code: 'const foo = new RegExp(/^[^*]*[*]?$/)',
errors: createError('/^[^*]*[*]?$/', String.raw`/^[^*]*\*?$/`),
output: String.raw`const foo = new RegExp(/^[^*]*\*?$/)`,
errors: createError('/^[^*]*[*]?$/', '/^[^*]*\\*?$/'),
output: 'const foo = new RegExp(/^[^*]*\\*?$/)',
},
// No `flags`
{
code: 'const foo = new RegExp(/[0-9]/)',
errors: createError('/[0-9]/', String.raw`/\d/`),
output: String.raw`const foo = new RegExp(/\d/)`,
errors: createError('/[0-9]/', '/\\d/'),
output: 'const foo = new RegExp(/\\d/)',
},
// `flags` not `Literal`
{
code: 'const foo = new RegExp(/[0-9]/, ig)',
errors: createError('/[0-9]/', String.raw`/\d/`),
output: String.raw`const foo = new RegExp(/\d/, ig)`,
errors: createError('/[0-9]/', '/\\d/'),
output: 'const foo = new RegExp(/\\d/, ig)',
},
// `flags` not `string`
{
code: 'const foo = new RegExp(/[0-9]/, 0)',
errors: createError('/[0-9]/', String.raw`/\d/`),
output: String.raw`const foo = new RegExp(/\d/, 0)`,
errors: createError('/[0-9]/', '/\\d/'),
output: 'const foo = new RegExp(/\\d/, 0)',
},
// `\s` rewrite
testCase(
String.raw`/[ \f\n\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]+/`,
String.raw`/\s+/`,
'/[ \\f\\n\\r\\t\\v\\u00a0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000\\ufeff]+/',
'/\\s+/',
),
// #499
{
code: String.raw`/^[a-z][a-z0-9\-]{5,29}$/`,
errors: createError(String.raw`/^[a-z][a-z0-9\-]{5,29}$/`, String.raw`/^[a-z][\da-z\-]{5,29}$/`),
output: String.raw`/^[a-z][\da-z\-]{5,29}$/`,
code: '/^[a-z][a-z0-9\\-]{5,29}$/',
errors: createError('/^[a-z][a-z0-9\\-]{5,29}$/', '/^[a-z][\\da-z\\-]{5,29}$/'),
output: '/^[a-z][\\da-z\\-]{5,29}$/',
},
// #477
testCase(
String.raw`/[ \n\t\r\]]/g`,
String.raw`/[\t\n\r \]]/g`,
'/[ \\n\\t\\r\\]]/g',
'/[\\t\\n\\r \\]]/g',
),
testCase(
String.raw`/[ \n\t\r\f"#'()/;[\\\]{}]/g`,
String.raw`/[\t\n\f\r "#'()/;[\\\]{}]/g`,
'/[ \\n\\t\\r\\f"#\'()/;[\\\\\\]{}]/g',
'/[\\t\\n\\f\\r "#\'()/;[\\\\\\]{}]/g',
),
testCase(
String.raw`/[ \n\t\r\f(){}:;@!'"\\\][#]|\/(?=\*)/g`,
String.raw`/[\t\n\f\r !"#'():;@[\\\]{}]|\/(?=\*)/g`,
'/[ \\n\\t\\r\\f(){}:;@!\'"\\\\\\][#]|\\/(?=\\*)/g',
'/[\\t\\n\\f\\r !"#\'():;@[\\\\\\]{}]|\\/(?=\\*)/g',
),
// #994
testCase(
String.raw`/\s?\s?\s?/`,
String.raw`/\s{0,3}/`,
'/\\s?\\s?\\s?/',
'/\\s{0,3}/',
),
// Actual message
{
code: '/[0-9]/',
output: String.raw`/\d/`,
output: '/\\d/',
errors: [
{
message: String.raw`/[0-9]/ can be optimized to /\d/.`,
message: '/[0-9]/ can be optimized to /\\d/.',
},
],
},
Expand All @@ -301,11 +301,11 @@ test({
// Not fixable
{
code: 'const foo = /[0-9]/.toString',
errors: createError('/[0-9]/', String.raw`/\d/`),
errors: createError('/[0-9]/', '/\\d/'),
},
{
code: 'const foo = /[0-9]/.source',
errors: createError('/[0-9]/', String.raw`/\d/`),
errors: createError('/[0-9]/', '/\\d/'),
},
],
});
Expand Down
Loading

0 comments on commit 9bc7b93

Please sign in to comment.