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 all commits
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,10 @@
// 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
1 | // TODO: This fixture might be too large, and if so should be split up.
2 |
> 3 | interface d implements e {}
| ^^^^^^^^^^^^ Interface declaration cannot have 'implements' clause.
4 |"
`;
@@ -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 @@ -56,7 +56,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
34 changes: 23 additions & 11 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -2623,6 +2623,28 @@ export class Converter {
this.#checkIllegalDecorators(node);

const interfaceHeritageClauses = node.heritageClauses ?? [];
const interfaceExtends: TSESTree.TSInterfaceHeritage[] = [];

for (const heritageClause of interfaceHeritageClauses) {
if (heritageClause.token !== SyntaxKind.ExtendsKeyword) {
this.#throwError(
heritageClause,
heritageClause.token === SyntaxKind.ImplementsKeyword
? "Interface declaration cannot have 'implements' clause."
: 'Unexpected token.',
);
}

for (const heritageType of heritageClause.types) {
interfaceExtends.push(
this.convertChild(
heritageType,
node,
) as TSESTree.TSInterfaceHeritage,
);
}
}

const result = this.createNode<TSESTree.TSInterfaceDeclaration>(node, {
type: AST_NODE_TYPES.TSInterfaceDeclaration,
body: this.createNode<TSESTree.TSInterfaceBody>(node, {
Expand All @@ -2631,17 +2653,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