Skip to content

Commit 9448c8c

Browse files
authored
text-encoding-identifier-case: Support template literals (#2905)
1 parent 6b41299 commit 9448c8c

4 files changed

Lines changed: 249 additions & 23 deletions

File tree

rules/text-encoding-identifier-case.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,37 @@ const shouldEnforceDash = node =>
4949
|| isJsxElementAttributes(node, {element: 'form', attributes: ['acceptCharset', 'accept-charset'].map(attribute => attribute.toLowerCase())})
5050
|| (isNewExpression(node.parent, {name: 'TextDecoder'}) && node.parent.arguments[0] === node);
5151

52+
const getStringLiteralValue = node => {
53+
if (node.type === 'Literal') {
54+
if (typeof node.value !== 'string') {
55+
return;
56+
}
57+
58+
return node.raw.slice(1, -1);
59+
}
60+
61+
if (
62+
node.type === 'TemplateLiteral'
63+
&& node.parent.type !== 'TaggedTemplateExpression'
64+
&& node.expressions.length === 0
65+
&& node.quasis.length === 1
66+
) {
67+
return node.quasis[0].value.raw;
68+
}
69+
};
70+
5271
/** @param {import('eslint').Rule.RuleContext} context */
5372
const create = context => {
5473
const options = context.options[0];
5574

56-
context.on('Literal', node => {
57-
if (typeof node.value !== 'string') {
75+
context.on(['Literal', 'TemplateLiteral'], node => {
76+
const value = getStringLiteralValue(node);
77+
if (!value) {
5878
return;
5979
}
6080

6181
const withDash = options.withDash || shouldEnforceDash(node);
6282

63-
const {raw} = node;
64-
const value = raw.slice(1, -1);
65-
6683
const replacement = getReplacement(value, withDash);
6784
if (!replacement || replacement === value) {
6885
return;

test/snapshots/text-encoding-identifier-case.js.md

Lines changed: 207 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,83 @@ Generated by [AVA](https://avajs.dev).
9999
1 | "ascii"␊
100100
`
101101

102-
## invalid(6): fs.readFile?.(file, "UTF-8")
102+
## invalid(6): `UTF-8`
103+
104+
> Input
105+
106+
`␊
107+
1 | \`UTF-8\`␊
108+
`
109+
110+
> Error 1/1
111+
112+
`␊
113+
Message:␊
114+
> 1 | \`UTF-8\`␊
115+
| ^^^^^^^ Prefer \`utf8\` over \`UTF-8\`.␊
116+
117+
Suggestion 1/1: Replace \`UTF-8\` with \`utf8\`.:␊
118+
1 | \`utf8\`␊
119+
`
120+
121+
## invalid(7): `utf-8`
122+
123+
> Input
124+
125+
`␊
126+
1 | \`utf-8\`␊
127+
`
128+
129+
> Error 1/1
130+
131+
`␊
132+
Message:␊
133+
> 1 | \`utf-8\`␊
134+
| ^^^^^^^ Prefer \`utf8\` over \`utf-8\`.␊
135+
136+
Suggestion 1/1: Replace \`utf-8\` with \`utf8\`.:␊
137+
1 | \`utf8\`␊
138+
`
139+
140+
## invalid(8): `Utf8`
141+
142+
> Input
143+
144+
`␊
145+
1 | \`Utf8\`␊
146+
`
147+
148+
> Error 1/1
149+
150+
`␊
151+
Message:␊
152+
> 1 | \`Utf8\`␊
153+
| ^^^^^^ Prefer \`utf8\` over \`Utf8\`.␊
154+
155+
Suggestion 1/1: Replace \`Utf8\` with \`utf8\`.:␊
156+
1 | \`utf8\`␊
157+
`
158+
159+
## invalid(9): `ASCII`
160+
161+
> Input
162+
163+
`␊
164+
1 | \`ASCII\`␊
165+
`
166+
167+
> Error 1/1
168+
169+
`␊
170+
Message:␊
171+
> 1 | \`ASCII\`␊
172+
| ^^^^^^^ Prefer \`ascii\` over \`ASCII\`.␊
173+
174+
Suggestion 1/1: Replace \`ASCII\` with \`ascii\`.:␊
175+
1 | \`ascii\`␊
176+
`
177+
178+
## invalid(10): fs.readFile?.(file, "UTF-8")
103179

104180
> Input
105181
@@ -118,7 +194,7 @@ Generated by [AVA](https://avajs.dev).
118194
1 | fs.readFile?.(file, "utf8")␊
119195
`
120196

121-
## invalid(7): fs?.readFile(file, "UTF-8")
197+
## invalid(11): fs?.readFile(file, "UTF-8")
122198

123199
> Input
124200
@@ -137,7 +213,7 @@ Generated by [AVA](https://avajs.dev).
137213
1 | fs?.readFile(file, "utf8")␊
138214
`
139215

140-
## invalid(8): readFile(file, "UTF-8")
216+
## invalid(12): readFile(file, "UTF-8")
141217

142218
> Input
143219
@@ -156,7 +232,7 @@ Generated by [AVA](https://avajs.dev).
156232
1 | readFile(file, "utf8")␊
157233
`
158234

159-
## invalid(9): fs.readFile(...file, "UTF-8")
235+
## invalid(13): fs.readFile(...file, "UTF-8")
160236

161237
> Input
162238
@@ -175,7 +251,7 @@ Generated by [AVA](https://avajs.dev).
175251
1 | fs.readFile(...file, "utf8")␊
176252
`
177253

178-
## invalid(10): new fs.readFile(file, "UTF-8")
254+
## invalid(14): new fs.readFile(file, "UTF-8")
179255

180256
> Input
181257
@@ -194,7 +270,7 @@ Generated by [AVA](https://avajs.dev).
194270
1 | new fs.readFile(file, "utf8")␊
195271
`
196272

197-
## invalid(11): fs.readFile(file, {encoding: "UTF-8"})
273+
## invalid(15): fs.readFile(file, {encoding: "UTF-8"})
198274

199275
> Input
200276
@@ -213,7 +289,7 @@ Generated by [AVA](https://avajs.dev).
213289
1 | fs.readFile(file, {encoding: "utf8"})␊
214290
`
215291

216-
## invalid(12): fs.readFile("UTF-8")
292+
## invalid(16): fs.readFile("UTF-8")
217293

218294
> Input
219295
@@ -232,7 +308,7 @@ Generated by [AVA](https://avajs.dev).
232308
1 | fs.readFile("utf8")␊
233309
`
234310

235-
## invalid(13): fs.readFile(file, "UTF-8", () => {})
311+
## invalid(17): fs.readFile(file, "UTF-8", () => {})
236312

237313
> Input
238314
@@ -251,7 +327,7 @@ Generated by [AVA](https://avajs.dev).
251327
1 | fs.readFile(file, "utf8", () => {})␊
252328
`
253329

254-
## invalid(14): fs.readFileSync(file, "UTF-8")
330+
## invalid(18): fs.readFileSync(file, "UTF-8")
255331

256332
> Input
257333
@@ -270,7 +346,7 @@ Generated by [AVA](https://avajs.dev).
270346
1 | fs.readFileSync(file, "utf8")␊
271347
`
272348

273-
## invalid(15): fs[readFile](file, "UTF-8")
349+
## invalid(19): fs[readFile](file, "UTF-8")
274350

275351
> Input
276352
@@ -289,7 +365,7 @@ Generated by [AVA](https://avajs.dev).
289365
1 | fs[readFile](file, "utf8")␊
290366
`
291367

292-
## invalid(16): fs["readFile"](file, "UTF-8")
368+
## invalid(20): fs["readFile"](file, "UTF-8")
293369

294370
> Input
295371
@@ -308,7 +384,7 @@ Generated by [AVA](https://avajs.dev).
308384
1 | fs["readFile"](file, "utf8")␊
309385
`
310386

311-
## invalid(17): await fs.readFile(file, "UTF-8",)
387+
## invalid(21): await fs.readFile(file, "UTF-8",)
312388

313389
> Input
314390
@@ -327,7 +403,7 @@ Generated by [AVA](https://avajs.dev).
327403
1 | await fs.readFile(file, "utf8",)␊
328404
`
329405

330-
## invalid(18): fs.promises.readFile(file, "UTF-8",)
406+
## invalid(22): fs.promises.readFile(file, "UTF-8",)
331407

332408
> Input
333409
@@ -346,7 +422,7 @@ Generated by [AVA](https://avajs.dev).
346422
1 | fs.promises.readFile(file, "utf8",)␊
347423
`
348424

349-
## invalid(19): whatever.readFile(file, "UTF-8",)
425+
## invalid(23): whatever.readFile(file, "UTF-8",)
350426

351427
> Input
352428
@@ -503,7 +579,122 @@ Generated by [AVA](https://avajs.dev).
503579
1 | "ascii";␊
504580
`
505581

506-
## invalid(7): fs.readFile(file, "utf8",);
582+
## invalid(7): `UTF-8`;
583+
584+
> Input
585+
586+
`␊
587+
Options:␊
588+
- withDash: true␊
589+
590+
Code:␊
591+
1 | \`UTF-8\`;␊
592+
`
593+
594+
> Error 1/1
595+
596+
`␊
597+
Message:␊
598+
> 1 | \`UTF-8\`;␊
599+
| ^^^^^^^ Prefer \`utf-8\` over \`UTF-8\`.␊
600+
601+
Suggestion 1/1: Replace \`UTF-8\` with \`utf-8\`.:␊
602+
1 | \`utf-8\`;␊
603+
`
604+
605+
## invalid(8): `UTF8`;
606+
607+
> Input
608+
609+
`␊
610+
Options:␊
611+
- withDash: true␊
612+
613+
Code:␊
614+
1 | \`UTF8\`;␊
615+
`
616+
617+
> Error 1/1
618+
619+
`␊
620+
Message:␊
621+
> 1 | \`UTF8\`;␊
622+
| ^^^^^^ Prefer \`utf-8\` over \`UTF8\`.␊
623+
624+
Suggestion 1/1: Replace \`UTF8\` with \`utf-8\`.:␊
625+
1 | \`utf-8\`;␊
626+
`
627+
628+
## invalid(9): `utf8`;
629+
630+
> Input
631+
632+
`␊
633+
Options:␊
634+
- withDash: true␊
635+
636+
Code:␊
637+
1 | \`utf8\`;␊
638+
`
639+
640+
> Error 1/1
641+
642+
`␊
643+
Message:␊
644+
> 1 | \`utf8\`;␊
645+
| ^^^^^^ Prefer \`utf-8\` over \`utf8\`.␊
646+
647+
Suggestion 1/1: Replace \`utf8\` with \`utf-8\`.:␊
648+
1 | \`utf-8\`;␊
649+
`
650+
651+
## invalid(10): `Utf8`;
652+
653+
> Input
654+
655+
`␊
656+
Options:␊
657+
- withDash: true␊
658+
659+
Code:␊
660+
1 | \`Utf8\`;␊
661+
`
662+
663+
> Error 1/1
664+
665+
`␊
666+
Message:␊
667+
> 1 | \`Utf8\`;␊
668+
| ^^^^^^ Prefer \`utf-8\` over \`Utf8\`.␊
669+
670+
Suggestion 1/1: Replace \`Utf8\` with \`utf-8\`.:␊
671+
1 | \`utf-8\`;␊
672+
`
673+
674+
## invalid(11): `ASCII`;
675+
676+
> Input
677+
678+
`␊
679+
Options:␊
680+
- withDash: true␊
681+
682+
Code:␊
683+
1 | \`ASCII\`;␊
684+
`
685+
686+
> Error 1/1
687+
688+
`␊
689+
Message:␊
690+
> 1 | \`ASCII\`;␊
691+
| ^^^^^^^ Prefer \`ascii\` over \`ASCII\`.␊
692+
693+
Suggestion 1/1: Replace \`ASCII\` with \`ascii\`.:␊
694+
1 | \`ascii\`;␊
695+
`
696+
697+
## invalid(12): fs.readFile(file, "utf8",);
507698

508699
> Input
509700
@@ -526,7 +717,7 @@ Generated by [AVA](https://avajs.dev).
526717
1 | fs.readFile(file, "utf-8",);␊
527718
`
528719

529-
## invalid(8): whatever.readFile(file, "UTF8",)
720+
## invalid(13): whatever.readFile(file, "UTF8",)
530721

531722
> Input
532723
181 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)