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

Fix compatibility between estree and TS plugin #9700

Merged
merged 1 commit into from Mar 23, 2019
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
24 changes: 10 additions & 14 deletions packages/babel-parser/src/plugins/typescript.js
Expand Up @@ -232,7 +232,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
"Argument in a type import must be a string literal",
);
}
node.argument = this.parseLiteral(this.state.value, "StringLiteral");

// For compatibility to estree we cannot call parseLiteral directly here
node.argument = this.parseExprAtom();
this.expect(tt.parenR);

if (this.eat(tt.dot)) {
Expand Down Expand Up @@ -646,12 +648,11 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.literal = (() => {
switch (this.state.type) {
case tt.num:
return this.parseLiteral(this.state.value, "NumericLiteral");
case tt.string:
return this.parseLiteral(this.state.value, "StringLiteral");
case tt._true:
case tt._false:
return this.parseBooleanLiteral();
// For compatibility to estree we cannot call parseLiteral directly here
return this.parseExprAtom();
default:
throw this.unexpected();
}
Expand Down Expand Up @@ -684,16 +685,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
case tt.plusMin:
if (this.state.value === "-") {
const node: N.TsLiteralType = this.startNode();
this.next();
if (!this.match(tt.num)) {
if (this.lookahead().type !== tt.num) {
throw this.unexpected();
}
node.literal = this.parseLiteral(
-this.state.value,
"NumericLiteral",
node.start,
node.loc.start,
);
node.literal = this.parseMaybeUnary();
return this.finishNode(node, "TSLiteralType");
}
break;
Expand Down Expand Up @@ -1108,7 +1103,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
const node: N.TsEnumMember = this.startNode();
// Computed property names are grammar errors in an enum, so accept just string literal or identifier.
node.id = this.match(tt.string)
? this.parseLiteral(this.state.value, "StringLiteral")
? this.parseExprAtom()
: this.parseIdentifier(/* liberal */ true);
if (this.eat(tt.eq)) {
node.initializer = this.parseMaybeAssign();
Expand Down Expand Up @@ -1213,7 +1208,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (!this.match(tt.string)) {
throw this.unexpected();
}
node.expression = this.parseLiteral(this.state.value, "StringLiteral");
// For compatibility to estree we cannot call parseLiteral directly here
node.expression = this.parseExprAtom();
this.expect(tt.parenR);
return this.finishNode(node, "TSExternalModuleReference");
}
Expand Down
@@ -0,0 +1,4 @@
enum A {
a,
"r"
}
132 changes: 132 additions & 0 deletions packages/babel-parser/test/fixtures/estree/typescript/enum/output.json
@@ -0,0 +1,132 @@
{
"type": "File",
"start": 0,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "TSEnumDeclaration",
"start": 0,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 5,
"end": 6,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 6
},
"identifierName": "A"
},
"name": "A"
},
"members": [
{
"type": "TSEnumMember",
"start": 11,
"end": 12,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 3
}
},
"id": {
"type": "Identifier",
"start": 11,
"end": 12,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 3
},
"identifierName": "a"
},
"name": "a"
}
},
{
"type": "TSEnumMember",
"start": 16,
"end": 19,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 5
}
},
"id": {
"type": "Literal",
"start": 16,
"end": 19,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 5
}
},
"value": "r",
"raw": "\"r\""
}
}
]
}
]
}
}
@@ -0,0 +1 @@
import x = require ("asdfasdf");
@@ -0,0 +1,3 @@
{
"sourceType": "module"
}
@@ -0,0 +1,99 @@
{
"type": "File",
"start": 0,
"end": 32,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 32
}
},
"program": {
"type": "Program",
"start": 0,
"end": 32,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 32
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "TSImportEqualsDeclaration",
"start": 0,
"end": 32,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 32
}
},
"isExport": false,
"id": {
"type": "Identifier",
"start": 7,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 8
},
"identifierName": "x"
},
"name": "x"
},
"moduleReference": {
"type": "TSExternalModuleReference",
"start": 11,
"end": 31,
"loc": {
"start": {
"line": 1,
"column": 11
},
"end": {
"line": 1,
"column": 31
}
},
"expression": {
"type": "Literal",
"start": 20,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 20
},
"end": {
"line": 1,
"column": 30
}
},
"value": "asdfasdf",
"raw": "\"asdfasdf\""
}
}
}
]
}
}
@@ -0,0 +1,3 @@
let x: typeof import('./x');
let Y: import('./y').Y;
let z: import("/z").foo.bar<string>;