Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

11 changes: 10 additions & 1 deletion packages/eslint-plugin/tests/rules/member-ordering.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4465,7 +4465,7 @@ abstract class Foo {
B: string;
public C() {}
private D() {}
abstract E() {}
E() {}
}
`,
errors: [
Expand All @@ -4478,6 +4478,15 @@ abstract class Foo {
line: 4,
messageId: 'incorrectGroupOrder',
},
{
column: 3,
data: {
name: 'E',
rank: 'private instance method',
},
line: 7,
messageId: 'incorrectGroupOrder',
},
],
},
{
Expand Down
Copy link
Member

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.

Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ ruleTester.run('naming-convention', rule, {
{
code: `
class Ignored {
private static abstract some_name() {}
private static some_name() {}
IgnoredDueToModifiers() {}
}
`,
Expand All @@ -644,7 +644,7 @@ ruleTester.run('naming-convention', rule, {
},
{
format: ['UPPER_CASE'],
modifiers: ['abstract', 'static'],
modifiers: ['static'],
selector: 'classMethod',
},
],
Expand Down Expand Up @@ -1875,7 +1875,7 @@ ruleTester.run('naming-convention', rule, {
{
code: `
class Ignored {
private static abstract some_name() {}
private static some_name() {}
IgnoredDueToModifiers() {}
}
`,
Expand All @@ -1886,7 +1886,7 @@ ruleTester.run('naming-convention', rule, {
},
{
format: ['snake_case'],
modifiers: ['abstract', 'static'],
modifiers: ['static'],
selector: 'classMethod',
},
],
Expand Down Expand Up @@ -2118,10 +2118,10 @@ ruleTester.run('naming-convention', rule, {
}
}
abstract class foo {
public abstract Bar() {
public Bar() {
return 42;
}
public abstract async async_bar() {
public async async_bar() {
return 42;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,6 @@ function foo(): Promise<string> | boolean {
code: `
abstract class Test {
abstract test1(): Promise<number>;

// abstract method with body is always an error but it still parses into valid AST
abstract test2(): Promise<number> {
return Promise.resolve(1);
}
}
`,
},
Expand Down
18 changes: 14 additions & 4 deletions packages/typescript-estree/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { getDecorators, getModifiers } from './getModifiers';
import {
canContainDirective,
createError,
declarationNameToString,
findNextToken,
getBinaryExpressionType,
getContainingFunction,
Expand Down Expand Up @@ -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.`,
Copy link
Member

Choose a reason for hiding this comment

The 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, {
Expand Down Expand Up @@ -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;

Expand Down
9 changes: 9 additions & 0 deletions packages/typescript-estree/src/node-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'(Missing)'

[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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This original declarationNameToString from typescript package have this, so I guess it's possible, but I don't know how to test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 makes sense, I think it's reasonable to leave in then. Thanks.

}
Loading