diff --git a/crates/swc/tests/fixture/issues-8xxx/8735/input/.swcrc b/crates/swc/tests/fixture/issues-8xxx/8735/input/.swcrc new file mode 100644 index 000000000000..65f433d6c2c3 --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8735/input/.swcrc @@ -0,0 +1,19 @@ +{ + "jsc": { + "parser": { + "syntax": "typescript", + "tsx": true + }, + "target": "es5", + "loose": false, + "minify": { + "compress": false, + "mangle": false + } + }, + "module": { + "type": "es6" + }, + "minify": false, + "isModule": true +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-8xxx/8735/input/input.tsx b/crates/swc/tests/fixture/issues-8xxx/8735/input/input.tsx new file mode 100644 index 000000000000..a68c40716e6f --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8735/input/input.tsx @@ -0,0 +1,5 @@ +function getFirstElement(array: T[]): T | null { + return array.length > 0 ? array[0] : null; +} + +let myFunction = getFirstElement as any; diff --git a/crates/swc/tests/fixture/issues-8xxx/8735/output/input.tsx b/crates/swc/tests/fixture/issues-8xxx/8735/output/input.tsx new file mode 100644 index 000000000000..4b12ddc03e08 --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8735/output/input.tsx @@ -0,0 +1,4 @@ +function getFirstElement(array) { + return array.length > 0 ? array[0] : null; +} +var myFunction = getFirstElement; diff --git a/crates/swc_ecma_parser/src/parser/expr.rs b/crates/swc_ecma_parser/src/parser/expr.rs index cbbffbef53a0..705b1e23f4e2 100644 --- a/crates/swc_ecma_parser/src/parser/expr.rs +++ b/crates/swc_ecma_parser/src/parser/expr.rs @@ -1226,7 +1226,7 @@ impl Parser { ) .map(|expr| (Box::new(Expr::TaggedTpl(expr)), true)) .map(Some) - } else if is!(p, '=') { + } else if is_one_of!(p, '=', "as") { Ok(Some(( Box::new(Expr::TsInstantiation(TsInstantiation { span: span!(p, start), @@ -1236,7 +1236,7 @@ impl Parser { }, type_args, })), - true, + false, ))) } else if no_call { unexpected!(p, "`") diff --git a/crates/swc_ecma_parser/tests/typescript/issue-8735/1/input.ts b/crates/swc_ecma_parser/tests/typescript/issue-8735/1/input.ts new file mode 100644 index 000000000000..a68c40716e6f --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript/issue-8735/1/input.ts @@ -0,0 +1,5 @@ +function getFirstElement(array: T[]): T | null { + return array.length > 0 ? array[0] : null; +} + +let myFunction = getFirstElement as any; diff --git a/crates/swc_ecma_parser/tests/typescript/issue-8735/1/input.ts.json b/crates/swc_ecma_parser/tests/typescript/issue-8735/1/input.ts.json new file mode 100644 index 000000000000..44d43e2a9f2f --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript/issue-8735/1/input.ts.json @@ -0,0 +1,371 @@ +{ + "type": "Script", + "span": { + "start": 1, + "end": 151, + "ctxt": 0 + }, + "body": [ + { + "type": "FunctionDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 10, + "end": 25, + "ctxt": 0 + }, + "value": "getFirstElement", + "optional": false + }, + "declare": false, + "params": [ + { + "type": "Parameter", + "span": { + "start": 29, + "end": 39, + "ctxt": 0 + }, + "decorators": [], + "pat": { + "type": "Identifier", + "span": { + "start": 29, + "end": 39, + "ctxt": 0 + }, + "value": "array", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 34, + "end": 39, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsArrayType", + "span": { + "start": 36, + "end": 39, + "ctxt": 0 + }, + "elemType": { + "type": "TsTypeReference", + "span": { + "start": 36, + "end": 37, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 36, + "end": 37, + "ctxt": 0 + }, + "value": "T", + "optional": false + }, + "typeParams": null + } + } + } + } + } + ], + "decorators": [], + "span": { + "start": 1, + "end": 101, + "ctxt": 0 + }, + "body": { + "type": "BlockStatement", + "span": { + "start": 51, + "end": 101, + "ctxt": 0 + }, + "stmts": [ + { + "type": "ReturnStatement", + "span": { + "start": 57, + "end": 99, + "ctxt": 0 + }, + "argument": { + "type": "ConditionalExpression", + "span": { + "start": 64, + "end": 98, + "ctxt": 0 + }, + "test": { + "type": "BinaryExpression", + "span": { + "start": 64, + "end": 80, + "ctxt": 0 + }, + "operator": ">", + "left": { + "type": "MemberExpression", + "span": { + "start": 64, + "end": 76, + "ctxt": 0 + }, + "object": { + "type": "Identifier", + "span": { + "start": 64, + "end": 69, + "ctxt": 0 + }, + "value": "array", + "optional": false + }, + "property": { + "type": "Identifier", + "span": { + "start": 70, + "end": 76, + "ctxt": 0 + }, + "value": "length", + "optional": false + } + }, + "right": { + "type": "NumericLiteral", + "span": { + "start": 79, + "end": 80, + "ctxt": 0 + }, + "value": 0.0, + "raw": "0" + } + }, + "consequent": { + "type": "MemberExpression", + "span": { + "start": 83, + "end": 91, + "ctxt": 0 + }, + "object": { + "type": "Identifier", + "span": { + "start": 83, + "end": 88, + "ctxt": 0 + }, + "value": "array", + "optional": false + }, + "property": { + "type": "Computed", + "span": { + "start": 88, + "end": 91, + "ctxt": 0 + }, + "expression": { + "type": "NumericLiteral", + "span": { + "start": 89, + "end": 90, + "ctxt": 0 + }, + "value": 0.0, + "raw": "0" + } + } + }, + "alternate": { + "type": "NullLiteral", + "span": { + "start": 94, + "end": 98, + "ctxt": 0 + } + } + } + } + ] + }, + "generator": false, + "async": false, + "typeParameters": { + "type": "TsTypeParameterDeclaration", + "span": { + "start": 25, + "end": 28, + "ctxt": 0 + }, + "parameters": [ + { + "type": "TsTypeParameter", + "span": { + "start": 26, + "end": 27, + "ctxt": 0 + }, + "name": { + "type": "Identifier", + "span": { + "start": 26, + "end": 27, + "ctxt": 0 + }, + "value": "T", + "optional": false + }, + "in": false, + "out": false, + "const": false, + "constraint": null, + "default": null + } + ] + }, + "returnType": { + "type": "TsTypeAnnotation", + "span": { + "start": 40, + "end": 50, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsUnionType", + "span": { + "start": 42, + "end": 50, + "ctxt": 0 + }, + "types": [ + { + "type": "TsTypeReference", + "span": { + "start": 42, + "end": 43, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 42, + "end": 43, + "ctxt": 0 + }, + "value": "T", + "optional": false + }, + "typeParams": null + }, + { + "type": "TsKeywordType", + "span": { + "start": 46, + "end": 50, + "ctxt": 0 + }, + "kind": "null" + } + ] + } + } + }, + { + "type": "VariableDeclaration", + "span": { + "start": 103, + "end": 151, + "ctxt": 0 + }, + "kind": "let", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 107, + "end": 150, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 107, + "end": 117, + "ctxt": 0 + }, + "value": "myFunction", + "optional": false, + "typeAnnotation": null + }, + "init": { + "type": "TsAsExpression", + "span": { + "start": 120, + "end": 150, + "ctxt": 0 + }, + "expression": { + "type": "TsInstantiation", + "span": { + "start": 120, + "end": 143, + "ctxt": 0 + }, + "expression": { + "type": "Identifier", + "span": { + "start": 120, + "end": 135, + "ctxt": 0 + }, + "value": "getFirstElement", + "optional": false + }, + "typeArguments": { + "type": "TsTypeParameterInstantiation", + "span": { + "start": 135, + "end": 143, + "ctxt": 0 + }, + "params": [ + { + "type": "TsKeywordType", + "span": { + "start": 136, + "end": 142, + "ctxt": 0 + }, + "kind": "number" + } + ] + } + }, + "typeAnnotation": { + "type": "TsKeywordType", + "span": { + "start": 147, + "end": 150, + "ctxt": 0 + }, + "kind": "any" + } + }, + "definite": false + } + ] + } + ], + "interpreter": null +} diff --git a/crates/swc_ecma_parser/tests/typescript/issue-8735/2/input.tsx b/crates/swc_ecma_parser/tests/typescript/issue-8735/2/input.tsx new file mode 100644 index 000000000000..a68c40716e6f --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript/issue-8735/2/input.tsx @@ -0,0 +1,5 @@ +function getFirstElement(array: T[]): T | null { + return array.length > 0 ? array[0] : null; +} + +let myFunction = getFirstElement as any; diff --git a/crates/swc_ecma_parser/tests/typescript/issue-8735/2/input.tsx.json b/crates/swc_ecma_parser/tests/typescript/issue-8735/2/input.tsx.json new file mode 100644 index 000000000000..44d43e2a9f2f --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript/issue-8735/2/input.tsx.json @@ -0,0 +1,371 @@ +{ + "type": "Script", + "span": { + "start": 1, + "end": 151, + "ctxt": 0 + }, + "body": [ + { + "type": "FunctionDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 10, + "end": 25, + "ctxt": 0 + }, + "value": "getFirstElement", + "optional": false + }, + "declare": false, + "params": [ + { + "type": "Parameter", + "span": { + "start": 29, + "end": 39, + "ctxt": 0 + }, + "decorators": [], + "pat": { + "type": "Identifier", + "span": { + "start": 29, + "end": 39, + "ctxt": 0 + }, + "value": "array", + "optional": false, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 34, + "end": 39, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsArrayType", + "span": { + "start": 36, + "end": 39, + "ctxt": 0 + }, + "elemType": { + "type": "TsTypeReference", + "span": { + "start": 36, + "end": 37, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 36, + "end": 37, + "ctxt": 0 + }, + "value": "T", + "optional": false + }, + "typeParams": null + } + } + } + } + } + ], + "decorators": [], + "span": { + "start": 1, + "end": 101, + "ctxt": 0 + }, + "body": { + "type": "BlockStatement", + "span": { + "start": 51, + "end": 101, + "ctxt": 0 + }, + "stmts": [ + { + "type": "ReturnStatement", + "span": { + "start": 57, + "end": 99, + "ctxt": 0 + }, + "argument": { + "type": "ConditionalExpression", + "span": { + "start": 64, + "end": 98, + "ctxt": 0 + }, + "test": { + "type": "BinaryExpression", + "span": { + "start": 64, + "end": 80, + "ctxt": 0 + }, + "operator": ">", + "left": { + "type": "MemberExpression", + "span": { + "start": 64, + "end": 76, + "ctxt": 0 + }, + "object": { + "type": "Identifier", + "span": { + "start": 64, + "end": 69, + "ctxt": 0 + }, + "value": "array", + "optional": false + }, + "property": { + "type": "Identifier", + "span": { + "start": 70, + "end": 76, + "ctxt": 0 + }, + "value": "length", + "optional": false + } + }, + "right": { + "type": "NumericLiteral", + "span": { + "start": 79, + "end": 80, + "ctxt": 0 + }, + "value": 0.0, + "raw": "0" + } + }, + "consequent": { + "type": "MemberExpression", + "span": { + "start": 83, + "end": 91, + "ctxt": 0 + }, + "object": { + "type": "Identifier", + "span": { + "start": 83, + "end": 88, + "ctxt": 0 + }, + "value": "array", + "optional": false + }, + "property": { + "type": "Computed", + "span": { + "start": 88, + "end": 91, + "ctxt": 0 + }, + "expression": { + "type": "NumericLiteral", + "span": { + "start": 89, + "end": 90, + "ctxt": 0 + }, + "value": 0.0, + "raw": "0" + } + } + }, + "alternate": { + "type": "NullLiteral", + "span": { + "start": 94, + "end": 98, + "ctxt": 0 + } + } + } + } + ] + }, + "generator": false, + "async": false, + "typeParameters": { + "type": "TsTypeParameterDeclaration", + "span": { + "start": 25, + "end": 28, + "ctxt": 0 + }, + "parameters": [ + { + "type": "TsTypeParameter", + "span": { + "start": 26, + "end": 27, + "ctxt": 0 + }, + "name": { + "type": "Identifier", + "span": { + "start": 26, + "end": 27, + "ctxt": 0 + }, + "value": "T", + "optional": false + }, + "in": false, + "out": false, + "const": false, + "constraint": null, + "default": null + } + ] + }, + "returnType": { + "type": "TsTypeAnnotation", + "span": { + "start": 40, + "end": 50, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsUnionType", + "span": { + "start": 42, + "end": 50, + "ctxt": 0 + }, + "types": [ + { + "type": "TsTypeReference", + "span": { + "start": 42, + "end": 43, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 42, + "end": 43, + "ctxt": 0 + }, + "value": "T", + "optional": false + }, + "typeParams": null + }, + { + "type": "TsKeywordType", + "span": { + "start": 46, + "end": 50, + "ctxt": 0 + }, + "kind": "null" + } + ] + } + } + }, + { + "type": "VariableDeclaration", + "span": { + "start": 103, + "end": 151, + "ctxt": 0 + }, + "kind": "let", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 107, + "end": 150, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 107, + "end": 117, + "ctxt": 0 + }, + "value": "myFunction", + "optional": false, + "typeAnnotation": null + }, + "init": { + "type": "TsAsExpression", + "span": { + "start": 120, + "end": 150, + "ctxt": 0 + }, + "expression": { + "type": "TsInstantiation", + "span": { + "start": 120, + "end": 143, + "ctxt": 0 + }, + "expression": { + "type": "Identifier", + "span": { + "start": 120, + "end": 135, + "ctxt": 0 + }, + "value": "getFirstElement", + "optional": false + }, + "typeArguments": { + "type": "TsTypeParameterInstantiation", + "span": { + "start": 135, + "end": 143, + "ctxt": 0 + }, + "params": [ + { + "type": "TsKeywordType", + "span": { + "start": 136, + "end": 142, + "ctxt": 0 + }, + "kind": "number" + } + ] + } + }, + "typeAnnotation": { + "type": "TsKeywordType", + "span": { + "start": 147, + "end": 150, + "ctxt": 0 + }, + "kind": "any" + } + }, + "definite": false + } + ] + } + ], + "interpreter": null +}