8000 chore(eslint-plugin): use `getConstraintInfo` in no-unnecessary-templ… · 43081j/typescript-eslint@21be05c · GitHub
[go: up one dir, main page]

Skip to content

Commit 21be05c

Browse files
committed
chore(eslint-plugin): use getConstraintInfo in no-unnecessary-template-expression
Switches the `no-unnecessary-template-expression` rule to use `getConstraintInfo` internally. Related typescript-eslint#10569
1 parent 4dbf48b commit 21be05c

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as ts from 'typescript';
55

66
import {
77
createRule,
8-
getConstrainedTypeAtLocation,
8+
getConstraintInfo,
99
getMovedNodeCode,
1010
getParserServices,
1111
isTypeFlagSet,
@@ -51,21 +51,29 @@ export default createRule<[], MessageId>({
5151
function isUnderlyingTypeString(
5252
expression: TSESTree.Expression,
5353
): expression is TSESTree.Identifier | TSESTree.StringLiteral {
54-
const type = getConstrainedTypeAtLocation(services, expression);
54+
const checker = services.program.getTypeChecker();
55+
const { constraintType } = getConstraintInfo(
56+
checker,
57+
services.getTypeAtLocation(expression),
58+
);
59+
60+
if (constraintType == null) {
61+
return false;
62+
}
5563

5664
const isString = (t: ts.Type): boolean => {
5765
return isTypeFlagSet(t, ts.TypeFlags.StringLike);
5866
};
5967

60-
if (type.isUnion()) {
61-
return type.types.every(isString);
68+
if (constraintType.isUnion()) {
69+
return constraintType.types.every(isString);
6270
}
6371

64-
if (type.isIntersection()) {
65-
return type.types.some(isString);
72+
if (constraintType.isIntersection()) {
73+
return constraintType.types.some(isString);
6674
}
6775

68-
return isString(type);
76+
return isString(constraintType);
6977
}
7078

7179
function isLiteral(

packages/eslint-plugin/tests/rules/no-unnecessary-template-expression.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,11 @@ this code has trailing whitespace: \${' '}
11421142
// intentional comment after
11431143
}\`;
11441144
`,
1145+
`
1146+
function getTpl<T>(input: T) {
1147+
return \`\${input}\`;
1148+
}
1149+
`,
11451150
],
11461151

11471152
invalid: [

0 commit comments

Comments
 (0)
0