Skip to content

Commit 8622286

Browse files
authored
fix(eslint-plugin): [no-useless-template-literals] incorrect bigint autofix result (#8283)
1 parent a911214 commit 8622286

2 files changed

Lines changed: 28 additions & 10 deletions

File tree

‎packages/eslint-plugin/src/rules/no-useless-template-literals.ts‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
createRule,
77
getConstrainedTypeAtLocation,
88
getParserServices,
9+
getStaticStringValue,
910
isTypeFlagSet,
1011
isUndefinedIdentifier,
1112
} from '../util';
@@ -118,16 +119,10 @@ export default createRule<[], MessageId>({
118119
]),
119120
];
120121

121-
// Remove quotes for string literals (i.e. `'a'` will become `a`).
122-
const isStringLiteral =
123-
isUnderlyingTypeString(expression) &&
124-
expression.type === AST_NODE_TYPES.Literal;
122+
const stringValue = getStaticStringValue(expression);
125123

126-
if (isStringLiteral) {
127-
const escapedValue = expression.value.replace(
128-
/([`$\\])/g,
129-
'\\$1',
130-
);
124+
if (stringValue != null) {
125+
const escapedValue = stringValue.replace(/([`$\\])/g, '\\$1');
131126

132127
fixes.push(fixer.replaceText(expression, escapedValue));
133128
}

‎packages/eslint-plugin/tests/rules/no-useless-template-literals.test.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ ruleTester.run('no-useless-template-literals', rule, {
1717
valid: [
1818
"const string = 'a';",
1919
'const string = `a`;',
20-
2120
`
2221
declare const string: 'a';
2322
\`\${string}b\`;
@@ -147,6 +146,30 @@ ruleTester.run('no-useless-template-literals', rule, {
147146
},
148147
],
149148
},
149+
{
150+
code: '`${1n}`;',
151+
output: '`1`;',
152+
errors: [
153+
{
154+
messageId: 'noUselessTemplateLiteral',
155+
line: 1,
156+
column: 4,
157+
endColumn: 6,
158+
},
159+
],
160+
},
161+
{
162+
code: '`${/a/}`;',
163+
output: '`/a/`;',
164+
errors: [
165+
{
166+
messageId: 'noUselessTemplateLiteral',
167+
line: 1,
168+
column: 4,
169+
endColumn: 7,
170+
},
171+
],
172+
},
150173

151174
{
152175
code: noFormat`\`\${ 1 }\`;`,

0 commit comments

Comments
 (0)