From b73a66b7b6ba237fa3181aa21acbedefb6508ab7 Mon Sep 17 00:00:00 2001 From: ota-meshi Date: Sat, 9 Mar 2024 17:34:37 +0900 Subject: [PATCH] test: fix eslint v9 testcases --- .../rules/no-contradiction-with-assertion.ts | 22 +- tests/lib/rules/no-control-character.ts | 26 +- tests/lib/rules/no-dupe-disjunctions.ts | 358 ++++++++++- tests/lib/rules/no-empty-alternative.ts | 23 +- tests/lib/rules/no-escape-backspace.ts | 14 +- tests/lib/rules/no-lazy-ends.ts | 180 +++++- .../rules/no-misleading-capturing-group.ts | 22 +- .../rules/no-misleading-unicode-character.ts | 29 +- tests/lib/rules/no-unused-capturing-group.ts | 575 ++++++++++++++++-- tests/lib/rules/no-useless-assertions.ts | 394 +++++++++++- tests/lib/rules/no-zero-quantifier.ts | 8 +- .../rules/optimal-lookaround-quantifier.ts | 37 +- 12 files changed, 1526 insertions(+), 162 deletions(-) diff --git a/tests/lib/rules/no-contradiction-with-assertion.ts b/tests/lib/rules/no-contradiction-with-assertion.ts index 24e08d9c5..615393843 100644 --- a/tests/lib/rules/no-contradiction-with-assertion.ts +++ b/tests/lib/rules/no-contradiction-with-assertion.ts @@ -28,7 +28,12 @@ tester.run("no-contradiction-with-assertion", rule as any, { errors: [ { messageId: "alwaysEnterQuantifier", - suggestions: [{ output: String.raw`/a\b-{1}a/` }], + suggestions: [ + { + messageId: "changeQuantifier", + output: String.raw`/a\b-{1}a/`, + }, + ], }, ], }, @@ -46,7 +51,12 @@ tester.run("no-contradiction-with-assertion", rule as any, { errors: [ { messageId: "cannotEnterQuantifier", - suggestions: [{ output: String.raw`/a\b-/` }], + suggestions: [ + { + messageId: "removeQuantifier", + output: String.raw`/a\b-/`, + }, + ], }, ], }, @@ -55,7 +65,12 @@ tester.run("no-contradiction-with-assertion", rule as any, { errors: [ { messageId: "cannotEnterQuantifier", - suggestions: [{ output: String.raw`/a\b-/v` }], + suggestions: [ + { + messageId: "removeQuantifier", + output: String.raw`/a\b-/v`, + }, + ], }, ], }, @@ -67,6 +82,7 @@ tester.run("no-contradiction-with-assertion", rule as any, { messageId: "alwaysEnterQuantifier", suggestions: [ { + messageId: "changeQuantifier", output: String.raw`/(^[\t ]*)#(?:comments-start|cs)[\s\S]+?^[ \t]*#(?:comments-end|ce)/m`, }, ], diff --git a/tests/lib/rules/no-control-character.ts b/tests/lib/rules/no-control-character.ts index f6a595a97..6e35043dd 100644 --- a/tests/lib/rules/no-control-character.ts +++ b/tests/lib/rules/no-control-character.ts @@ -51,7 +51,10 @@ tester.run("no-control-character", rule as any, { { messageId: "unexpected", suggestions: [ - { output: String.raw`new RegExp('\x1fFOO\\0')` }, + { + messageId: "escape", + output: String.raw`new RegExp('\x1fFOO\\0')`, + }, ], }, ], @@ -80,7 +83,12 @@ tester.run("no-control-character", rule as any, { errors: [ { messageId: "unexpected", - suggestions: [{ output: String.raw`RegExp('\\n')` }], + suggestions: [ + { + messageId: "escape", + output: String.raw`RegExp('\\n')`, + }, + ], }, ], }, @@ -89,7 +97,12 @@ tester.run("no-control-character", rule as any, { errors: [ { messageId: "unexpected", - suggestions: [{ output: String.raw`RegExp('\\n')` }], + suggestions: [ + { + messageId: "escape", + output: String.raw`RegExp('\\n')`, + }, + ], }, ], }, @@ -98,7 +111,12 @@ tester.run("no-control-character", rule as any, { errors: [ { messageId: "unexpected", - suggestions: [{ output: String.raw`RegExp('\\n')` }], + suggestions: [ + { + messageId: "escape", + output: String.raw`RegExp('\\n')`, + }, + ], }, ], }, diff --git a/tests/lib/rules/no-dupe-disjunctions.ts b/tests/lib/rules/no-dupe-disjunctions.ts index 8297eceaa..079ff14ce 100644 --- a/tests/lib/rules/no-dupe-disjunctions.ts +++ b/tests/lib/rules/no-dupe-disjunctions.ts @@ -60,6 +60,7 @@ tester.run("no-dupe-disjunctions", rule as any, { column: 4, endLine: 1, endColumn: 5, + suggestions: [{ messageId: "remove", output: `/a/` }], }, ], }, @@ -73,6 +74,7 @@ tester.run("no-dupe-disjunctions", rule as any, { column: 5, endLine: 1, endColumn: 6, + suggestions: [{ messageId: "remove", output: `/(a)/` }], }, ], }, @@ -86,6 +88,7 @@ tester.run("no-dupe-disjunctions", rule as any, { column: 7, endLine: 1, endColumn: 8, + suggestions: [{ messageId: "remove", output: `/(?:a)/` }], }, ], }, @@ -97,6 +100,9 @@ tester.run("no-dupe-disjunctions", rule as any, { "Unexpected useless alternative. This alternative is a strict subset of '(?=[a-c])' and can be removed.", column: 12, endColumn: 17, + suggestions: [ + { messageId: "remove", output: `/(?=[a-c])/` }, + ], }, ], }, @@ -110,6 +116,7 @@ tester.run("no-dupe-disjunctions", rule as any, { column: 7, endLine: 1, endColumn: 8, + suggestions: [{ messageId: "remove", output: `/(?=a)/` }], }, ], }, @@ -123,43 +130,89 @@ tester.run("no-dupe-disjunctions", rule as any, { column: 8, endLine: 1, endColumn: 9, + suggestions: [{ messageId: "remove", output: `/(?<=a)/` }], }, ], }, { code: `/(?:[ab]|[ab])/`, errors: [ - "Unexpected duplicate alternative. This alternative can be removed.", + { + message: + "Unexpected duplicate alternative. This alternative can be removed.", + column: 10, + suggestions: [ + { messageId: "remove", output: `/(?:[ab])/` }, + ], + }, ], }, { code: `/(?:[ab]|[ba])/`, errors: [ - "Unexpected duplicate alternative. This alternative can be removed.", + { + message: + "Unexpected duplicate alternative. This alternative can be removed.", + column: 10, + suggestions: [ + { messageId: "remove", output: `/(?:[ab])/` }, + ], + }, ], }, { code: String.raw`/(?:[\da-z]|[a-z\d])/`, errors: [ - "Unexpected duplicate alternative. This alternative can be removed.", + { + message: + "Unexpected duplicate alternative. This alternative can be removed.", + column: 13, + suggestions: [ + { + messageId: "remove", + output: String.raw`/(?:[\da-z])/`, + }, + ], + }, ], }, { code: `/((?:ab|ba)|(?:ab|ba))/`, errors: [ - "Unexpected duplicate alternative. This alternative can be removed.", + { + message: + "Unexpected duplicate alternative. This alternative can be removed.", + column: 13, + suggestions: [ + { messageId: "remove", output: `/((?:ab|ba))/` }, + ], + }, ], }, { code: `/((?:ab|ba)|(?:ab|ba))/v`, errors: [ - "Unexpected duplicate alternative. This alternative can be removed.", + { + message: + "Unexpected duplicate alternative. This alternative can be removed.", + column: 13, + suggestions: [ + { messageId: "remove", output: `/((?:ab|ba))/v` }, + ], + }, ], }, { code: `/((?:ab|ba)|(?:ba|ab))/`, errors: [ - "Unexpected duplicate alternative. This alternative can be removed.", + { + message: + "Unexpected duplicate alternative. This alternative can be removed.", + column: 13, + suggestions: [ + { messageId: "remove", output: `/((?:ab|ba))/` }, + ], + }, ], }, { @@ -171,7 +224,12 @@ tester.run("no-dupe-disjunctions", rule as any, { { code: `/(?:a|ab)/`, errors: [ - "Unexpected useless alternative. This alternative is already covered by 'a' and can be removed.", + { + message: + "Unexpected useless alternative. This alternative is already covered by 'a' and can be removed.", + column: 7, + suggestions: [{ messageId: "remove", output: `/(?:a)/` }], + }, ], }, { @@ -184,6 +242,7 @@ tester.run("no-dupe-disjunctions", rule as any, { column: 7, endLine: 1, endColumn: 8, + suggestions: [{ messageId: "remove", output: `/(?:.|b)/` }], }, { message: @@ -192,19 +251,30 @@ tester.run("no-dupe-disjunctions", rule as any, { column: 9, endLine: 1, endColumn: 10, + suggestions: [{ messageId: "remove", output: `/(?:.|a)/` }], }, ], }, { code: `/.|abc/`, errors: [ - "Unexpected useless alternative. This alternative is already covered by '.' and can be removed.", + { + message: + "Unexpected useless alternative. This alternative is already covered by '.' and can be removed.", + column: 4, + suggestions: [{ messageId: "remove", output: `/./` }], + }, ], }, { code: `/a|abc/`, errors: [ - "Unexpected useless alternative. This alternative is already covered by 'a' and can be removed.", + { + message: + "Unexpected useless alternative. This alternative is already covered by 'a' and can be removed.", + column: 4, + suggestions: [{ messageId: "remove", output: `/a/` }], + }, ], }, { @@ -214,21 +284,45 @@ tester.run("no-dupe-disjunctions", rule as any, { message: "Unexpected useless alternative. This alternative is already covered by '\\w' and can be removed.", column: 5, + suggestions: [ + { + messageId: "remove", + output: String.raw`/\w|123|_|[A-Z]|\$| /`, + }, + ], }, { message: "Unexpected useless alternative. This alternative is already covered by '\\w' and can be removed.", column: 9, + suggestions: [ + { + messageId: "remove", + output: String.raw`/\w|abc|_|[A-Z]|\$| /`, + }, + ], }, { message: "Unexpected useless alternative. This alternative is a strict subset of '\\w' and can be removed.", column: 13, + suggestions: [ + { + messageId: "remove", + output: String.raw`/\w|abc|123|[A-Z]|\$| /`, + }, + ], }, { message: "Unexpected useless alternative. This alternative is a strict subset of '\\w' and can be removed.", column: 15, + suggestions: [ + { + messageId: "remove", + output: String.raw`/\w|abc|123|_|\$| /`, + }, + ], }, ], }, @@ -239,11 +333,23 @@ tester.run("no-dupe-disjunctions", rule as any, { message: "Unexpected useless alternative. This alternative is a strict subset of '\\W' and can be removed.", column: 21, + suggestions: [ + { + messageId: "remove", + output: String.raw`/\W|abc|123|_|[A-Z]| /`, + }, + ], }, { message: "Unexpected useless alternative. This alternative is a strict subset of '\\W' and can be removed.", column: 24, + suggestions: [ + { + messageId: "remove", + output: String.raw`/\W|abc|123|_|[A-Z]|\$/`, + }, + ], }, ], }, @@ -254,6 +360,12 @@ tester.run("no-dupe-disjunctions", rule as any, { message: "Unexpected useless alternative. This alternative is a strict subset of '\\s' and can be removed.", column: 24, + suggestions: [ + { + messageId: "remove", + output: String.raw`/\s|abc|123|_|[A-Z]|\$/`, + }, + ], }, ], }, @@ -264,26 +376,56 @@ tester.run("no-dupe-disjunctions", rule as any, { message: "Unexpected useless alternative. This alternative is already covered by '\\S' and can be removed.", column: 5, + suggestions: [ + { + messageId: "remove", + output: String.raw`/\S|123|_|[A-Z]|\$| /`, + }, + ], }, { message: "Unexpected useless alternative. This alternative is already covered by '\\S' and can be removed.", column: 9, + suggestions: [ + { + messageId: "remove", + output: String.raw`/\S|abc|_|[A-Z]|\$| /`, + }, + ], }, { message: "Unexpected useless alternative. This alternative is a strict subset of '\\S' and can be removed.", column: 13, + suggestions: [ + { + messageId: "remove", + output: String.raw`/\S|abc|123|[A-Z]|\$| /`, + }, + ], }, { message: "Unexpected useless alternative. This alternative is a strict subset of '\\S' and can be removed.", column: 15, + suggestions: [ + { + messageId: "remove", + output: String.raw`/\S|abc|123|_|\$| /`, + }, + ], }, { message: "Unexpected useless alternative. This alternative is a strict subset of '\\S' and can be removed.", column: 21, + suggestions: [ + { + messageId: "remove", + output: String.raw`/\S|abc|123|_|[A-Z]| /`, + }, + ], }, ], }, @@ -294,37 +436,78 @@ tester.run("no-dupe-disjunctions", rule as any, { message: "Unexpected useless alternative. This alternative is a strict subset of '[^\\S]' and can be removed.", column: 27, + suggestions: [ + { + messageId: "remove", + output: String.raw`/[^\S]|abc|123|_|[A-Z]|\$/`, + }, + ], }, ], }, { code: String.raw`/[^\r]|./`, errors: [ - "Unexpected useless alternative. This alternative is a strict subset of '[^\\r]' and can be removed.", + { + message: + "Unexpected useless alternative. This alternative is a strict subset of '[^\\r]' and can be removed.", + column: 8, + suggestions: [ + { messageId: "remove", output: String.raw`/[^\r]/` }, + ], + }, ], }, { code: String.raw`/\s|\S|./`, errors: [ - "Unexpected useless alternative. This alternative is a strict subset of '\\s|\\S' and can be removed.", + { + message: + "Unexpected useless alternative. This alternative is a strict subset of '\\s|\\S' and can be removed.", + column: 8, + suggestions: [ + { messageId: "remove", output: String.raw`/\s|\S/` }, + ], + }, ], }, { code: `/(?:ya?ml|yml)$/`, errors: [ - "Unexpected useless alternative. This alternative is a strict subset of 'ya?ml' and can be removed.", + { + message: + "Unexpected useless alternative. This alternative is a strict subset of 'ya?ml' and can be removed.", + column: 11, + suggestions: [ + { messageId: "remove", output: `/(?:ya?ml)$/` }, + ], + }, ], }, { code: `/(?:yml|ya?ml)$/`, errors: [ - "Unexpected useless alternative. This alternative is a strict subset of 'ya?ml' and can be removed.", + { + message: + "Unexpected useless alternative. This alternative is a strict subset of 'ya?ml' and can be removed.", + column: 5, + suggestions: [ + { messageId: "remove", output: `/(?:ya?ml)$/` }, + ], + }, ], }, { code: `/(?:yml|ya?ml)/`, errors: [ - "Unexpected useless alternative. This alternative is a strict subset of 'ya?ml' and can be removed.", + { + message: + "Unexpected useless alternative. This alternative is a strict subset of 'ya?ml' and can be removed.", + column: 5, + suggestions: [ + { messageId: "remove", output: `/(?:ya?ml)/` }, + ], + }, ], }, { @@ -337,7 +520,17 @@ tester.run("no-dupe-disjunctions", rule as any, { { code: String.raw`/(?:\p{Lu}\p{L}*|[A-Z]\w*)/u`, errors: [ - "Unexpected useless alternative. This alternative is already covered by '\\p{Lu}\\p{L}*' and can be removed.", + { + message: + "Unexpected useless alternative. This alternative is already covered by '\\p{Lu}\\p{L}*' and can be removed.", + column: 18, + suggestions: [ + { + messageId: "remove", + output: String.raw`/(?:\p{Lu}\p{L}*)/u`, + }, + ], + }, ], }, { @@ -350,19 +543,41 @@ tester.run("no-dupe-disjunctions", rule as any, { { code: String(/FOO|foo(?:bar)?/i), errors: [ - "Unexpected useless alternative. This alternative is already covered by 'FOO' and can be removed.", + { + message: + "Unexpected useless alternative. This alternative is already covered by 'FOO' and can be removed.", + column: 6, + suggestions: [{ messageId: "remove", output: `/FOO/i` }], + }, ], }, { code: String(/foo(?:bar)?|foo/), errors: [ - "Unexpected useless alternative. This alternative is a strict subset of 'foo(?:bar)?' and can be removed.", + { + message: + "Unexpected useless alternative. This alternative is a strict subset of 'foo(?:bar)?' and can be removed.", + column: 14, + suggestions: [ + { messageId: "remove", output: `/foo(?:bar)?/` }, + ], + }, ], }, { code: String(/(?=[\t ]+[\S]{1,}|[\t ]+['"][\S]|[\t ]+$|$)/), errors: [ - "Unexpected useless alternative. This alternative is a strict subset of '[\\t ]+[\\S]{1,}' and can be removed.", + { + message: + "Unexpected useless alternative. This alternative is a strict subset of '[\\t ]+[\\S]{1,}' and can be removed.", + column: 20, + suggestions: [ + { + messageId: "remove", + output: String.raw`/(?=[\t ]+[\S]{1,}|[\t ]+$|$)/`, + }, + ], + }, ], }, { @@ -378,11 +593,23 @@ tester.run("no-dupe-disjunctions", rule as any, { message: "Unexpected useless alternative. This alternative is a strict subset of '\\w+' and can be removed.", column: 7, + suggestions: [ + { + messageId: "remove", + output: String.raw`/\b(?:foo|\w+)\b/`, + }, + ], }, { message: "Unexpected useless alternative. This alternative is a strict subset of '\\w+' and can be removed.", column: 10, + suggestions: [ + { + messageId: "remove", + output: String.raw`/\b(?:\d|\w+)\b/`, + }, + ], }, ], }, @@ -396,7 +623,17 @@ tester.run("no-dupe-disjunctions", rule as any, { { code: String(/\d|[a-z]|_|\w/i), errors: [ - "Unexpected useless alternative. This alternative is a strict subset of '\\d|[a-z]|_' and can be removed.", + { + message: + "Unexpected useless alternative. This alternative is a strict subset of '\\d|[a-z]|_' and can be removed.", + column: 13, + suggestions: [ + { + messageId: "remove", + output: String.raw`/\d|[a-z]|_/i`, + }, + ], + }, ], }, { @@ -406,7 +643,9 @@ tester.run("no-dupe-disjunctions", rule as any, { message: "Unexpected useless element. All paths of '(?:ba|ac)' that go through 'ba' are a strict subset of '(?:ab|ba)'. This element can be removed.", column: 16, - suggestions: [{ output: String(/((?:ab|ba)|(?:ac))/) }], + suggestions: [ + { messageId: "remove", output: `/((?:ab|ba)|(?:ac))/` }, + ], }, ], }, @@ -417,7 +656,7 @@ tester.run("no-dupe-disjunctions", rule as any, { message: "Unexpected useless alternative. This alternative is a strict subset of 'a+' and can be removed.", column: 5, - suggestions: [{ output: String(/a+|b|c/) }], + suggestions: [{ messageId: "remove", output: `/a+|b|c/` }], }, ], }, @@ -428,7 +667,9 @@ tester.run("no-dupe-disjunctions", rule as any, { message: "Unexpected useless element. All paths of '(?:a|b|c)' that go through 'a' are a strict subset of 'a+'. This element can be removed.", column: 8, - suggestions: [{ output: String(/a+|(?:b|c)/) }], + suggestions: [ + { messageId: "remove", output: `/a+|(?:b|c)/` }, + ], }, ], }, @@ -439,7 +680,7 @@ tester.run("no-dupe-disjunctions", rule as any, { message: "Unexpected useless element. All paths of '[abc]' that go through 'a' (U+0061) are a strict subset of 'a+'. This element can be removed.", column: 6, - suggestions: [{ output: String(/a+|[bc]/) }], + suggestions: [{ messageId: "remove", output: `/a+|[bc]/` }], }, ], }, @@ -450,7 +691,9 @@ tester.run("no-dupe-disjunctions", rule as any, { message: "Unexpected useless element. All paths of '[a-c]' that go through 'a' (U+0061) are a strict subset of 'a+'. This element can be removed.", column: 6, - suggestions: [{ output: String(/a+|[b-c]/) }], + suggestions: [ + { messageId: "replaceRange", output: `/a+|[b-c]/` }, + ], }, ], }, @@ -461,7 +704,7 @@ tester.run("no-dupe-disjunctions", rule as any, { message: "Unexpected useless alternative. This alternative is already covered by 'a' and can be removed.", column: 4, - suggestions: [{ output: String(/a|ba/) }], + suggestions: [{ messageId: "remove", output: `/a|ba/` }], }, ], }, @@ -472,7 +715,7 @@ tester.run("no-dupe-disjunctions", rule as any, { message: "Unexpected useless element. All paths of '(a|b)a' that go through 'a' are already covered by 'a'. This element can be removed.", column: 5, - suggestions: [{ output: String(/a|(b)a/) }], + suggestions: [{ messageId: "remove", output: `/a|(b)a/` }], }, ], }, @@ -494,7 +737,7 @@ tester.run("no-dupe-disjunctions", rule as any, { message: "Unexpected useless element. All paths of '[ab]a' that go through 'a' (U+0061) are already covered by 'a'. This element can be removed.", column: 5, - suggestions: [{ output: String(/a|[b]a/) }], + suggestions: [{ messageId: "remove", output: `/a|[b]a/` }], }, ], }, @@ -506,25 +749,48 @@ tester.run("no-dupe-disjunctions", rule as any, { "Unexpected useless alternative. This alternative is a strict subset of 'jso?n?' and can be removed.", column: 5, endColumn: 7, + suggestions: [ + { messageId: "remove", output: `/(?:jso?n?)$/` }, + ], }, ], }, { code: String.raw`/A+_|A*_/`, errors: [ - "Unexpected useless alternative. This alternative is a strict subset of 'A*_' and can be removed.", + { + message: + "Unexpected useless alternative. This alternative is a strict subset of 'A*_' and can be removed.", + line: 1, + suggestions: [{ messageId: "remove", output: `/A*_/` }], + }, ], }, { code: String.raw`/(?:A+|A*)_/`, errors: [ - "Unexpected useless alternative. This alternative is a strict subset of 'A*' and can be removed.", + { + message: + "Unexpected useless alternative. This alternative is a strict subset of 'A*' and can be removed.", + line: 1, + suggestions: [{ messageId: "remove", output: `/(?:A*)_/` }], + }, ], }, { code: String.raw`/[\q{a|bb}]|bb/v`, errors: [ - "Unexpected useless alternative. This alternative is a strict subset of '[\\q{a|bb}]' and can be removed.", + { + message: + "Unexpected useless alternative. This alternative is a strict subset of '[\\q{a|bb}]' and can be removed.", + line: 1, + suggestions: [ + { + messageId: "remove", + output: String.raw`/[\q{a|bb}]/v`, + }, + ], + }, ], }, { @@ -710,6 +976,15 @@ tester.run("no-dupe-disjunctions", rule as any, { message: "Unexpected useless alternative. This alternative is already covered by 'a' and can be removed.", line: 2, + suggestions: [ + { + messageId: "remove", + output: ` + const a = /a/.source; + const b = RegExp(\`\\b(\${a})\\b\`); + `, + }, + ], }, ], }, @@ -725,11 +1000,21 @@ tester.run("no-dupe-disjunctions", rule as any, { message: "Unexpected duplicate alternative. This alternative can be removed. This ambiguity might cause exponential backtracking.", line: 2, + suggestions: [ + { + messageId: "remove", + output: ` + const a = /a/.source; + const b = RegExp(\`\\b(\${a})\\b\`); + `, + }, + ], }, { message: "Unexpected duplicate alternative. This alternative can be removed.", line: 3, + suggestions: [], }, ], }, @@ -750,18 +1035,27 @@ tester.run("no-dupe-disjunctions", rule as any, { message: "Unexpected useless alternative. This alternative is already covered by 'a?' and can be removed.", line: 2, - column: 33, + suggestions: [ + { + messageId: "remove", + output: ` + const partial = /a?/.source; + const whole = RegExp(\`^(?:\${partial})+$\`); + `, + }, + ], }, { message: "Unexpected overlap. This alternative overlaps with 'a?'. The overlap is 'a'. This ambiguity might cause exponential backtracking.", line: 2, - column: 33, + suggestions: [], }, { message: "Unexpected overlap. This alternative overlaps with 'a?'. The overlap is 'a'. This ambiguity is likely to cause exponential backtracking.", line: 3, + suggestions: [], }, ], }, diff --git a/tests/lib/rules/no-empty-alternative.ts b/tests/lib/rules/no-empty-alternative.ts index dff65e64c..c153453cd 100644 --- a/tests/lib/rules/no-empty-alternative.ts +++ b/tests/lib/rules/no-empty-alternative.ts @@ -17,7 +17,9 @@ tester.run("no-empty-alternative", rule as any, { { messageId: "empty", column: 2, - suggestions: [{ output: `/(?:||||)??/` }], + suggestions: [ + { messageId: "suggest", output: `/(?:||||)??/` }, + ], }, ], }, @@ -27,7 +29,9 @@ tester.run("no-empty-alternative", rule as any, { { messageId: "empty", column: 8, - suggestions: [{ output: `/((?:a+|b+)?)/` }], + suggestions: [ + { messageId: "suggest", output: `/((?:a+|b+)?)/` }, + ], }, ], }, @@ -46,7 +50,10 @@ tester.run("no-empty-alternative", rule as any, { errors: [ { messageId: "empty", - suggestions: [{ output: String.raw`/(?(?:a|b)?)/` }], + column: 13, + suggestions: [ + { messageId: "suggest", output: `/(?(?:a|b)?)/` }, + ], }, ], }, @@ -55,7 +62,10 @@ tester.run("no-empty-alternative", rule as any, { errors: [ { messageId: "empty", - suggestions: [{ output: String.raw`/(?:a|b)?f/` }], + column: 8, + suggestions: [ + { messageId: "suggest", output: `/(?:a|b)?f/` }, + ], }, ], }, @@ -64,7 +74,10 @@ tester.run("no-empty-alternative", rule as any, { errors: [ { messageId: "empty", - suggestions: [{ output: String.raw`/(?:(?:a|b)?)+f/` }], + column: 8, + suggestions: [ + { messageId: "suggest", output: `/(?:(?:a|b)?)+f/` }, + ], }, ], }, diff --git a/tests/lib/rules/no-escape-backspace.ts b/tests/lib/rules/no-escape-backspace.ts index f15449c3a..d4ac13831 100644 --- a/tests/lib/rules/no-escape-backspace.ts +++ b/tests/lib/rules/no-escape-backspace.ts @@ -24,7 +24,12 @@ tester.run("no-escape-backspace", rule as any, { message: "Unexpected '[\\b]'. Use '\\u0008' instead.", column: 3, endColumn: 5, - suggestions: [{ output: "/[\\u0008]/" }], + suggestions: [ + { + messageId: "suggest", + output: String.raw`/[\u0008]/`, + }, + ], }, ], }, @@ -35,7 +40,12 @@ tester.run("no-escape-backspace", rule as any, { message: "Unexpected '[\\b]'. Use '\\u0008' instead.", column: 6, endColumn: 8, - suggestions: [{ output: String.raw`/[\q{\u0008}]/v` }], + suggestions: [ + { + messageId: "suggest", + output: String.raw`/[\q{\u0008}]/v`, + }, + ], }, ], }, diff --git a/tests/lib/rules/no-lazy-ends.ts b/tests/lib/rules/no-lazy-ends.ts index 5de2b385e..a5b440dbd 100644 --- a/tests/lib/rules/no-lazy-ends.ts +++ b/tests/lib/rules/no-lazy-ends.ts @@ -41,8 +41,14 @@ tester.run("no-lazy-ends", rule as any, { line: 1, column: 2, suggestions: [ - { output: "/(?:)/.test(str)" }, - { output: "/a?/.test(str)" }, + { + messageId: "suggestRemoveElement", + output: `/(?:)/.test(str)`, + }, + { + messageId: "suggestMakeGreedy", + output: `/a?/.test(str)`, + }, ], }, ], @@ -56,8 +62,14 @@ tester.run("no-lazy-ends", rule as any, { line: 1, column: 2, suggestions: [ - { output: "/(?:)/.test(str)" }, - { output: "/a*/.test(str)" }, + { + messageId: "suggestRemoveElement", + output: `/(?:)/.test(str)`, + }, + { + messageId: "suggestMakeGreedy", + output: `/a*/.test(str)`, + }, ], }, ], @@ -71,8 +83,14 @@ tester.run("no-lazy-ends", rule as any, { line: 1, column: 2, suggestions: [ - { output: "/a/.test(str)" }, - { output: "/a+/.test(str)" }, + { + messageId: "suggestRemoveQuantifier", + output: `/a/.test(str)`, + }, + { + messageId: "suggestMakeGreedy", + output: `/a+/.test(str)`, + }, ], }, ], @@ -86,8 +104,14 @@ tester.run("no-lazy-ends", rule as any, { line: 1, column: 2, suggestions: [ - { output: "/a{3}/.test(str)" }, - { output: "/a{3,7}/.test(str)" }, + { + messageId: "suggestRange", + output: `/a{3}/.test(str)`, + }, + { + messageId: "suggestMakeGreedy", + output: `/a{3,7}/.test(str)`, + }, ], }, ], @@ -101,8 +125,14 @@ tester.run("no-lazy-ends", rule as any, { line: 1, column: 2, suggestions: [ - { output: "/a{3}/.test(str)" }, - { output: "/a{3,}/.test(str)" }, + { + messageId: "suggestRange", + output: `/a{3}/.test(str)`, + }, + { + messageId: "suggestMakeGreedy", + output: `/a{3,}/.test(str)`, + }, ], }, ], @@ -117,8 +147,14 @@ tester.run("no-lazy-ends", rule as any, { line: 1, column: 9, suggestions: [ - { output: `/(?:a|b(c))/.test(str)` }, - { output: `/(?:a|b(c+))/.test(str)` }, + { + messageId: "suggestRemoveQuantifier", + output: `/(?:a|b(c))/.test(str)`, + }, + { + messageId: "suggestMakeGreedy", + output: `/(?:a|b(c+))/.test(str)`, + }, ], }, ], @@ -132,8 +168,14 @@ tester.run("no-lazy-ends", rule as any, { line: 1, column: 9, suggestions: [ - { output: `/a(?:c|ab)?/.test(str)` }, - { output: `/a(?:c|ab+)?/.test(str)` }, + { + messageId: "suggestRemoveQuantifier", + output: `/a(?:c|ab)?/.test(str)`, + }, + { + messageId: "suggestMakeGreedy", + output: `/a(?:c|ab+)?/.test(str)`, + }, ], }, ], @@ -154,6 +196,32 @@ tester.run("no-lazy-ends", rule as any, { "The quantifier and the quantified element can be removed because the quantifier is lazy and has a minimum of 0.", line: 7, column: 26, + suggestions: [ + { + messageId: "suggestRemoveElement", + output: ` + /* ✓ GOOD */ + const any = /[\\s\\S]*?/.source; + const pattern = RegExp(\`(\${any})<\\/script>\`, "g"); + + /* ✗ BAD */ + const foo = /(?:)/ + foo.exec(str) + `, + }, + { + messageId: "suggestMakeGreedy", + output: ` + /* ✓ GOOD */ + const any = /[\\s\\S]*?/.source; + const pattern = RegExp(\`(\${any})<\\/script>\`, "g"); + + /* ✗ BAD */ + const foo = /[\\s\\S]*/ + foo.exec(str) + `, + }, + ], }, ], }, @@ -174,6 +242,32 @@ tester.run("no-lazy-ends", rule as any, { "The quantifier and the quantified element can be removed because the quantifier is lazy and has a minimum of 0.", line: 7, column: 26, + suggestions: [ + { + messageId: "suggestRemoveElement", + output: ` + /* ✓ GOOD */ + const any = /[\\s\\S]*?/.source; + const pattern = RegExp(\`(\${any})<\\/script>\`, "g"); + + /* ✗ BAD */ + const foo = /(?:)/ + foo.exec(str) + `, + }, + { + messageId: "suggestMakeGreedy", + output: ` + /* ✓ GOOD */ + const any = /[\\s\\S]*?/.source; + const pattern = RegExp(\`(\${any})<\\/script>\`, "g"); + + /* ✗ BAD */ + const foo = /[\\s\\S]*/ + foo.exec(str) + `, + }, + ], }, ], }, @@ -193,12 +287,60 @@ tester.run("no-lazy-ends", rule as any, { "The quantifier and the quantified element can be removed because the quantifier is lazy and has a minimum of 0.", line: 3, column: 26, + suggestions: [ + { + messageId: "suggestRemoveElement", + output: ` + /* ✗ BAD */ + const any = /(?:)/.source; + const pattern = RegExp(\`(\${any})<\\/script>\`, "g"); + + const foo = /[\\s\\S]*?/ + foo.exec(str) + `, + }, + { + messageId: "suggestMakeGreedy", + output: ` + /* ✗ BAD */ + const any = /[\\s\\S]*/.source; + const pattern = RegExp(\`(\${any})<\\/script>\`, "g"); + + const foo = /[\\s\\S]*?/ + foo.exec(str) + `, + }, + ], }, { message: "The quantifier and the quantified element can be removed because the quantifier is lazy and has a minimum of 0.", line: 6, column: 26, + suggestions: [ + { + messageId: "suggestRemoveElement", + output: ` + /* ✗ BAD */ + const any = /[\\s\\S]*?/.source; + const pattern = RegExp(\`(\${any})<\\/script>\`, "g"); + + const foo = /(?:)/ + foo.exec(str) + `, + }, + { + messageId: "suggestMakeGreedy", + output: ` + /* ✗ BAD */ + const any = /[\\s\\S]*?/.source; + const pattern = RegExp(\`(\${any})<\\/script>\`, "g"); + + const foo = /[\\s\\S]*/ + foo.exec(str) + `, + }, + ], }, ], }, @@ -210,6 +352,16 @@ tester.run("no-lazy-ends", rule as any, { "The quantifier and the quantified element can be removed because the quantifier is lazy and has a minimum of 0.", line: 1, column: 2, + suggestions: [ + { + messageId: "suggestRemoveElement", + output: `/(?:)/v.test(str)`, + }, + { + messageId: "suggestMakeGreedy", + output: String.raw`/[\q{ab|}]?/v.test(str)`, + }, + ], }, ], }, diff --git a/tests/lib/rules/no-misleading-capturing-group.ts b/tests/lib/rules/no-misleading-capturing-group.ts index 1dd2b7939..e5391353c 100644 --- a/tests/lib/rules/no-misleading-capturing-group.ts +++ b/tests/lib/rules/no-misleading-capturing-group.ts @@ -27,13 +27,31 @@ tester.run("no-misleading-capturing-group", rule as any, { { code: String.raw`/\d+(\d*)/`, errors: [ - "'\\d*' can be removed because it is already included by '\\d+'. This makes the capturing group misleading, because it actually captures less text than its pattern suggests.", + { + message: + "'\\d*' can be removed because it is already included by '\\d+'. This makes the capturing group misleading, because it actually captures less text than its pattern suggests.", + suggestions: [ + { + messageId: "suggestionRemove", + output: String.raw`/\d+()/`, + }, + ], + }, ], }, { code: String.raw`/(?:!\d+|%\w+)(\d*)/`, errors: [ - "'\\d*' can be removed because it is already included by '\\d+' and '\\w+'. This makes the capturing group misleading, because it actually captures less text than its pattern suggests.", + { + message: + "'\\d*' can be removed because it is already included by '\\d+' and '\\w+'. This makes the capturing group misleading, because it actually captures less text than its pattern suggests.", + suggestions: [ + { + messageId: "suggestionRemove", + output: String.raw`/(?:!\d+|%\w+)()/`, + }, + ], + }, ], }, diff --git a/tests/lib/rules/no-misleading-unicode-character.ts b/tests/lib/rules/no-misleading-unicode-character.ts index 7dd365f4e..1ff1bcbc6 100644 --- a/tests/lib/rules/no-misleading-unicode-character.ts +++ b/tests/lib/rules/no-misleading-unicode-character.ts @@ -12,8 +12,8 @@ tester.run("no-misleading-unicode-character", rule as any, { valid: [ `/[👍]/u`, `/👍+/u`, - `/[\uD83D\uDC4D]/u`, - `/[\u{1F44D}]/u`, + String.raw`/[\uD83D\uDC4D]/u`, + String.raw`/[\u{1F44D}]/u`, `/❇️/`, `/Á/`, `/[❇]/`, @@ -73,7 +73,12 @@ tester.run("no-misleading-unicode-character", rule as any, { errors: [ { messageId: "characterClass", - suggestions: [{ output: `/👍foo/` }], + suggestions: [ + { + messageId: "fixCharacterClass", + output: String.raw`/👍foo/`, + }, + ], }, ], }, @@ -123,24 +128,6 @@ tester.run("no-misleading-unicode-character", rule as any, { options: [{ fixable: true }], errors: [{ messageId: "characterClass" }], }, - { - code: `/[\u0041\u0301]/`, - output: `/\u0041\u0301/`, - options: [{ fixable: true }], - errors: [{ messageId: "characterClass" }], - }, - { - code: `/[\u0041\u0301]/u`, - output: `/\u0041\u0301/u`, - options: [{ fixable: true }], - errors: [{ messageId: "characterClass" }], - }, - { - code: `/[\u{41}\u{301}]/u`, - output: `/\u{41}\u{301}/u`, - options: [{ fixable: true }], - errors: [{ messageId: "characterClass" }], - }, { code: `/[❇️]/`, output: `/❇️/`, diff --git a/tests/lib/rules/no-unused-capturing-group.ts b/tests/lib/rules/no-unused-capturing-group.ts index e51efe1ab..1ccc4efee 100644 --- a/tests/lib/rules/no-unused-capturing-group.ts +++ b/tests/lib/rules/no-unused-capturing-group.ts @@ -174,50 +174,190 @@ tester.run("no-unused-capturing-group", rule as any, { output: String.raw`'2000-12-31'.replace(/(?:\d{4})-(?:\d{2})-(?:\d{2})/, 'Date') `, options: [{ fixable: true }], errors: [ - "Capturing group number 1 is defined but never used.", - "Capturing group number 2 is defined but never used.", - "Capturing group number 3 is defined but never used.", + { + message: + "Capturing group number 1 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw`'2000-12-31'.replace(/(?:\d{4})-(\d{2})-(\d{2})/, 'Date') `, + }, + ], + }, + { + message: + "Capturing group number 2 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw`'2000-12-31'.replace(/(\d{4})-(?:\d{2})-(\d{2})/, 'Date') `, + }, + ], + }, + { + message: + "Capturing group number 3 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw`'2000-12-31'.replace(/(\d{4})-(\d{2})-(?:\d{2})/, 'Date') `, + }, + ], + }, ], }, { code: String.raw`'2000-12-31'.replace(/(\d{4})-(\d{2})-(\d{2})/, '$1/$2') // "2000/12"`, output: String.raw`'2000-12-31'.replace(/(\d{4})-(\d{2})-(?:\d{2})/, '$1/$2') // "2000/12"`, options: [{ fixable: true }], - errors: ["Capturing group number 3 is defined but never used."], + errors: [ + { + message: + "Capturing group number 3 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw`'2000-12-31'.replace(/(\d{4})-(\d{2})-(?:\d{2})/, '$1/$2') // "2000/12"`, + }, + ], + }, + ], }, { code: String.raw`'2000-12-31'.replace(/(?\d{4})-(?\d{2})-(?\d{2})/u, '$/$') // "2000/12"`, output: String.raw`'2000-12-31'.replace(/(?\d{4})-(?\d{2})-(?:\d{2})/u, '$/$') // "2000/12"`, options: [{ fixable: true }], - errors: ["Capturing group 'd' is defined but never used."], + errors: [ + { + message: "Capturing group 'd' is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw`'2000-12-31'.replace(/(?\d{4})-(?\d{2})-(?:\d{2})/u, '$/$') // "2000/12"`, + }, + ], + }, + ], }, { code: String.raw`/(\d{4})-(\d{2})-(\d{2})/.test('2000-12-31') // true`, errors: [ - "Capturing group number 1 is defined but never used.", - "Capturing group number 2 is defined but never used.", - "Capturing group number 3 is defined but never used.", + { + message: + "Capturing group number 1 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw`/(?:\d{4})-(\d{2})-(\d{2})/.test('2000-12-31') // true`, + }, + ], + }, + { + message: + "Capturing group number 2 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw`/(\d{4})-(?:\d{2})-(\d{2})/.test('2000-12-31') // true`, + }, + ], + }, + { + message: + "Capturing group number 3 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw`/(\d{4})-(\d{2})-(?:\d{2})/.test('2000-12-31') // true`, + }, + ], + }, ], }, { code: String.raw`'2000-12-31 2001-01-01'.match(/(\d{4})-(\d{2})-(\d{2})/g) // ["2000-12-31", "2001-01-01"]`, errors: [ - "Capturing group number 1 is defined but never used.", - "Capturing group number 2 is defined but never used.", - "Capturing group number 3 is defined but never used.", + { + message: + "Capturing group number 1 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw`'2000-12-31 2001-01-01'.match(/(?:\d{4})-(\d{2})-(\d{2})/g) // ["2000-12-31", "2001-01-01"]`, + }, + ], + }, + { + message: + "Capturing group number 2 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw`'2000-12-31 2001-01-01'.match(/(\d{4})-(?:\d{2})-(\d{2})/g) // ["2000-12-31", "2001-01-01"]`, + }, + ], + }, + { + message: + "Capturing group number 3 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw`'2000-12-31 2001-01-01'.match(/(\d{4})-(\d{2})-(?:\d{2})/g) // ["2000-12-31", "2001-01-01"]`, + }, + ], + }, ], }, { code: String.raw`'2000-12-31'.search(/(\d{4})-(\d{2})-(\d{2})/) // 0`, errors: [ - "Capturing group number 1 is defined but never used.", - "Capturing group number 2 is defined but never used.", - "Capturing group number 3 is defined but never used.", + { + message: + "Capturing group number 1 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw`'2000-12-31'.search(/(?:\d{4})-(\d{2})-(\d{2})/) // 0`, + }, + ], + }, + { + message: + "Capturing group number 2 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw`'2000-12-31'.search(/(\d{4})-(?:\d{2})-(\d{2})/) // 0`, + }, + ], + }, + { + message: + "Capturing group number 3 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw`'2000-12-31'.search(/(\d{4})-(\d{2})-(?:\d{2})/) // 0`, + }, + ], + }, ], }, { code: "var replaced = '2000-12-31'.replace(/(\\d{4})-(\\d{2})-(\\d{2})/, (_, y, m) => `${y}/${m}`)", - errors: ["Capturing group number 3 is defined but never used."], + errors: [ + { + message: + "Capturing group number 3 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: `var replaced = '2000-12-31'.replace(/(\\d{4})-(\\d{2})-(?:\\d{2})/, (_, y, m) => \`\${y}/\${m}\`)`, + }, + ], + }, + ], }, { code: String.raw` @@ -225,12 +365,81 @@ tester.run("no-unused-capturing-group", rule as any, { var replaced = '2000-12-31'.replace(/(?\d{4})-(?\d{2})-(?\d{2})/u, '$/$/$
') `, errors: [ - "Capturing group number 1 is defined but never used.", - "Capturing group number 2 is defined but never used.", - "Capturing group number 3 is defined but never used.", - "Capturing group 'y' is defined but never used.", - "Capturing group 'm' is defined but never used.", - "Capturing group 'd' is defined but never used.", + { + message: + "Capturing group number 1 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw` + var replaced = '2000-12-31'.replace(/(?:\d{4})-(\d{2})-(\d{2})/, '$4/$5/$6') + var replaced = '2000-12-31'.replace(/(?\d{4})-(?\d{2})-(?\d{2})/u, '$/$/$
') + `, + }, + ], + }, + { + message: + "Capturing group number 2 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw` + var replaced = '2000-12-31'.replace(/(\d{4})-(?:\d{2})-(\d{2})/, '$4/$5/$6') + var replaced = '2000-12-31'.replace(/(?\d{4})-(?\d{2})-(?\d{2})/u, '$/$/$
') + `, + }, + ], + }, + { + message: + "Capturing group number 3 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw` + var replaced = '2000-12-31'.replace(/(\d{4})-(\d{2})-(?:\d{2})/, '$4/$5/$6') + var replaced = '2000-12-31'.replace(/(?\d{4})-(?\d{2})-(?\d{2})/u, '$/$/$
') + `, + }, + ], + }, + { + message: "Capturing group 'y' is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw` + var replaced = '2000-12-31'.replace(/(\d{4})-(\d{2})-(\d{2})/, '$4/$5/$6') + var replaced = '2000-12-31'.replace(/(?:\d{4})-(?\d{2})-(?\d{2})/u, '$/$/$
') + `, + }, + ], + }, + { + message: "Capturing group 'm' is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw` + var replaced = '2000-12-31'.replace(/(\d{4})-(\d{2})-(\d{2})/, '$4/$5/$6') + var replaced = '2000-12-31'.replace(/(?\d{4})-(?:\d{2})-(?\d{2})/u, '$/$/$
') + `, + }, + ], + }, + { + message: "Capturing group 'd' is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw` + var replaced = '2000-12-31'.replace(/(\d{4})-(\d{2})-(\d{2})/, '$4/$5/$6') + var replaced = '2000-12-31'.replace(/(?\d{4})-(?\d{2})-(?:\d{2})/u, '$/$/$
') + `, + }, + ], + }, ], }, { @@ -238,9 +447,42 @@ tester.run("no-unused-capturing-group", rule as any, { var replaced = '2000-12-31'.replaceAll(/(\d{4})-(\d{2})-(\d{2})/g, 'foo') `, errors: [ - "Capturing group number 1 is defined but never used.", - "Capturing group number 2 is defined but never used.", - "Capturing group number 3 is defined but never used.", + { + message: + "Capturing group number 1 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw` + var replaced = '2000-12-31'.replaceAll(/(?:\d{4})-(\d{2})-(\d{2})/g, 'foo') + `, + }, + ], + }, + { + message: + "Capturing group number 2 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw` + var replaced = '2000-12-31'.replaceAll(/(\d{4})-(?:\d{2})-(\d{2})/g, 'foo') + `, + }, + ], + }, + { + message: + "Capturing group number 3 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw` + var replaced = '2000-12-31'.replaceAll(/(\d{4})-(\d{2})-(?:\d{2})/g, 'foo') + `, + }, + ], + }, ], }, { @@ -253,6 +495,12 @@ tester.run("no-unused-capturing-group", rule as any, { column: 61, endLine: 1, endColumn: 68, + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw`var index = '2000-12-31 2000-12-31'.search(/(\d{4})-(\d{2})-(?:\d{2}) \1-\2-\2/)`, + }, + ], }, ], }, @@ -267,7 +515,23 @@ tester.run("no-unused-capturing-group", rule as any, { var m = matches[2] // "12" // var d = matches[3] // "31" `, - errors: ["Capturing group number 3 is defined but never used."], + errors: [ + { + message: + "Capturing group number 3 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw` + var matches = '2000-12-31'.match(/(\d{4})-(\d{2})-(?:\d{2})/) + var y = matches[1] // "2000" + var m = matches[2] // "12" + // var d = matches[3] // "31" + `, + }, + ], + }, + ], }, { code: String.raw` @@ -295,7 +559,19 @@ tester.run("no-unused-capturing-group", rule as any, { code: String.raw` const {y,m} = '2000-12-31'.match(/(?\d{4})-(?\d{2})-(?\d{2})/)["groups"] `, - errors: ["Capturing group 'd' is defined but never used."], + errors: [ + { + message: "Capturing group 'd' is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw` + const {y,m} = '2000-12-31'.match(/(?\d{4})-(?\d{2})-(?:\d{2})/)["groups"] + `, + }, + ], + }, + ], }, { code: String.raw` @@ -319,8 +595,40 @@ tester.run("no-unused-capturing-group", rule as any, { `, errors: [ - "Capturing group number 2 is defined but never used.", - "Capturing group number 3 is defined but never used.", + { + message: + "Capturing group number 2 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw` + for (const matches of '2000-12-31 2000-12-31'.matchAll(/(\d{4})-(?:\d{2})-(\d{2})/g)) { + var y = matches[1] // "2000" + // var m = matches[2] // "12" + // var d = matches[3] // "31" + } + + `, + }, + ], + }, + { + message: + "Capturing group number 3 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw` + for (const matches of '2000-12-31 2000-12-31'.matchAll(/(\d{4})-(\d{2})-(?:\d{2})/g)) { + var y = matches[1] // "2000" + // var m = matches[2] // "12" + // var d = matches[3] // "31" + } + + `, + }, + ], + }, ], }, { @@ -332,7 +640,24 @@ tester.run("no-unused-capturing-group", rule as any, { // var d = matches[3] // "31" } `, - errors: ["Capturing group 'd' is defined but never used."], + errors: [ + { + message: "Capturing group 'd' is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw` + const re = '2000-12-31 2000-12-31'.matchAll(/(?\d{4})-(?\d{2})-(?:\d{2})/g) + for (const matches of re) { + var y = matches[1] // "2000" + var m = matches.groups.m // "12" + // var d = matches[3] // "31" + } + `, + }, + ], + }, + ], }, { code: String.raw` @@ -344,7 +669,25 @@ tester.run("no-unused-capturing-group", rule as any, { // var d = matches[3] // "31" } `, - errors: ["Capturing group 'd' is defined but never used."], + errors: [ + { + message: "Capturing group 'd' is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw` + let re + re = '2000-12-31 2000-12-31'.matchAll(/(?\d{4})-(?\d{2})-(?:\d{2})/g) + for (const matches of re) { + var y = matches[1] // "2000" + var m = matches.groups.m // "12" + // var d = matches[3] // "31" + } + `, + }, + ], + }, + ], }, { code: String.raw` @@ -357,7 +700,27 @@ tester.run("no-unused-capturing-group", rule as any, { // var d = matches[3] // "31" } `, - errors: ["Capturing group number 3 is defined but never used."], + errors: [ + { + message: + "Capturing group number 3 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw` + let re + re = '2000-12-31 2000-12-31'.matchAll(/(?\d{4})-(?\d{2})-(?:\d{2})/g) + for (const matches of re) { + var y = matches[1] // "2000" + const {...groups} = matches.groups + var m = groups.m // "12" + // var d = matches[3] // "31" + } + `, + }, + ], + }, + ], }, { code: String.raw` @@ -368,7 +731,25 @@ tester.run("no-unused-capturing-group", rule as any, { // var d = matches[3] // "31" } `, - errors: ["Capturing group number 3 is defined but never used."], + errors: [ + { + message: + "Capturing group number 3 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw` + const result = [...'2000-12-31 2000-12-31'.matchAll(/(?\d{4})-(?\d{2})-(?:\d{2})/g)] + for (const matches of result) { + var y = matches[1] // "2000" + var m = matches.groups.m // "12" + // var d = matches[3] // "31" + } + `, + }, + ], + }, + ], }, { code: String.raw` @@ -384,8 +765,47 @@ tester.run("no-unused-capturing-group", rule as any, { } `, errors: [ - "Capturing group number 3 is defined but never used.", - "Capturing group 'd' is defined but never used.", + { + message: + "Capturing group number 3 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw` + const result = [ + { 1: 2000, groups: { m: 12 } }, + ...'2000-12-31 2000-12-31'.matchAll(/(?\d{4})-(?\d{2})-(?:\d{2})/g), + ...'2000/12/31 2000/12/31'.matchAll(/(?\d{4})\/(?\d{2})\/(?\d{2})/g), + ] + for (const matches of result) { + var y = matches[1] + var m = matches.groups.m + // var d = matches[3] + } + `, + }, + ], + }, + { + message: "Capturing group 'd' is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw` + const result = [ + { 1: 2000, groups: { m: 12 } }, + ...'2000-12-31 2000-12-31'.matchAll(/(?\d{4})-(?\d{2})-(\d{2})/g), + ...'2000/12/31 2000/12/31'.matchAll(/(?\d{4})\/(?\d{2})\/(?:\d{2})/g), + ] + for (const matches of result) { + var y = matches[1] + var m = matches.groups.m + // var d = matches[3] + } + `, + }, + ], + }, ], }, { @@ -500,7 +920,17 @@ tester.run("no-unused-capturing-group", rule as any, { }, { code: `'str'.replace(/(?\\w+)/, () => {});`, - errors: ["Capturing group 'foo' is defined but never used."], + errors: [ + { + message: "Capturing group 'foo' is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw`'str'.replace(/(?:\w+)/, () => {});`, + }, + ], + }, + ], }, { code: String.raw` @@ -510,7 +940,20 @@ tester.run("no-unused-capturing-group", rule as any, { const bs = [...'abc_abc'.matchAll(/a(?:b)/g)].map(m => m[0]) `, options: [{ fixable: true }], - errors: ["Capturing group number 1 is defined but never used."], + errors: [ + { + message: + "Capturing group number 1 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw` + const bs = [...'abc_abc'.matchAll(/a(?:b)/g)].map(m => m[0]) + `, + }, + ], + }, + ], }, { code: String.raw` @@ -520,7 +963,20 @@ tester.run("no-unused-capturing-group", rule as any, { ;[...'abc_abc'.matchAll(/a(b)(?:c)/g)].forEach(m => console.log(m[1])) `, options: [{ fixable: true }], - errors: ["Capturing group number 2 is defined but never used."], + errors: [ + { + message: + "Capturing group number 2 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw` + ;[...'abc_abc'.matchAll(/a(b)(?:c)/g)].forEach(m => console.log(m[1])) + `, + }, + ], + }, + ], }, { code: String.raw` @@ -559,7 +1015,21 @@ tester.run("no-unused-capturing-group", rule as any, { const replaced = text.replace(/(?:[\q{Lorem|ipsum}])/gv, '**Lorem ipsum**') `, options: [{ fixable: true }], - errors: ["Capturing group number 1 is defined but never used."], + errors: [ + { + message: + "Capturing group number 1 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw` + const text = 'Lorem ipsum dolor sit amet' + const replaced = text.replace(/(?:[\q{Lorem|ipsum}])/gv, '**Lorem ipsum**') + `, + }, + ], + }, + ], }, // hasIndices { @@ -570,7 +1040,20 @@ tester.run("no-unused-capturing-group", rule as any, { console.log(re.exec('foo').indices[2]) `, options: [{ fixable: true }], - errors: ["Capturing group number 1 is defined but never used."], + errors: [ + { + message: + "Capturing group number 1 is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw`const re = /(?:foo)/d + console.log(re.exec('foo').indices[2]) + `, + }, + ], + }, + ], }, { code: String.raw`const re = /(?foo)/d @@ -580,7 +1063,19 @@ tester.run("no-unused-capturing-group", rule as any, { console.log(re.exec('foo').indices.groups.x) `, options: [{ fixable: true }], - errors: ["Capturing group 'f' is defined but never used."], + errors: [ + { + message: "Capturing group 'f' is defined but never used.", + suggestions: [ + { + messageId: "makeNonCapturing", + output: String.raw`const re = /(?:foo)/d + console.log(re.exec('foo').indices.groups.x) + `, + }, + ], + }, + ], }, ], }) diff --git a/tests/lib/rules/no-useless-assertions.ts b/tests/lib/rules/no-useless-assertions.ts index b2220e23a..31e3ef0d5 100644 --- a/tests/lib/rules/no-useless-assertions.ts +++ b/tests/lib/rules/no-useless-assertions.ts @@ -277,6 +277,12 @@ tester.run("no-useless-assertions", rule as any, { { message: "'$' will always accept because it is followed by a line-terminator character.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/foo\n/m`, + }, + ], }, ], }, @@ -314,25 +320,57 @@ tester.run("no-useless-assertions", rule as any, { { code: String.raw`/(?=\w)hello/`, errors: [ - { message: "The lookahead '(?=\\w)' will always accept." }, + { + message: "The lookahead '(?=\\w)' will always accept.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/hello/`, + }, + ], + }, ], }, { code: String.raw`/(?=\w)\d/`, errors: [ - { message: "The lookahead '(?=\\w)' will always accept." }, + { + message: "The lookahead '(?=\\w)' will always accept.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/\d/`, + }, + ], + }, ], }, { code: String.raw`/(?=\w)\w/`, errors: [ - { message: "The lookahead '(?=\\w)' will always accept." }, + { + message: "The lookahead '(?=\\w)' will always accept.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/\w/`, + }, + ], + }, ], }, { code: String.raw`/(?=\w)(?:a+|b*c?|\d)d/`, errors: [ - { message: "The lookahead '(?=\\w)' will always accept." }, + { + message: "The lookahead '(?=\\w)' will always accept.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/(?:a+|b*c?|\d)d/`, + }, + ], + }, ], }, { @@ -341,6 +379,12 @@ tester.run("no-useless-assertions", rule as any, { { message: "The negative lookahead '(?!\\w)' will always reject.", + suggestions: [ + { + messageId: "rejectSuggestion", + output: String.raw`/[]/`, + }, + ], }, ], }, @@ -350,6 +394,12 @@ tester.run("no-useless-assertions", rule as any, { { message: "The negative lookahead '(?!\\w)' will always reject.", + suggestions: [ + { + messageId: "rejectSuggestion", + output: String.raw`/[]/`, + }, + ], }, ], }, @@ -359,6 +409,12 @@ tester.run("no-useless-assertions", rule as any, { { message: "The negative lookahead '(?!\\w)' will always reject.", + suggestions: [ + { + messageId: "rejectSuggestion", + output: String.raw`/[]/`, + }, + ], }, ], }, @@ -368,6 +424,12 @@ tester.run("no-useless-assertions", rule as any, { { message: "The negative lookahead '(?!\\w)' will always reject.", + suggestions: [ + { + messageId: "rejectSuggestion", + output: String.raw`/[]/`, + }, + ], }, ], }, @@ -375,12 +437,30 @@ tester.run("no-useless-assertions", rule as any, { { code: String.raw`/(?=\w),/`, errors: [ - { message: "The lookahead '(?=\\w)' will always reject." }, + { + message: "The lookahead '(?=\\w)' will always reject.", + suggestions: [ + { + messageId: "rejectSuggestion", + output: String.raw`/[]/`, + }, + ], + }, ], }, { code: String.raw`/(?=a)(,|b|c|(da)+)a/`, - errors: [{ message: "The lookahead '(?=a)' will always reject." }], + errors: [ + { + message: "The lookahead '(?=a)' will always reject.", + suggestions: [ + { + messageId: "rejectSuggestion", + output: String.raw`/[]/`, + }, + ], + }, + ], }, { code: String.raw`/(?!\w),/`, @@ -388,6 +468,12 @@ tester.run("no-useless-assertions", rule as any, { { message: "The negative lookahead '(?!\\w)' will always accept.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/,/`, + }, + ], }, ], }, @@ -397,6 +483,12 @@ tester.run("no-useless-assertions", rule as any, { { message: "The negative lookahead '(?!a)' will always accept.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/(,|b|c|(da)+)a/`, + }, + ], }, ], }, @@ -404,7 +496,15 @@ tester.run("no-useless-assertions", rule as any, { { code: String.raw`/(\d)(?=\w)\1/`, errors: [ - { message: "The lookahead '(?=\\w)' will always accept." }, + { + message: "The lookahead '(?=\\w)' will always accept.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/(\d)\1/`, + }, + ], + }, ], }, { @@ -413,6 +513,12 @@ tester.run("no-useless-assertions", rule as any, { { message: "The negative lookahead '(?!\\w)' will always reject.", + suggestions: [ + { + messageId: "rejectSuggestion", + output: String.raw`/[]/`, + }, + ], }, ], }, @@ -423,6 +529,12 @@ tester.run("no-useless-assertions", rule as any, { { message: "'\\b' will always accept because it is preceded by a word character and followed by a non-word character.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/[a-z_]\w*(?=\s*;)/`, + }, + ], }, ], }, @@ -432,6 +544,12 @@ tester.run("no-useless-assertions", rule as any, { { message: "The negative lookahead '(?!\\\\)' will always accept.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/[a-z_]\w*(?=\s*;)/`, + }, + ], }, ], }, @@ -441,10 +559,22 @@ tester.run("no-useless-assertions", rule as any, { { message: "The negative lookahead '(?!\\\\)' will always accept.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/[a-z_]\w*\b(?=\s*;)/`, + }, + ], }, { message: "'\\b' will always accept because it is preceded by a word character and followed by a non-word character.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/[a-z_]\w*(?!\\)(?=\s*;)/`, + }, + ], }, ], }, @@ -454,100 +584,308 @@ tester.run("no-useless-assertions", rule as any, { { message: "'\\b' will always accept because it is preceded by a word character and followed by a non-word character.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/[a-z_]\w*(?!\\)(?=\s*;)/`, + }, + ], }, { message: "The negative lookahead '(?!\\\\)' will always accept.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/[a-z_]\w*\b(?=\s*;)/`, + }, + ], }, ], }, { code: String.raw`/(?=a|$)b/u`, - errors: ["The lookahead '(?=a|$)' will always reject."], + errors: [ + { + message: "The lookahead '(?=a|$)' will always reject.", + suggestions: [ + { + messageId: "rejectSuggestion", + output: String.raw`/[]/u`, + }, + ], + }, + ], }, { code: String.raw`/^^a$$/`, errors: [ - "'^' will always accept because it is never preceded by a character.", - "'$' will always accept because it is never followed by a character.", + { + message: + "'^' will always accept because it is never preceded by a character.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/^a$$/`, + }, + ], + }, + { + message: + "'$' will always accept because it is never followed by a character.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/^^a$/`, + }, + ], + }, ], }, { code: String.raw`/^^a$$/m`, errors: [ - "'^' will always accept because it is preceded by a line-terminator character or the start of the input string.", - "'$' will always accept because it is followed by a line-terminator character or the end of the input string.", + { + message: + "'^' will always accept because it is preceded by a line-terminator character or the start of the input string.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/^a$$/m`, + }, + ], + }, + { + message: + "'$' will always accept because it is followed by a line-terminator character or the end of the input string.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/^^a$/m`, + }, + ], + }, ], }, { code: String.raw`/\b^a\b$/u`, errors: [ - "'\\b' will always accept because it is preceded by a non-word character and followed by a word character.", - "'\\b' will always accept because it is preceded by a word character and followed by a non-word character.", + { + message: + "'\\b' will always accept because it is preceded by a non-word character and followed by a word character.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/^a\b$/u`, + }, + ], + }, + { + message: + "'\\b' will always accept because it is preceded by a word character and followed by a non-word character.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/\b^a$/u`, + }, + ], + }, ], }, { code: String.raw`/^\ba$\b/`, errors: [ - "'\\b' will always accept because it is preceded by a non-word character and followed by a word character.", - "'\\b' will always accept because it is preceded by a word character and followed by a non-word character.", + { + message: + "'\\b' will always accept because it is preceded by a non-word character and followed by a word character.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/^a$\b/`, + }, + ], + }, + { + message: + "'\\b' will always accept because it is preceded by a word character and followed by a non-word character.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/^\ba$/`, + }, + ], + }, ], }, { code: String.raw`/\b\ba\b\b/u`, errors: [ - "'\\b' will always accept because it is preceded by a non-word character and followed by a word character.", - "'\\b' will always accept because it is preceded by a word character and followed by a non-word character.", + { + message: + "'\\b' will always accept because it is preceded by a non-word character and followed by a word character.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/\ba\b\b/u`, + }, + ], + }, + { + message: + "'\\b' will always accept because it is preceded by a word character and followed by a non-word character.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/\b\ba\b/u`, + }, + ], + }, ], }, { code: String.raw`/Java(?=Script)$/`, - errors: ["The lookahead '(?=Script)' will always reject."], + errors: [ + { + message: "The lookahead '(?=Script)' will always reject.", + suggestions: [ + { + messageId: "rejectSuggestion", + output: String.raw`/[]/`, + }, + ], + }, + ], }, { code: String.raw`/Java$(?=Script)/`, errors: [ - "'$' will always reject because it is followed by a character.", + { + message: + "'$' will always reject because it is followed by a character.", + suggestions: [ + { + messageId: "rejectSuggestion", + output: String.raw`/[]/`, + }, + ], + }, ], }, { code: String.raw`/a$(?!.)/s`, errors: [ - "'$' will always accept because it is never followed by a character.", + { + message: + "'$' will always accept because it is never followed by a character.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/a(?!.)/s`, + }, + ], + }, ], }, { code: String.raw`/a(?!.)$/s`, - errors: ["The negative lookahead '(?!.)' will always accept."], + errors: [ + { + message: + "The negative lookahead '(?!.)' will always accept.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/a$/s`, + }, + ], + }, + ], }, { code: String.raw`/^(\b|\B-[a-z]{1,10}-)((?:repeating-)?(?:linear|radial)-gradient)/`, errors: [ - "'\\b' will always accept because it is preceded by a non-word character and followed by a word character.", - "'\\B' will always accept because it is preceded by a non-word character and followed by a non-word character.", + { + message: + "'\\b' will always accept because it is preceded by a non-word character and followed by a word character.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/^(|\B-[a-z]{1,10}-)((?:repeating-)?(?:linear|radial)-gradient)/`, + }, + ], + }, + { + message: + "'\\B' will always accept because it is preceded by a non-word character and followed by a non-word character.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/^(\b|-[a-z]{1,10}-)((?:repeating-)?(?:linear|radial)-gradient)/`, + }, + ], + }, ], }, { code: String.raw`/(?!$)$/`, - errors: ["The negative lookahead '(?!$)' will always reject."], + errors: [ + { + message: + "The negative lookahead '(?!$)' will always reject.", + suggestions: [ + { + messageId: "rejectSuggestion", + output: String.raw`/[]/`, + }, + ], + }, + ], }, { code: String.raw`/(?=a|$)a/`, - errors: ["The lookahead '(?=a|$)' will always accept."], + errors: [ + { + message: "The lookahead '(?=a|$)' will always accept.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/a/`, + }, + ], + }, + ], }, { code: String.raw`/(?=(?=[a-f])(?=a|A)[\w%])a/`, errors: [ - "The lookahead '(?=(?=[a-f])(?=a|A)[\\w%])' will always accept.", + { + message: + "The lookahead '(?=(?=[a-f])(?=a|A)[\\w%])' will always accept.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/a/`, + }, + ], + }, ], }, { code: String.raw`/(?=(?=[a-f])(?=[aA])\w(?<=[aA])(?<=[a-f]))a/`, errors: [ - "The lookahead '(?=(?=[a-f])(?=[aA])\\w(?<=[aA])(?<=[a-f]))' will always accept.", + { + message: + "The lookahead '(?=(?=[a-f])(?=[aA])\\w(?<=[aA])(?<=[a-f]))' will always accept.", + suggestions: [ + { + messageId: "acceptSuggestion", + output: String.raw`/a/`, + }, + ], + }, ], }, diff --git a/tests/lib/rules/no-zero-quantifier.ts b/tests/lib/rules/no-zero-quantifier.ts index f1714b7a1..91cf98c54 100644 --- a/tests/lib/rules/no-zero-quantifier.ts +++ b/tests/lib/rules/no-zero-quantifier.ts @@ -18,7 +18,7 @@ tester.run("no-zero-quantifier", rule as any, { messageId: "unexpected", column: 2, endColumn: 6, - suggestions: [{ output: `/(?:)/` }], + suggestions: [{ messageId: "remove", output: `/(?:)/` }], }, ], }, @@ -27,7 +27,7 @@ tester.run("no-zero-quantifier", rule as any, { errors: [ { messageId: "unexpected", - suggestions: [{ output: `/(?:)/v` }], + suggestions: [{ messageId: "remove", output: `/(?:)/v` }], }, ], }, @@ -38,7 +38,7 @@ tester.run("no-zero-quantifier", rule as any, { messageId: "unexpected", column: 2, endColumn: 8, - suggestions: [{ output: `/(?:)/` }], + suggestions: [{ messageId: "remove", output: `/(?:)/` }], }, ], }, @@ -49,7 +49,7 @@ tester.run("no-zero-quantifier", rule as any, { messageId: "unexpected", column: 2, endColumn: 9, - suggestions: [{ output: `/b/` }], + suggestions: [{ messageId: "remove", output: `/b/` }], }, ], }, diff --git a/tests/lib/rules/optimal-lookaround-quantifier.ts b/tests/lib/rules/optimal-lookaround-quantifier.ts index d4a38ce12..80775cc51 100644 --- a/tests/lib/rules/optimal-lookaround-quantifier.ts +++ b/tests/lib/rules/optimal-lookaround-quantifier.ts @@ -18,7 +18,9 @@ tester.run("optimal-lookaround-quantifier", rule as any, { message: "The quantified expression 'a*' at the end of the expression tree should only be matched a constant number of times. The expression can be removed without affecting the lookaround.", column: 6, - suggestions: [{ output: `/(?=b)/` }], + suggestions: [ + { messageId: "suggestRemove", output: `/(?=b)/` }, + ], }, ], }, @@ -29,7 +31,9 @@ tester.run("optimal-lookaround-quantifier", rule as any, { message: "The quantified expression 'a*' at the end of the expression tree should only be matched a constant number of times. The expression can be removed without affecting the lookaround.", column: 6, - suggestions: [{ output: `/(?=b)/v` }], + suggestions: [ + { messageId: "suggestRemove", output: `/(?=b)/v` }, + ], }, ], }, @@ -40,7 +44,12 @@ tester.run("optimal-lookaround-quantifier", rule as any, { message: "The quantified expression 'c*' at the end of the expression tree should only be matched a constant number of times. The expression can be removed without affecting the lookaround.", column: 14, - suggestions: [{ output: `/(?=(?:a|b|ab))/` }], + suggestions: [ + { + messageId: "suggestRemove", + output: `/(?=(?:a|b|ab))/`, + }, + ], }, ], }, @@ -51,7 +60,12 @@ tester.run("optimal-lookaround-quantifier", rule as any, { message: "The quantified expression 'c+' at the end of the expression tree should only be matched a constant number of times. The expression can be replaced with 'c' (no quantifier) without affecting the lookaround.", column: 14, - suggestions: [{ output: `/(?=(?:a|b|abc))/` }], + suggestions: [ + { + messageId: "suggestReplace", + output: `/(?=(?:a|b|abc))/`, + }, + ], }, ], }, @@ -62,7 +76,12 @@ tester.run("optimal-lookaround-quantifier", rule as any, { message: "The quantified expression 'c{4,9}' at the end of the expression tree should only be matched a constant number of times. The expression can be replaced with 'c{4}' without affecting the lookaround.", column: 14, - suggestions: [{ output: `/(?=(?:a|b|abc{4}))/` }], + suggestions: [ + { + messageId: "suggestReplace", + output: `/(?=(?:a|b|abc{4}))/`, + }, + ], }, ], }, @@ -73,7 +92,9 @@ tester.run("optimal-lookaround-quantifier", rule as any, { message: "The quantified expression '[a-c]*' at the start of the expression tree should only be matched a constant number of times. The expression can be removed without affecting the lookaround.", column: 6, - suggestions: [{ output: `/(?<=)/` }], + suggestions: [ + { messageId: "suggestRemove", output: `/(?<=)/` }, + ], }, ], }, @@ -84,7 +105,9 @@ tester.run("optimal-lookaround-quantifier", rule as any, { message: "The quantified expression '(?:d|c)*' at the start of the expression tree should only be matched a constant number of times. The expression can be removed without affecting the lookaround.", column: 6, - suggestions: [{ output: `/(?<=ab)/` }], + suggestions: [ + { messageId: "suggestRemove", output: `/(?<=ab)/` }, + ], }, ], },