Skip to content

Commit

Permalink
no-hex-escape: Ignore String.raw (#2343)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed May 8, 2024
1 parent bfc5447 commit cc02a7f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
14 changes: 13 additions & 1 deletion rules/no-hex-escape.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const {replaceTemplateElement} = require('./fix/index.js');
const {isStringLiteral, isRegexLiteral} = require('./ast/index.js');
const {isNodeMatches} = require('./utils/index.js');

const MESSAGE_ID = 'no-hex-escape';
const messages = {
Expand Down Expand Up @@ -29,7 +30,18 @@ const create = context => ({
return checkEscape(context, node, node.raw);
}
},
TemplateElement: node => checkEscape(context, node, node.value.raw),
TemplateElement(node) {
const templateLiteral = node.parent;
if (
templateLiteral.parent.type === 'TaggedTemplateExpression'
&& templateLiteral.parent.quasi === templateLiteral
&& isNodeMatches(templateLiteral.parent.tag, ['String.raw'])
) {
return;
}

return checkEscape(context, node, node.value.raw);
},
});

/** @type {import('eslint').Rule.RuleModule} */
Expand Down
12 changes: 12 additions & 0 deletions test/no-hex-escape.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const tests = {
'const foo = `foo\\\\x12foo\\\\x34`',
'const foo = `\\\\\\\\xd8\\\\\\\\x3d\\\\\\\\xdc\\\\\\\\xa9`',
'const foo = `foo\\\\\\\\x12foo\\\\\\\\x34`',
'const foo = String.raw`\\\\xb1`',
],
invalid: [
{
Expand Down Expand Up @@ -193,6 +194,17 @@ const tests = {
// eslint-disable-next-line no-template-curly-in-string
output: 'const foo = `\\u00b1${foo}\\u00b1${foo}`',
},
{
code: 'const foo = `\\xb1```',
errors: [error],
output: 'const foo = `\\u00b1```',
},
// TODO: Not safe #2341
{
code: 'const foo = tagged`\\xb1`',
errors: [error],
output: 'const foo = tagged`\\u00b1`',
},
],
};

Expand Down

0 comments on commit cc02a7f

Please sign in to comment.