Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): [strict-interface-implementation] add rule#11711
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
Draft
JoshuaKGoldberg wants to merge1 commit intotypescript-eslint:mainChoose a base branch fromJoshuaKGoldberg:strict-interface-implementation
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+281 −0
Draft
Changes fromall commits
Commits
File 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
159 changes: 159 additions & 0 deletionspackages/eslint-plugin/src/rules/strict-interface-implementation.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,159 @@ | ||
| import type { TSESTree } from '@typescript-eslint/utils'; | ||
| import type * as ts from 'typescript'; | ||
| import { AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
| import type { NodeWithKey } from '../util'; | ||
| import { | ||
| createRule, | ||
| getParserServices, | ||
| getStaticMemberAccessValue, | ||
| isNodeWithKey, | ||
| } from '../util'; | ||
| type NodeWithStaticKey = Exclude< | ||
| NodeWithKey, | ||
| | TSESTree.MemberExpressionComputedName | ||
| | TSESTree.MemberExpressionNonComputedName | ||
| >; | ||
| export default createRule({ | ||
| name: 'strict-interface-implementation', | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: | ||
| 'Enforce classes are fully assignable to any interfaces they implement', | ||
| requiresTypeChecking: true, | ||
| }, | ||
| fixable: 'code', | ||
| messages: { | ||
| unassignable: | ||
| 'This {{target}} is not fully assignable to the interface {{interface}} type for {{name}}.', | ||
| }, | ||
| schema: [], | ||
| }, | ||
| defaultOptions: [], | ||
| create(context) { | ||
| const services = getParserServices(context); | ||
| const checker = services.program.getTypeChecker(); | ||
| function checkClassImplements( | ||
| node: TSESTree.ClassDeclaration | TSESTree.ClassExpression, | ||
| base: ts.Type, | ||
| ) { | ||
| for (const element of node.body.body) { | ||
| if (element.type === AST_NODE_TYPES.MethodDefinition) { | ||
| checkMethod(element, base); | ||
| } else if (isNodeWithKey(element)) { | ||
| checkProperty(element, base); | ||
| } | ||
| } | ||
| } | ||
| function checkMethod(element: TSESTree.MethodDefinition, base: ts.Type) { | ||
| const methodName = getStaticMemberAccessValue(element, context); | ||
| if (typeof methodName !== 'string') { | ||
| return; | ||
| } | ||
| const baseMethod = base.getProperty(methodName); | ||
| if (!baseMethod?.valueDeclaration) { | ||
| return; | ||
| } | ||
| const baseType = checker.getTypeAtLocation(baseMethod.valueDeclaration); | ||
| const derivedType = services.getTypeAtLocation(element); | ||
| if (isMethodAssignable(baseType, derivedType)) { | ||
| return; | ||
| } | ||
| context.report({ | ||
| node: element.key, | ||
| messageId: 'unassignable', | ||
| data: { | ||
| name: methodName, | ||
| interface: checker.typeToString(base), | ||
| target: 'method', | ||
| }, | ||
| }); | ||
| } | ||
| function isMethodAssignable(base: ts.Type, derived: ts.Type) { | ||
| const baseSignature = base.getCallSignatures()[0]; | ||
| const derivedSignature = derived.getCallSignatures()[0]; | ||
| if ( | ||
| derivedSignature.parameters.length > baseSignature.parameters.length | ||
| ) { | ||
| return false; | ||
| } | ||
| for (let i = 0; i < baseSignature.parameters.length; i += 1) { | ||
| const baseType = checker.getTypeOfSymbol(baseSignature.parameters[i]); | ||
| const derivedType = checker.getTypeOfSymbol( | ||
| derivedSignature.parameters[i], | ||
| ); | ||
| if (!checker.isTypeAssignableTo(baseType, derivedType)) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| function checkProperty(element: NodeWithStaticKey, base: ts.Type) { | ||
| const propertyName = getStaticMemberAccessValue(element, context); | ||
| if (typeof propertyName !== 'string') { | ||
| return; | ||
| } | ||
| const baseProperty = base.getProperty(propertyName); | ||
| if (!baseProperty?.valueDeclaration) { | ||
| return; | ||
| } | ||
| const baseType = checker.getTypeAtLocation(baseProperty.valueDeclaration); | ||
| const derivedType = services.getTypeAtLocation(element); | ||
| if (checker.isTypeAssignableTo(baseType, derivedType)) { | ||
| return; | ||
| } | ||
| context.report({ | ||
| node: element.key, | ||
| messageId: 'unassignable', | ||
| data: { | ||
| name: propertyName, | ||
| interface: checker.typeToString(base), | ||
| target: 'property', | ||
| }, | ||
| }); | ||
| } | ||
| function getSuperClassImplements( | ||
| superClass: TSESTree.LeftHandSideExpression, | ||
| ) { | ||
| // TODO | ||
| } | ||
| return { | ||
| 'ClassDeclaration, ClassExpression'( | ||
| node: TSESTree.ClassDeclaration | TSESTree.ClassExpression, | ||
| ) { | ||
| for (const base of node.implements) { | ||
| checkClassImplements(node, services.getTypeAtLocation(base)); | ||
| } | ||
| if (node.superClass) { | ||
| for (const base of getSuperClassImplements(node.superClass)) { | ||
| checkClassImplements(node, base); | ||
| } | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }); |
15 changes: 15 additions & 0 deletionspackages/eslint-plugin/src/util/misc.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
107 changes: 107 additions & 0 deletionspackages/eslint-plugin/tests/rules/strict-interface-implementation.test.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,107 @@ | ||
| import { RuleTester } from '@typescript-eslint/rule-tester'; | ||
| import rule from '../../src/rules/strict-interface-implementation'; | ||
| import { getFixturesRootDir } from '../RuleTester'; | ||
| const rootDir = getFixturesRootDir(); | ||
| const ruleTester = new RuleTester({ | ||
| languageOptions: { | ||
| parserOptions: { | ||
| project: './tsconfig.json', | ||
| tsconfigRootDir: rootDir, | ||
| }, | ||
| }, | ||
| }); | ||
| ruleTester.run('strict-interface-implementation', rule, { | ||
| valid: [ | ||
| 'class Standalone {}', | ||
| 'const Standalone = class {};', | ||
| 'const Standalone = class Standalone {};', | ||
| ` | ||
| interface Base {} | ||
| class Derived implements Base {} | ||
| `, | ||
| ` | ||
| interface Base { | ||
| process(): void; | ||
| } | ||
| class Derived implements Base { | ||
| process() {} | ||
| } | ||
| `, | ||
| ` | ||
| interface Base { | ||
| value: string; | ||
| } | ||
| class Derived implements Base { | ||
| value: string; | ||
| } | ||
| `, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: ` | ||
| interface Base { | ||
| value: string | undefined; | ||
| } | ||
| class Derived implements Base { | ||
| value: string; | ||
| } | ||
| `, | ||
| errors: [ | ||
| { | ||
| data: { | ||
| interface: 'Base', | ||
| name: 'value', | ||
| target: 'property', | ||
| }, | ||
| messageId: 'unassignable', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: ` | ||
| interface Base { | ||
| process(value: string | null): void; | ||
| } | ||
| class Derived implements Base { | ||
| public process(value: string) {} | ||
| } | ||
| `, | ||
| errors: [ | ||
| { | ||
| data: { | ||
| interface: 'Base', | ||
| name: 'process', | ||
| target: 'method', | ||
| }, | ||
| messageId: 'unassignable', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: ` | ||
| interface Base { | ||
| process(value?: string): void; | ||
| } | ||
| class Derived implements Base { | ||
| public process(value: string) {} | ||
| } | ||
| `, | ||
| errors: [ | ||
| { | ||
| data: { | ||
| interface: 'Base', | ||
| name: 'process', | ||
| target: 'method', | ||
| }, | ||
| messageId: 'unassignable', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.