diff --git a/es2panda/test/type_extractor/testcases/test-index-signature-expected.txt b/es2panda/test/type_extractor/testcases/test-index-signature-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..9db60c6c5ca14f3a072c20c79abc391f2a3092ad --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-index-signature-expected.txt @@ -0,0 +1,877 @@ +======> literal array buffer <====== +------------------------------------ +slot _-1 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: -2 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _9 +}, +------------------------------------ +slot _-2 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: -2 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _8 +}, +------------------------------------ +slot _-3 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 0 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _2 +}, +{ + index: 4 + tag: 0 + val: 2 +}, +{ + index: 5 + tag: 2 + val: 1 +}, +{ + index: 6 + tag: 0 + val: 24 +}, +{ + index: 7 + tag: 24 + val: _4 +}, +{ + index: 8 + tag: 0 + val: 2 +}, +{ + index: 9 + tag: 2 + val: 5 +}, +{ + index: 10 + tag: 0 + val: 24 +}, +{ + index: 11 + tag: 24 + val: _6 +}, +{ + index: 12 + tag: 0 + val: 2 +}, +{ + index: 13 + tag: 2 + val: 8 +}, +{ + index: 14 + tag: 0 + val: 24 +}, +{ + index: 15 + tag: 24 + val: _7 +}, +{ + index: 16 + tag: 0 + val: 2 +}, +{ + index: 17 + tag: 2 + val: 12 +}, +{ + index: 18 + tag: 0 + val: 24 +}, +{ + index: 19 + tag: 24 + val: _8 +}, +{ + index: 20 + tag: 0 + val: 2 +}, +{ + index: 21 + tag: 2 + val: 27 +}, +{ + index: 22 + tag: 0 + val: 24 +}, +{ + index: 23 + tag: 24 + val: _9 +}, +------------------------------------ +slot _0 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 9 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _1 +}, +{ + index: 4 + tag: 0 + val: 24 +}, +{ + index: 5 + tag: 24 + val: _2 +}, +{ + index: 6 + tag: 0 + val: 24 +}, +{ + index: 7 + tag: 24 + val: _3 +}, +{ + index: 8 + tag: 0 + val: 24 +}, +{ + index: 9 + tag: 24 + val: _4 +}, +{ + index: 10 + tag: 0 + val: 24 +}, +{ + index: 11 + tag: 24 + val: _5 +}, +{ + index: 12 + tag: 0 + val: 24 +}, +{ + index: 13 + tag: 24 + val: _6 +}, +{ + index: 14 + tag: 0 + val: 24 +}, +{ + index: 15 + tag: 24 + val: _7 +}, +{ + index: 16 + tag: 0 + val: 24 +}, +{ + index: 17 + tag: 24 + val: _8 +}, +{ + index: 18 + tag: 0 + val: 24 +}, +{ + index: 19 + tag: 24 + val: _9 +}, +{ + index: 20 + tag: 0 + val: 2 +}, +{ + index: 21 + tag: 2 + val: 0 +}, +------------------------------------ +slot _1 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 8 +}, +{ + index: 2 + tag: 0 + val: 2 +}, +{ + index: 3 + tag: 2 + val: 0 +}, +{ + index: 4 + tag: 0 + val: 2 +}, +{ + index: 5 + tag: 2 + val: 0 +}, +{ + index: 6 + tag: 0 + val: 2 +}, +{ + index: 7 + tag: 2 + val: 0 +}, +------------------------------------ +slot _10 +{ + index: 0 + tag: 2 + val: 0 +}, +{ + index: 1 + tag: 2 + val: 0 +}, +{ + index: 2 + tag: 2 + val: 0 +}, +{ + index: 3 + tag: 2 + val: 0 +}, +{ + index: 4 + tag: 2 + val: 0 +}, +{ + index: 5 + tag: 2 + val: 0 +}, +------------------------------------ +slot _11 +{ + index: 0 + tag: 0 + val: 5 +}, +{ + index: 1 + tag: 5 + val: a +}, +{ + index: 2 + tag: 0 + val: 2 +}, +{ + index: 3 + tag: 2 + val: 1 +}, +{ + index: 4 + tag: 0 + val: 5 +}, +{ + index: 5 + tag: 5 + val: b +}, +{ + index: 6 + tag: 0 + val: 2 +}, +{ + index: 7 + tag: 2 + val: 2 +}, +------------------------------------ +slot _12 +{ + index: 0 + tag: 0 + val: 5 +}, +{ + index: 1 + tag: 5 + val: a +}, +{ + index: 2 + tag: 0 + val: 2 +}, +{ + index: 3 + tag: 2 + val: 3 +}, +{ + index: 4 + tag: 0 + val: 5 +}, +{ + index: 5 + tag: 5 + val: b +}, +{ + index: 6 + tag: 0 + val: 2 +}, +{ + index: 7 + tag: 2 + val: 4 +}, +------------------------------------ +slot _13 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 0 +}, +------------------------------------ +slot _14 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 0 +}, +------------------------------------ +slot _2 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 11 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _1 +}, +{ + index: 4 + tag: 0 + val: 2 +}, +{ + index: 5 + tag: 2 + val: 1 +}, +{ + index: 6 + tag: 0 + val: 25 +}, +{ + index: 7 + tag: 25 + val: 4 +}, +{ + index: 8 + tag: 0 + val: 25 +}, +{ + index: 9 + tag: 25 + val: 1 +}, +------------------------------------ +slot _3 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 6 +}, +{ + index: 2 + tag: 0 + val: 2 +}, +{ + index: 3 + tag: 2 + val: 0 +}, +{ + index: 4 + tag: 0 + val: 2 +}, +{ + index: 5 + tag: 2 + val: 0 +}, +------------------------------------ +slot _4 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 11 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _3 +}, +{ + index: 4 + tag: 0 + val: 2 +}, +{ + index: 5 + tag: 2 + val: 1 +}, +{ + index: 6 + tag: 0 + val: 25 +}, +{ + index: 7 + tag: 25 + val: 4 +}, +{ + index: 8 + tag: 0 + val: 25 +}, +{ + index: 9 + tag: 25 + val: 1 +}, +------------------------------------ +slot _5 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 1 +}, +{ + index: 2 + tag: 0 + val: 2 +}, +{ + index: 3 + tag: 2 + val: 0 +}, +{ + index: 4 + tag: 0 + val: 25 +}, +{ + index: 5 + tag: 25 + val: 0 +}, +{ + index: 6 + tag: 0 + val: 2 +}, +{ + index: 7 + tag: 2 + val: 0 +}, +{ + index: 8 + tag: 0 + val: 2 +}, +{ + index: 9 + tag: 2 + val: 0 +}, +{ + index: 10 + tag: 0 + val: 2 +}, +{ + index: 11 + tag: 2 + val: 0 +}, +{ + index: 12 + tag: 0 + val: 2 +}, +{ + index: 13 + tag: 2 + val: 0 +}, +{ + index: 14 + tag: 0 + val: 2 +}, +{ + index: 15 + tag: 2 + val: 0 +}, +------------------------------------ +slot _6 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 11 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _5 +}, +{ + index: 4 + tag: 0 + val: 2 +}, +{ + index: 5 + tag: 2 + val: 2 +}, +{ + index: 6 + tag: 0 + val: 25 +}, +{ + index: 7 + tag: 25 + val: 1 +}, +{ + index: 8 + tag: 0 + val: 25 +}, +{ + index: 9 + tag: 25 + val: 1 +}, +{ + index: 10 + tag: 0 + val: 25 +}, +{ + index: 11 + tag: 25 + val: 4 +}, +{ + index: 12 + tag: 0 + val: 25 +}, +{ + index: 13 + tag: 25 + val: 1 +}, +------------------------------------ +slot _7 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 1 +}, +{ + index: 2 + tag: 0 + val: 2 +}, +{ + index: 3 + tag: 2 + val: 0 +}, +{ + index: 4 + tag: 0 + val: 24 +}, +{ + index: 5 + tag: 24 + val: _6 +}, +{ + index: 6 + tag: 0 + val: 2 +}, +{ + index: 7 + tag: 2 + val: 0 +}, +{ + index: 8 + tag: 0 + val: 2 +}, +{ + index: 9 + tag: 2 + val: 0 +}, +{ + index: 10 + tag: 0 + val: 2 +}, +{ + index: 11 + tag: 2 + val: 0 +}, +{ + index: 12 + tag: 0 + val: 2 +}, +{ + index: 13 + tag: 2 + val: 0 +}, +{ + index: 14 + tag: 0 + val: 2 +}, +{ + index: 15 + tag: 2 + val: 0 +}, +------------------------------------ +slot _8 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 2 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _6 +}, +------------------------------------ +slot _9 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 2 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _7 +}, diff --git a/es2panda/test/type_extractor/testcases/test-index-signature.ts b/es2panda/test/type_extractor/testcases/test-index-signature.ts new file mode 100644 index 0000000000000000000000000000000000000000..2fae1568943b598119ef5ee5ecae57c377948237 --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-index-signature.ts @@ -0,0 +1,42 @@ +/* + * 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. + */ + + +interface I { + [key: string]: number; +} + +const interfaceObj: I = { + a: 1, + b: 2 +}; + +let literalObj: {[key: string]: number} = {"a": 3, "b": 4}; + +class Rectangle { + [id: number]: number; + [property: string]: number; +} + +class Square extends Rectangle {}; + +let logoA = new Rectangle(); +logoA[0] = 1; +logoA["Width"] = 3; +logoA["Length"] = 5; + +let logoB = new Square(); +logoB[0] = 2; +logoB["Width"] = logoB["Length"] = 4; diff --git a/es2panda/typescript/extractor/typeRecorder.cpp b/es2panda/typescript/extractor/typeRecorder.cpp index b2c958458dc1c7fde3c5ae542961bf07c24897f1..fa9964eb5e7fa2b7961b64496df0c5ca3bd9e7df 100644 --- a/es2panda/typescript/extractor/typeRecorder.cpp +++ b/es2panda/typescript/extractor/typeRecorder.cpp @@ -149,6 +149,16 @@ void TypeRecorder::SetNodeTypeIndex(const ir::AstNode *node, int64_t index) ASSERT(GetNodeTypeIndex(node) == index); } +void TypeRecorder::UpdateNodeTypeIndex(const ir::AstNode *node, int64_t index) +{ + if (node == nullptr) { + return; + } + + nodeTypeIndex_[node] = index; + ASSERT(GetNodeTypeIndex(node) == index); +} + int64_t TypeRecorder::GetVariableTypeIndex(const binder::Variable *variable) const { return FindValue(variableTypeIndex_, variable, PRIMITIVETYPE_ANY); diff --git a/es2panda/typescript/extractor/typeRecorder.h b/es2panda/typescript/extractor/typeRecorder.h index 465b1e51bf2efe56041f826bfdab9bb777bc9fc1..51ba8e55fca123972c2372d3622f04ad4a139f04 100644 --- a/es2panda/typescript/extractor/typeRecorder.h +++ b/es2panda/typescript/extractor/typeRecorder.h @@ -65,6 +65,7 @@ public: int64_t GetNodeTypeIndex(const ir::AstNode *node) const; void SetNodeTypeIndex(const ir::AstNode *node, int64_t index); + void UpdateNodeTypeIndex(const ir::AstNode *node, int64_t index); int64_t GetVariableTypeIndex(const binder::Variable *variable) const; void SetVariableTypeIndex(const binder::Variable *variable, int64_t index); diff --git a/es2panda/typescript/extractor/typeSystem.h b/es2panda/typescript/extractor/typeSystem.h index 361c74faec1f74ba887a898deefc9c224b80fbdb..f7956333bf9185d67172beb40ea95d130883bf9a 100644 --- a/es2panda/typescript/extractor/typeSystem.h +++ b/es2panda/typescript/extractor/typeSystem.h @@ -805,7 +805,7 @@ private: // Update current type to IndexSignture Type IndexSigType indexSigType(extractor_, typeIndexShift_, &indexSignatures_); typeIndexShift_ = indexSigType.GetTypeIndexShift(); - recorder_->SetNodeTypeIndex(classDef, typeIndexShift_); + recorder_->UpdateNodeTypeIndex(classDef, typeIndexShift_); } } @@ -1024,7 +1024,7 @@ private: // Update current type to IndexSignture Type IndexSigType indexSigType(extractor_, typeIndexShift_, &indexSignatures_); typeIndexShift_ = indexSigType.GetTypeIndexShift(); - recorder_->SetNodeTypeIndex(interfaceDef, typeIndexShift_); + recorder_->UpdateNodeTypeIndex(interfaceDef, typeIndexShift_); } }