Skip to content

Commit e6aed3a

Browse files
authored
style: enable unicorn/prefer-string-raw (#830)
1 parent aa14446 commit e6aed3a

21 files changed

+603
-205
lines changed

eslint.config.mjs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { FlatCompat } from "@eslint/eslintrc"
55
import js from "@eslint/js"
66
import myPlugin from "@ota-meshi/eslint-plugin"
77
import regexp from "eslint-plugin-regexp"
8+
import unicorn from "eslint-plugin-unicorn"
89
import tseslint from "typescript-eslint"
910

1011
const __filename = fileURLToPath(import.meta.url)
@@ -173,7 +174,10 @@ export default [
173174
{
174175
files: ["lib/rules/**"],
175176
rules: {
176-
"eslint-plugin/report-message-format": ["error", "[^a-z].*\\.$"],
177+
"eslint-plugin/report-message-format": [
178+
"error",
179+
String.raw`[^a-z].*\.$`,
180+
],
177181
},
178182
},
179183
{
@@ -242,4 +246,11 @@ export default [
242246
"n/no-missing-import": "off",
243247
},
244248
},
249+
{
250+
ignores: ["lib/**/*", "**/*.json"],
251+
plugins: { unicorn },
252+
rules: {
253+
"unicorn/prefer-string-raw": "error",
254+
},
255+
},
245256
]

package-lock.json

Lines changed: 427 additions & 61 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
"eslint-plugin-n": "^17.9.0",
9898
"eslint-plugin-prettier": "^5.1.3",
9999
"eslint-plugin-regexp": "~2.7.0",
100+
"eslint-plugin-unicorn": "^59.0.1",
100101
"eslint-plugin-vue": "^9.26.0",
101102
"eslint-plugin-yml": "^1.14.0",
102103
"eslint-snapshot-rule-tester": "^0.1.0",

tests/lib/rules/control-character-escape.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ tester.run("control-character-escape", rule as any, {
1414
String.raw`RegExp(/\0\t\n\v\f\r/, "i")`,
1515
String.raw`RegExp("\0\t\n\v\f\r", "i")`,
1616
String.raw`RegExp("\\0\\t\\n\\v\\f\\r", "i")`,
17-
"/\\t/",
17+
String.raw`/\t/`,
1818
"new RegExp('\t')",
1919
String.raw`/[\q{\0\t\n\v\f\r}]/v`,
2020
],
@@ -26,7 +26,7 @@ tester.run("control-character-escape", rule as any, {
2626
String.raw`RegExp("\\cJ")`,
2727
String.raw`RegExp("\\u{a}", "u")`,
2828

29-
"/\\u0009/",
29+
String.raw`/\u0009/`,
3030
"/\t/",
3131
String.raw`
3232
const s = "\\u0009"

tests/lib/rules/match-any.ts

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,60 +10,69 @@ const tester = new SnapshotRuleTester({
1010

1111
tester.run("match-any", rule as any, {
1212
valid: [
13-
"/[\\s\\S]/",
13+
String.raw`/[\s\S]/`,
1414
"/./s",
1515
"/./",
16-
"/[\\s\\d]/",
17-
"/\\S\\s/",
18-
"/[\\1-\\uFFFF]/",
16+
String.raw`/[\s\d]/`,
17+
String.raw`/\S\s/`,
18+
String.raw`/[\1-\uFFFF]/`,
1919
{
20-
code: "/[\\S\\s]/",
21-
options: [{ allows: ["[\\S\\s]"] }],
20+
code: String.raw`/[\S\s]/`,
21+
options: [{ allows: [String.raw`[\S\s]`] }],
2222
},
2323
{
2424
code: "/[^]/",
2525
options: [{ allows: ["[^]"] }],
2626
},
2727
{
28-
code: "/[\\s\\S][\\S\\s][^]./s",
29-
options: [{ allows: ["[\\s\\S]", "[\\S\\s]", "[^]", "dotAll"] }],
28+
code: String.raw`/[\s\S][\S\s][^]./s`,
29+
options: [
30+
{
31+
allows: [
32+
String.raw`[\s\S]`,
33+
String.raw`[\S\s]`,
34+
"[^]",
35+
"dotAll",
36+
],
37+
},
38+
],
3039
},
31-
"/[^\\S\\s]/",
40+
String.raw`/[^\S\s]/`,
3241
{
33-
code: "/[^\\s\\S]/",
42+
code: String.raw`/[^\s\S]/`,
3443
options: [{ allows: ["[^]"] }],
3544
},
36-
"/[^\\d\\D]/",
37-
"/[^\\D\\d]/",
38-
"/[^\\w\\W]/",
39-
"/[^\\W\\w]/",
40-
"/[^\\0-\\uFFFF]/",
41-
"/[^\\p{ASCII}\\P{ASCII}]/u",
42-
"/[^\\P{ASCII}\\p{ASCII}]/u",
43-
"/[^\\s\\S\\0-\\uFFFF]/",
45+
String.raw`/[^\d\D]/`,
46+
String.raw`/[^\D\d]/`,
47+
String.raw`/[^\w\W]/`,
48+
String.raw`/[^\W\w]/`,
49+
String.raw`/[^\0-\uFFFF]/`,
50+
String.raw`/[^\p{ASCII}\P{ASCII}]/u`,
51+
String.raw`/[^\P{ASCII}\p{ASCII}]/u`,
52+
String.raw`/[^\s\S\0-\uFFFF]/`,
4453
String.raw`/[\S\s\q{abc}]/v`,
4554
],
4655
invalid: [
47-
"/[\\S\\s]/",
56+
String.raw`/[\S\s]/`,
4857
String.raw`/[\S\s]/v`,
4958
String.raw`/[\S\s\q{a|b|c}]/v`,
5059
String.raw`/[[\S\s\q{abc}]--\q{abc}]/v`,
5160
"/[^]/",
52-
"/[\\d\\D]/",
53-
"/[\\0-\\uFFFF]/",
54-
"/[\\s\\S][\\S\\s][^]./s",
61+
String.raw`/[\d\D]/`,
62+
String.raw`/[\0-\uFFFF]/`,
63+
String.raw`/[\s\S][\S\s][^]./s`,
5564
{
56-
code: "/[\\s\\S][\\S\\s][^]./s",
65+
code: String.raw`/[\s\S][\S\s][^]./s`,
5766
options: [{ allows: ["[^]"] }],
5867
},
5968
{
60-
code: "/[\\s\\S] [\\S\\s] [^] ./s",
69+
code: String.raw`/[\s\S] [\S\s] [^] ./s`,
6170
// Only one character class gets fixed because they all depend on the `s` flag.
6271
// This shared dependency causes all of their fixes to conflict, so only one fix can be applied.
6372
options: [{ allows: ["dotAll"] }],
6473
},
6574
{
66-
code: "/. [\\S\\s] [^] ./s",
75+
code: String.raw`/. [\S\s] [^] ./s`,
6776
options: [{ allows: ["dotAll"] }],
6877
},
6978
{
@@ -88,10 +97,10 @@ tester.run("match-any", rule as any, {
8897
`,
8998
options: [{ allows: ["[^]"] }],
9099
},
91-
"/[\\p{ASCII}\\P{ASCII}]/u",
92-
"/[\\p{Script=Hiragana}\\P{Script=Hiragana}]/u",
93-
"/[\\s\\S\\0-\\uFFFF]/",
94-
"/[\\w\\D]/",
95-
"/[\\P{ASCII}\\w\\0-AZ-\\xFF]/u",
100+
String.raw`/[\p{ASCII}\P{ASCII}]/u`,
101+
String.raw`/[\p{Script=Hiragana}\P{Script=Hiragana}]/u`,
102+
String.raw`/[\s\S\0-\uFFFF]/`,
103+
String.raw`/[\w\D]/`,
104+
String.raw`/[\P{ASCII}\w\0-AZ-\xFF]/u`,
96105
],
97106
})

tests/lib/rules/no-dupe-characters-character-class.ts

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,54 +10,54 @@ const tester = new SnapshotRuleTester({
1010

1111
tester.run("no-dupe-characters-character-class", rule as any, {
1212
valid: [
13-
"var re = /[a-zA-Z0-9\\s]/",
13+
String.raw`var re = /[a-zA-Z0-9\s]/`,
1414
"/[abc]/",
1515
"/[a][a][a]/",
16-
"/[0-9\\D]/",
17-
"/[\\S \\f\\n\\r\\t\\v\\u00a0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000\\ufeff]/",
18-
"/\\s \\f\\n\\r\\t\\v\\u00a0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000\\ufeff/",
19-
"/[\\WA-Za-z0-9_]/",
20-
"/[\\w \\/-:]/",
21-
"/[\\w\\p{L}]/u",
22-
"/\\p{ASCII}abc/u",
16+
String.raw`/[0-9\D]/`,
17+
String.raw`/[\S \f\n\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]/`,
18+
String.raw`/\s \f\n\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff/`,
19+
String.raw`/[\WA-Za-z0-9_]/`,
20+
String.raw`/[\w \/-:]/`,
21+
String.raw`/[\w\p{L}]/u`,
22+
String.raw`/\p{ASCII}abc/u`,
2323
String.raw`/[\u1fff-\u2020\s]/`,
2424
String.raw`/[\q{a}\q{ab}\q{abc}[\w--[ab]][\w&&b]]/v`,
2525
// error
26-
"var r = new RegExp('[\\\\wA-Za-z0-9_][invalid');",
26+
String.raw`var r = new RegExp('[\\wA-Za-z0-9_][invalid');`,
2727
],
2828
invalid: [
29-
"var re = /[\\\\(\\\\)]/",
30-
"var re = /[a-z\\\\s]/",
29+
String.raw`var re = /[\\(\\)]/`,
30+
String.raw`var re = /[a-z\\s]/`,
3131
"/[aaa]/",
32-
"/[0-9\\d]/",
33-
"/[\\f\\u000C]/",
32+
String.raw`/[0-9\d]/`,
33+
String.raw`/[\f\u000C]/`,
3434
"RegExp(/[bb]/)",
35-
"/[\\s \\f\\n\\r\\t\\v\\u00a0\\u1680\\u180e\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000\\ufeff]/",
35+
String.raw`/[\s \f\n\r\t\v\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]/`,
3636
"/[\\t\t \\u0009]/",
37-
"/[\\wA-Z a-z:0-9,_]/",
37+
String.raw`/[\wA-Z a-z:0-9,_]/`,
3838
"/[!-z_abc-]/",
39-
"/[\\w_abc-][\\s \\t\\r\\n\\u2000\\u3000]/",
39+
String.raw`/[\w_abc-][\s \t\r\n\u2000\u3000]/`,
4040
"/[a-z a-z]/",
4141
"/[a-z A-Z]/i",
4242
"/[a-d e-h_d-e+c-d]/",
4343
"/[3-6 3-6_2-4+5-7]/",
4444
"/[3-6 3-6_5-7]/",
45-
"/[\\s\\s \\s]/",
46-
"/[\\S\\S \\sa]/",
47-
"/[\\d 0-9_!-z]/",
48-
"/[\\W\\W\\w \\d\\d\\D]/",
49-
"/[\\p{ASCII}\\P{ASCII}\\p{Script=Hiragana}\\P{Script=Hiragana}\\p{ASCII}\\p{Script=Hiragana}]/u",
50-
"/[\\p{ASCII} abc\\P{ASCII}]/u",
51-
"/[\\P{Script=Hiragana} abc\\p{Script=Hiragana}]/u",
52-
"/[\\w /-7+8-:]/",
53-
"/[ -/\\s]/",
54-
"/[\\wA-_]/",
45+
String.raw`/[\s\s \s]/`,
46+
String.raw`/[\S\S \sa]/`,
47+
String.raw`/[\d 0-9_!-z]/`,
48+
String.raw`/[\W\W\w \d\d\D]/`,
49+
String.raw`/[\p{ASCII}\P{ASCII}\p{Script=Hiragana}\P{Script=Hiragana}\p{ASCII}\p{Script=Hiragana}]/u`,
50+
String.raw`/[\p{ASCII} abc\P{ASCII}]/u`,
51+
String.raw`/[\P{Script=Hiragana} abc\p{Script=Hiragana}]/u`,
52+
String.raw`/[\w /-7+8-:]/`,
53+
String.raw`/[ -/\s]/`,
54+
String.raw`/[\wA-_]/`,
5555
String.raw`/[\w0-z]/`,
5656
String.raw`/[\t-\uFFFF\s]/`,
57-
"/[\\Sa]/",
58-
"/[a-z\\p{L}]/u",
59-
"/[\\d\\p{ASCII}]/u",
60-
"/[\\t\\s]/",
57+
String.raw`/[\Sa]/`,
58+
String.raw`/[a-z\p{L}]/u`,
59+
String.raw`/[\d\p{ASCII}]/u`,
60+
String.raw`/[\t\s]/`,
6161
String.raw`/[A-Z a-\uFFFF]/i`,
6262
String.raw`/[\xA0-\uFFFF\s]/`,
6363
String.raw`/[\u1fff-\u2005\s]/`,
@@ -78,7 +78,7 @@ tester.run("no-dupe-characters-character-class", rule as any, {
7878
// sometimes, we can't can't remove the element
7979
String.raw`/[\x01-\d\x03\w]/`,
8080
String.raw`/[\s0-\s9]/`,
81-
"/[\\x0x9]/",
81+
String.raw`/[\x0x9]/`,
8282
// v flags
8383
String.raw`/[\q{a}aa-c[\w--b][\w&&a]]/v`,
8484
String.raw`/[\q{abc}\q{abc|ab}[\q{abc}--b][\q{abc}&&\q{abc|ab}]]/v`,

tests/lib/rules/no-empty-capturing-group.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ const tester = new SnapshotRuleTester({
99
})
1010

1111
tester.run("no-empty-capturing-group", rule as any, {
12-
valid: ["/(a)/", "/a(\\bb)/", "/a(\\b|b)/", String.raw`/a([\q{a}])/v`],
12+
valid: [
13+
"/(a)/",
14+
String.raw`/a(\bb)/`,
15+
String.raw`/a(\b|b)/`,
16+
String.raw`/a([\q{a}])/v`,
17+
],
1318
invalid: [
14-
"/a(\\b)/",
19+
String.raw`/a(\b)/`,
1520
"/a($)/",
1621
"/(^)a/",
1722
"/()a/",
18-
"/(\\b\\b|(?:\\B|$))a/",
23+
String.raw`/(\b\b|(?:\B|$))a/`,
1924
String.raw`/a([\q{}])/v`,
2025
],
2126
})

tests/lib/rules/no-empty-lookarounds-assertion.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ tester.run("no-empty-lookarounds-assertion", rule as any, {
1818
"/x($)/",
1919
"/(?=(?=.).*)/",
2020
"/(?=$|a)/",
21-
"/(?=\\ba*\\b)/",
22-
'/b?r(#*)"(?:[^"]|"(?!\\1))*"\\1/',
21+
String.raw`/(?=\ba*\b)/`,
22+
String.raw`/b?r(#*)"(?:[^"]|"(?!\1))*"\1/`,
2323
String.raw`/x(?=[\q{a}])/v`,
2424
],
2525
invalid: [

tests/lib/rules/no-escape-backspace.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ const tester = new SnapshotRuleTester({
1010

1111
tester.run("no-escape-backspace", rule as any, {
1212
valid: [
13-
"/\\b/",
14-
"/\\u0008/",
15-
"/\\ch/",
16-
"/\\cH/",
13+
String.raw`/\b/`,
14+
String.raw`/\u0008/`,
15+
String.raw`/\ch/`,
16+
String.raw`/\cH/`,
1717
String.raw`/[\q{\u0008}]/v`,
1818
],
19-
invalid: ["/[\\b]/", String.raw`/[\q{\b}]/v`],
19+
invalid: [String.raw`/[\b]/`, String.raw`/[\q{\b}]/v`],
2020
})

tests/lib/rules/no-invisible-character.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ tester.run("no-invisible-character", rule as any, {
1414
"/ /",
1515
"/[a]/",
1616
"/[ ]/",
17-
"/\\t/",
18-
"new RegExp('\\t')",
17+
String.raw`/\t/`,
18+
String.raw`new RegExp('\t')`,
1919
`
2020
const a = '' + '\t';
2121
new RegExp(a)`,

tests/lib/rules/no-obscure-range.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const tester = new SnapshotRuleTester({
1010

1111
tester.run("no-obscure-range", rule as any, {
1212
valid: [
13-
"/[\\d\\0-\\x1f\\cA-\\cZ\\2-\\5\\012-\\123\\x10-\\uffff a-z a-f]/",
13+
String.raw`/[\d\0-\x1f\cA-\cZ\2-\5\012-\123\x10-\uffff a-z a-f]/`,
1414
{
1515
code: "/[а-я А-Я]/",
1616
options: [{ allowed: ["alphanumeric", "а-я", "А-Я"] }],
@@ -37,17 +37,17 @@ tester.run("no-obscure-range", rule as any, {
3737
},
3838
},
3939

40-
"/[\\1-\\x13]/",
41-
"/[\\x20-\\113]/",
40+
String.raw`/[\1-\x13]/`,
41+
String.raw`/[\x20-\113]/`,
4242

43-
"/[\\n-\\r]/",
43+
String.raw`/[\n-\r]/`,
4444

45-
"/[\\cA-Z]/",
45+
String.raw`/[\cA-Z]/`,
4646

4747
"/[A-z]/",
4848
"/[0-A]/",
4949
"/[Z-a]/",
50-
"/[A-\\x43]/",
50+
String.raw`/[A-\x43]/`,
5151

5252
"/[*+-/]/",
5353
String.raw`/[[ -\/]--+]/v`,

tests/lib/rules/no-octal.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@ const tester = new SnapshotRuleTester({
99
})
1010

1111
tester.run("no-octal", rule as any, {
12-
valid: ["/\\0/", "/[\\7]/", "/[\\1-\\4]/", String.raw`/[\q{\0}]/v`],
12+
valid: [
13+
String.raw`/\0/`,
14+
String.raw`/[\7]/`,
15+
String.raw`/[\1-\4]/`,
16+
String.raw`/[\q{\0}]/v`,
17+
],
1318
invalid: [
14-
"/\\07/",
15-
"/\\077/",
16-
"/[\\077]/",
17-
"/\\0777/",
18-
"/\\7/",
19-
"/\\1\\2/",
20-
"/()\\1\\2/",
19+
String.raw`/\07/`,
20+
String.raw`/\077/`,
21+
String.raw`/[\077]/`,
22+
String.raw`/\0777/`,
23+
String.raw`/\7/`,
24+
String.raw`/\1\2/`,
25+
String.raw`/()\1\2/`,
2126
],
2227
})

0 commit comments

Comments
 (0)