Skip to content

Commit cc02a7f

Browse files
authored
no-hex-escape: Ignore String.raw (#2343)
1 parent bfc5447 commit cc02a7f

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

rules/no-hex-escape.js

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

56
const MESSAGE_ID = 'no-hex-escape';
67
const messages = {
@@ -29,7 +30,18 @@ const create = context => ({
2930
return checkEscape(context, node, node.raw);
3031
}
3132
},
32-
TemplateElement: node => checkEscape(context, node, node.value.raw),
33+
TemplateElement(node) {
34+
const templateLiteral = node.parent;
35+
if (
36+
templateLiteral.parent.type === 'TaggedTemplateExpression'
37+
&& templateLiteral.parent.quasi === templateLiteral
38+
&& isNodeMatches(templateLiteral.parent.tag, ['String.raw'])
39+
) {
40+
return;
41+
}
42+
43+
return checkEscape(context, node, node.value.raw);
44+
},
3345
});
3446

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

test/no-hex-escape.mjs

+12
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const tests = {
3838
'const foo = `foo\\\\x12foo\\\\x34`',
3939
'const foo = `\\\\\\\\xd8\\\\\\\\x3d\\\\\\\\xdc\\\\\\\\xa9`',
4040
'const foo = `foo\\\\\\\\x12foo\\\\\\\\x34`',
41+
'const foo = String.raw`\\\\xb1`',
4142
],
4243
invalid: [
4344
{
@@ -193,6 +194,17 @@ const tests = {
193194
// eslint-disable-next-line no-template-curly-in-string
194195
output: 'const foo = `\\u00b1${foo}\\u00b1${foo}`',
195196
},
197+
{
198+
code: 'const foo = `\\xb1```',
199+
errors: [error],
200+
output: 'const foo = `\\u00b1```',
201+
},
202+
// TODO: Not safe #2341
203+
{
204+
code: 'const foo = tagged`\\xb1`',
205+
errors: [error],
206+
output: 'const foo = tagged`\\u00b1`',
207+
},
196208
],
197209
};
198210

0 commit comments

Comments
 (0)