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

babel-parser(ts): Raise recoverable error for abstract interface #12771

Merged
merged 4 commits into from Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 2 additions & 4 deletions packages/babel-parser/src/parser/util.js
Expand Up @@ -90,10 +90,8 @@ export default class UtilParser extends Tokenizer {
);
}

hasPrecedingLineBreak(): boolean {
return lineBreak.test(
this.input.slice(this.state.lastTokEnd, this.state.start),
);
hasPrecedingLineBreak(state: State = this.state): boolean {
return lineBreak.test(this.input.slice(state.lastTokEnd, state.start));
}

// TODO
Expand Down
55 changes: 41 additions & 14 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -89,6 +89,8 @@ const TSErrors = Object.freeze({
"Tuple members must all have names or all not have names.",
NonAbstractClassHasAbstractMethod:
"Abstract methods can only appear within an abstract class.",
NonClassMethodPropertyHasAbstractModifer:
"'abstract' modifier can only appear on a class, method, or property declaration.",
OptionalTypeBeforeRequired:
"A required element cannot follow an optional element.",
PatternIsOptional:
Expand Down Expand Up @@ -1585,20 +1587,13 @@ export default (superClass: Class<Parser>): Class<Parser> =>
): ?N.Declaration {
switch (value) {
case "abstract":
if (this.tsCheckLineTerminatorAndMatch(tt._class, next)) {
const cls: N.ClassDeclaration = node;
cls.abstract = true;
if (next) {
this.next();
if (!this.match(tt._class)) {
this.unexpected(null, tt._class);
}
}
return this.parseClass(
cls,
/* isStatement */ true,
/* optionalId */ false,
);
if (
this.tsCheckLineTerminatorAndMatch(tt._class, next) ||
// for interface
this.tsCheckLineTerminatorAndMatch(tt.name, next)
) {
if (next) this.next();
return this.tsParseAbstractDeclaration(node);
}
break;

Expand Down Expand Up @@ -2849,4 +2844,36 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.state.inAbstractClass = oldInAbstractClass;
}
}

tsParseAbstractDeclaration(
node: any,
): N.ClassDeclaration | N.TsInterfaceDeclaration {
if (this.match(tt._class)) {
node.abstract = true;
return this.parseClass(
(node: N.ClassDeclaration),
/* isStatement */ true,
/* optionalId */ false,
);
} else if (this.isContextual("interface")) {
// for invalid abstract interface

// To avoid
// abstract interface
// Foo {}
if (!this.hasPrecedingLineBreak(this.lookahead())) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Is there a way to avoid this lookahead?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe with a combination of lineBreak.test and this.nextTokenStart.
If it works, it would be useful to extract it to a hasFollowingLineBreak() method.

Copy link
Member Author

Choose a reason for hiding this comment

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

It seems to work fine! da8ce69

node.abstract = true;
this.raise(
node.start,
TSErrors.NonClassMethodPropertyHasAbstractModifer,
);
this.next();
return this.tsParseInterfaceDeclaration(
(node: N.TsInterfaceDeclaration),
);
}
} else {
this.unexpected(null, tt._class);
}
}
};
@@ -0,0 +1,3 @@
abstract interface Foo {
foo: string;
}
@@ -0,0 +1,50 @@
{
"type": "File",
"start":0,"end":41,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"errors": [
"SyntaxError: 'abstract' modifier can only appear on a class, method, or property declaration. (1:0)"
],
"program": {
"type": "Program",
"start":0,"end":41,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TSInterfaceDeclaration",
"start":0,"end":41,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"abstract": true,
"id": {
"type": "Identifier",
"start":19,"end":22,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":22},"identifierName":"Foo"},
"name": "Foo"
},
"body": {
"type": "TSInterfaceBody",
"start":23,"end":41,"loc":{"start":{"line":1,"column":23},"end":{"line":3,"column":1}},
"body": [
{
"type": "TSPropertySignature",
"start":27,"end":39,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":14}},
"key": {
"type": "Identifier",
"start":27,"end":30,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":5},"identifierName":"foo"},
"name": "foo"
},
"computed": false,
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":30,"end":38,"loc":{"start":{"line":2,"column":5},"end":{"line":2,"column":13}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":32,"end":38,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":13}}
}
}
}
]
}
}
],
"directives": []
}
}

This file was deleted.

@@ -0,0 +1,38 @@
{
"type": "File",
"start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"errors": [
"SyntaxError: 'abstract' modifier can only appear on a class, method, or property declaration. (1:7)"
],
"program": {
"type": "Program",
"start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExportNamedDeclaration",
"start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"exportKind": "type",
"specifiers": [],
"source": null,
"declaration": {
"type": "TSInterfaceDeclaration",
"start":7,"end":32,"loc":{"start":{"line":1,"column":7},"end":{"line":3,"column":1}},
"abstract": true,
"id": {
"type": "Identifier",
"start":26,"end":27,"loc":{"start":{"line":1,"column":26},"end":{"line":1,"column":27},"identifierName":"I"},
"name": "I"
},
"body": {
"type": "TSInterfaceBody",
"start":28,"end":32,"loc":{"start":{"line":1,"column":28},"end":{"line":3,"column":1}},
"body": []
}
}
}
],
"directives": []
}
}
@@ -0,0 +1,2 @@
abstract interface
Foo {}
@@ -0,0 +1,50 @@
{
"type": "File",
"start":0,"end":25,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":6}},
"errors": [
"SyntaxError: Missing semicolon (1:8)",
"SyntaxError: Missing semicolon (2:3)"
],
"program": {
"type": "Program",
"start":0,"end":25,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":6}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}},
"expression": {
"type": "Identifier",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8},"identifierName":"abstract"},
"name": "abstract"
}
},
{
"type": "ExpressionStatement",
"start":9,"end":18,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":18}},
"expression": {
"type": "Identifier",
"start":9,"end":18,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":18},"identifierName":"interface"},
"name": "interface"
}
},
{
"type": "ExpressionStatement",
"start":19,"end":22,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":3}},
"expression": {
"type": "Identifier",
"start":19,"end":22,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":3},"identifierName":"Foo"},
"name": "Foo"
}
},
{
"type": "BlockStatement",
"start":23,"end":25,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":6}},
"body": [],
"directives": []
}
],
"directives": []
}
}