From 45bd444bd1b2cfa02f725a12e004958ae2c36a13 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 9 May 2024 00:01:38 +0800 Subject: [PATCH] `escape-case`: Ignore `String.raw` (#2342) --- rules/escape-case.js | 22 +++++++++++++++++----- test/escape-case.mjs | 13 +++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/rules/escape-case.js b/rules/escape-case.js index 85a84f69ea..b20c65d614 100644 --- a/rules/escape-case.js +++ b/rules/escape-case.js @@ -1,6 +1,7 @@ 'use strict'; const {replaceTemplateElement} = require('./fix/index.js'); const {isRegexLiteral, isStringLiteral} = require('./ast/index.js'); +const {isNodeMatches} = require('./utils/index.js'); const MESSAGE_ID = 'escape-case'; const messages = { @@ -42,11 +43,22 @@ const create = context => { } }); - context.on('TemplateElement', node => getProblem({ - node, - original: node.value.raw, - fix: (fixer, fixed) => replaceTemplateElement(fixer, node, fixed), - })); + context.on('TemplateElement', node => { + const templateLiteral = node.parent; + if ( + templateLiteral.parent.type === 'TaggedTemplateExpression' + && templateLiteral.parent.quasi === templateLiteral + && isNodeMatches(templateLiteral.parent.tag, ['String.raw']) + ) { + return; + } + + return getProblem({ + node, + original: node.value.raw, + fix: (fixer, fixed) => replaceTemplateElement(fixer, node, fixed), + }); + }); }; /** @type {import('eslint').Rule.RuleModule} */ diff --git a/test/escape-case.mjs b/test/escape-case.mjs index c8650853da..e527d11c90 100644 --- a/test/escape-case.mjs +++ b/test/escape-case.mjs @@ -41,6 +41,7 @@ test({ 'const foo = `foo\\\\\\\\xbar`;', 'const foo = `foo\\\\\\\\ubarbaz`;', 'const foo = `\\ca`;', + 'const foo = String.raw`\\uAaAa`;', // Literal regex 'const foo = /foo\\xA9/', @@ -190,6 +191,18 @@ test({ errors, output: 'const foo = `foo \\\\\\uD834`;', }, + // TODO: This is not safe, it will be broken if `tagged` uses `arguments[0].raw` + // #2341 + { + code: 'const foo = tagged`\\uAaAa`;', + errors, + output: 'const foo = tagged`\\uAAAA`;', + }, + { + code: 'const foo = `\\uAaAa```;', + errors, + output: 'const foo = `\\uAAAA```;', + }, // Mixed cases {