From b4ca4381e37e37c5fba4fc5f5dfcfa7509b1c2f1 Mon Sep 17 00:00:00 2001 From: songqi Date: Fri, 17 Feb 2023 11:46:28 +0800 Subject: [PATCH] Fix Parser of Ternary Operator Issue: I6FSDH Tests: parser/compiler/tsc/test262 Signed-off-by: songqi Change-Id: I7011d9e5a271819261eb05a11a36550dd721dfd3 --- es2panda/parser/expressionParser.cpp | 5 + es2panda/parser/parserImpl.cpp | 11 +- es2panda/parser/parserImpl.h | 2 +- .../test-ts-ternary-operator-1-expected.txt | 5 + .../controlFlow/test-ts-ternary-operator-1.ts | 31 + .../test-ts-ternary-operator-2-expected.txt | 1 + .../controlFlow/test-ts-ternary-operator-2.ts | 22 + .../test-ts-ternary-operator-3-expected.txt | 1 + .../controlFlow/test-ts-ternary-operator-3.ts | 22 + .../test-ts-ternary-operator-1-expected.txt | 319 ++++ .../parser/ts/test-ts-ternary-operator-1.ts | 19 + .../test-ts-ternary-operator-2-expected.txt | 1389 +++++++++++++++++ .../parser/ts/test-ts-ternary-operator-2.ts | 23 + 13 files changed, 1845 insertions(+), 5 deletions(-) create mode 100644 es2panda/test/compiler/ts/cases/conformance/controlFlow/test-ts-ternary-operator-1-expected.txt create mode 100644 es2panda/test/compiler/ts/cases/conformance/controlFlow/test-ts-ternary-operator-1.ts create mode 100644 es2panda/test/compiler/ts/cases/conformance/controlFlow/test-ts-ternary-operator-2-expected.txt create mode 100644 es2panda/test/compiler/ts/cases/conformance/controlFlow/test-ts-ternary-operator-2.ts create mode 100644 es2panda/test/compiler/ts/cases/conformance/controlFlow/test-ts-ternary-operator-3-expected.txt create mode 100644 es2panda/test/compiler/ts/cases/conformance/controlFlow/test-ts-ternary-operator-3.ts create mode 100644 es2panda/test/parser/ts/test-ts-ternary-operator-1-expected.txt create mode 100644 es2panda/test/parser/ts/test-ts-ternary-operator-1.ts create mode 100644 es2panda/test/parser/ts/test-ts-ternary-operator-2-expected.txt create mode 100644 es2panda/test/parser/ts/test-ts-ternary-operator-2.ts diff --git a/es2panda/parser/expressionParser.cpp b/es2panda/parser/expressionParser.cpp index 63a8035399e..1e2bb8c8d05 100644 --- a/es2panda/parser/expressionParser.cpp +++ b/es2panda/parser/expressionParser.cpp @@ -624,6 +624,11 @@ ir::Expression *ParserImpl::ParseCoverParenthesizedExpressionAndArrowParameterLi options |= TypeAnnotationParsingOptions::CAN_BE_TS_TYPE_PREDICATE; returnTypeAnnotation = ParseTsTypeAnnotation(&options); options &= ~TypeAnnotationParsingOptions::CAN_BE_TS_TYPE_PREDICATE; + + if (returnTypeAnnotation == nullptr) { + lexer_->Rewind(startPos); + return expr; + } } if (lexer_->GetToken().Type() != lexer::TokenType::PUNCTUATOR_ARROW) { diff --git a/es2panda/parser/parserImpl.cpp b/es2panda/parser/parserImpl.cpp index 32c18e95c4c..1ad750ab2e5 100644 --- a/es2panda/parser/parserImpl.cpp +++ b/es2panda/parser/parserImpl.cpp @@ -455,7 +455,7 @@ ir::Expression *ParserImpl::ParseTsThisTypeOrTsTypePredicate(ir::Expression *typ return ParseTsThisType(throwError); } -ir::Expression *ParserImpl::ParseTsTemplateLiteralType() +ir::Expression *ParserImpl::ParseTsTemplateLiteralType(bool throwError) { ASSERT(lexer_->GetToken().Type() == lexer::TokenType::PUNCTUATOR_BACK_TICK); lexer::SourcePosition startLoc = lexer_->GetToken().Start(); @@ -494,7 +494,10 @@ ir::Expression *ParserImpl::ParseTsTemplateLiteralType() } if (lexer_->GetToken().Type() != lexer::TokenType::PUNCTUATOR_RIGHT_BRACE) { - ThrowSyntaxError("Unexpected token, expected '}'."); + if (throwError) { + ThrowSyntaxError("Unexpected token, expected '}'."); + } + return nullptr; } references.push_back(reference); @@ -615,7 +618,7 @@ ir::Expression *ParserImpl::ParseTsTypeAnnotationElement(ir::Expression *typeAnn *options & TypeAnnotationParsingOptions::THROW_ERROR); } case lexer::TokenType::PUNCTUATOR_BACK_TICK: { - return ParseTsTemplateLiteralType(); + return ParseTsTemplateLiteralType(*options & TypeAnnotationParsingOptions::THROW_ERROR); } default: { break; @@ -1078,7 +1081,7 @@ ir::Expression *ParserImpl::ParseTsTypeReferenceOrQuery(TypeAnnotationParsingOpt ThrowSyntaxError("Unexpected token."); } - typeParamInst = ParseTsTypeParameterInstantiation(); + typeParamInst = ParseTsTypeParameterInstantiation(options & TypeAnnotationParsingOptions::THROW_ERROR); } if (lexer_->GetToken().Type() == lexer::TokenType::PUNCTUATOR_LEFT_SQUARE_BRACKET && diff --git a/es2panda/parser/parserImpl.h b/es2panda/parser/parserImpl.h index de716a4cd74..ed645531880 100644 --- a/es2panda/parser/parserImpl.h +++ b/es2panda/parser/parserImpl.h @@ -274,7 +274,7 @@ private: bool throwError); ir::Expression *ParseTsThisTypeOrTsTypePredicate(ir::Expression *typeAnnotation, bool canBeTsTypePredicate, bool throwError); - ir::Expression *ParseTsTemplateLiteralType(); + ir::Expression *ParseTsTemplateLiteralType(bool throwError); ir::Expression *ParseTsTypeAnnotationElement(ir::Expression *typeAnnotation, TypeAnnotationParsingOptions *options); ir::ModifierFlags ParseModifiers(); diff --git a/es2panda/test/compiler/ts/cases/conformance/controlFlow/test-ts-ternary-operator-1-expected.txt b/es2panda/test/compiler/ts/cases/conformance/controlFlow/test-ts-ternary-operator-1-expected.txt new file mode 100644 index 00000000000..bb0264d94e1 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/controlFlow/test-ts-ternary-operator-1-expected.txt @@ -0,0 +1,5 @@ +1 +196608 +8 +4 +196610 diff --git a/es2panda/test/compiler/ts/cases/conformance/controlFlow/test-ts-ternary-operator-1.ts b/es2panda/test/compiler/ts/cases/conformance/controlFlow/test-ts-ternary-operator-1.ts new file mode 100644 index 00000000000..fbd61ac3f43 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/controlFlow/test-ts-ternary-operator-1.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +var a = 3, b = 2, c = 1; +const res1 = a < b ? (a < c ? a : c) : (b < c ? b : c); +print(res1); + +const res2 = (a > b) ? ((a << 16)) : ((b << 16)); +print(res2); + +const res3 = (a < b) ? (1) : (b << 2); +print(res3); + +const res4 = (a > b) ? (1 << 2) : (b << 2); +print(res4); + +const res5 = (a > b) ? ((a << 16) | b) : ((b << 16) | a); +print(res5); diff --git a/es2panda/test/compiler/ts/cases/conformance/controlFlow/test-ts-ternary-operator-2-expected.txt b/es2panda/test/compiler/ts/cases/conformance/controlFlow/test-ts-ternary-operator-2-expected.txt new file mode 100644 index 00000000000..abcacf21960 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/controlFlow/test-ts-ternary-operator-2-expected.txt @@ -0,0 +1 @@ +[str_2] diff --git a/es2panda/test/compiler/ts/cases/conformance/controlFlow/test-ts-ternary-operator-2.ts b/es2panda/test/compiler/ts/cases/conformance/controlFlow/test-ts-ternary-operator-2.ts new file mode 100644 index 00000000000..cfa166f1c08 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/controlFlow/test-ts-ternary-operator-2.ts @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +var a = 2, b = 1; +function func(p: string) { + return "str" + p; +} +const res = (a < b) ? ("str_1") : (`[${func("_2")}]`); +print(res); diff --git a/es2panda/test/compiler/ts/cases/conformance/controlFlow/test-ts-ternary-operator-3-expected.txt b/es2panda/test/compiler/ts/cases/conformance/controlFlow/test-ts-ternary-operator-3-expected.txt new file mode 100644 index 00000000000..d00491fd7e5 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/controlFlow/test-ts-ternary-operator-3-expected.txt @@ -0,0 +1 @@ +1 diff --git a/es2panda/test/compiler/ts/cases/conformance/controlFlow/test-ts-ternary-operator-3.ts b/es2panda/test/compiler/ts/cases/conformance/controlFlow/test-ts-ternary-operator-3.ts new file mode 100644 index 00000000000..e30922179ca --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/controlFlow/test-ts-ternary-operator-3.ts @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +var a = 1; +var c = 2; +function f(b: number = a < c ? 1 : 2) { + return b; +} +print(f()); diff --git a/es2panda/test/parser/ts/test-ts-ternary-operator-1-expected.txt b/es2panda/test/parser/ts/test-ts-ternary-operator-1-expected.txt new file mode 100644 index 00000000000..4a5532dce96 --- /dev/null +++ b/es2panda/test/parser/ts/test-ts-ternary-operator-1-expected.txt @@ -0,0 +1,319 @@ +{ + "type": "Program", + "statements": [ + { + "type": "FunctionDeclaration", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "getPropAccessor", + "loc": { + "start": { + "line": 17, + "column": 10 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "key", + "loc": { + "start": { + "line": 17, + "column": 26 + }, + "end": { + "line": 17, + "column": 29 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "ConditionalExpression", + "test": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "test", + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 16 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "key", + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 20 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 21 + } + } + }, + "consequent": { + "type": "TemplateLiteral", + "expressions": [ + { + "type": "Identifier", + "name": "key", + "loc": { + "start": { + "line": 18, + "column": 29 + }, + "end": { + "line": 18, + "column": 32 + } + } + } + ], + "quasis": [ + { + "type": "TemplateElement", + "value": { + "raw": ".", + "cooked": "." + }, + "loc": { + "start": { + "line": 18, + "column": 26 + }, + "end": { + "line": 18, + "column": 27 + } + } + }, + { + "type": "TemplateElement", + "value": { + "raw": "", + "cooked": "" + }, + "loc": { + "start": { + "line": 18, + "column": 33 + }, + "end": { + "line": 18, + "column": 33 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 24 + }, + "end": { + "line": 18, + "column": 35 + } + } + }, + "alternate": { + "type": "TemplateLiteral", + "expressions": [ + { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "escapeForJS", + "loc": { + "start": { + "line": 18, + "column": 43 + }, + "end": { + "line": 18, + "column": 54 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "key", + "loc": { + "start": { + "line": 18, + "column": 55 + }, + "end": { + "line": 18, + "column": 58 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 18, + "column": 43 + }, + "end": { + "line": 18, + "column": 59 + } + } + } + ], + "quasis": [ + { + "type": "TemplateElement", + "value": { + "raw": "[", + "cooked": "[" + }, + "loc": { + "start": { + "line": 18, + "column": 40 + }, + "end": { + "line": 18, + "column": 41 + } + } + }, + { + "type": "TemplateElement", + "value": { + "raw": "]", + "cooked": "]" + }, + "loc": { + "start": { + "line": 18, + "column": 60 + }, + "end": { + "line": 18, + "column": 61 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 38 + }, + "end": { + "line": 18, + "column": 63 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 63 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 64 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 31 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 19, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 20, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/ts/test-ts-ternary-operator-1.ts b/es2panda/test/parser/ts/test-ts-ternary-operator-1.ts new file mode 100644 index 00000000000..8c3b493b05c --- /dev/null +++ b/es2panda/test/parser/ts/test-ts-ternary-operator-1.ts @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +function getPropAccessor(key) { + return test(key) ? (`.${key}`) : (`[${escapeForJS(key)}]`); +} diff --git a/es2panda/test/parser/ts/test-ts-ternary-operator-2-expected.txt b/es2panda/test/parser/ts/test-ts-ternary-operator-2-expected.txt new file mode 100644 index 00000000000..b2e9f62cfe5 --- /dev/null +++ b/es2panda/test/parser/ts/test-ts-ternary-operator-2-expected.txt @@ -0,0 +1,1389 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "ConditionalExpression", + "test": { + "type": "BinaryExpression", + "operator": "<", + "left": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 2 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 6 + } + } + }, + "consequent": { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 17, + "column": 32 + }, + "end": { + "line": 17, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 25 + }, + "end": { + "line": 17, + "column": 34 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 23 + }, + "end": { + "line": 17, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 36 + } + } + }, + "alternate": { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSStringKeyword", + "loc": { + "start": { + "line": 17, + "column": 43 + }, + "end": { + "line": 17, + "column": 49 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "StringLiteral", + "value": "1", + "loc": { + "start": { + "line": 17, + "column": 62 + }, + "end": { + "line": 17, + "column": 65 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 55 + }, + "end": { + "line": 17, + "column": 66 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 53 + }, + "end": { + "line": 17, + "column": 68 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 39 + }, + "end": { + "line": 17, + "column": 68 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 39 + }, + "end": { + "line": 17, + "column": 68 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 68 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 68 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "ConditionalExpression", + "test": { + "type": "BinaryExpression", + "operator": "<", + "left": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + "consequent": { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 18, + "column": 32 + }, + "end": { + "line": 18, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 25 + }, + "end": { + "line": 18, + "column": 34 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 23 + }, + "end": { + "line": 18, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 36 + } + } + }, + "alternate": { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "p1", + "typeAnnotation": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 18, + "column": 44 + }, + "end": { + "line": 18, + "column": 50 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 40 + }, + "end": { + "line": 18, + "column": 42 + } + } + }, + { + "type": "Identifier", + "name": "p2", + "typeAnnotation": { + "type": "TSStringKeyword", + "loc": { + "start": { + "line": 18, + "column": 56 + }, + "end": { + "line": 18, + "column": 62 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 52 + }, + "end": { + "line": 18, + "column": 54 + } + } + } + ], + "returnType": { + "type": "TSStringKeyword", + "loc": { + "start": { + "line": 18, + "column": 65 + }, + "end": { + "line": 18, + "column": 71 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "StringLiteral", + "value": "1", + "loc": { + "start": { + "line": 18, + "column": 84 + }, + "end": { + "line": 18, + "column": 87 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 77 + }, + "end": { + "line": 18, + "column": 88 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 75 + }, + "end": { + "line": 18, + "column": 90 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 39 + }, + "end": { + "line": 18, + "column": 90 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 39 + }, + "end": { + "line": 18, + "column": 90 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 90 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 90 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "ConditionalExpression", + "test": { + "type": "BinaryExpression", + "operator": "<", + "left": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "consequent": { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "p", + "typeAnnotation": { + "type": "TSStringKeyword", + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 20 + } + } + } + ], + "returnType": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 19, + "column": 22 + }, + "end": { + "line": 19, + "column": 28 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 19, + "column": 41 + }, + "end": { + "line": 19, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 34 + }, + "end": { + "line": 19, + "column": 43 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 32 + }, + "end": { + "line": 19, + "column": 45 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 45 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 45 + } + } + }, + "alternate": { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSStringKeyword", + "loc": { + "start": { + "line": 19, + "column": 52 + }, + "end": { + "line": 19, + "column": 58 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "StringLiteral", + "value": "1", + "loc": { + "start": { + "line": 19, + "column": 71 + }, + "end": { + "line": 19, + "column": 74 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 64 + }, + "end": { + "line": 19, + "column": 75 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 62 + }, + "end": { + "line": 19, + "column": 77 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 48 + }, + "end": { + "line": 19, + "column": 77 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 48 + }, + "end": { + "line": 19, + "column": 77 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 19, + "column": 77 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 19, + "column": 77 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 6 + } + } + }, + "init": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 21, + "column": 9 + }, + "end": { + "line": 21, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 10 + } + } + }, + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "b", + "loc": { + "start": { + "line": 21, + "column": 12 + }, + "end": { + "line": 21, + "column": 13 + } + } + }, + "init": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 21, + "column": 16 + }, + "end": { + "line": 21, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 12 + }, + "end": { + "line": 21, + "column": 17 + } + } + }, + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 21, + "column": 19 + }, + "end": { + "line": 21, + "column": 20 + } + } + }, + "init": { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 21, + "column": 23 + }, + "end": { + "line": 21, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 19 + }, + "end": { + "line": 21, + "column": 24 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 21, + "column": 25 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "ConditionalExpression", + "test": { + "type": "BinaryExpression", + "operator": "<", + "left": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 22, + "column": 1 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 1 + }, + "end": { + "line": 22, + "column": 6 + } + } + }, + "consequent": { + "type": "SequenceExpression", + "expressions": [ + { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 22, + "column": 10 + }, + "end": { + "line": 22, + "column": 11 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 22, + "column": 14 + }, + "end": { + "line": 22, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 10 + }, + "end": { + "line": 22, + "column": 15 + } + } + }, + { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "b", + "loc": { + "start": { + "line": 22, + "column": 17 + }, + "end": { + "line": 22, + "column": 18 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 22, + "column": 21 + }, + "end": { + "line": 22, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 17 + }, + "end": { + "line": 22, + "column": 22 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 22, + "column": 23 + } + } + }, + "alternate": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 22, + "column": 26 + }, + "end": { + "line": 22, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 1 + }, + "end": { + "line": 22, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 1 + }, + "end": { + "line": 22, + "column": 27 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "ConditionalExpression", + "test": { + "type": "BinaryExpression", + "operator": "<", + "left": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 23, + "column": 1 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 1 + }, + "end": { + "line": 23, + "column": 6 + } + } + }, + "consequent": { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "AssignmentPattern", + "left": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 23, + "column": 10 + }, + "end": { + "line": 23, + "column": 11 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 23, + "column": 14 + }, + "end": { + "line": 23, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 10 + }, + "end": { + "line": 23, + "column": 15 + } + } + }, + { + "type": "AssignmentPattern", + "left": { + "type": "Identifier", + "name": "b", + "loc": { + "start": { + "line": 23, + "column": 17 + }, + "end": { + "line": 23, + "column": 18 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 23, + "column": 21 + }, + "end": { + "line": 23, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 17 + }, + "end": { + "line": 23, + "column": 22 + } + } + } + ], + "returnType": { + "type": "TSAnyKeyword", + "loc": { + "start": { + "line": 23, + "column": 25 + }, + "end": { + "line": 23, + "column": 28 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "Identifier", + "name": "b", + "loc": { + "start": { + "line": 23, + "column": 41 + }, + "end": { + "line": 23, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 34 + }, + "end": { + "line": 23, + "column": 42 + } + } + } + ], + "loc": { + "start": { + "line": 23, + "column": 32 + }, + "end": { + "line": 23, + "column": 44 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 44 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 44 + } + } + }, + "alternate": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 23, + "column": 47 + }, + "end": { + "line": 23, + "column": 48 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 1 + }, + "end": { + "line": 23, + "column": 48 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 1 + }, + "end": { + "line": 23, + "column": 48 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 24, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/ts/test-ts-ternary-operator-2.ts b/es2panda/test/parser/ts/test-ts-ternary-operator-2.ts new file mode 100644 index 00000000000..883818e4f33 --- /dev/null +++ b/es2panda/test/parser/ts/test-ts-ternary-operator-2.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +1 < 2 ? (): number => { return 1; } : (): string => { return "1"; } +1 < 2 ? (): number => { return 1; } : (p1: number, p2: string): string => { return "1"; } +1 < 2 ? (p: string): number => { return 1; } : (): string => { return "1"; } + +var a = 1, b = 2, c = 3; +1 < 2 ? (a = 1, b = 2) : c +1 < 2 ? (a = 1, b = 2): any => { return b } : c -- Gitee