Skip to content

Commit

Permalink
chore(eslint-plugin): use getConstraintInfo in no-unnecessary-templ…
Browse files Browse the repository at this point in the history
…ate-expression (typescript-eslint#10578)

Switches the `no-unnecessary-template-expression` rule to use
`getConstraintInfo` internally.

Related typescript-eslint#10569
  • Loading branch information
43081j authored Jan 3, 2025
1 parent d63ea3b commit c7154bf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as ts from 'typescript';

import {
createRule,
getConstrainedTypeAtLocation,
getConstraintInfo,
getMovedNodeCode,
getParserServices,
isTypeFlagSet,
Expand Down Expand Up @@ -51,21 +51,29 @@ export default createRule<[], MessageId>({
function isUnderlyingTypeString(
expression: TSESTree.Expression,
): expression is TSESTree.Identifier | TSESTree.StringLiteral {
const type = getConstrainedTypeAtLocation(services, expression);
const checker = services.program.getTypeChecker();
const { constraintType } = getConstraintInfo(
checker,
services.getTypeAtLocation(expression),
);

if (constraintType == null) {
return false;
}

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

if (type.isUnion()) {
return type.types.every(isString);
if (constraintType.isUnion()) {
return constraintType.types.every(isString);
}

if (type.isIntersection()) {
return type.types.some(isString);
if (constraintType.isIntersection()) {
return constraintType.types.some(isString);
}

return isString(type);
return isString(constraintType);
}

function isLiteral(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,11 @@ this code has trailing whitespace: \${' '}
// intentional comment after
}\`;
`,
`
function getTpl<T>(input: T) {
return \`\${input}\`;
}
`,
],

invalid: [
Expand Down

0 comments on commit c7154bf

Please sign in to comment.