Fix compatibility between estree and TS plugin (#9700)

This commit is contained in:
Daniel Tschinder 2019-03-23 16:47:50 -07:00 committed by GitHub
parent 419d8052d3
commit ef0722b4b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 1242 additions and 20 deletions

View File

@ -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)) {
@ -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();
}
@ -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;
@ -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();
@ -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");
}

View File

@ -0,0 +1,4 @@
enum A {
a,
"r"
}

View File

@ -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\""
}
}
]
}
]
}
}

View File

@ -0,0 +1 @@
import x = require ("asdfasdf");

View File

@ -0,0 +1,3 @@
{
"sourceType": "module"
}

View File

@ -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\""
}
}
}
]
}
}

View File

@ -0,0 +1,3 @@
let x: typeof import('./x');
let Y: import('./y').Y;
let z: import("/z").foo.bar<string>;

View File

@ -0,0 +1,441 @@
{
"type": "File",
"start": 0,
"end": 89,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 36
}
},
"program": {
"type": "Program",
"start": 0,
"end": 89,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 36
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 28
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 4,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 27
}
},
"id": {
"type": "Identifier",
"start": 4,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 27
},
"identifierName": "x"
},
"name": "x",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 5,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 27
}
},
"typeAnnotation": {
"type": "TSTypeQuery",
"start": 7,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 27
}
},
"exprName": {
"type": "TSImportType",
"start": 14,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 14
},
"end": {
"line": 1,
"column": 27
}
},
"argument": {
"type": "Literal",
"start": 21,
"end": 26,
"loc": {
"start": {
"line": 1,
"column": 21
},
"end": {
"line": 1,
"column": 26
}
},
"value": "./x",
"raw": "'./x'"
}
}
}
}
},
"init": null
}
],
"kind": "let"
},
{
"type": "VariableDeclaration",
"start": 29,
"end": 52,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 23
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 33,
"end": 51,
"loc": {
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 2,
"column": 22
}
},
"id": {
"type": "Identifier",
"start": 33,
"end": 51,
"loc": {
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 2,
"column": 22
},
"identifierName": "Y"
},
"name": "Y",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 34,
"end": 51,
"loc": {
"start": {
"line": 2,
"column": 5
},
"end": {
"line": 2,
"column": 22
}
},
"typeAnnotation": {
"type": "TSImportType",
"start": 36,
"end": 51,
"loc": {
"start": {
"line": 2,
"column": 7
},
"end": {
"line": 2,
"column": 22
}
},
"argument": {
"type": "Literal",
"start": 43,
"end": 48,
"loc": {
"start": {
"line": 2,
"column": 14
},
"end": {
"line": 2,
"column": 19
}
},
"value": "./y",
"raw": "'./y'"
},
"qualifier": {
"type": "Identifier",
"start": 50,
"end": 51,
"loc": {
"start": {
"line": 2,
"column": 21
},
"end": {
"line": 2,
"column": 22
},
"identifierName": "Y"
},
"name": "Y"
}
}
}
},
"init": null
}
],
"kind": "let"
},
{
"type": "VariableDeclaration",
"start": 53,
"end": 89,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 36
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 57,
"end": 88,
"loc": {
"start": {
"line": 3,
"column": 4
},
"end": {
"line": 3,
"column": 35
}
},
"id": {
"type": "Identifier",
"start": 57,
"end": 88,
"loc": {
"start": {
"line": 3,
"column": 4
},
"end": {
"line": 3,
"column": 35
},
"identifierName": "z"
},
"name": "z",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start": 58,
"end": 88,
"loc": {
"start": {
"line": 3,
"column": 5
},
"end": {
"line": 3,
"column": 35
}
},
"typeAnnotation": {
"type": "TSImportType",
"start": 60,
"end": 88,
"loc": {
"start": {
"line": 3,
"column": 7
},
"end": {
"line": 3,
"column": 35
}
},
"argument": {
"type": "Literal",
"start": 67,
"end": 71,
"loc": {
"start": {
"line": 3,
"column": 14
},
"end": {
"line": 3,
"column": 18
}
},
"value": "/z",
"raw": "\"/z\""
},
"qualifier": {
"type": "TSQualifiedName",
"start": 73,
"end": 80,
"loc": {
"start": {
"line": 3,
"column": 20
},
"end": {
"line": 3,
"column": 27
}
},
"left": {
"type": "Identifier",
"start": 73,
"end": 76,
"loc": {
"start": {
"line": 3,
"column": 20
},
"end": {
"line": 3,
"column": 23
},
"identifierName": "foo"
},
"name": "foo"
},
"right": {
"type": "Identifier",
"start": 77,
"end": 80,
"loc": {
"start": {
"line": 3,
"column": 24
},
"end": {
"line": 3,
"column": 27
},
"identifierName": "bar"
},
"name": "bar"
}
},
"typeParameters": {
"type": "TSTypeParameterInstantiation",
"start": 80,
"end": 88,
"loc": {
"start": {
"line": 3,
"column": 27
},
"end": {
"line": 3,
"column": 35
}
},
"params": [
{
"type": "TSStringKeyword",
"start": 81,
"end": 87,
"loc": {
"start": {
"line": 3,
"column": 28
},
"end": {
"line": 3,
"column": 34
}
}
}
]
}
}
}
},
"init": null
}
],
"kind": "let"
}
]
}
}

View File

@ -0,0 +1,7 @@
type Foo = false;
type Foo2 = true;
type Foo3 = "string";
type Foo4 = 123;
type Foo4 = 123.4;
type Foo5 = -123;
type Foo5 = -123.5;

View File

@ -0,0 +1,516 @@
{
"type": "File",
"start": 0,
"end": 131,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 7,
"column": 19
}
},
"program": {
"type": "Program",
"start": 0,
"end": 131,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 7,
"column": 19
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "TSTypeAliasDeclaration",
"start": 0,
"end": 17,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 17
}
},
"id": {
"type": "Identifier",
"start": 5,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 8
},
"identifierName": "Foo"
},
"name": "Foo"
},
"typeAnnotation": {
"type": "TSLiteralType",
"start": 11,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 11
},
"end": {
"line": 1,
"column": 16
}
},
"literal": {
"type": "Literal",
"start": 11,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 11
},
"end": {
"line": 1,
"column": 16
}
},
"value": false,
"raw": "false"
}
}
},
{
"type": "TSTypeAliasDeclaration",
"start": 18,
"end": 35,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 17
}
},
"id": {
"type": "Identifier",
"start": 23,
"end": 27,
"loc": {
"start": {
"line": 2,
"column": 5
},
"end": {
"line": 2,
"column": 9
},
"identifierName": "Foo2"
},
"name": "Foo2"
},
"typeAnnotation": {
"type": "TSLiteralType",
"start": 30,
"end": 34,
"loc": {
"start": {
"line": 2,
"column": 12
},
"end": {
"line": 2,
"column": 16
}
},
"literal": {
"type": "Literal",
"start": 30,
"end": 34,
"loc": {
"start": {
"line": 2,
"column": 12
},
"end": {
"line": 2,
"column": 16
}
},
"value": true,
"raw": "true"
}
}
},
{
"type": "TSTypeAliasDeclaration",
"start": 36,
"end": 57,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 21
}
},
"id": {
"type": "Identifier",
"start": 41,
"end": 45,
"loc": {
"start": {
"line": 3,
"column": 5
},
"end": {
"line": 3,
"column": 9
},
"identifierName": "Foo3"
},
"name": "Foo3"
},
"typeAnnotation": {
"type": "TSLiteralType",
"start": 48,
"end": 56,
"loc": {
"start": {
"line": 3,
"column": 12
},
"end": {
"line": 3,
"column": 20
}
},
"literal": {
"type": "Literal",
"start": 48,
"end": 56,
"loc": {
"start": {
"line": 3,
"column": 12
},
"end": {
"line": 3,
"column": 20
}
},
"value": "string",
"raw": "\"string\""
}
}
},
{
"type": "TSTypeAliasDeclaration",
"start": 58,
"end": 74,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 4,
"column": 16
}
},
"id": {
"type": "Identifier",
"start": 63,
"end": 67,
"loc": {
"start": {
"line": 4,
"column": 5
},
"end": {
"line": 4,
"column": 9
},
"identifierName": "Foo4"
},
"name": "Foo4"
},
"typeAnnotation": {
"type": "TSLiteralType",
"start": 70,
"end": 73,
"loc": {
"start": {
"line": 4,
"column": 12
},
"end": {
"line": 4,
"column": 15
}
},
"literal": {
"type": "Literal",
"start": 70,
"end": 73,
"loc": {
"start": {
"line": 4,
"column": 12
},
"end": {
"line": 4,
"column": 15
}
},
"value": 123,
"raw": "123"
}
}
},
{
"type": "TSTypeAliasDeclaration",
"start": 75,
"end": 93,
"loc": {
"start": {
"line": 5,
"column": 0
},
"end": {
"line": 5,
"column": 18
}
},
"id": {
"type": "Identifier",
"start": 80,
"end": 84,
"loc": {
"start": {
"line": 5,
"column": 5
},
"end": {
"line": 5,
"column": 9
},
"identifierName": "Foo4"
},
"name": "Foo4"
},
"typeAnnotation": {
"type": "TSLiteralType",
"start": 87,
"end": 92,
"loc": {
"start": {
"line": 5,
"column": 12
},
"end": {
"line": 5,
"column": 17
}
},
"literal": {
"type": "Literal",
"start": 87,
"end": 92,
"loc": {
"start": {
"line": 5,
"column": 12
},
"end": {
"line": 5,
"column": 17
}
},
"value": 123.4,
"raw": "123.4"
}
}
},
{
"type": "TSTypeAliasDeclaration",
"start": 94,
"end": 111,
"loc": {
"start": {
"line": 6,
"column": 0
},
"end": {
"line": 6,
"column": 17
}
},
"id": {
"type": "Identifier",
"start": 99,
"end": 103,
"loc": {
"start": {
"line": 6,
"column": 5
},
"end": {
"line": 6,
"column": 9
},
"identifierName": "Foo5"
},
"name": "Foo5"
},
"typeAnnotation": {
"type": "TSLiteralType",
"start": 106,
"end": 110,
"loc": {
"start": {
"line": 6,
"column": 12
},
"end": {
"line": 6,
"column": 16
}
},
"literal": {
"type": "UnaryExpression",
"start": 106,
"end": 110,
"loc": {
"start": {
"line": 6,
"column": 12
},
"end": {
"line": 6,
"column": 16
}
},
"operator": "-",
"prefix": true,
"argument": {
"type": "Literal",
"start": 107,
"end": 110,
"loc": {
"start": {
"line": 6,
"column": 13
},
"end": {
"line": 6,
"column": 16
}
},
"value": 123,
"raw": "123"
}
}
}
},
{
"type": "TSTypeAliasDeclaration",
"start": 112,
"end": 131,
"loc": {
"start": {
"line": 7,
"column": 0
},
"end": {
"line": 7,
"column": 19
}
},
"id": {
"type": "Identifier",
"start": 117,
"end": 121,
"loc": {
"start": {
"line": 7,
"column": 5
},
"end": {
"line": 7,
"column": 9
},
"identifierName": "Foo5"
},
"name": "Foo5"
},
"typeAnnotation": {
"type": "TSLiteralType",
"start": 124,
"end": 130,
"loc": {
"start": {
"line": 7,
"column": 12
},
"end": {
"line": 7,
"column": 18
}
},
"literal": {
"type": "UnaryExpression",
"start": 124,
"end": 130,
"loc": {
"start": {
"line": 7,
"column": 12
},
"end": {
"line": 7,
"column": 18
}
},
"operator": "-",
"prefix": true,
"argument": {
"type": "Literal",
"start": 125,
"end": 130,
"loc": {
"start": {
"line": 7,
"column": 13
},
"end": {
"line": 7,
"column": 18
}
},
"value": 123.5,
"raw": "123.5"
}
}
}
}
]
}
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["typescript", "estree"]
}

View File

@ -103,7 +103,7 @@
}
},
"literal": {
"type": "NumericLiteral",
"type": "UnaryExpression",
"start": 7,
"end": 9,
"loc": {
@ -116,11 +116,28 @@
"column": 9
}
},
"extra": {
"rawValue": -1,
"raw": "-1"
"operator": "-",
"prefix": true,
"argument": {
"type": "NumericLiteral",
"start": 8,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 8
},
"value": -1
"end": {
"line": 1,
"column": 9
}
},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
}
}
}