diff --git a/es2panda/lexer/token/token.cpp b/es2panda/lexer/token/token.cpp index 61847be1433226cb9e9e002630c3fe3ec9b55e9a..9bf57990021fe0f9374bec3fa9d5517b452928aa 100644 --- a/es2panda/lexer/token/token.cpp +++ b/es2panda/lexer/token/token.cpp @@ -15,6 +15,8 @@ #include "token.h" +#include "letters.h" + namespace panda::es2panda::lexer { bool Token::IsAccessability() const @@ -132,9 +134,10 @@ bool Token::IsPunctuatorToken(TokenType type) return (type >= TokenType::PUNCTUATOR_NULLISH_COALESCING && type <= TokenType::PUNCTUATOR_ARROW); } -bool Token::IsTsParamToken(TokenType type) +bool Token::IsTsParamToken(TokenType type, char32_t nextChar) { - return (type == TokenType::PUNCTUATOR_COLON || type == TokenType::PUNCTUATOR_QUESTION_MARK); + return (type == TokenType::PUNCTUATOR_COLON || (type == TokenType::PUNCTUATOR_QUESTION_MARK && + (nextChar == LEX_CHAR_COMMA || nextChar == LEX_CHAR_COLON || nextChar == LEX_CHAR_RIGHT_PAREN))); } const char *TokenToString(TokenType type) diff --git a/es2panda/lexer/token/token.h b/es2panda/lexer/token/token.h index 26edb60d38c145034069ad58cb977c668d3cdd0b..e7824396178c2bfe513cef1670fac42795ae71ab 100644 --- a/es2panda/lexer/token/token.h +++ b/es2panda/lexer/token/token.h @@ -122,7 +122,7 @@ public: static bool IsBinaryLvalueToken(TokenType type); static bool IsUpdateToken(TokenType type); static bool IsPunctuatorToken(TokenType type); - static bool IsTsParamToken(TokenType type); + static bool IsTsParamToken(TokenType type, char32_t nextChar); private: friend class KeywordsUtil; diff --git a/es2panda/parser/expressionParser.cpp b/es2panda/parser/expressionParser.cpp index cb6d065fdfac8cde6ee7ae8a66c60692ad52e3bf..d3aa38c134740febeb3491f7c4de07199e3786bf 100644 --- a/es2panda/parser/expressionParser.cpp +++ b/es2panda/parser/expressionParser.cpp @@ -256,7 +256,7 @@ ir::Expression *ParserImpl::ParseArrayExpression(ExpressionParseFlags flags) } if (Extension() == ScriptExtension::TS && (flags & ExpressionParseFlags::ALLOW_TS_PARAM_TOKEN) && - lexer::Token::IsTsParamToken(lexer_->GetToken().Type())) { + lexer::Token::IsTsParamToken(lexer_->GetToken().Type(), lexer_->Lookahead())) { context_.Status() |= ParserStatus::FUNCTION_PARAM; ParsePotentialTsFunctionParameter(ExpressionParseFlags::NO_OPTS, arrayExpressionNode); } @@ -551,7 +551,8 @@ ir::Expression *ParserImpl::ParseCoverParenthesizedExpressionAndArrowParameterLi restElement->SetGrouped(); restElement->SetStart(start); - if (Extension() == ScriptExtension::TS && lexer::Token::IsTsParamToken(lexer_->GetToken().Type())) { + if (Extension() == ScriptExtension::TS && + lexer::Token::IsTsParamToken(lexer_->GetToken().Type(), lexer_->Lookahead())) { ParsePotentialTsFunctionParameter(ExpressionParseFlags::IN_REST, restElement); } @@ -976,7 +977,7 @@ ir::Expression *ParserImpl::ParsePrimaryExpression(ExpressionParseFlags flags) lexer_->NextToken(); if (Extension() == ScriptExtension::TS && (flags & ExpressionParseFlags::ALLOW_TS_PARAM_TOKEN) && - lexer::Token::IsTsParamToken(lexer_->GetToken().Type())) { + lexer::Token::IsTsParamToken(lexer_->GetToken().Type(), lexer_->Lookahead())) { context_.Status() |= ParserStatus::FUNCTION_PARAM; ParsePotentialTsFunctionParameter(ExpressionParseFlags::NO_OPTS, identNode); } @@ -2152,7 +2153,7 @@ ir::ObjectExpression *ParserImpl::ParseObjectExpression(ExpressionParseFlags fla } if (Extension() == ScriptExtension::TS && (flags & ExpressionParseFlags::ALLOW_TS_PARAM_TOKEN) && - lexer::Token::IsTsParamToken(lexer_->GetToken().Type())) { + lexer::Token::IsTsParamToken(lexer_->GetToken().Type(), lexer_->Lookahead())) { context_.Status() |= ParserStatus::FUNCTION_PARAM; ParsePotentialTsFunctionParameter(ExpressionParseFlags::NO_OPTS, objectExpression); } diff --git a/es2panda/test/parser/ts/test-arrow-function3-expected.txt b/es2panda/test/parser/ts/test-arrow-function3-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..6acf39acce7278bf042b2c8dbfb6ef55751e74e6 --- /dev/null +++ b/es2panda/test/parser/ts/test-arrow-function3-expected.txt @@ -0,0 +1,292 @@ +{ + "type": "Program", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "TSBooleanKeyword", + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 16 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 6 + } + } + }, + "init": { + "type": "BooleanLiteral", + "value": true, + "loc": { + "start": { + "line": 17, + "column": 19 + }, + "end": { + "line": 17, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 23 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 24 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "b", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + "init": { + "type": "ConditionalExpression", + "test": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 11 + } + } + }, + "consequent": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + "alternate": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 20 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "c", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "init": { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "a", + "optional": true, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 13 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 19 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 19, + "column": 19 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 19, + "column": 19 + } + } +} diff --git a/es2panda/test/parser/ts/test-arrow-function3.ts b/es2panda/test/parser/ts/test-arrow-function3.ts new file mode 100644 index 0000000000000000000000000000000000000000..571ebe40a12063448e2881c1c3d5e02874ee27ef --- /dev/null +++ b/es2panda/test/parser/ts/test-arrow-function3.ts @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2022 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 : boolean = true; +var b = (a ? 1 : 2) +var c = (a?) => {} \ No newline at end of file diff --git a/es2panda/test/test_tsc_ignore_list.txt b/es2panda/test/test_tsc_ignore_list.txt index 84265f688bd851066a72c919451cfd99845b85d2..8e94cefa388115261ddd1677620b732ec4488e97 100644 --- a/es2panda/test/test_tsc_ignore_list.txt +++ b/es2panda/test/test_tsc_ignore_list.txt @@ -6,7 +6,6 @@ es2panda/test/TypeScript/tests/cases/compiler/collisionArgumentsInterfaceMembers es2panda/test/TypeScript/tests/cases/compiler/computedPropertiesTransformedInOtherwiseNonTSClasses.ts es2panda/test/TypeScript/tests/cases/compiler/constructorOverloads5.ts es2panda/test/TypeScript/tests/cases/compiler/continueTarget3.ts -es2panda/test/TypeScript/tests/cases/compiler/controlFlowCaching.ts es2panda/test/TypeScript/tests/cases/compiler/declarationEmitDestructuringOptionalBindingParametersInOverloads.ts es2panda/test/TypeScript/tests/cases/compiler/discriminantsAndPrimitives.ts es2panda/test/TypeScript/tests/cases/compiler/elidedEmbeddedStatementsReplacedWithSemicolon.ts @@ -86,7 +85,6 @@ es2panda/test/TypeScript/tests/cases/conformance/interfaces/interfaceDeclaration es2panda/test/TypeScript/tests/cases/conformance/interfaces/interfaceDeclarations/asiPreventsParsingAsInterface03.ts es2panda/test/TypeScript/tests/cases/conformance/interfaces/interfaceDeclarations/asiPreventsParsingAsInterface04.ts es2panda/test/TypeScript/tests/cases/conformance/internalModules/moduleDeclarations/asiPreventsParsingAsNamespace04.ts -es2panda/test/TypeScript/tests/cases/conformance/parser/ecmascript5/ArrowFunctionExpressions/parserArrowFunctionExpression6.ts es2panda/test/TypeScript/tests/cases/conformance/parser/ecmascript5/RegressionTests/parser579071.ts es2panda/test/TypeScript/tests/cases/conformance/parser/ecmascript5/Statements/ContinueStatements/parser_continueTarget3.ts es2panda/test/TypeScript/tests/cases/conformance/parser/ecmascript5/parserUnicodeWhitespaceCharacter1.ts @@ -94,7 +92,6 @@ es2panda/test/TypeScript/tests/cases/conformance/scanner/ecmascript3/scannerES3N es2panda/test/TypeScript/tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts es2panda/test/TypeScript/tests/cases/conformance/types/members/objectTypeWithStringNamedNumericProperty.ts es2panda/test/TypeScript/tests/cases/conformance/types/specifyingTypes/typeQueries/typeQueryWithReservedWords.ts -es2panda/test/TypeScript/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAndParenthesizedExpressions01.ts es2panda/test/TypeScript/tests/cases/conformance/types/tuple/named/namedTupleMembers.ts es2panda/test/TypeScript/tests/cases/conformance/types/typeParameters/typeArgumentLists/constraintSatisfactionWithEmptyObject.ts es2panda/test/TypeScript/tests/cases/conformance/types/uniqueSymbol/uniqueSymbols.ts