Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
fix(eslint-plugin): [restrict-template-expressions] check base types in allow list#11764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
JoshuaKGoldberg merged 10 commits intotypescript-eslint:mainfromHigangssh:fix/restrict-template-expressions-base-typesNov 28, 2025
+237 −41
Merged
Changes fromall commits
Commits
Show all changes
10 commits Select commitHold shift + click to select a range
dfca648 fix(restrict-template-expressions): check base types in allow list
Higangssh6ac61a0 fix: improve test coverage and fix import formatting
Higangssh95ad776 fix: improve test coverage and fix linting issues
Higangssh8eaddd5 chore: trigger CI rebuild
Higangsshe77fd7a Merge remote-tracking branch 'upstream/main' into fix/restrict-templa…
Higangsshc6b7ac8 refactor: deduplicate base type checking logic
Higangssh29092d8 remove unnecessary test comments
Higangssh90532b0 add multiple inheritance tests
Higangsshf89753c chore: retrigger CI
Higangssh80be898 Merge branch 'main'
JoshuaKGoldbergFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
50 changes: 10 additions & 40 deletionspackages/eslint-plugin/src/rules/no-base-to-string.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
7 changes: 6 additions & 1 deletionpackages/eslint-plugin/src/rules/restrict-template-expressions.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletionspackages/eslint-plugin/src/util/baseTypeUtils.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import type { ParserServicesWithTypeInformation } from '@typescript-eslint/utils'; | ||
| import type { InterfaceType, Type } from 'typescript'; | ||
| import { isObjectFlagSet, isObjectType } from 'ts-api-utils'; | ||
| import * as tsutils from 'ts-api-utils'; | ||
| import * as ts from 'typescript'; | ||
| function getBaseTypesForType( | ||
| checker: ts.TypeChecker, | ||
| type: ts.Type, | ||
| ): readonly ts.Type[] { | ||
| if (!tsutils.isObjectType(type)) { | ||
| return []; | ||
| } | ||
| const interfaceTarget = tsutils.isTypeReference(type) ? type.target : type; | ||
| const interfaceType = | ||
| tsutils.isObjectFlagSet( | ||
| interfaceTarget, | ||
| ts.ObjectFlags.Interface | ts.ObjectFlags.Class, | ||
| ) && (interfaceTarget as ts.InterfaceType); | ||
| if (!interfaceType) { | ||
| return []; | ||
| } | ||
| return checker.getBaseTypes(interfaceType); | ||
| } | ||
| export function hasBaseTypes(type: Type): type is InterfaceType { | ||
| return ( | ||
| isObjectType(type) && | ||
| isObjectFlagSet(type, ts.ObjectFlags.Interface | ts.ObjectFlags.Class) | ||
| ); | ||
| } | ||
| /** | ||
| * Recursively checks if a type or any of its base types matches the provided | ||
| * matcher function. | ||
| * @param services Parser services with type information | ||
| * @param matcher Function to test if a type matches the desired criteria | ||
| * @param type The type to check | ||
| * @param seen Set of already visited types to prevent infinite recursion | ||
| * @returns `true` if the type or any of its base types match the matcher | ||
| */ | ||
| export function matchesTypeOrBaseType( | ||
| services: ParserServicesWithTypeInformation, | ||
| matcher: (type: Type) => boolean, | ||
| type: Type, | ||
| seen = new Set<Type>(), | ||
| ): boolean { | ||
| if (seen.has(type)) { | ||
| return false; | ||
| } | ||
| seen.add(type); | ||
| if (matcher(type)) { | ||
| return true; | ||
| } | ||
| const checker = services.program.getTypeChecker(); | ||
| return getBaseTypesForType(checker, type).some(base => | ||
| matchesTypeOrBaseType(services, matcher, base, seen), | ||
| ); | ||
| } |
1 change: 1 addition & 0 deletionspackages/eslint-plugin/src/util/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletionspackages/eslint-plugin/tests/rules/restrict-template-expressions.test.ts
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. [Testing] Oh, sory, I just realized - this is missing test coverage for extending multiple classes.
etc. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.