Skip to content

Commit

Permalink
feat(typescript-estree): throw errors on interface with implements (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Mar 8, 2023
1 parent c562e18 commit 67e05c8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
@@ -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

0 comments on commit 67e05c8

Please sign in to comment.