From 750a125aeb37a0a6eaa9c430f531423ed7eed6cf Mon Sep 17 00:00:00 2001 From: zhangchen168 Date: Tue, 13 Sep 2022 10:25:21 +0800 Subject: [PATCH] Fix identifier comparison of interface methodsignatures Enable the same method signature identifier in the interface Add a test case and expectation: es2panda/test/parser/ts/test-interface-method-identifier1.ts es2panda/test/parser/ts/test-interface-method-identifier1-expected.txt Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I5Q942 Test: test262,parser tests Signed-off-by: zhangchen168 Change-Id: I16d86fd49117d0f71a81ed9913a83d3cbedaa5f2 --- es2panda/parser/parserImpl.cpp | 22 +- es2panda/parser/parserImpl.h | 2 +- ...-interface-method-identifier1-expected.txt | 700 ++++++++++++++++++ .../ts/test-interface-method-identifier1.ts | 21 + 4 files changed, 738 insertions(+), 7 deletions(-) create mode 100644 es2panda/test/parser/ts/test-interface-method-identifier1-expected.txt create mode 100644 es2panda/test/parser/ts/test-interface-method-identifier1.ts diff --git a/es2panda/parser/parserImpl.cpp b/es2panda/parser/parserImpl.cpp index d3039e2a13..3f089b240a 100644 --- a/es2panda/parser/parserImpl.cpp +++ b/es2panda/parser/parserImpl.cpp @@ -1357,8 +1357,18 @@ util::StringView GetTSPropertyName(ir::Expression *key) } } -void ParserImpl::CheckObjectTypeForDuplicatedProperties(ir::Expression *key, ArenaVector &members) +void ParserImpl::CheckObjectTypeForDuplicatedProperties(ir::Expression *member, ArenaVector &members) { + ir::Expression *key = nullptr; + + if (member->IsTSPropertySignature()) { + key = member->AsTSPropertySignature()->Key(); + } else if (member->IsTSMethodSignature()) { + key = member->AsTSMethodSignature()->Key(); + } else { + return; + } + if (!key->IsIdentifier() && !key->IsNumberLiteral() && !key->IsStringLiteral()) { return; } @@ -1384,6 +1394,10 @@ void ParserImpl::CheckObjectTypeForDuplicatedProperties(ir::Expression *key, Are continue; } + if (member->IsTSMethodSignature() && it->Type() == ir::AstNodeType::TS_METHOD_SIGNATURE) { + continue; + } + if (GetTSPropertyName(key) == GetTSPropertyName(compare)) { ThrowSyntaxError("Duplicated identifier", key->Start()); } @@ -1401,11 +1415,7 @@ ArenaVector ParserImpl::ParseTsTypeLiteralOrInterface() while (lexer_->GetToken().Type() != lexer::TokenType::PUNCTUATOR_RIGHT_BRACE) { ir::Expression *member = ParseTsTypeLiteralOrInterfaceMember(); - if (member->IsTSPropertySignature()) { - CheckObjectTypeForDuplicatedProperties(member->AsTSPropertySignature()->Key(), members); - } else if (member->IsTSMethodSignature()) { - CheckObjectTypeForDuplicatedProperties(member->AsTSMethodSignature()->Key(), members); - } + CheckObjectTypeForDuplicatedProperties(member, members); members.push_back(member); diff --git a/es2panda/parser/parserImpl.h b/es2panda/parser/parserImpl.h index ed541f881c..2755b19e90 100644 --- a/es2panda/parser/parserImpl.h +++ b/es2panda/parser/parserImpl.h @@ -324,7 +324,7 @@ private: ir::TSParameterProperty *CreateTsParameterProperty(ir::Expression *parameter, ir::ModifierFlags modifiers); ir::Expression *ParseFunctionParameter(bool isDeclare); void CreateTSVariableForProperty(ir::AstNode *node, const ir::Expression *key, binder::VariableFlags flags); - void CheckObjectTypeForDuplicatedProperties(ir::Expression *key, ArenaVector &members); + void CheckObjectTypeForDuplicatedProperties(ir::Expression *member, ArenaVector &members); // ExpressionParser.Cpp diff --git a/es2panda/test/parser/ts/test-interface-method-identifier1-expected.txt b/es2panda/test/parser/ts/test-interface-method-identifier1-expected.txt new file mode 100644 index 0000000000..dae1cab055 --- /dev/null +++ b/es2panda/test/parser/ts/test-interface-method-identifier1-expected.txt @@ -0,0 +1,700 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSInterfaceDeclaration", + "body": { + "type": "TSInterfaceBody", + "body": [ + { + "type": "TSMethodSignature", + "computed": false, + "optional": false, + "key": { + "type": "Identifier", + "name": "then", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 9 + } + } + }, + "params": [ + { + "type": "Identifier", + "name": "success", + "typeAnnotation": { + "type": "TSFunctionType", + "params": [ + { + "type": "Identifier", + "name": "value", + "typeAnnotation": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 31 + }, + "end": { + "line": 18, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 31 + }, + "end": { + "line": 18, + "column": 32 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 24 + }, + "end": { + "line": 18, + "column": 29 + } + } + } + ], + "returnType": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "U", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 37 + }, + "end": { + "line": 18, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 37 + }, + "end": { + "line": 18, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 23 + }, + "end": { + "line": 18, + "column": 38 + } + } + }, + "optional": true, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 20 + } + } + } + ], + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "U", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 12 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 12 + } + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 41 + }, + "end": { + "line": 18, + "column": 48 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "U", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 49 + }, + "end": { + "line": 18, + "column": 50 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 49 + }, + "end": { + "line": 18, + "column": 50 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 41 + }, + "end": { + "line": 18, + "column": 51 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 41 + }, + "end": { + "line": 18, + "column": 48 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 52 + } + } + }, + { + "type": "TSMethodSignature", + "computed": false, + "optional": false, + "key": { + "type": "Identifier", + "name": "then", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 9 + } + } + }, + "params": [ + { + "type": "Identifier", + "name": "success", + "typeAnnotation": { + "type": "TSFunctionType", + "params": [ + { + "type": "Identifier", + "name": "value", + "typeAnnotation": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 31 + }, + "end": { + "line": 19, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 31 + }, + "end": { + "line": 19, + "column": 32 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 24 + }, + "end": { + "line": 19, + "column": 29 + } + } + } + ], + "returnType": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 37 + }, + "end": { + "line": 19, + "column": 44 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "U", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 45 + }, + "end": { + "line": 19, + "column": 46 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 45 + }, + "end": { + "line": 19, + "column": 46 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 37 + }, + "end": { + "line": 19, + "column": 47 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 37 + }, + "end": { + "line": 19, + "column": 44 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 23 + }, + "end": { + "line": 19, + "column": 44 + } + } + }, + "optional": true, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 20 + } + } + } + ], + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "U", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 10 + }, + "end": { + "line": 19, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 10 + }, + "end": { + "line": 19, + "column": 12 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 12 + } + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 50 + }, + "end": { + "line": 19, + "column": 57 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "U", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 58 + }, + "end": { + "line": 19, + "column": 59 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 58 + }, + "end": { + "line": 19, + "column": 59 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 50 + }, + "end": { + "line": 19, + "column": 60 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 50 + }, + "end": { + "line": 19, + "column": 57 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 61 + } + } + }, + { + "type": "TSPropertySignature", + "computed": false, + "optional": false, + "readonly": false, + "key": { + "type": "Identifier", + "name": "value", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 10 + } + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 22 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "id": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 11 + }, + "end": { + "line": 17, + "column": 18 + } + } + }, + "extends": [], + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 19 + }, + "end": { + "line": 17, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 19 + }, + "end": { + "line": 17, + "column": 21 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 21, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 22, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/ts/test-interface-method-identifier1.ts b/es2panda/test/parser/ts/test-interface-method-identifier1.ts new file mode 100644 index 0000000000..d00de1478a --- /dev/null +++ b/es2panda/test/parser/ts/test-interface-method-identifier1.ts @@ -0,0 +1,21 @@ +/* + * 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. + */ + + +interface Promise { + then(success?: (value: T) => U): Promise; + then(success?: (value: T) => Promise): Promise; + value: T; +} -- Gitee