diff --git a/es2panda/binder/declaration.h b/es2panda/binder/declaration.h index 757eb2e7f598946e7bf3c47825d51fd0b78fc3cc..8126d0f14ab176ae08da91486c3510abd093251f 100644 --- a/es2panda/binder/declaration.h +++ b/es2panda/binder/declaration.h @@ -110,6 +110,11 @@ public: return HasFlag(DeclarationFlags::IMPORT | DeclarationFlags::EXPORT); } + bool IsExportDeclInTsModule() const + { + return HasFlag(DeclarationFlags::EXPORT_IN_TSMODULE); + } + void SetDeclare(bool isDeclare) { isDeclare_ = isDeclare; diff --git a/es2panda/binder/variableFlags.h b/es2panda/binder/variableFlags.h index 4fca36514c29252fe6d1f3038c301ca3df342aeb..d870bca045a6af04201abb9707f6f0966b94b042 100644 --- a/es2panda/binder/variableFlags.h +++ b/es2panda/binder/variableFlags.h @@ -162,6 +162,7 @@ enum class DeclarationFlags { IMPORT = 1 << 0, EXPORT = 1 << 1, NAMESPACE_IMPORT = 1 << 2, + EXPORT_IN_TSMODULE = 1 << 3, }; DEFINE_BITOPS(DeclarationFlags) diff --git a/es2panda/lexer/token/tokenType.h b/es2panda/lexer/token/tokenType.h index 1c076bf81b82ce05a705afcc5aac829580a6a89a..019744e2c690af1c4c0fd6afccc5e26e0dac2ddc 100644 --- a/es2panda/lexer/token/tokenType.h +++ b/es2panda/lexer/token/tokenType.h @@ -162,11 +162,11 @@ enum class TokenType { KEYW_GLOBAL, KEYW_INFER, KEYW_DECLARE, + KEYW_IS, + KEYW_ASSERTS, KEYW_ARGUMENTS, KEYW_EVAL, KEYW_STATIC, - KEYW_IS, - KEYW_ASSERTS, /* strict mode future reserved keywords */ KEYW_PRIVATE, diff --git a/es2panda/parser/expressionParser.cpp b/es2panda/parser/expressionParser.cpp index 1e2bb8c8d05841ff928df061da54a745f88d5d31..1cff021a8e01808f1f3d94123ea1866399bb4f1f 100644 --- a/es2panda/parser/expressionParser.cpp +++ b/es2panda/parser/expressionParser.cpp @@ -1369,10 +1369,13 @@ ir::Expression *ParserImpl::ParseOptionalChain(ir::Expression *leftSideExpr) } ir::ArrowFunctionExpression *ParserImpl::ParsePotentialArrowExpression(ir::Expression **returnExpression, - const lexer::SourcePosition &startLoc) + const lexer::SourcePosition &startLoc, + bool ignoreCallExpression) { ir::TSTypeParameterDeclaration *typeParamDecl = nullptr; + const auto savedPos = lexer_->Save(); + switch (lexer_->GetToken().Type()) { case lexer::TokenType::KEYW_FUNCTION: { *returnExpression = ParseFunctionExpression(ParserStatus::ASYNC_FUNCTION); @@ -1410,8 +1413,6 @@ ir::ArrowFunctionExpression *ParserImpl::ParsePotentialArrowExpression(ir::Expre return nullptr; } - const auto savedPos = lexer_->Save(); - typeParamDecl = ParseTsTypeParameterDeclaration(false); if (!typeParamDecl) { lexer_->Rewind(savedPos); @@ -1425,6 +1426,10 @@ ir::ArrowFunctionExpression *ParserImpl::ParsePotentialArrowExpression(ir::Expre [[fallthrough]]; } case lexer::TokenType::PUNCTUATOR_LEFT_PARENTHESIS: { + if (ignoreCallExpression) { + lexer_->Rewind(savedPos); + break; + } ir::CallExpression *callExpression = ParseCallExpression(*returnExpression, false, true); ir::Expression *returnTypeAnnotation = nullptr; @@ -1670,7 +1675,8 @@ ir::Expression *ParserImpl::ParseMemberExpression(bool ignoreCallExpression, Exp } if (isAsync && !lexer_->GetToken().NewLine()) { - ir::ArrowFunctionExpression *arrow = ParsePotentialArrowExpression(&returnExpression, startLoc); + ir::ArrowFunctionExpression *arrow = ParsePotentialArrowExpression(&returnExpression, startLoc, + ignoreCallExpression); if (arrow) { return arrow; diff --git a/es2panda/parser/parserFlags.h b/es2panda/parser/parserFlags.h index 630c26c77c5a128c186e81968024e59a93b37e17..ba98d47feba56a88ac66d59698527565686ba92d 100644 --- a/es2panda/parser/parserFlags.h +++ b/es2panda/parser/parserFlags.h @@ -41,6 +41,7 @@ enum class VariableParsingFlags { STOP_AT_IN = (1 << 6), EXPORTED = (1 << 7), IN_FOR = (1 << 8), + EXPORTED_IN_TSMODULE = (1 << 9), }; DEFINE_BITOPS(VariableParsingFlags) diff --git a/es2panda/parser/parserImpl.cpp b/es2panda/parser/parserImpl.cpp index ee42125108bdb40a96a55f358650cfe89e2e4e37..9f60a3e509210f16e888142c903a5cbfbb35f147 100644 --- a/es2panda/parser/parserImpl.cpp +++ b/es2panda/parser/parserImpl.cpp @@ -2134,8 +2134,9 @@ void ParserImpl::ParseClassKeyModifiers(ClassElmentDescriptor *desc) char32_t nextCp = lexer_->Lookahead(); if ((Extension() == ScriptExtension::JS && nextCp != LEX_CHAR_LEFT_PAREN) || - (Extension() == ScriptExtension::TS && nextCp != LEX_CHAR_EQUALS && nextCp != LEX_CHAR_SEMICOLON && - nextCp != LEX_CHAR_LEFT_PAREN && nextCp != LEX_CHAR_LESS_THAN)) { + (Extension() == ScriptExtension::TS && + nextCp != LEX_CHAR_EQUALS && nextCp != LEX_CHAR_SEMICOLON && nextCp != LEX_CHAR_LEFT_PAREN && + nextCp != LEX_CHAR_LESS_THAN && nextCp != LEX_CHAR_QUESTION && nextCp != LEX_CHAR_COLON)) { if (lexer_->GetToken().KeywordType() == lexer::TokenType::KEYW_GET) { if (desc->isPrivateIdent) { ThrowSyntaxError("Private identifier can not be getter"); diff --git a/es2panda/parser/parserImpl.h b/es2panda/parser/parserImpl.h index d71abfaa5179c1c8dbc9be06dcb05d2b65180da9..6d50ea02a0179b2fbb594c756c24de61bd24a834 100644 --- a/es2panda/parser/parserImpl.h +++ b/es2panda/parser/parserImpl.h @@ -370,7 +370,8 @@ private: ir::Expression *ParseBinaryExpression(ir::Expression *left); ir::CallExpression *ParseCallExpression(ir::Expression *callee, bool isOptionalChain = false, bool isAsync = false); ir::ArrowFunctionExpression *ParsePotentialArrowExpression(ir::Expression **returnExpression, - const lexer::SourcePosition &startLoc); + const lexer::SourcePosition &startLoc, + bool ignoreCallExpression); void ValidateUpdateExpression(ir::Expression *returnExpression, bool isChainExpression); bool ParsePotentialTsGenericFunctionCall(ir::Expression **returnExpression, const lexer::SourcePosition &startLoc, diff --git a/es2panda/parser/statementParser.cpp b/es2panda/parser/statementParser.cpp index 52d7cf6cf8909bad440d1daaadf3f20edce5ffe5..9fc6077eb26c79d53fcf3552a57d5c5a6ba2a426 100644 --- a/es2panda/parser/statementParser.cpp +++ b/es2panda/parser/statementParser.cpp @@ -2052,6 +2052,9 @@ ir::VariableDeclarator *ParserImpl::ParseVariableDeclarator(VariableParsingFlags binder::Decl *decl = nullptr; binder::DeclarationFlags declflag = (flags & VariableParsingFlags::EXPORTED) ? binder::DeclarationFlags::EXPORT : binder::DeclarationFlags::NONE; + if (flags & VariableParsingFlags::EXPORTED_IN_TSMODULE) { + declflag |= binder::DeclarationFlags::EXPORT_IN_TSMODULE; + } if (flags & VariableParsingFlags::VAR) { decl = Binder()->AddDecl(startLoc, declflag, isDeclare, binding->Name()); @@ -2552,7 +2555,8 @@ ir::ExportNamedDeclaration *ParserImpl::ParseNamedExportDeclaration(const lexer: ThrowSyntaxError("Decorators are not valid here.", decorators.front()->Start()); } - VariableParsingFlags flag = isTsModule ? VariableParsingFlags::NO_OPTS : VariableParsingFlags::EXPORTED; + VariableParsingFlags flag = isTsModule ? + VariableParsingFlags::EXPORTED_IN_TSMODULE : VariableParsingFlags::EXPORTED; ParserStatus status = isTsModule ? ParserStatus::NO_OPTS : ParserStatus::EXPORT_REACHED; switch (lexer_->GetToken().Type()) { diff --git a/es2panda/parser/transformer/transformer.cpp b/es2panda/parser/transformer/transformer.cpp index f381314e962cb0a0dcead6dbb99c9fbae1ba7dc1..a5d5f46ba31aaaab5f0d40bbbbee34a6d11a1759 100644 --- a/es2panda/parser/transformer/transformer.cpp +++ b/es2panda/parser/transformer/transformer.cpp @@ -165,13 +165,13 @@ binder::Scope *Transformer::FindExportVariableInTsModuleScope(util::StringView n bool isExport = false; auto currentScope = Scope(); while (currentScope != nullptr) { - binder::Variable *v = currentScope->FindLocal(name, binder::ResolveBindingOptions::ALL); + binder::Variable *v = currentScope->FindLocal(name, binder::ResolveBindingOptions::BINDINGS); bool isTSModuleScope = currentScope->IsTSModuleScope(); if (v != nullptr) { if (!v->Declaration()->IsVarDecl() && !v->Declaration()->IsLetDecl() && !v->Declaration()->IsConstDecl()) { break; } - if (isTSModuleScope && currentScope->AsTSModuleScope()->FindExportVariable(name)) { + if (isTSModuleScope && v->Declaration()->IsExportDeclInTsModule()) { isExport = true; } break; @@ -1369,7 +1369,7 @@ ir::UpdateNodes Transformer::VisitTsModuleDeclaration(ir::TSModuleDeclaration *n util::StringView name = GetNameFromModuleDeclaration(node); - auto findRes = Scope()->FindLocal(name, binder::ResolveBindingOptions::ALL); + auto findRes = Scope()->FindLocal(name, binder::ResolveBindingOptions::BINDINGS); if (findRes == nullptr) { res.push_back(CreateVariableDeclarationForTSEnumOrTSModule(name, node, isExport)); } diff --git a/es2panda/test/compiler/ts/cases/compiler/test-ts-namespace-20-expected.txt b/es2panda/test/compiler/ts/cases/compiler/test-ts-namespace-20-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..9ebf9fcfe832fb9a979666aa9da818ffc71bce37 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/compiler/test-ts-namespace-20-expected.txt @@ -0,0 +1,2 @@ +type +-1 diff --git a/es2panda/test/compiler/ts/cases/compiler/test-ts-namespace-20.ts b/es2panda/test/compiler/ts/cases/compiler/test-ts-namespace-20.ts new file mode 100644 index 0000000000000000000000000000000000000000..c1d0c1c8638951ea3785ec47832ec6dfc7d07ca2 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/compiler/test-ts-namespace-20.ts @@ -0,0 +1,25 @@ +/* + * 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. + */ + + +type ns = String; +namespace ns { + export var ns: number = -1; +} + +let e0: ns = 'type'; + +print(e0); +print(ns.ns); diff --git a/es2panda/test/compiler/ts/cases/compiler/test-ts-namespace-21-expected.txt b/es2panda/test/compiler/ts/cases/compiler/test-ts-namespace-21-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..c7bb083e28cef86c191b34f964bca803cf233c49 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/compiler/test-ts-namespace-21-expected.txt @@ -0,0 +1,6 @@ +0 0 +0 0 +1 1 +0 0 +-1 -1 +0 0 diff --git a/es2panda/test/compiler/ts/cases/compiler/test-ts-namespace-21.ts b/es2panda/test/compiler/ts/cases/compiler/test-ts-namespace-21.ts new file mode 100644 index 0000000000000000000000000000000000000000..0db9d4b769094e4cf45e9862bfc30b9e4f025129 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/compiler/test-ts-namespace-21.ts @@ -0,0 +1,35 @@ +/* + * 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. + */ + + +var i: number = -1; + +namespace I { + export var i: number = 0; + print(i, 0); +} + +namespace I { + print(i, 0); +} + +namespace I { + var i: number = 1; + print(i, 1); + print(I.i, 0); +} + +print(i, -1); +print(I.i, 0); diff --git a/es2panda/test/compiler/ts/cases/compiler/test-ts-namespace-22-expected.txt b/es2panda/test/compiler/ts/cases/compiler/test-ts-namespace-22-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..d00491fd7e5bb6fa28c517a0bb32b8b506539d4d --- /dev/null +++ b/es2panda/test/compiler/ts/cases/compiler/test-ts-namespace-22-expected.txt @@ -0,0 +1 @@ +1 diff --git a/es2panda/test/compiler/ts/cases/compiler/test-ts-namespace-22.ts b/es2panda/test/compiler/ts/cases/compiler/test-ts-namespace-22.ts new file mode 100644 index 0000000000000000000000000000000000000000..7d52a47d3b31d4fbd23f42c4f11780ffbb73061c --- /dev/null +++ b/es2panda/test/compiler/ts/cases/compiler/test-ts-namespace-22.ts @@ -0,0 +1,24 @@ +/* + * 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. + */ + + +namespace ns { + export var a = 1; +} + +namespace ns { + type a = number; + print(a); +} diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-class-name-1-expected.txt b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-class-name-1-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..be6662c536ccfa8245b80fc388db1d5e2661e48a --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-class-name-1-expected.txt @@ -0,0 +1 @@ +Class_async diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-class-name-1.ts b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-class-name-1.ts new file mode 100644 index 0000000000000000000000000000000000000000..b0bfd1ff272b6b8d970575bac69e1418a4ddae48 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-class-name-1.ts @@ -0,0 +1,27 @@ +/* + * 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. + */ + + +class async { + name: string; + constructor(name: string) { + this.name = name; + } + getName(): string { + return this.name; + } +} +var obj = new async("Class_async"); +print(obj.getName()); diff --git a/es2panda/test/parser/ts/test-class-definition27-expected.txt b/es2panda/test/parser/ts/test-class-definition27-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..e79a84e69847d798d93f04c2ec5d2030dbe89631 --- /dev/null +++ b/es2panda/test/parser/ts/test-class-definition27-expected.txt @@ -0,0 +1,695 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "async", + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "column": 12 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 15 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 15 + } + } + }, + "superClass": null, + "implements": [], + "constructor": { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "name", + "typeAnnotation": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "T", + "loc": { + "start": { + "line": 19, + "column": 23 + }, + "end": { + "line": 19, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 23 + }, + "end": { + "line": 19, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 21 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 20, + "column": 8 + }, + "end": { + "line": 20, + "column": 12 + } + } + }, + "property": { + "type": "Identifier", + "name": "name", + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 20, + "column": 17 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 20, + "column": 8 + }, + "end": { + "line": 20, + "column": 17 + } + } + }, + "right": { + "type": "Identifier", + "name": "name", + "loc": { + "start": { + "line": 20, + "column": 20 + }, + "end": { + "line": 20, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 8 + }, + "end": { + "line": 20, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 8 + }, + "end": { + "line": 20, + "column": 25 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 26 + }, + "end": { + "line": 21, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 16 + }, + "end": { + "line": 21, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 16 + }, + "end": { + "line": 21, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 21, + "column": 6 + } + } + }, + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "name", + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 9 + } + } + }, + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "T", + "loc": { + "start": { + "line": 18, + "column": 11 + }, + "end": { + "line": 18, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 11 + }, + "end": { + "line": 18, + "column": 12 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 9 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "getName", + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 12 + } + } + }, + "kind": "method", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "T", + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 22, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 22, + "column": 17 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 23, + "column": 15 + }, + "end": { + "line": 23, + "column": 19 + } + } + }, + "property": { + "type": "Identifier", + "name": "name", + "loc": { + "start": { + "line": 23, + "column": 20 + }, + "end": { + "line": 23, + "column": 24 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 23, + "column": 15 + }, + "end": { + "line": 23, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 8 + }, + "end": { + "line": 23, + "column": 25 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 18 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 12 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 12 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 24, + "column": 6 + } + } + } + ], + "indexSignatures": [], + "loc": { + "start": { + "line": 17, + "column": 16 + }, + "end": { + "line": 25, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 25, + "column": 2 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "xxx", + "loc": { + "start": { + "line": 27, + "column": 6 + }, + "end": { + "line": 27, + "column": 9 + } + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "loc": { + "start": { + "line": 27, + "column": 12 + }, + "end": { + "line": 27, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 1 + }, + "end": { + "line": 27, + "column": 19 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "obj", + "loc": { + "start": { + "line": 28, + "column": 5 + }, + "end": { + "line": 28, + "column": 8 + } + } + }, + "init": { + "type": "NewExpression", + "callee": { + "type": "Identifier", + "name": "async", + "loc": { + "start": { + "line": 28, + "column": 15 + }, + "end": { + "line": 28, + "column": 20 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "xxx", + "loc": { + "start": { + "line": 28, + "column": 21 + }, + "end": { + "line": 28, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 21 + }, + "end": { + "line": 28, + "column": 24 + } + } + } + ], + "loc": { + "start": { + "line": 28, + "column": 20 + }, + "end": { + "line": 28, + "column": 25 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "Class_async", + "loc": { + "start": { + "line": 28, + "column": 26 + }, + "end": { + "line": 28, + "column": 39 + } + } + } + ], + "loc": { + "start": { + "line": 28, + "column": 11 + }, + "end": { + "line": 28, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 5 + }, + "end": { + "line": 28, + "column": 40 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 28, + "column": 1 + }, + "end": { + "line": 28, + "column": 41 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 29, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/ts/test-class-definition27.ts b/es2panda/test/parser/ts/test-class-definition27.ts new file mode 100644 index 0000000000000000000000000000000000000000..0369612ab8419f54e62ffe177342579c287c1a63 --- /dev/null +++ b/es2panda/test/parser/ts/test-class-definition27.ts @@ -0,0 +1,28 @@ +/* + * 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. + */ + + +class async { + name: T; + constructor(name: T) { + this.name = name; + } + getName(): T { + return this.name; + } +} + +type xxx = string; +var obj = new async("Class_async"); diff --git a/es2panda/test/parser/ts/test-class-method-optional-expected.txt b/es2panda/test/parser/ts/test-class-method-optional-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..0a319a0729f10e6f20cf1a95764ad27911b01dff --- /dev/null +++ b/es2panda/test/parser/ts/test-class-method-optional-expected.txt @@ -0,0 +1,321 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "column": 8 + } + } + }, + "superClass": null, + "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": [], + "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": 17, + "column": 1 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 0, + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 18 + } + } + }, + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "end": { + "line": 18, + "column": 14 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 18 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "b", + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "static": false, + "readonly": false, + "declare": false, + "optional": true, + "computed": false, + "typeAnnotation": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 15 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "get", + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 8 + } + } + }, + "kind": "method", + "static": false, + "optional": true, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 20, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 20 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 20 + } + } + } + ], + "indexSignatures": [], + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "decorators": [], + "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-class-method-optional.ts b/es2panda/test/parser/ts/test-class-method-optional.ts new file mode 100644 index 0000000000000000000000000000000000000000..f722db603c2914ca35c4760a418ae01a2c8bd65f --- /dev/null +++ b/es2panda/test/parser/ts/test-class-method-optional.ts @@ -0,0 +1,21 @@ +/* + * 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. + */ + + +class A { + a: number = 0; + b?: number; + get?(): number; +} diff --git a/es2panda/test/parser/ts/test-class-property-1-expected.txt b/es2panda/test/parser/ts/test-class-property-1-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..ae534760220a4bb2a72129e8eb0492a7c091760e --- /dev/null +++ b/es2panda/test/parser/ts/test-class-property-1-expected.txt @@ -0,0 +1,258 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "column": 8 + } + } + }, + "superClass": null, + "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": [], + "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": 17, + "column": 1 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "set", + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 8 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 0, + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 16 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "get", + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 8 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 19, + "column": 20 + }, + "end": { + "line": 19, + "column": 21 + } + } + }, + "static": false, + "readonly": false, + "declare": false, + "optional": true, + "computed": false, + "typeAnnotation": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 19, + "column": 11 + }, + "end": { + "line": 19, + "column": 17 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 21 + } + } + } + ], + "indexSignatures": [], + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 20, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 21, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/ts/test-class-property-1.ts b/es2panda/test/parser/ts/test-class-property-1.ts new file mode 100644 index 0000000000000000000000000000000000000000..8b8c1844df0c4205b87a919553c308c6191cd909 --- /dev/null +++ b/es2panda/test/parser/ts/test-class-property-1.ts @@ -0,0 +1,20 @@ +/* + * 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. + */ + + +class A { + set: number = 0; + get?: number = 1; +} diff --git a/es2panda/test/parser/ts/test-function-name-1-expected.txt b/es2panda/test/parser/ts/test-function-name-1-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..0b4e7c5b9973fff421d41c3c49178a3b47593b28 --- /dev/null +++ b/es2panda/test/parser/ts/test-function-name-1-expected.txt @@ -0,0 +1,213 @@ +{ + "type": "Program", + "statements": [ + { + "type": "FunctionDeclaration", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "is", + "loc": { + "start": { + "line": 17, + "column": 10 + }, + "end": { + "line": 17, + "column": 12 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSStringKeyword", + "loc": { + "start": { + "line": 17, + "column": 16 + }, + "end": { + "line": 17, + "column": 22 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "StringLiteral", + "value": "is", + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 23 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + { + "type": "FunctionDeclaration", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "asserts", + "loc": { + "start": { + "line": 21, + "column": 10 + }, + "end": { + "line": 21, + "column": 17 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSStringKeyword", + "loc": { + "start": { + "line": 21, + "column": 21 + }, + "end": { + "line": 21, + "column": 27 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "StringLiteral", + "value": "asserts", + "loc": { + "start": { + "line": 22, + "column": 11 + }, + "end": { + "line": 22, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 4 + }, + "end": { + "line": 22, + "column": 21 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 28 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 23, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 24, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/ts/test-function-name-1.ts b/es2panda/test/parser/ts/test-function-name-1.ts new file mode 100644 index 0000000000000000000000000000000000000000..879f52ec15805572c14f420738930e6c7140dbac --- /dev/null +++ b/es2panda/test/parser/ts/test-function-name-1.ts @@ -0,0 +1,23 @@ +/* + * 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. + */ + + +function is(): string { + return "is"; +} + +function asserts(): string { + return "asserts"; +} diff --git a/es2panda/test/parser/ts/test-keyword-identify9-expected.txt b/es2panda/test/parser/ts/test-keyword-identify9-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..1ab6e229fb60c1f7609993803f42f234e0e82cc1 --- /dev/null +++ b/es2panda/test/parser/ts/test-keyword-identify9-expected.txt @@ -0,0 +1,913 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "is", + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "column": 9 + } + } + }, + "superClass": null, + "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": [], + "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": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 13 + } + } + }, + "body": [], + "indexSignatures": [], + "loc": { + "start": { + "line": 17, + "column": 10 + }, + "end": { + "line": 17, + "column": 13 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 13 + } + } + }, + { + "type": "TSModuleDeclaration", + "id": { + "type": "Identifier", + "name": "is", + "loc": { + "start": { + "line": 19, + "column": 11 + }, + "end": { + "line": 19, + "column": 13 + } + } + }, + "body": { + "type": "TSModuleBlock", + "body": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "is", + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 11 + } + } + }, + "init": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 20, + "column": 14 + }, + "end": { + "line": 20, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 15 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 16 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "asserts", + "loc": { + "start": { + "line": 21, + "column": 9 + }, + "end": { + "line": 21, + "column": 16 + } + } + }, + "init": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 21, + "column": 19 + }, + "end": { + "line": 21, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 9 + }, + "end": { + "line": 21, + "column": 20 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 21 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "async", + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 22, + "column": 14 + } + } + }, + "init": { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 22, + "column": 17 + }, + "end": { + "line": 22, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 22, + "column": 18 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 19 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "declare": false, + "global": false, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 25, + "column": 6 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "asserts", + "loc": { + "start": { + "line": 25, + "column": 7 + }, + "end": { + "line": 25, + "column": 14 + } + } + }, + "superClass": null, + "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": [], + "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": 25, + "column": 1 + }, + "end": { + "line": 29, + "column": 2 + } + } + }, + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "is", + "loc": { + "start": { + "line": 26, + "column": 5 + }, + "end": { + "line": 26, + "column": 7 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 26, + "column": 10 + }, + "end": { + "line": 26, + "column": 11 + } + } + }, + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 5 + }, + "end": { + "line": 26, + "column": 11 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "asserts", + "loc": { + "start": { + "line": 27, + "column": 5 + }, + "end": { + "line": 27, + "column": 12 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 27, + "column": 15 + }, + "end": { + "line": 27, + "column": 16 + } + } + }, + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 5 + }, + "end": { + "line": 27, + "column": 16 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "async", + "loc": { + "start": { + "line": 28, + "column": 5 + }, + "end": { + "line": 28, + "column": 10 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 28, + "column": 13 + }, + "end": { + "line": 28, + "column": 14 + } + } + }, + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 5 + }, + "end": { + "line": 28, + "column": 14 + } + } + } + ], + "indexSignatures": [], + "loc": { + "start": { + "line": 25, + "column": 15 + }, + "end": { + "line": 29, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 1 + }, + "end": { + "line": 29, + "column": 2 + } + } + }, + { + "type": "FunctionDeclaration", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "async", + "loc": { + "start": { + "line": 31, + "column": 10 + }, + "end": { + "line": 31, + "column": 15 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 31, + "column": 18 + }, + "end": { + "line": 31, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 1 + }, + "end": { + "line": 31, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 1 + }, + "end": { + "line": 31, + "column": 21 + } + } + }, + { + "type": "FunctionDeclaration", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "assertsUse", + "loc": { + "start": { + "line": 33, + "column": 10 + }, + "end": { + "line": 33, + "column": 20 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "asserts", + "typeAnnotation": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "T", + "loc": { + "start": { + "line": 33, + "column": 33 + }, + "end": { + "line": 33, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 33 + }, + "end": { + "line": 33, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 24 + }, + "end": { + "line": 33, + "column": 31 + } + } + } + ], + "returnType": { + "type": "TSTypePredicate", + "parameterName": { + "type": "Identifier", + "name": "asserts", + "loc": { + "start": { + "line": 33, + "column": 45 + }, + "end": { + "line": 33, + "column": 52 + } + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "NonNullable", + "loc": { + "start": { + "line": 33, + "column": 56 + }, + "end": { + "line": 33, + "column": 67 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "T", + "loc": { + "start": { + "line": 33, + "column": 68 + }, + "end": { + "line": 33, + "column": 69 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 68 + }, + "end": { + "line": 33, + "column": 69 + } + } + } + ], + "loc": { + "start": { + "line": 33, + "column": 56 + }, + "end": { + "line": 33, + "column": 70 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 56 + }, + "end": { + "line": 33, + "column": 67 + } + } + }, + "asserts": true, + "loc": { + "start": { + "line": 33, + "column": 37 + }, + "end": { + "line": 33, + "column": 67 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "loc": { + "start": { + "line": 33, + "column": 21 + }, + "end": { + "line": 33, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 21 + }, + "end": { + "line": 33, + "column": 23 + } + } + } + ], + "loc": { + "start": { + "line": 33, + "column": 20 + }, + "end": { + "line": 33, + "column": 23 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 33, + "column": 71 + }, + "end": { + "line": 33, + "column": 74 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 1 + }, + "end": { + "line": 33, + "column": 74 + } + } + }, + "loc": { + "start": { + "line": 33, + "column": 1 + }, + "end": { + "line": 33, + "column": 74 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 34, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/ts/test-keyword-identify9.ts b/es2panda/test/parser/ts/test-keyword-identify9.ts new file mode 100644 index 0000000000000000000000000000000000000000..5dbe13a7ab95401a452008162c0459533246c097 --- /dev/null +++ b/es2panda/test/parser/ts/test-keyword-identify9.ts @@ -0,0 +1,33 @@ +/* + * 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. + */ + + +class is { } + +namespace is { + var is = 1; + var asserts = 2; + var async = 3; +} + +class asserts { + is = 1; + asserts = 2; + async = 3; +} + +function async() { } + +function assertsUse(asserts: T): asserts asserts is NonNullable { }