Skip to content
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

feat(typescript-estree): throw errors on interface with implements #6551

Merged
merged 7 commits into from Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures legacy-fixtures errorRecovery _error_ interface-implements TSESTree - Error 1`] = `"NO ERROR"`;
exports[`AST Fixtures legacy-fixtures errorRecovery _error_ interface-implements TSESTree - Error 1`] = `[TSError: Interface declaration cannot have 'implements' clause.]`;
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures legacy-fixtures errorRecovery _error_ interface-implements Error Alignment 1`] = `"Babel errored but TSESTree didn't"`;
exports[`AST Fixtures legacy-fixtures errorRecovery _error_ interface-implements Error Alignment 1`] = `"Both errored"`;
Expand Up @@ -61,7 +61,6 @@ Object {
"legacy-fixtures/errorRecovery/fixtures/_error_/enum-with-keywords/fixture.ts",
"legacy-fixtures/errorRecovery/fixtures/_error_/index-signature-parameters/fixture.ts",
"legacy-fixtures/errorRecovery/fixtures/_error_/interface-empty-extends/fixture.ts",
"legacy-fixtures/errorRecovery/fixtures/_error_/interface-implements/fixture.ts",
"legacy-fixtures/errorRecovery/fixtures/_error_/interface-index-signature-private/fixture.ts",
"legacy-fixtures/errorRecovery/fixtures/_error_/interface-index-signature-protected/fixture.ts",
"legacy-fixtures/errorRecovery/fixtures/_error_/interface-index-signature-public/fixture.ts",
Expand Down
30 changes: 19 additions & 11 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -2583,6 +2583,24 @@ export class Converter {

case SyntaxKind.InterfaceDeclaration: {
const interfaceHeritageClauses = node.heritageClauses ?? [];
const interfaceExtends: TSESTree.TSInterfaceHeritage[] = [];

for (const heritageClause of interfaceHeritageClauses) {
if (heritageClause.token !== SyntaxKind.ExtendsKeyword) {
throw createError(
this.ast,
heritageClause.pos,
"Interface declaration cannot have 'implements' clause.",
);
}

interfaceExtends.push(
...heritageClause.types.map(
n => this.convertChild(n, node) as TSESTree.TSInterfaceHeritage,
),
);
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
}

const result = this.createNode<TSESTree.TSInterfaceDeclaration>(node, {
type: AST_NODE_TYPES.TSInterfaceDeclaration,
body: this.createNode<TSESTree.TSInterfaceBody>(node, {
Expand All @@ -2591,17 +2609,7 @@ export class Converter {
range: [node.members.pos - 1, node.end],
}),
declare: hasModifier(SyntaxKind.DeclareKeyword, node),
extends: interfaceHeritageClauses
.filter(
heritageClause =>
heritageClause.token === SyntaxKind.ExtendsKeyword,
)
.flatMap(heritageClause =>
heritageClause.types.map(
n => this.convertChild(n, node) as TSESTree.TSInterfaceHeritage,
),
),

extends: interfaceExtends,
id: this.convertChild(node.name),
typeParameters:
node.typeParameters &&
Expand Down