Skip to content

Commit 45bd444

Browse files
authored
escape-case: Ignore String.raw (#2342)
1 parent cc02a7f commit 45bd444

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

rules/escape-case.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
const {replaceTemplateElement} = require('./fix/index.js');
33
const {isRegexLiteral, isStringLiteral} = require('./ast/index.js');
4+
const {isNodeMatches} = require('./utils/index.js');
45

56
const MESSAGE_ID = 'escape-case';
67
const messages = {
@@ -42,11 +43,22 @@ const create = context => {
4243
}
4344
});
4445

45-
context.on('TemplateElement', node => getProblem({
46-
node,
47-
original: node.value.raw,
48-
fix: (fixer, fixed) => replaceTemplateElement(fixer, node, fixed),
49-
}));
46+
context.on('TemplateElement', node => {
47+
const templateLiteral = node.parent;
48+
if (
49+
templateLiteral.parent.type === 'TaggedTemplateExpression'
50+
&& templateLiteral.parent.quasi === templateLiteral
51+
&& isNodeMatches(templateLiteral.parent.tag, ['String.raw'])
52+
) {
53+
return;
54+
}
55+
56+
return getProblem({
57+
node,
58+
original: node.value.raw,
59+
fix: (fixer, fixed) => replaceTemplateElement(fixer, node, fixed),
60+
});
61+
});
5062
};
5163

5264
/** @type {import('eslint').Rule.RuleModule} */

test/escape-case.mjs

+13
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ test({
4141
'const foo = `foo\\\\\\\\xbar`;',
4242
'const foo = `foo\\\\\\\\ubarbaz`;',
4343
'const foo = `\\ca`;',
44+
'const foo = String.raw`\\uAaAa`;',
4445

4546
// Literal regex
4647
'const foo = /foo\\xA9/',
@@ -190,6 +191,18 @@ test({
190191
errors,
191192
output: 'const foo = `foo \\\\\\uD834`;',
192193
},
194+
// TODO: This is not safe, it will be broken if `tagged` uses `arguments[0].raw`
195+
// #2341
196+
{
197+
code: 'const foo = tagged`\\uAaAa`;',
198+
errors,
199+
output: 'const foo = tagged`\\uAAAA`;',
200+
},
201+
{
202+
code: 'const foo = `\\uAaAa```;',
203+
errors,
204+
output: 'const foo = `\\uAAAA```;',
205+
},
193206

194207
// Mixed cases
195208
{

0 commit comments

Comments
 (0)