Skip to content

Commit

Permalink
fix(eslint-plugin): [no-unnecessary-template-expression] should under…
Browse files Browse the repository at this point in the history
…line template syntax with squiggly lines (typescript-eslint#10044)

* fix no-unnecessary-template-expression to include template syntax with a squiggly

* Update packages/eslint-plugin/src/rules/no-unnecessary-template-expression.ts

Co-authored-by: Kirk Waiblinger <kirk.waiblinger@gmail.com>

* Update packages/eslint-plugin/src/rules/no-unnecessary-template-expression.ts

Co-authored-by: Kirk Waiblinger <kirk.waiblinger@gmail.com>

---------

Co-authored-by: Kirk Waiblinger <kirk.waiblinger@gmail.com>
  • Loading branch information
ronami and kirkwaiblinger authored Sep 25, 2024
1 parent b6f4b2f commit 6dbf234
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
getMemberHeadLoc,
getParameterPropertyHeadLoc,
} from '../util/getMemberHeadLoc';
import { rangeToLoc } from '../util/rangeToLoc';

type AccessibilityLevel =
| 'explicit' // require an accessor (including public)
Expand Down Expand Up @@ -387,13 +388,3 @@ export default createRule<Options, MessageIds>({
};
},
});

function rangeToLoc(
sourceCode: TSESLint.SourceCode,
range: TSESLint.AST.Range,
): TSESTree.SourceLocation {
return {
start: sourceCode.getLocFromIndex(range[0]),
end: sourceCode.getLocFromIndex(range[1]),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
isTypeFlagSet,
isUndefinedIdentifier,
} from '../util';
import { rangeToLoc } from '../util/rangeToLoc';

type MessageId = 'noUnnecessaryTemplateExpression';

Expand Down Expand Up @@ -105,7 +106,10 @@ export default createRule<[], MessageId>({

if (hasSingleStringVariable) {
context.report({
node: node.expressions[0],
loc: rangeToLoc(context.sourceCode, [
node.expressions[0].range[0] - 2,
node.expressions[0].range[1] + 1,
]),
messageId: 'noUnnecessaryTemplateExpression',
fix(fixer): TSESLint.RuleFix | null {
const wrappingCode = getMovedNodeCode({
Expand Down Expand Up @@ -245,20 +249,17 @@ export default createRule<[], MessageId>({
]);
}

const warnLocStart = prevQuasi.range[1] - 2;
const warnLocEnd = nextQuasi.range[0] + 1;

context.report({
node: expression,
loc: rangeToLoc(context.sourceCode, [warnLocStart, warnLocEnd]),
messageId: 'noUnnecessaryTemplateExpression',
fix(fixer): TSESLint.RuleFix[] {
return [
// Remove the quasis' parts that are related to the current expression.
fixer.removeRange([
prevQuasi.range[1] - 2,
expression.range[0],
]),
fixer.removeRange([
expression.range[1],
nextQuasi.range[0] + 1,
]),
fixer.removeRange([warnLocStart, expression.range[0]]),
fixer.removeRange([expression.range[1], warnLocEnd]),

...fixers.flatMap(cb => cb(fixer)),
];
Expand Down
11 changes: 11 additions & 0 deletions packages/eslint-plugin/src/util/rangeToLoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';

export function rangeToLoc(
sourceCode: TSESLint.SourceCode,
range: TSESLint.AST.Range,
): TSESTree.SourceLocation {
return {
start: sourceCode.getLocFromIndex(range[0]),
end: sourceCode.getLocFromIndex(range[1]),
};
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6dbf234

Please sign in to comment.