Skip to content

Commit

Permalink
escape-case: Ignore String.raw (#2342)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored May 8, 2024
1 parent cc02a7f commit 45bd444
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
22 changes: 17 additions & 5 deletions rules/escape-case.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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} */
Expand Down
13 changes: 13 additions & 0 deletions test/escape-case.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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/',
Expand Down Expand Up @@ -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
{
Expand Down

0 comments on commit 45bd444

Please sign in to comment.