Skip to content

Commit

Permalink
fix(eslint-plugin): [no-useless-template-literals] incorrect bigint a…
Browse files Browse the repository at this point in the history
…utofix result
  • Loading branch information
yeonjuan committed Jan 21, 2024
1 parent 920f909 commit 5bbfc11
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
13 changes: 4 additions & 9 deletions packages/eslint-plugin/src/rules/no-useless-template-literals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
createRule,
getConstrainedTypeAtLocation,
getParserServices,
getStaticStringValue,
isTypeFlagSet,
isUndefinedIdentifier,
} from '../util';
Expand Down Expand Up @@ -118,16 +119,10 @@ export default createRule<[], MessageId>({
]),
];

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

if (isStringLiteral) {
const escapedValue = expression.value.replace(
/([`$\\])/g,
'\\$1',
);
if (stringValue != null) {
const escapedValue = stringValue.replace(/([`$\\])/g, '\\$1');

fixes.push(fixer.replaceText(expression, escapedValue));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ ruleTester.run('no-useless-template-literals', rule, {
valid: [
"const string = 'a';",
'const string = `a`;',

`
declare const string: 'a';
\`\${string}b\`;
Expand Down Expand Up @@ -147,6 +146,30 @@ ruleTester.run('no-useless-template-literals', rule, {
},
],
},
{
code: '`${1n}`;',
output: '`1`;',
errors: [
{
messageId: 'noUselessTemplateLiteral',
line: 1,
column: 4,
endColumn: 6,
},
],
},
{
code: '`${/a/}`;',
output: '`/a/`;',
errors: [
{
messageId: 'noUselessTemplateLiteral',
line: 1,
column: 4,
endColumn: 7,
},
],
},

{
code: noFormat`\`\${ 1 }\`;`,
Expand Down

0 comments on commit 5bbfc11

Please sign in to comment.