Fix parsing of type annotation (#816)

This commit is contained in:
강동윤 2020-06-03 12:19:05 +09:00 committed by GitHub
parent ab7c9b1287
commit d7bb5c18ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 330 additions and 3 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "swc_ecma_parser"
version = "0.24.0"
version = "0.24.2"
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
license = "Apache-2.0/MIT"
repository = "https://github.com/swc-project/swc.git"

View File

@ -454,6 +454,9 @@ pub struct Context {
in_property_name: bool,
in_forced_jsx_context: bool,
/// If true, `:` should not be treated as a type annotation.
in_case_cond: bool,
}
#[derive(Clone, Copy)]

View File

@ -620,7 +620,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
let return_type = if !self.ctx().in_cond_expr
&& self.input.syntax().typescript()
&& is!(':')
&& !self.input.has_linebreak_between_cur_and_peeked()
&& !self.ctx().in_case_cond
{
Some(self.parse_ts_type_or_type_predicate_ann(&tok!(':'))?)
} else {

View File

@ -465,8 +465,15 @@ impl<'a, I: Tokens> Parser<'a, I> {
let is_case = is!("case");
let case_start = cur_pos!();
bump!();
let ctx = Context {
in_case_cond: true,
..p.ctx()
};
let test = if is_case {
p.include_in_expr(true).parse_expr().map(Some)?
p.with_ctx(ctx)
.include_in_expr(true)
.parse_expr()
.map(Some)?
} else {
if let Some(previous) = span_of_previous_default {
syntax_error!(SyntaxError::MultipleDefault { previous });

View File

@ -0,0 +1,12 @@
const testing = (
param: string,
other: number,
):
| string
| number
| number
| number
| string =>
{
return 5;
};

View File

@ -0,0 +1,200 @@
{
"type": "Module",
"span": {
"start": 0,
"end": 145,
"ctxt": 0
},
"body": [
{
"type": "VariableDeclaration",
"span": {
"start": 0,
"end": 145,
"ctxt": 0
},
"kind": "const",
"declare": false,
"declarations": [
{
"type": "VariableDeclarator",
"span": {
"start": 6,
"end": 144,
"ctxt": 0
},
"id": {
"type": "Identifier",
"span": {
"start": 6,
"end": 13,
"ctxt": 0
},
"value": "testing",
"typeAnnotation": null,
"optional": false
},
"init": {
"type": "ArrowFunctionExpression",
"span": {
"start": 16,
"end": 144,
"ctxt": 0
},
"params": [
{
"type": "Identifier",
"span": {
"start": 22,
"end": 35,
"ctxt": 0
},
"value": "param",
"typeAnnotation": {
"type": "TsTypeAnnotation",
"span": {
"start": 27,
"end": 35,
"ctxt": 0
},
"typeAnnotation": {
"type": "TsKeywordType",
"span": {
"start": 29,
"end": 35,
"ctxt": 0
},
"kind": "string"
}
},
"optional": false
},
{
"type": "Identifier",
"span": {
"start": 41,
"end": 54,
"ctxt": 0
},
"value": "other",
"typeAnnotation": {
"type": "TsTypeAnnotation",
"span": {
"start": 46,
"end": 54,
"ctxt": 0
},
"typeAnnotation": {
"type": "TsKeywordType",
"span": {
"start": 48,
"end": 54,
"ctxt": 0
},
"kind": "number"
}
},
"optional": false
}
],
"body": {
"type": "BlockStatement",
"span": {
"start": 127,
"end": 144,
"ctxt": 0
},
"stmts": [
{
"type": "ReturnStatement",
"span": {
"start": 133,
"end": 142,
"ctxt": 0
},
"argument": {
"type": "NumericLiteral",
"span": {
"start": 140,
"end": 141,
"ctxt": 0
},
"value": 5.0
}
}
]
},
"async": false,
"generator": false,
"typeParameters": null,
"returnType": {
"type": "TsTypeAnnotation",
"span": {
"start": 57,
"end": 123,
"ctxt": 0
},
"typeAnnotation": {
"type": "TsUnionType",
"span": {
"start": 63,
"end": 123,
"ctxt": 0
},
"types": [
{
"type": "TsKeywordType",
"span": {
"start": 65,
"end": 71,
"ctxt": 0
},
"kind": "string"
},
{
"type": "TsKeywordType",
"span": {
"start": 78,
"end": 84,
"ctxt": 0
},
"kind": "number"
},
{
"type": "TsKeywordType",
"span": {
"start": 91,
"end": 97,
"ctxt": 0
},
"kind": "number"
},
{
"type": "TsKeywordType",
"span": {
"start": 104,
"end": 110,
"ctxt": 0
},
"kind": "number"
},
{
"type": "TsKeywordType",
"span": {
"start": 117,
"end": 123,
"ctxt": 0
},
"kind": "string"
}
]
}
}
},
"definite": false
}
]
}
],
"interpreter": null
}

View File

@ -0,0 +1,5 @@
const testing = ():
string =>
{
return "";
};

View File

@ -0,0 +1,100 @@
{
"type": "Module",
"span": {
"start": 0,
"end": 53,
"ctxt": 0
},
"body": [
{
"type": "VariableDeclaration",
"span": {
"start": 0,
"end": 53,
"ctxt": 0
},
"kind": "const",
"declare": false,
"declarations": [
{
"type": "VariableDeclarator",
"span": {
"start": 6,
"end": 52,
"ctxt": 0
},
"id": {
"type": "Identifier",
"span": {
"start": 6,
"end": 13,
"ctxt": 0
},
"value": "testing",
"typeAnnotation": null,
"optional": false
},
"init": {
"type": "ArrowFunctionExpression",
"span": {
"start": 16,
"end": 52,
"ctxt": 0
},
"params": [],
"body": {
"type": "BlockStatement",
"span": {
"start": 34,
"end": 52,
"ctxt": 0
},
"stmts": [
{
"type": "ReturnStatement",
"span": {
"start": 40,
"end": 50,
"ctxt": 0
},
"argument": {
"type": "StringLiteral",
"span": {
"start": 47,
"end": 49,
"ctxt": 0
},
"value": "",
"hasEscape": false
}
}
]
},
"async": false,
"generator": false,
"typeParameters": null,
"returnType": {
"type": "TsTypeAnnotation",
"span": {
"start": 18,
"end": 30,
"ctxt": 0
},
"typeAnnotation": {
"type": "TsKeywordType",
"span": {
"start": 24,
"end": 30,
"ctxt": 0
},
"kind": "string"
}
}
},
"definite": false
}
]
}
],
"interpreter": null
}