From 7884ba613972fec5c3b8d7429ba2b11c4e82444f Mon Sep 17 00:00:00 2001 From: zhangchen168 Date: Mon, 10 Oct 2022 10:37:16 +0800 Subject: [PATCH] Support the function overload with export keyword Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I5URVH Test: Test262,es2abc regression,es2abc tsc. Signed-off-by: zhangchen168 Change-Id: I6df372288bae85918b25193e44fa72e841b3816a --- es2panda/parser/statementParser.cpp | 5 + ...test_export-function-overload-expected.txt | 401 ++++++++++++++++++ .../ts/test_export-function-overload.ts | 20 + es2panda/test/test_tsc_ignore_list.txt | 1 - 4 files changed, 426 insertions(+), 1 deletion(-) create mode 100644 es2panda/test/parser/ts/test_export-function-overload-expected.txt create mode 100644 es2panda/test/parser/ts/test_export-function-overload.ts diff --git a/es2panda/parser/statementParser.cpp b/es2panda/parser/statementParser.cpp index fb0b61b1f8..c1bb9523d0 100644 --- a/es2panda/parser/statementParser.cpp +++ b/es2panda/parser/statementParser.cpp @@ -2225,6 +2225,10 @@ void ParserImpl::AddExportLocalEntryItem(const ir::Statement *declNode, bool isT ThrowSyntaxError("A class or function declaration without the default modifier mush have a name.", declNode->Start()); } + if (declNode->IsFunctionDeclaration() && + declNode->AsFunctionDeclaration()->Function()->IsOverload()) { + return; + } if (isTsModule) { tsModuleScope->AddExportVariable(name->Name()); } else { @@ -2404,6 +2408,7 @@ ir::ExportNamedDeclaration *ParserImpl::ParseNamedExportDeclaration(const lexer: VariableParsingFlags flag = isTsModule ? VariableParsingFlags::NO_OPTS : VariableParsingFlags::EXPORTED; ParserStatus status = isTsModule ? ParserStatus::NO_OPTS : ParserStatus::EXPORT_REACHED; + switch (lexer_->GetToken().Type()) { case lexer::TokenType::KEYW_VAR: { decl = ParseVariableDeclaration(flag | VariableParsingFlags::VAR, isDeclare); diff --git a/es2panda/test/parser/ts/test_export-function-overload-expected.txt b/es2panda/test/parser/ts/test_export-function-overload-expected.txt new file mode 100644 index 0000000000..339bb2c0ae --- /dev/null +++ b/es2panda/test/parser/ts/test_export-function-overload-expected.txt @@ -0,0 +1,401 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ExportNamedDeclaration", + "declaration": { + "type": "TSDeclareFunction", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "func", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 21 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 17, + "column": 25 + }, + "end": { + "line": 17, + "column": 31 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 22 + }, + "end": { + "line": 17, + "column": 23 + } + } + }, + { + "type": "Identifier", + "name": "b", + "typeAnnotation": { + "type": "TSStringKeyword", + "loc": { + "start": { + "line": 17, + "column": 36 + }, + "end": { + "line": 17, + "column": 42 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 33 + }, + "end": { + "line": 17, + "column": 34 + } + } + }, + { + "type": "RestElement", + "argument": { + "type": "Identifier", + "name": "args", + "typeAnnotation": { + "type": "TSArrayType", + "elementType": { + "type": "TSAnyKeyword", + "loc": { + "start": { + "line": 17, + "column": 53 + }, + "end": { + "line": 17, + "column": 56 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 53 + }, + "end": { + "line": 17, + "column": 58 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 47 + }, + "end": { + "line": 17, + "column": 51 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 44 + }, + "end": { + "line": 17, + "column": 51 + } + } + } + ], + "returnType": { + "type": "TSVoidKeyword", + "loc": { + "start": { + "line": 17, + "column": 61 + }, + "end": { + "line": 17, + "column": 65 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 17, + "column": 66 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 17, + "column": 66 + } + } + }, + "source": null, + "specifiers": [], + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 66 + } + } + }, + { + "type": "ExportNamedDeclaration", + "declaration": { + "type": "FunctionDeclaration", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "func", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 21 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 18, + "column": 25 + }, + "end": { + "line": 18, + "column": 31 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 22 + }, + "end": { + "line": 18, + "column": 23 + } + } + }, + { + "type": "Identifier", + "name": "b", + "typeAnnotation": { + "type": "TSStringKeyword", + "loc": { + "start": { + "line": 18, + "column": 36 + }, + "end": { + "line": 18, + "column": 42 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 33 + }, + "end": { + "line": 18, + "column": 34 + } + } + } + ], + "returnType": { + "type": "TSAnyKeyword", + "loc": { + "start": { + "line": 18, + "column": 45 + }, + "end": { + "line": 18, + "column": 48 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "BinaryExpression", + "operator": "+", + "left": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 13 + } + } + }, + "right": { + "type": "Identifier", + "name": "b", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 16 + }, + "end": { + "line": 19, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 49 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "source": null, + "specifiers": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 20, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 20, + "column": 2 + } + } +} diff --git a/es2panda/test/parser/ts/test_export-function-overload.ts b/es2panda/test/parser/ts/test_export-function-overload.ts new file mode 100644 index 0000000000..593f76bc07 --- /dev/null +++ b/es2panda/test/parser/ts/test_export-function-overload.ts @@ -0,0 +1,20 @@ +/* + * 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. + */ + + +export function func(a: number, b: string, ...args: any[]): void; +export function func(a: number, b: string): any { + return a + b; +} \ 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 ec89e64c81..52ee428e17 100644 --- a/es2panda/test/test_tsc_ignore_list.txt +++ b/es2panda/test/test_tsc_ignore_list.txt @@ -52,7 +52,6 @@ es2panda/test/TypeScript/tests/cases/compiler/sourceMapValidationDestructuringFo es2panda/test/TypeScript/tests/cases/compiler/sourceMapValidationDestructuringForObjectBindingPatternDefaultValues2.ts es2panda/test/TypeScript/tests/cases/compiler/systemNamespaceAliasEmit.ts es2panda/test/TypeScript/tests/cases/compiler/taggedTemplateStringWithSymbolExpression01.ts -es2panda/test/TypeScript/tests/cases/compiler/unusedLocalsAndParametersOverloadSignatures.ts es2panda/test/TypeScript/tests/cases/compiler/withStatementInternalComments.ts es2panda/test/TypeScript/tests/cases/conformance/async/es2017/asyncAwait_es2017.ts es2panda/test/TypeScript/tests/cases/conformance/classes/members/privateNames/privateNameComputedPropertyName1.ts -- Gitee