-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(typescript-estree): forbid abstract method and accessor to have implementation #11657
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5e37414
7fbd6e6
f936621
a187b90
8c9ad39
101d790
5ca86a8
c30d11d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| abstract class Foo { | ||
| abstract constructor() { } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| abstract class Foo { | ||
| abstract get getter() { } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| abstract class Foo { | ||
| abstract method() { } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| abstract class Foo { | ||
| abstract set setter(v) { } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ import { getDecorators, getModifiers } from './getModifiers'; | |
| import { | ||
| canContainDirective, | ||
| createError, | ||
| declarationNameToString, | ||
| findNextToken, | ||
| getBinaryExpressionType, | ||
| getContainingFunction, | ||
|
|
@@ -1599,6 +1600,18 @@ export class Converter { | |
| } | ||
| // otherwise, it is a non-type accessor - intentional fallthrough | ||
| case SyntaxKind.MethodDeclaration: { | ||
| const isAbstract = hasModifier(SyntaxKind.AbstractKeyword, node); | ||
|
|
||
| if (isAbstract && node.body) { | ||
| this.#throwError( | ||
| node.name, | ||
| node.kind === SyntaxKind.GetAccessor || | ||
| node.kind === SyntaxKind.SetAccessor | ||
| ? 'An abstract accessor cannot have an implementation.' | ||
| : `Method '${declarationNameToString(node.name, this.ast)}' cannot have an implementation because it is marked abstract.`, | ||
|
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. [Praise] These are great error messages! 👏 |
||
| ); | ||
| } | ||
|
|
||
| const method = this.createNode< | ||
| TSESTree.FunctionExpression | TSESTree.TSEmptyBodyFunctionExpression | ||
| >(node, { | ||
|
|
@@ -1654,10 +1667,7 @@ export class Converter { | |
| /** | ||
| * TypeScript class methods can be defined as "abstract" | ||
| */ | ||
| const methodDefinitionType = hasModifier( | ||
| SyntaxKind.AbstractKeyword, | ||
| node, | ||
| ) | ||
| const methodDefinitionType = isAbstract | ||
| ? AST_NODE_TYPES.TSAbstractMethodDefinition | ||
| : AST_NODE_TYPES.MethodDefinition; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -919,3 +919,12 @@ export function getNamespaceModifiers( | |
| } | ||
| return modifiers; | ||
| } | ||
|
|
||
| // `ts.declarationNameToString` | ||
| export function declarationNameToString( | ||
| name: ts.Node, | ||
| ast: ts.SourceFile, | ||
| ): string { | ||
| const text = ast.text.slice(name.pos, name.end).trimStart(); | ||
| return text || '(Missing)'; | ||
|
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] This doesn't show up in any of the snapshots. Can you either add a test showing it (if it's possible?) or remove it?
Contributor
Author
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. This original
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. 👍 makes sense, I think it's reasonable to leave in then. Thanks. |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Praise] Nice, always happy to see this file cleaned up.