Throw error if TypeScript class has empty implements (#9292)

This commit is contained in:
Brian Ng 2019-01-07 15:27:27 -06:00 committed by GitHub
parent 9803253363
commit 03022d169e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 129 additions and 4 deletions

View File

@ -905,11 +905,21 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return this.finishNode(node, "TSTypeAssertion");
}
tsParseHeritageClause(): $ReadOnlyArray<N.TsExpressionWithTypeArguments> {
return this.tsParseDelimitedList(
tsParseHeritageClause(
descriptor: string,
): $ReadOnlyArray<N.TsExpressionWithTypeArguments> {
const originalStart = this.state.start;
const delimitedList = this.tsParseDelimitedList(
"HeritageClauseElement",
this.tsParseExpressionWithTypeArguments.bind(this),
);
if (!delimitedList.length) {
this.raise(originalStart, `'${descriptor}' list cannot be empty.`);
}
return delimitedList;
}
tsParseExpressionWithTypeArguments(): N.TsExpressionWithTypeArguments {
@ -930,7 +940,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.id = this.parseIdentifier();
node.typeParameters = this.tsTryParseTypeParameters();
if (this.eat(tt._extends)) {
node.extends = this.tsParseHeritageClause();
node.extends = this.tsParseHeritageClause("extends");
}
const body: N.TSInterfaceBody = this.startNode();
body.body = this.tsInType(this.tsParseObjectTypeMembers.bind(this));
@ -1879,7 +1889,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.superTypeParameters = this.tsParseTypeArguments();
}
if (this.eatContextual("implements")) {
node.implements = this.tsParseHeritageClause();
node.implements = this.tsParseHeritageClause("implements");
}
}

View File

@ -0,0 +1,2 @@
interface foo extends {
}

View File

@ -0,0 +1,3 @@
{
"throws": "'extends' list cannot be empty. (1:22)"
}

View File

@ -0,0 +1,2 @@
class Foo extends Bar implements {
}

View File

@ -0,0 +1,3 @@
{
"throws": "'implements' list cannot be empty. (1:33)"
}

View File

@ -0,0 +1,2 @@
class Foo implements {
}

View File

@ -0,0 +1,3 @@
{
"throws": "'implements' list cannot be empty. (1:21)"
}

View File

@ -0,0 +1 @@
foo<>()

View File

@ -0,0 +1,99 @@
{
"type": "File",
"start": 0,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 7
}
},
"program": {
"type": "Program",
"start": 0,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 7
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 7
}
},
"expression": {
"type": "CallExpression",
"start": 0,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 7
}
},
"callee": {
"type": "Identifier",
"start": 0,
"end": 3,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 3
},
"identifierName": "foo"
},
"name": "foo"
},
"arguments": [],
"typeParameters": {
"type": "TSTypeParameterInstantiation",
"start": 3,
"end": 5,
"loc": {
"start": {
"line": 1,
"column": 3
},
"end": {
"line": 1,
"column": 5
}
},
"params": []
}
}
}
],
"directives": []
}
}