[babel 8] Don't create TSParenthesizedType nodes by default (#12608)

Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com>
Co-authored-by: Sergey Melyukov <s.melukov@gmail.com>
This commit is contained in:
Huáng Jùnliàng 2021-01-12 19:13:09 -05:00 committed by GitHub
parent 2338b052ad
commit 62290aa1ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 140 additions and 19 deletions

View File

@ -0,0 +1 @@
type T = ({});

View File

@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": false
}

View File

@ -0,0 +1 @@
type T = ({});

View File

@ -0,0 +1,4 @@
{
"parserOpts": { "createParenthesizedExpressions": true },
"BABEL_8_BREAKING": false
}

View File

@ -0,0 +1,4 @@
{
"parserOpts": { "createParenthesizedExpressions": true },
"BABEL_8_BREAKING": true
}

View File

@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": true
}

View File

@ -1 +1 @@
type T = ({});
type T = {};

View File

@ -842,6 +842,18 @@ export default (superClass: Class<Parser>): Class<Parser> =>
case tt.bracketL:
return this.tsParseTupleType();
case tt.parenL:
if (process.env.BABEL_8_BREAKING) {
if (!this.options.createParenthesizedExpressions) {
const startPos = this.state.start;
this.next();
const type = this.tsParseType();
this.expect(tt.parenR);
this.addExtra(type, "parenthesized", true);
this.addExtra(type, "parenStart", startPos);
return type;
}
}
return this.tsParseParenthesizedType();
case tt.backQuote:
return this.tsParseTemplateLiteralType();

View File

@ -0,0 +1,4 @@
{
"plugins": ["typescript"],
"createParenthesizedExpressions": true
}

View File

@ -0,0 +1,4 @@
{
"plugins": ["typescript"],
"createParenthesizedExpressions": true
}

View File

@ -0,0 +1 @@
let x: [string, number?, (string | number)?]

View File

@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": false
}

View File

@ -0,0 +1,74 @@
{
"type": "File",
"start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":44}},
"program": {
"type": "Program",
"start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":44}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":44}},
"declarations": [
{
"type": "VariableDeclarator",
"start":4,"end":44,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":44}},
"id": {
"type": "Identifier",
"start":4,"end":44,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":44},"identifierName":"x"},
"name": "x",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":5,"end":44,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":44}},
"typeAnnotation": {
"type": "TSTupleType",
"start":7,"end":44,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":44}},
"elementTypes": [
{
"type": "TSStringKeyword",
"start":8,"end":14,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":14}}
},
{
"type": "TSOptionalType",
"start":16,"end":23,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":23}},
"typeAnnotation": {
"type": "TSNumberKeyword",
"start":16,"end":22,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":22}}
}
},
{
"type": "TSOptionalType",
"start":25,"end":43,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":43}},
"typeAnnotation": {
"type": "TSParenthesizedType",
"start":25,"end":42,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":42}},
"typeAnnotation": {
"type": "TSUnionType",
"start":26,"end":41,"loc":{"start":{"line":1,"column":26},"end":{"line":1,"column":41}},
"types": [
{
"type": "TSStringKeyword",
"start":26,"end":32,"loc":{"start":{"line":1,"column":26},"end":{"line":1,"column":32}}
},
{
"type": "TSNumberKeyword",
"start":35,"end":41,"loc":{"start":{"line":1,"column":35},"end":{"line":1,"column":41}}
}
]
}
}
}
]
}
}
},
"init": null
}
],
"kind": "let"
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": true
}

View File

@ -39,24 +39,24 @@
},
{
"type": "TSOptionalType",
"start":25,"end":43,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":43}},
"start":26,"end":43,"loc":{"start":{"line":1,"column":26},"end":{"line":1,"column":43}},
"typeAnnotation": {
"type": "TSParenthesizedType",
"start":25,"end":42,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":42}},
"typeAnnotation": {
"type": "TSUnionType",
"start":26,"end":41,"loc":{"start":{"line":1,"column":26},"end":{"line":1,"column":41}},
"types": [
{
"type": "TSStringKeyword",
"start":26,"end":32,"loc":{"start":{"line":1,"column":26},"end":{"line":1,"column":32}}
},
{
"type": "TSNumberKeyword",
"start":35,"end":41,"loc":{"start":{"line":1,"column":35},"end":{"line":1,"column":41}}
}
]
}
"type": "TSUnionType",
"start":26,"end":41,"loc":{"start":{"line":1,"column":26},"end":{"line":1,"column":41}},
"extra": {
"parenthesized": true,
"parenStart": 25
},
"types": [
{
"type": "TSStringKeyword",
"start":26,"end":32,"loc":{"start":{"line":1,"column":26},"end":{"line":1,"column":32}}
},
{
"type": "TSNumberKeyword",
"start":35,"end":41,"loc":{"start":{"line":1,"column":35},"end":{"line":1,"column":41}}
}
]
}
}
]