From 60e85fb2a44c476110f62eb9d61466c0d87ef8cf Mon Sep 17 00:00:00 2001 From: xucheng46 Date: Wed, 23 Nov 2022 13:20:17 +0000 Subject: [PATCH] Add tests for es2abc Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I62X3L Test: parser tests, compiler tests, tsc tests, test262 Signed-off-by: xucheng46 Change-Id: I690e6a0797fb51f4124c8618af0997c2ada23de2 --- es2panda/es2panda.cpp | 8 +- es2panda/ir/module/exportDefaultDeclaration.h | 10 + es2panda/ir/module/exportNamedDeclaration.h | 5 + es2panda/ir/statements/ifStatement.cpp | 36 +- es2panda/ir/statements/ifStatement.h | 21 +- .../parser/module/sourceTextModuleRecord.cpp | 16 +- .../parser/module/sourceTextModuleRecord.h | 1 + es2panda/parser/statementParser.cpp | 31 +- es2panda/parser/transformer/transformer.cpp | 54 +- es2panda/parser/transformer/transformer.h | 3 + .../expressions/test-expression1-expected.txt | 6 + .../language/expressions/test-expression1.js | 34 + .../ts-test-expression-1-expected.txt | 14 + .../expressions/ts-test-expression-1.ts | 65 ++ .../ts/test-class-definiton22-expected.txt | 635 ++++++++++++ .../test/parser/ts/test-class-definiton22.ts | 22 + .../ts/test-export-interface-expected.txt | 115 +++ .../test/parser/ts/test-export-interface.ts | 18 + .../ts/type_checker/expression_1-expected.txt | 413 ++++++++ .../parser/ts/type_checker/expression_1.ts | 22 + .../ts/type_checker/expression_2-expected.txt | 865 ++++++++++++++++ .../parser/ts/type_checker/expression_2.ts | 30 + .../ts/type_checker/expression_3-expected.txt | 937 ++++++++++++++++++ .../parser/ts/type_checker/expression_3.ts | 34 + .../ts/type_checker/expression_4-expected.txt | 887 +++++++++++++++++ .../parser/ts/type_checker/expression_4.ts | 33 + 26 files changed, 4285 insertions(+), 30 deletions(-) create mode 100644 es2panda/test/compiler/js/language/expressions/test-expression1-expected.txt create mode 100644 es2panda/test/compiler/js/language/expressions/test-expression1.js create mode 100644 es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-expression-1-expected.txt create mode 100644 es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-expression-1.ts create mode 100644 es2panda/test/parser/ts/test-class-definiton22-expected.txt create mode 100644 es2panda/test/parser/ts/test-class-definiton22.ts create mode 100644 es2panda/test/parser/ts/test-export-interface-expected.txt create mode 100644 es2panda/test/parser/ts/test-export-interface.ts create mode 100644 es2panda/test/parser/ts/type_checker/expression_1-expected.txt create mode 100644 es2panda/test/parser/ts/type_checker/expression_1.ts create mode 100644 es2panda/test/parser/ts/type_checker/expression_2-expected.txt create mode 100644 es2panda/test/parser/ts/type_checker/expression_2.ts create mode 100644 es2panda/test/parser/ts/type_checker/expression_3-expected.txt create mode 100644 es2panda/test/parser/ts/type_checker/expression_3.ts create mode 100644 es2panda/test/parser/ts/type_checker/expression_4-expected.txt create mode 100644 es2panda/test/parser/ts/type_checker/expression_4.ts diff --git a/es2panda/es2panda.cpp b/es2panda/es2panda.cpp index 5ab01d0d6b..629a7c8bab 100644 --- a/es2panda/es2panda.cpp +++ b/es2panda/es2panda.cpp @@ -84,15 +84,15 @@ panda::pandasm::Program *Compiler::Compile(const SourceFile &input, const Compil checker->StartChecker(); } - if (options.parseOnly) { - return nullptr; - } - if (ast.Extension() == ScriptExtension::TS) { transformer_->Transform(&ast); ast.Binder()->IdentifierAnalysis(binder::ResolveBindingFlags::ALL); } + if (options.parseOnly) { + return nullptr; + } + std::string debugInfoSourceFile = options.debugInfoSourceFile.empty() ? sourcefile : options.debugInfoSourceFile; auto *prog = compiler_->Compile(&ast, options, debugInfoSourceFile); diff --git a/es2panda/ir/module/exportDefaultDeclaration.h b/es2panda/ir/module/exportDefaultDeclaration.h index 6ab246e25a..0f86411f8f 100644 --- a/es2panda/ir/module/exportDefaultDeclaration.h +++ b/es2panda/ir/module/exportDefaultDeclaration.h @@ -41,6 +41,16 @@ public: return decl_; } + AstNode *Decl() + { + return decl_; + } + + void SetDecl(AstNode *decl) + { + decl_ = decl; + } + bool IsExportEquals() const { return exportEquals_; diff --git a/es2panda/ir/module/exportNamedDeclaration.h b/es2panda/ir/module/exportNamedDeclaration.h index 3185da787e..51730b538e 100644 --- a/es2panda/ir/module/exportNamedDeclaration.h +++ b/es2panda/ir/module/exportNamedDeclaration.h @@ -62,6 +62,11 @@ public: return decl_; } + void SetDecl(Statement *decl) + { + decl_ = decl; + } + const StringLiteral *Source() const { return source_; diff --git a/es2panda/ir/statements/ifStatement.cpp b/es2panda/ir/statements/ifStatement.cpp index 7f0a35b5fb..67e2a4cef1 100644 --- a/es2panda/ir/statements/ifStatement.cpp +++ b/es2panda/ir/statements/ifStatement.cpp @@ -15,11 +15,12 @@ #include "ifStatement.h" -#include -#include -#include -#include -#include +#include "compiler/base/condition.h" +#include "compiler/core/pandagen.h" +#include "ir/astDump.h" +#include "ir/statements/blockStatement.h" +#include "ir/expression.h" +#include "typescript/checker.h" namespace panda::es2panda::ir { @@ -75,14 +76,33 @@ checker::Type *IfStatement::Check(checker::Checker *checker) const return nullptr; } -void IfStatement::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder) +void IfStatement::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder) { test_ = std::get(cb(test_))->AsExpression(); - consequent_ = std::get(cb(consequent_))->AsStatement(); + consequent_ = UpdateIfStatementChildStatement(cb, binder, consequent_, consequentScope_); if (alternate_) { - alternate_ = std::get(cb(alternate_))->AsStatement(); + alternate_ = UpdateIfStatementChildStatement(cb, binder, alternate_, alternateScope_); } } +Statement *IfStatement::UpdateIfStatementChildStatement(const NodeUpdater &cb, + const binder::Binder *binder, + Statement *statement, + binder::Scope *scope) const +{ + auto newStatement = cb(statement); + if (std::holds_alternative(newStatement)) { + return std::get(newStatement)->AsStatement(); + } + + ASSERT(std::holds_alternative>(newStatement)); + ArenaVector statements(binder->Allocator()->Adapter()); + auto newStatements = std::get>(newStatement); + for (auto *it : newStatements) { + statements.push_back(it->AsStatement()); + } + return binder->Allocator()->New(scope, std::move(statements)); +} + } // namespace panda::es2panda::ir diff --git a/es2panda/ir/statements/ifStatement.h b/es2panda/ir/statements/ifStatement.h index 7380df429b..e299327eac 100644 --- a/es2panda/ir/statements/ifStatement.h +++ b/es2panda/ir/statements/ifStatement.h @@ -16,7 +16,7 @@ #ifndef ES2PANDA_IR_STATEMENT_IF_STATEMENT_H #define ES2PANDA_IR_STATEMENT_IF_STATEMENT_H -#include +#include "ir/statement.h" namespace panda::es2panda::compiler { class PandaGen; @@ -33,8 +33,14 @@ class Expression; class IfStatement : public Statement { public: - explicit IfStatement(Expression *test, Statement *consequent, Statement *alternate) - : Statement(AstNodeType::IF_STATEMENT), test_(test), consequent_(consequent), alternate_(alternate) + explicit IfStatement(Expression *test, Statement *consequent, binder::Scope *consequentScope, + Statement *alternate, binder::Scope *alternateScope) + : Statement(AstNodeType::IF_STATEMENT), + test_(test), + consequent_(consequent), + consequentScope_(consequentScope), + alternate_(alternate), + alternateScope_(alternateScope) { } @@ -56,12 +62,19 @@ public: void Dump(ir::AstDumper *dumper) const override; void Compile(compiler::PandaGen *pg) const override; checker::Type *Check(checker::Checker *checker) const override; - void UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder) override; + void UpdateSelf(const NodeUpdater &cb, binder::Binder *binder) override; protected: + Statement *UpdateIfStatementChildStatement(const NodeUpdater &cb, + const binder::Binder *binder, + Statement *statement, + binder::Scope *scope) const; + Expression *test_; Statement *consequent_; + binder::Scope *consequentScope_; Statement *alternate_; + binder::Scope *alternateScope_; }; } // namespace panda::es2panda::ir diff --git a/es2panda/parser/module/sourceTextModuleRecord.cpp b/es2panda/parser/module/sourceTextModuleRecord.cpp index 1883403bc2..a6b213ed4a 100644 --- a/es2panda/parser/module/sourceTextModuleRecord.cpp +++ b/es2panda/parser/module/sourceTextModuleRecord.cpp @@ -169,14 +169,22 @@ namespace panda::es2panda::parser { for (auto it = localExportEntries_.begin(); it != localExportEntries_.end(); it = localExportEntries_.upper_bound(it->first)) { - moduleScope->AssignIndexToModuleVariable(it->first, index); - index++; + CheckAndAssignIndex(moduleScope, it->first, &index); } index = 0; for (const auto &elem : regularImportEntries_) { - moduleScope->AssignIndexToModuleVariable(elem.first, index); - index++; + CheckAndAssignIndex(moduleScope, elem.first, &index); + } + } + + void SourceTextModuleRecord::CheckAndAssignIndex(binder::ModuleScope *moduleScope, + util::StringView name, + uint32_t *index) const + { + if (moduleScope->FindLocal(name) != nullptr) { + moduleScope->AssignIndexToModuleVariable(name, *index); + (*index)++; } } } // namespace panda::es2panda::parser diff --git a/es2panda/parser/module/sourceTextModuleRecord.h b/es2panda/parser/module/sourceTextModuleRecord.h index cea4266aba..7e08fbe0f4 100644 --- a/es2panda/parser/module/sourceTextModuleRecord.h +++ b/es2panda/parser/module/sourceTextModuleRecord.h @@ -149,6 +149,7 @@ public: private: bool HasDuplicateExport(util::StringView exportName) const; void ConvertLocalExportToIndirect(ImportEntry *importEntry, ExportEntry *exportEntry); + void CheckAndAssignIndex(binder::ModuleScope *moduleScope, util::StringView name, uint32_t *inde) const; ArenaAllocator *allocator_; ModuleRequestMap moduleRequestsMap_; diff --git a/es2panda/parser/statementParser.cpp b/es2panda/parser/statementParser.cpp index b9c73c0ad8..93b5a3978d 100644 --- a/es2panda/parser/statementParser.cpp +++ b/es2panda/parser/statementParser.cpp @@ -1552,7 +1552,15 @@ ir::IfStatement *ParserImpl::ParseIfStatement() } lexer_->NextToken(); - ir::Statement *consequent = ParseStatement(StatementParsingFlags::IF_ELSE); + + ir::Statement *consequent = nullptr; + auto consequentCtx = binder::LexicalScope(Binder()); + if (lexer_->GetToken().Type() == lexer::TokenType::PUNCTUATOR_LEFT_BRACE) { + consequent = ParseBlockStatement(consequentCtx.GetScope()); + lexer_->NextToken(); + } else { + consequent = ParseStatement(StatementParsingFlags::IF_ELSE); + } if (Extension() == ScriptExtension::TS && consequent->IsEmptyStatement()) { ThrowSyntaxError("The body of an if statement cannot be the empty statement"); @@ -1560,14 +1568,23 @@ ir::IfStatement *ParserImpl::ParseIfStatement() endLoc = consequent->End(); ir::Statement *alternate = nullptr; + binder::Scope *alternateScope = nullptr; if (lexer_->GetToken().Type() == lexer::TokenType::KEYW_ELSE) { lexer_->NextToken(); // eat ELSE keyword - alternate = ParseStatement(StatementParsingFlags::IF_ELSE); + auto alternateCtx = binder::LexicalScope(Binder()); + if (lexer_->GetToken().Type() == lexer::TokenType::PUNCTUATOR_LEFT_BRACE) { + alternate = ParseBlockStatement(alternateCtx.GetScope()); + lexer_->NextToken(); + } else { + alternate = ParseStatement(StatementParsingFlags::IF_ELSE); + } endLoc = alternate->End(); + alternateScope = alternateCtx.GetScope(); } - auto *ifStatement = AllocNode(test, consequent, alternate); + auto *ifStatement = AllocNode(test, consequent, consequentCtx.GetScope(), + alternate, alternateScope); ifStatement->SetRange({startLoc, endLoc}); return ifStatement; } @@ -2124,6 +2141,10 @@ ir::WhileStatement *ParserImpl::ParseWhileStatement() void ParserImpl::AddImportEntryItem(const ir::StringLiteral *source, const ArenaVector *specifiers) { + if (context_.IsTsModule()) { + return; + } + ASSERT(source != nullptr); auto *moduleRecord = GetSourceTextModuleRecord(); ASSERT(moduleRecord != nullptr); @@ -2249,6 +2270,10 @@ void ParserImpl::AddExportStarEntryItem(const lexer::SourcePosition &startLoc, c void ParserImpl::AddExportDefaultEntryItem(const ir::AstNode *declNode) { + if (context_.IsTsModule()) { + return; + } + ASSERT(declNode != nullptr); if (declNode->IsTSInterfaceDeclaration() || (declNode->IsFunctionDeclaration() && declNode->AsFunctionDeclaration()->Function()->IsOverload())) { diff --git a/es2panda/parser/transformer/transformer.cpp b/es2panda/parser/transformer/transformer.cpp index 759caa569d..eccc66a4e2 100644 --- a/es2panda/parser/transformer/transformer.cpp +++ b/es2panda/parser/transformer/transformer.cpp @@ -38,6 +38,7 @@ #include "ir/expressions/sequenceExpression.h" #include "ir/expressions/templateLiteral.h" #include "ir/expressions/thisExpression.h" +#include "ir/module/exportDefaultDeclaration.h" #include "ir/module/exportNamedDeclaration.h" #include "ir/module/exportSpecifier.h" #include "ir/statements/blockStatement.h" @@ -253,13 +254,25 @@ ir::UpdateNodes Transformer::VisitTSNode(ir::AstNode *childNode) return res; } - if (!IsTsModule()) { - return VisitTSNodes(childNode); + if (IsTsModule()) { + auto res = VisitExportNamedVariable(decl); + SetOriginalNode(res, node); + return res; } - auto res = VisitExportNamedVariable(decl); - SetOriginalNode(res, childNode); - return res; + if (decl->IsClassDeclaration()) { + return VisitExportClassDeclaration(node); + } + + return VisitTSNodes(node); + } + case ir::AstNodeType::EXPORT_DEFAULT_DECLARATION: { + auto *node = childNode->AsExportDefaultDeclaration(); + auto *decl = node->Decl(); + if (!decl || !decl->IsClassDeclaration()) { + return VisitTSNodes(childNode); + } + return VisitExportClassDeclaration(node); } case ir::AstNodeType::TS_IMPORT_EQUALS_DECLARATION: { auto *node = childNode->AsTSImportEqualsDeclaration(); @@ -309,6 +322,33 @@ ir::UpdateNodes Transformer::VisitTSNode(ir::AstNode *childNode) } } +template +ir::UpdateNodes Transformer::VisitExportClassDeclaration(T *node) +{ + auto *decl = node->Decl(); + auto newDecl = VisitTSNode(decl); + if (std::holds_alternative(newDecl)) { + auto statement = std::get(newDecl); + ASSERT(statement->IsClassDeclaration()); + node->SetDecl(statement->AsClassDeclaration()); + return node; + } else { + auto statements = std::get>(newDecl); + std::vector res; + auto firstStatement = statements.front(); + statements.erase(statements.begin()); + if (firstStatement->IsVariableDeclaration()) { + node->SetDecl(firstStatement->AsVariableDeclaration()); + } else { + ASSERT(firstStatement->IsClassDeclaration()); + node->SetDecl(firstStatement->AsClassDeclaration()); + } + res.push_back(node); + res.insert(res.end(), statements.begin(), statements.end()); + return res; + } +} + util::StringView Transformer::CreateNewVariable(bool needAddToStatements) { util::StringView name = CreateNewVariableName(); @@ -776,9 +816,9 @@ std::vector Transformer::CreateMethodDecorators(util::StringView return res; } -ir::Expression *Transformer::CreateDecoratorTarget(util::StringView className, bool targetCtor) +ir::Expression *Transformer::CreateDecoratorTarget(util::StringView className, bool isStatic) { - if (targetCtor) { + if (isStatic) { return CreateReferenceIdentifier(className); } return CreateClassPrototype(className); diff --git a/es2panda/parser/transformer/transformer.h b/es2panda/parser/transformer/transformer.h index 3d109cdb75..9e90beb7f2 100644 --- a/es2panda/parser/transformer/transformer.h +++ b/es2panda/parser/transformer/transformer.h @@ -186,6 +186,9 @@ private: binder::Scope *FindEnumMemberScope(const util::StringView name) const; ir::MemberExpression *CreateMemberExpressionFromIdentifier(binder::Scope *scope, ir::Identifier *node); + template + ir::UpdateNodes VisitExportClassDeclaration(T *node); + bool IsTsModule() const { return (tsModuleList_.size() != 0); diff --git a/es2panda/test/compiler/js/language/expressions/test-expression1-expected.txt b/es2panda/test/compiler/js/language/expressions/test-expression1-expected.txt new file mode 100644 index 0000000000..88c5027b76 --- /dev/null +++ b/es2panda/test/compiler/js/language/expressions/test-expression1-expected.txt @@ -0,0 +1,6 @@ +2 +0 +true +true +false +false diff --git a/es2panda/test/compiler/js/language/expressions/test-expression1.js b/es2panda/test/compiler/js/language/expressions/test-expression1.js new file mode 100644 index 0000000000..8c1204d53d --- /dev/null +++ b/es2panda/test/compiler/js/language/expressions/test-expression1.js @@ -0,0 +1,34 @@ +/* + * 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=1; +var b=2; +var c = a < b; +var d = a <= b; +var e = a > b; +var f = a >= b; +a = a++; +b = b--; +a = +a; +b = -b; +a += a; +b -= b; +print(a); +print(b); +print(c); +print(d); +print(e); +print(f); diff --git a/es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-expression-1-expected.txt b/es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-expression-1-expected.txt new file mode 100644 index 0000000000..a423883f4d --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-expression-1-expected.txt @@ -0,0 +1,14 @@ +1 2 3 4 5 +1 +0 +true +3 +1 +str +1 +1 +1 +5 +1 2 +5 +1 diff --git a/es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-expression-1.ts b/es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-expression-1.ts new file mode 100644 index 0000000000..9499a074d8 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/expressions/ts-test-expression-1.ts @@ -0,0 +1,65 @@ +/* + * 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 N=[3, 4, 5] +var a = [1, 2, ...N]; +print(...a) + +var b = () :number => { return 1 } + +print(b()) + +var c = 1; +c += c; +c *= c; +c -= c; +c = -c; +c++; +++c; +c--; +--c; +print(c) + +var d = c != c; +var e = 1 | 2; +e = e & 3; +d = e == 3; +print(d) +print(e) + +var f = { + a: 1, + b: "str", + c: 1 as number, + d: () => { return 1 }, + e: { a: 1 }, + f: c = 5, + g: [1,2] +} + +print(f.a) +print(f.b) +print(f.c) +print(f.d()) +print(f.e.a) +print(f.f) +print(...f.g) + +switch (c) { + case 0 : print(0); + case 5 : print(5); + default: print(1); +} diff --git a/es2panda/test/parser/ts/test-class-definiton22-expected.txt b/es2panda/test/parser/ts/test-class-definiton22-expected.txt new file mode 100644 index 0000000000..9e668d18b4 --- /dev/null +++ b/es2panda/test/parser/ts/test-class-definiton22-expected.txt @@ -0,0 +1,635 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSInterfaceDeclaration", + "body": { + "type": "TSInterfaceBody", + "body": [], + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 14 + } + } + }, + "id": { + "type": "Identifier", + "name": "I", + "loc": { + "start": { + "line": 17, + "column": 11 + }, + "end": { + "line": 17, + "column": 12 + } + } + }, + "extends": [], + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 14 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "C", + "loc": { + "start": { + "line": 18, + "column": 7 + }, + "end": { + "line": 18, + "column": 8 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "end": { + "line": 18, + "column": 11 + } + } + }, + "superClass": null, + "implements": [ + { + "type": "TSClassImplements", + "expression": { + "type": "Identifier", + "name": "I", + "loc": { + "start": { + "line": 18, + "column": 23 + }, + "end": { + "line": 18, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 23 + }, + "end": { + "line": 18, + "column": 26 + } + } + } + ], + "constructor": { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "body": [], + "indexSignatures": [ + { + "type": "TSIndexSignature", + "parameters": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 19, + "column": 6 + }, + "end": { + "line": 19, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 4 + }, + "end": { + "line": 19, + "column": 5 + } + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "T", + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 19, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 19, + "column": 15 + } + } + }, + "readonly": false, + "loc": { + "start": { + "line": 19, + "column": 4 + }, + "end": { + "line": 19, + "column": 15 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 25 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "D", + "loc": { + "start": { + "line": 21, + "column": 7 + }, + "end": { + "line": 21, + "column": 8 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "loc": { + "start": { + "line": 21, + "column": 9 + }, + "end": { + "line": 21, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 9 + }, + "end": { + "line": 21, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 8 + }, + "end": { + "line": 21, + "column": 11 + } + } + }, + "superClass": { + "type": "Identifier", + "name": "C", + "loc": { + "start": { + "line": 21, + "column": 20 + }, + "end": { + "line": 21, + "column": 21 + } + } + }, + "superTypeParameters": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "T", + "loc": { + "start": { + "line": 21, + "column": 22 + }, + "end": { + "line": 21, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 22 + }, + "end": { + "line": 21, + "column": 23 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 21 + }, + "end": { + "line": 21, + "column": 24 + } + } + }, + "implements": [], + "constructor": { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "RestElement", + "argument": { + "type": "Identifier", + "name": "args", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Super", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "arguments": [ + { + "type": "SpreadElement", + "argument": { + "type": "Identifier", + "name": "args", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "body": [], + "indexSignatures": [], + "loc": { + "start": { + "line": 21, + "column": 25 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 22, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 23, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/ts/test-class-definiton22.ts b/es2panda/test/parser/ts/test-class-definiton22.ts new file mode 100644 index 0000000000..e7d3305995 --- /dev/null +++ b/es2panda/test/parser/ts/test-class-definiton22.ts @@ -0,0 +1,22 @@ +/* + * 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 I{} +class C implements I { + [a:number]:T; +} +class D extends C { +} diff --git a/es2panda/test/parser/ts/test-export-interface-expected.txt b/es2panda/test/parser/ts/test-export-interface-expected.txt new file mode 100644 index 0000000000..566da39a2e --- /dev/null +++ b/es2panda/test/parser/ts/test-export-interface-expected.txt @@ -0,0 +1,115 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSInterfaceDeclaration", + "body": { + "type": "TSInterfaceBody", + "body": [], + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 15 + } + } + }, + "id": { + "type": "Identifier", + "name": "I", + "loc": { + "start": { + "line": 17, + "column": 11 + }, + "end": { + "line": 17, + "column": 12 + } + } + }, + "extends": [], + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 15 + } + } + }, + { + "type": "ExportNamedDeclaration", + "declaration": null, + "source": null, + "specifiers": [ + { + "type": "ExportSpecifier", + "local": { + "type": "Identifier", + "name": "I", + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 10 + } + } + }, + "exported": { + "type": "Identifier", + "name": "I", + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 10 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 19, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/ts/test-export-interface.ts b/es2panda/test/parser/ts/test-export-interface.ts new file mode 100644 index 0000000000..659eaa6eaa --- /dev/null +++ b/es2panda/test/parser/ts/test-export-interface.ts @@ -0,0 +1,18 @@ +/* + * 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 I {} +export {I} diff --git a/es2panda/test/parser/ts/type_checker/expression_1-expected.txt b/es2panda/test/parser/ts/type_checker/expression_1-expected.txt new file mode 100644 index 0000000000..777c349301 --- /dev/null +++ b/es2panda/test/parser/ts/type_checker/expression_1-expected.txt @@ -0,0 +1,413 @@ +{ + "type": "Program", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "N", + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 6 + } + } + }, + "init": { + "type": "ArrayExpression", + "elements": [ + { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 17, + "column": 9 + } + } + }, + { + "type": "NumberLiteral", + "value": 4, + "loc": { + "start": { + "line": 17, + "column": 10 + }, + "end": { + "line": 17, + "column": 11 + } + } + }, + { + "type": "NumberLiteral", + "value": 5, + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 13 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 14 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 14 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + "init": { + "type": "ArrayExpression", + "elements": [ + { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 11 + } + } + }, + { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 13 + } + } + }, + { + "type": "SpreadElement", + "argument": { + "type": "Identifier", + "name": "N", + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 19 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "b", + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + "init": { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 20, + "column": 14 + }, + "end": { + "line": 20, + "column": 20 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 20, + "column": 32 + }, + "end": { + "line": 20, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 25 + }, + "end": { + "line": 20, + "column": 33 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 24 + }, + "end": { + "line": 20, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 34 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 20, + "column": 34 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 22, + "column": 1 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "right": { + "type": "Identifier", + "name": "b", + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 1 + }, + "end": { + "line": 22, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 1 + }, + "end": { + "line": 22, + "column": 7 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 23, + "column": 1 + } + } +} +TypeError: Type '() => number' is not assignable to type 'number[]'. [expression_1.ts:22:1] diff --git a/es2panda/test/parser/ts/type_checker/expression_1.ts b/es2panda/test/parser/ts/type_checker/expression_1.ts new file mode 100644 index 0000000000..3b581c0155 --- /dev/null +++ b/es2panda/test/parser/ts/type_checker/expression_1.ts @@ -0,0 +1,22 @@ +/* + * 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 N=[3,4,5] +var a = [1,2,...N]; + +var b = () : number => {return 1} + +a = b; diff --git a/es2panda/test/parser/ts/type_checker/expression_2-expected.txt b/es2panda/test/parser/ts/type_checker/expression_2-expected.txt new file mode 100644 index 0000000000..7f2466026a --- /dev/null +++ b/es2panda/test/parser/ts/type_checker/expression_2-expected.txt @@ -0,0 +1,865 @@ +{ + "type": "Program", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 6 + } + } + }, + "init": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 10 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 11 + } + } + }, + { + "type": "SwitchStatement", + "discriminant": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 10 + } + } + }, + "cases": [ + { + "type": "SwitchCase", + "test": { + "type": "NumberLiteral", + "value": 0, + "loc": { + "start": { + "line": 20, + "column": 10 + }, + "end": { + "line": 20, + "column": 11 + } + } + }, + "consequent": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "+=", + "left": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 20, + "column": 14 + }, + "end": { + "line": 20, + "column": 15 + } + } + }, + "right": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 20, + "column": 19 + }, + "end": { + "line": 20, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 14 + }, + "end": { + "line": 20, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 14 + }, + "end": { + "line": 20, + "column": 21 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 21 + } + } + }, + { + "type": "SwitchCase", + "test": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 21, + "column": 10 + }, + "end": { + "line": 21, + "column": 11 + } + } + }, + "consequent": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "*=", + "left": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 21, + "column": 15 + } + } + }, + "right": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 21, + "column": 19 + }, + "end": { + "line": 21, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 21, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 21, + "column": 21 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 21 + } + } + }, + { + "type": "SwitchCase", + "test": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 22, + "column": 10 + }, + "end": { + "line": 22, + "column": 11 + } + } + }, + "consequent": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "-=", + "left": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 22, + "column": 14 + }, + "end": { + "line": 22, + "column": 15 + } + } + }, + "right": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 22, + "column": 19 + }, + "end": { + "line": 22, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 14 + }, + "end": { + "line": 22, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 14 + }, + "end": { + "line": 22, + "column": 21 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 21 + } + } + }, + { + "type": "SwitchCase", + "test": { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 23, + "column": 10 + }, + "end": { + "line": 23, + "column": 11 + } + } + }, + "consequent": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 23, + "column": 14 + }, + "end": { + "line": 23, + "column": 15 + } + } + }, + "right": { + "type": "UnaryExpression", + "operator": "+", + "prefix": true, + "argument": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 23, + "column": 19 + }, + "end": { + "line": 23, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 18 + }, + "end": { + "line": 23, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 14 + }, + "end": { + "line": 23, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 14 + }, + "end": { + "line": 23, + "column": 21 + } + } + } + ], + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 21 + } + } + }, + { + "type": "SwitchCase", + "test": { + "type": "NumberLiteral", + "value": 4, + "loc": { + "start": { + "line": 24, + "column": 10 + }, + "end": { + "line": 24, + "column": 11 + } + } + }, + "consequent": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 24, + "column": 14 + }, + "end": { + "line": 24, + "column": 15 + } + } + }, + "right": { + "type": "UnaryExpression", + "operator": "-", + "prefix": true, + "argument": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 24, + "column": 19 + }, + "end": { + "line": 24, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 18 + }, + "end": { + "line": 24, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 14 + }, + "end": { + "line": 24, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 14 + }, + "end": { + "line": 24, + "column": 21 + } + } + } + ], + "loc": { + "start": { + "line": 24, + "column": 5 + }, + "end": { + "line": 24, + "column": 21 + } + } + }, + { + "type": "SwitchCase", + "test": { + "type": "NumberLiteral", + "value": 5, + "loc": { + "start": { + "line": 25, + "column": 10 + }, + "end": { + "line": 25, + "column": 11 + } + } + }, + "consequent": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "UpdateExpression", + "operator": "++", + "prefix": false, + "argument": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 25, + "column": 14 + }, + "end": { + "line": 25, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 14 + }, + "end": { + "line": 25, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 14 + }, + "end": { + "line": 25, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 25, + "column": 5 + }, + "end": { + "line": 25, + "column": 18 + } + } + }, + { + "type": "SwitchCase", + "test": { + "type": "NumberLiteral", + "value": 6, + "loc": { + "start": { + "line": 26, + "column": 10 + }, + "end": { + "line": 26, + "column": 11 + } + } + }, + "consequent": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "UpdateExpression", + "operator": "++", + "prefix": true, + "argument": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 26, + "column": 16 + }, + "end": { + "line": 26, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 14 + }, + "end": { + "line": 26, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 14 + }, + "end": { + "line": 26, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 26, + "column": 5 + }, + "end": { + "line": 26, + "column": 18 + } + } + }, + { + "type": "SwitchCase", + "test": { + "type": "NumberLiteral", + "value": 7, + "loc": { + "start": { + "line": 27, + "column": 10 + }, + "end": { + "line": 27, + "column": 11 + } + } + }, + "consequent": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "UpdateExpression", + "operator": "--", + "prefix": false, + "argument": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 27, + "column": 14 + }, + "end": { + "line": 27, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 14 + }, + "end": { + "line": 27, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 14 + }, + "end": { + "line": 27, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 27, + "column": 5 + }, + "end": { + "line": 27, + "column": 18 + } + } + }, + { + "type": "SwitchCase", + "test": { + "type": "NumberLiteral", + "value": 8, + "loc": { + "start": { + "line": 28, + "column": 10 + }, + "end": { + "line": 28, + "column": 11 + } + } + }, + "consequent": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "UpdateExpression", + "operator": "--", + "prefix": true, + "argument": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 28, + "column": 16 + }, + "end": { + "line": 28, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 14 + }, + "end": { + "line": 28, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 14 + }, + "end": { + "line": 28, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 28, + "column": 5 + }, + "end": { + "line": 28, + "column": 18 + } + } + }, + { + "type": "SwitchCase", + "test": null, + "consequent": [ + { + "type": "BreakStatement", + "label": null, + "loc": { + "start": { + "line": 29, + "column": 14 + }, + "end": { + "line": 29, + "column": 20 + } + } + } + ], + "loc": { + "start": { + "line": 29, + "column": 5 + }, + "end": { + "line": 29, + "column": 20 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 30, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 31, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/ts/type_checker/expression_2.ts b/es2panda/test/parser/ts/type_checker/expression_2.ts new file mode 100644 index 0000000000..bd8ce6f867 --- /dev/null +++ b/es2panda/test/parser/ts/type_checker/expression_2.ts @@ -0,0 +1,30 @@ +/* + * 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 c = 1; + +switch (c) { + case 0 : c += c; + case 1 : c *= c; + case 2 : c -= c; + case 3 : c = +c; + case 4 : c = -c; + case 5 : c++; + case 6 : ++c; + case 7 : c--; + case 8 : --c; + default: break; +} diff --git a/es2panda/test/parser/ts/type_checker/expression_3-expected.txt b/es2panda/test/parser/ts/type_checker/expression_3-expected.txt new file mode 100644 index 0000000000..91fb8015ca --- /dev/null +++ b/es2panda/test/parser/ts/type_checker/expression_3-expected.txt @@ -0,0 +1,937 @@ +{ + "type": "Program", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 6 + } + } + }, + "init": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 10 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 11 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "+=", + "left": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "right": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 19, + "column": 6 + }, + "end": { + "line": 19, + "column": 7 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 19, + "column": 7 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 19, + "column": 8 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "*=", + "left": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "right": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 20, + "column": 6 + }, + "end": { + "line": 20, + "column": 7 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 20, + "column": 7 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 20, + "column": 8 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "-=", + "left": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "right": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 21, + "column": 6 + }, + "end": { + "line": 21, + "column": 7 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 21, + "column": 7 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 21, + "column": 8 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 22, + "column": 1 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "right": { + "type": "UnaryExpression", + "operator": "+", + "prefix": true, + "argument": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 22, + "column": 6 + }, + "end": { + "line": 22, + "column": 7 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 7 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 1 + }, + "end": { + "line": 22, + "column": 7 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 1 + }, + "end": { + "line": 22, + "column": 8 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 23, + "column": 1 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "right": { + "type": "UnaryExpression", + "operator": "-", + "prefix": true, + "argument": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 23, + "column": 6 + }, + "end": { + "line": 23, + "column": 7 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 7 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 1 + }, + "end": { + "line": 23, + "column": 7 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 1 + }, + "end": { + "line": 23, + "column": 8 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "UpdateExpression", + "operator": "++", + "prefix": false, + "argument": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 24, + "column": 1 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 1 + }, + "end": { + "line": 24, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 1 + }, + "end": { + "line": 24, + "column": 5 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "UpdateExpression", + "operator": "++", + "prefix": true, + "argument": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 25, + "column": 3 + }, + "end": { + "line": 25, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 1 + }, + "end": { + "line": 25, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 1 + }, + "end": { + "line": 25, + "column": 5 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "UpdateExpression", + "operator": "--", + "prefix": false, + "argument": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 26, + "column": 1 + }, + "end": { + "line": 26, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 1 + }, + "end": { + "line": 26, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 1 + }, + "end": { + "line": 26, + "column": 5 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "UpdateExpression", + "operator": "--", + "prefix": true, + "argument": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 27, + "column": 3 + }, + "end": { + "line": 27, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 1 + }, + "end": { + "line": 27, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 1 + }, + "end": { + "line": 27, + "column": 5 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "b", + "loc": { + "start": { + "line": 29, + "column": 5 + }, + "end": { + "line": 29, + "column": 6 + } + } + }, + "init": { + "type": "BinaryExpression", + "operator": "!=", + "left": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 29, + "column": 9 + }, + "end": { + "line": 29, + "column": 10 + } + } + }, + "right": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 29, + "column": 14 + }, + "end": { + "line": 29, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 9 + }, + "end": { + "line": 29, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 5 + }, + "end": { + "line": 29, + "column": 15 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 29, + "column": 1 + }, + "end": { + "line": 29, + "column": 16 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 30, + "column": 5 + }, + "end": { + "line": 30, + "column": 6 + } + } + }, + "init": { + "type": "BinaryExpression", + "operator": "|", + "left": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 30, + "column": 9 + }, + "end": { + "line": 30, + "column": 10 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 30, + "column": 13 + }, + "end": { + "line": 30, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 9 + }, + "end": { + "line": 30, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 5 + }, + "end": { + "line": 30, + "column": 14 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 30, + "column": 1 + }, + "end": { + "line": 30, + "column": 15 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 31, + "column": 1 + }, + "end": { + "line": 31, + "column": 2 + } + } + }, + "right": { + "type": "BinaryExpression", + "operator": "&", + "left": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 31, + "column": 5 + }, + "end": { + "line": 31, + "column": 6 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 31, + "column": 9 + }, + "end": { + "line": 31, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 5 + }, + "end": { + "line": 31, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 1 + }, + "end": { + "line": 31, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 1 + }, + "end": { + "line": 31, + "column": 11 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "b", + "loc": { + "start": { + "line": 32, + "column": 1 + }, + "end": { + "line": 32, + "column": 2 + } + } + }, + "right": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 32, + "column": 5 + }, + "end": { + "line": 32, + "column": 6 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 5, + "loc": { + "start": { + "line": 32, + "column": 10 + }, + "end": { + "line": 32, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 5 + }, + "end": { + "line": 32, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 1 + }, + "end": { + "line": 32, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 1 + }, + "end": { + "line": 32, + "column": 12 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "b", + "loc": { + "start": { + "line": 34, + "column": 1 + }, + "end": { + "line": 34, + "column": 2 + } + } + }, + "right": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 34, + "column": 5 + }, + "end": { + "line": 34, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 34, + "column": 1 + }, + "end": { + "line": 34, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 34, + "column": 1 + }, + "end": { + "line": 34, + "column": 7 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 35, + "column": 1 + } + } +} +TypeError: Type 'number' is not assignable to type 'boolean'. [expression_3.ts:34:1] diff --git a/es2panda/test/parser/ts/type_checker/expression_3.ts b/es2panda/test/parser/ts/type_checker/expression_3.ts new file mode 100644 index 0000000000..e73e72ea39 --- /dev/null +++ b/es2panda/test/parser/ts/type_checker/expression_3.ts @@ -0,0 +1,34 @@ +/* + * 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 = 1; + +a += a; +a *= a; +a -= a; +a = +a; +a = -a; +a++; +++a; +a--; +--a; + +var b = a != a; +var c = 1 | 2; +c = c & 3; +b = c == 5; + +b = c; diff --git a/es2panda/test/parser/ts/type_checker/expression_4-expected.txt b/es2panda/test/parser/ts/type_checker/expression_4-expected.txt new file mode 100644 index 0000000000..1e38e3c01f --- /dev/null +++ b/es2panda/test/parser/ts/type_checker/expression_4-expected.txt @@ -0,0 +1,887 @@ +{ + "type": "Program", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + "init": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 10 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 11 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "b", + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + "init": { + "type": "ObjectExpression", + "properties": [ + { + "type": "Property", + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 6 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 21, + "column": 8 + }, + "end": { + "line": 21, + "column": 9 + } + } + }, + "kind": "init", + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 9 + } + } + }, + { + "type": "Property", + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "name": "b", + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 6 + } + } + }, + "value": { + "type": "StringLiteral", + "value": "str", + "loc": { + "start": { + "line": 22, + "column": 8 + }, + "end": { + "line": 22, + "column": 13 + } + } + }, + "kind": "init", + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 13 + } + } + }, + { + "type": "Property", + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 6 + } + } + }, + "value": { + "type": "TSAsExpression", + "expression": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 23, + "column": 8 + }, + "end": { + "line": 23, + "column": 9 + } + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 23, + "column": 13 + }, + "end": { + "line": 23, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 8 + }, + "end": { + "line": 23, + "column": 20 + } + } + }, + "kind": "init", + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 20 + } + } + }, + { + "type": "Property", + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "name": "d", + "loc": { + "start": { + "line": 24, + "column": 5 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + "value": { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 24, + "column": 23 + }, + "end": { + "line": 24, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 16 + }, + "end": { + "line": 24, + "column": 24 + } + } + } + ], + "loc": { + "start": { + "line": 24, + "column": 14 + }, + "end": { + "line": 24, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 8 + }, + "end": { + "line": 24, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 8 + }, + "end": { + "line": 24, + "column": 26 + } + } + }, + "kind": "init", + "loc": { + "start": { + "line": 24, + "column": 5 + }, + "end": { + "line": 24, + "column": 26 + } + } + }, + { + "type": "Property", + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "name": "e", + "loc": { + "start": { + "line": 25, + "column": 5 + }, + "end": { + "line": 25, + "column": 6 + } + } + }, + "value": { + "type": "ObjectExpression", + "properties": [ + { + "type": "Property", + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 25, + "column": 10 + }, + "end": { + "line": 25, + "column": 11 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 25, + "column": 13 + }, + "end": { + "line": 25, + "column": 14 + } + } + }, + "kind": "init", + "loc": { + "start": { + "line": 25, + "column": 10 + }, + "end": { + "line": 25, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 25, + "column": 8 + }, + "end": { + "line": 25, + "column": 16 + } + } + }, + "kind": "init", + "loc": { + "start": { + "line": 25, + "column": 5 + }, + "end": { + "line": 25, + "column": 16 + } + } + }, + { + "type": "Property", + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "name": "f", + "loc": { + "start": { + "line": 26, + "column": 5 + }, + "end": { + "line": 26, + "column": 6 + } + } + }, + "value": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 26, + "column": 8 + }, + "end": { + "line": 26, + "column": 9 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 26, + "column": 12 + }, + "end": { + "line": 26, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 8 + }, + "end": { + "line": 26, + "column": 13 + } + } + }, + "kind": "init", + "loc": { + "start": { + "line": 26, + "column": 5 + }, + "end": { + "line": 26, + "column": 13 + } + } + }, + { + "type": "Property", + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "name": "g", + "loc": { + "start": { + "line": 27, + "column": 5 + }, + "end": { + "line": 27, + "column": 6 + } + } + }, + "value": { + "type": "ArrayExpression", + "elements": [ + { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 27, + "column": 9 + }, + "end": { + "line": 27, + "column": 10 + } + } + }, + { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 27, + "column": 11 + }, + "end": { + "line": 27, + "column": 12 + } + } + } + ], + "loc": { + "start": { + "line": 27, + "column": 8 + }, + "end": { + "line": 27, + "column": 13 + } + } + }, + "kind": "init", + "loc": { + "start": { + "line": 27, + "column": 5 + }, + "end": { + "line": 27, + "column": 13 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 28, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 28, + "column": 2 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 28, + "column": 2 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 30, + "column": 1 + }, + "end": { + "line": 30, + "column": 2 + } + } + }, + "right": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "b", + "loc": { + "start": { + "line": 30, + "column": 5 + }, + "end": { + "line": 30, + "column": 6 + } + } + }, + "property": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 30, + "column": 7 + }, + "end": { + "line": 30, + "column": 8 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 30, + "column": 5 + }, + "end": { + "line": 30, + "column": 8 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 1 + }, + "end": { + "line": 30, + "column": 8 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 1 + }, + "end": { + "line": 30, + "column": 9 + } + } + }, + { + "type": "ExportNamedDeclaration", + "declaration": { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "g", + "typeAnnotation": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 32, + "column": 16 + }, + "end": { + "line": 32, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 12 + }, + "end": { + "line": 32, + "column": 13 + } + } + }, + "init": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 32, + "column": 25 + }, + "end": { + "line": 32, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 12 + }, + "end": { + "line": 32, + "column": 26 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 32, + "column": 8 + }, + "end": { + "line": 32, + "column": 27 + } + } + }, + "source": null, + "specifiers": [], + "loc": { + "start": { + "line": 32, + "column": 1 + }, + "end": { + "line": 32, + "column": 27 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "g", + "loc": { + "start": { + "line": 33, + "column": 1 + }, + "end": { + "line": 33, + "column": 2 + } + } + }, + "right": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "b", + "loc": { + "start": { + "line": 33, + "column": 5 + }, + "end": { + "line": 33, + "column": 6 + } + } + }, + "property": { + "type": "Identifier", + "name": "b", + "loc": { + "start": { + "line": 33, + "column": 7 + }, + "end": { + "line": 33, + "column": 8 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 33, + "column": 5 + }, + "end": { + "line": 33, + "column": 8 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 1 + }, + "end": { + "line": 33, + "column": 8 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 1 + }, + "end": { + "line": 33, + "column": 9 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 34, + "column": 1 + } + } +} +TypeError: Type 'string' is not assignable to type 'number'. [expression_4.ts:33:1] diff --git a/es2panda/test/parser/ts/type_checker/expression_4.ts b/es2panda/test/parser/ts/type_checker/expression_4.ts new file mode 100644 index 0000000000..614dd9f5db --- /dev/null +++ b/es2panda/test/parser/ts/type_checker/expression_4.ts @@ -0,0 +1,33 @@ +/* + * 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 = 1; + +var b = { + a: 1, + b: "str", + c: 1 as number, + d: () => { return 1 }, + e: { a: 1 }, + f: a = 1, + g: [1,2] +} + +a = b.a; + +export var g : number = 1; +g = b.b; -- Gitee