From 06c094a17ea6a449819792819d8670cf87e65e26 Mon Sep 17 00:00:00 2001 From: zengyuan Date: Thu, 28 Sep 2023 16:28:34 +0800 Subject: [PATCH] Support feature: TSC 4.3 adds the override keyword ISSUE: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I853VA?from=project-issue TEST: es2panda/test/parser es2panda/test/compiler Signed-off-by: zengyuan Change-Id: I4b11659a54a1a7ee0efd461c60bc234d92971fea --- es2panda/ir/astNode.h | 5 +- es2panda/ir/base/classProperty.cpp | 1 + es2panda/ir/base/methodDefinition.cpp | 1 + es2panda/ir/ts/tsParameterProperty.cpp | 1 + es2panda/ir/ts/tsParameterProperty.h | 9 +- es2panda/lexer/scripts/keywords.rb | 1 + es2panda/lexer/token/tokenType.h | 1 + es2panda/parser/expressionParser.cpp | 18 +- es2panda/parser/parserImpl.cpp | 39 +- es2panda/parser/parserImpl.h | 2 +- .../compiler/test-ts-override-1-expected.txt | 1 + .../ts/cases/compiler/test-ts-override-1.ts | 28 + .../test-ts-parameter-property-expected.txt | 10 + .../test/parser/ts/test_override-expected.txt | 1 + es2panda/test/parser/ts/test_override.ts | 20 + .../parser/ts/test_override1-expected.txt | 797 ++++++++++++++++++ es2panda/test/parser/ts/test_override1.ts | 25 + .../parser/ts/test_override2-expected.txt | 685 +++++++++++++++ es2panda/test/parser/ts/test_override2.ts | 25 + .../parser/ts/test_override3-expected.txt | 1 + es2panda/test/parser/ts/test_override3.ts | 25 + .../parser/ts/test_override4-expected.txt | 408 +++++++++ es2panda/test/parser/ts/test_override4.ts | 25 + .../parser/ts/test_override5-expected.txt | 1 + es2panda/test/parser/ts/test_override5.ts | 25 + .../parser/ts/test_override6-expected.txt | 73 ++ es2panda/test/parser/ts/test_override6.ts | 17 + .../parser/ts/test_override7-expected.txt | 1 + es2panda/test/parser/ts/test_override7.ts | 19 + ...lass-constructor4-transformed-expected.txt | 1 + 30 files changed, 2245 insertions(+), 21 deletions(-) create mode 100644 es2panda/test/compiler/ts/cases/compiler/test-ts-override-1-expected.txt create mode 100644 es2panda/test/compiler/ts/cases/compiler/test-ts-override-1.ts create mode 100644 es2panda/test/parser/ts/test_override-expected.txt create mode 100644 es2panda/test/parser/ts/test_override.ts create mode 100644 es2panda/test/parser/ts/test_override1-expected.txt create mode 100644 es2panda/test/parser/ts/test_override1.ts create mode 100644 es2panda/test/parser/ts/test_override2-expected.txt create mode 100644 es2panda/test/parser/ts/test_override2.ts create mode 100644 es2panda/test/parser/ts/test_override3-expected.txt create mode 100644 es2panda/test/parser/ts/test_override3.ts create mode 100644 es2panda/test/parser/ts/test_override4-expected.txt create mode 100644 es2panda/test/parser/ts/test_override4.ts create mode 100644 es2panda/test/parser/ts/test_override5-expected.txt create mode 100644 es2panda/test/parser/ts/test_override5.ts create mode 100644 es2panda/test/parser/ts/test_override6-expected.txt create mode 100644 es2panda/test/parser/ts/test_override6.ts create mode 100644 es2panda/test/parser/ts/test_override7-expected.txt create mode 100644 es2panda/test/parser/ts/test_override7.ts diff --git a/es2panda/ir/astNode.h b/es2panda/ir/astNode.h index 83fac9d58a..73a0589899 100644 --- a/es2panda/ir/astNode.h +++ b/es2panda/ir/astNode.h @@ -73,9 +73,10 @@ enum class ModifierFlags { DEFINITE = 1 << 8, ABSTRACT = 1 << 9, ACCESSOR = 1 << 10, + OVERRIDE = 1 << 11, ACCESS = PUBLIC | PROTECTED | PRIVATE, - ALL = STATIC | ASYNC | ACCESS | DECLARE | READONLY | ABSTRACT | ACCESSOR, - ALLOWED_IN_CTOR_PARAMETER = ACCESS | READONLY, + ALL = STATIC | ASYNC | ACCESS | DECLARE | READONLY | ABSTRACT | ACCESSOR | OVERRIDE, + ALLOWED_IN_CTOR_PARAMETER = ACCESS | READONLY | OVERRIDE, }; DEFINE_BITOPS(ModifierFlags) diff --git a/es2panda/ir/base/classProperty.cpp b/es2panda/ir/base/classProperty.cpp index 2d5f4ca2f4..63a1a8eed0 100644 --- a/es2panda/ir/base/classProperty.cpp +++ b/es2panda/ir/base/classProperty.cpp @@ -53,6 +53,7 @@ void ClassProperty::Dump(ir::AstDumper *dumper) const {"abstract", AstDumper::Optional((modifiers_ & ModifierFlags::ABSTRACT) != 0)}, {"static", (modifiers_ & ModifierFlags::STATIC) != 0}, {"readonly", (modifiers_ & ModifierFlags::READONLY) != 0}, + {"override", AstDumper::Optional((modifiers_ & ModifierFlags::OVERRIDE) != 0)}, {"declare", (modifiers_ & ModifierFlags::DECLARE) != 0}, {"optional", (modifiers_ & ModifierFlags::OPTIONAL) != 0}, {"computed", isComputed_}, diff --git a/es2panda/ir/base/methodDefinition.cpp b/es2panda/ir/base/methodDefinition.cpp index b2ca65e9e2..3aa9a4f783 100644 --- a/es2panda/ir/base/methodDefinition.cpp +++ b/es2panda/ir/base/methodDefinition.cpp @@ -88,6 +88,7 @@ void MethodDefinition::Dump(ir::AstDumper *dumper) const {"abstract", AstDumper::Optional((modifiers_ & ModifierFlags::ABSTRACT) != 0)}, {"static", (modifiers_ & ModifierFlags::STATIC) != 0}, {"optional", (modifiers_ & ModifierFlags::OPTIONAL) != 0}, + {"override", AstDumper::Optional((modifiers_ & ModifierFlags::OVERRIDE) != 0)}, {"computed", isComputed_}, {"value", value_}, {"overloads", overloads_}, diff --git a/es2panda/ir/ts/tsParameterProperty.cpp b/es2panda/ir/ts/tsParameterProperty.cpp index f1a33df64b..008f86ca38 100644 --- a/es2panda/ir/ts/tsParameterProperty.cpp +++ b/es2panda/ir/ts/tsParameterProperty.cpp @@ -34,6 +34,7 @@ void TSParameterProperty::Dump(ir::AstDumper *dumper) const ? "private" : accessibility_ == AccessibilityOption::PROTECTED ? "protected" : "undefined"}, {"readonly", readonly_}, + {"override", override_}, {"static", static_}, {"export", export_}, {"parameter", parameter_}}); diff --git a/es2panda/ir/ts/tsParameterProperty.h b/es2panda/ir/ts/tsParameterProperty.h index 9d351d9e41..4d62b190f4 100644 --- a/es2panda/ir/ts/tsParameterProperty.h +++ b/es2panda/ir/ts/tsParameterProperty.h @@ -34,11 +34,12 @@ enum class AccessibilityOption { NO_OPTS, PUBLIC, PRIVATE, PROTECTED }; class TSParameterProperty : public Expression { public: explicit TSParameterProperty(AccessibilityOption accessibility, Expression *parameter, bool readonly, - bool isStatic, bool isExport) + bool isOverride, bool isStatic, bool isExport) : Expression(AstNodeType::TS_PARAMETER_PROPERTY), accessibility_(accessibility), parameter_(parameter), readonly_(readonly), + override_(isOverride), static_(isStatic), export_(isExport) { @@ -54,6 +55,11 @@ public: return readonly_; } + bool IsOverride() const + { + return override_; + } + bool IsStatic() const { return static_; @@ -84,6 +90,7 @@ private: AccessibilityOption accessibility_; Expression *parameter_; bool readonly_; + bool override_; bool static_; bool export_; }; diff --git a/es2panda/lexer/scripts/keywords.rb b/es2panda/lexer/scripts/keywords.rb index 0b2e0b8dbf..26820005b8 100644 --- a/es2panda/lexer/scripts/keywords.rb +++ b/es2panda/lexer/scripts/keywords.rb @@ -120,6 +120,7 @@ keywords = [ "object" => ["TokenType::LITERAL_IDENT", "TokenType::KEYW_OBJECT"], "of" => ["TokenType::LITERAL_IDENT", "TokenType::KEYW_OF"], "out" => ["TokenType::LITERAL_IDENT", "TokenType::KEYW_OUT"], + "override" => ["TokenType::LITERAL_IDENT", "TokenType::KEYW_OVERRIDE"], }, # keywords start with 'p' diff --git a/es2panda/lexer/token/tokenType.h b/es2panda/lexer/token/tokenType.h index 3d7b64d245..6008271130 100644 --- a/es2panda/lexer/token/tokenType.h +++ b/es2panda/lexer/token/tokenType.h @@ -105,6 +105,7 @@ enum class TokenType { KEYW_REQUIRE, KEYW_ABSTRACT, KEYW_SATISFIES, + KEYW_OVERRIDE, /* reserved keywords */ FIRST_KEYW, diff --git a/es2panda/parser/expressionParser.cpp b/es2panda/parser/expressionParser.cpp index 263640e52a..b5b87f0f46 100644 --- a/es2panda/parser/expressionParser.cpp +++ b/es2panda/parser/expressionParser.cpp @@ -840,15 +840,15 @@ ir::Expression *ParserImpl::ParseAssignmentExpression(ir::Expression *lhsExpress return binaryAssignmentExpression; } case lexer::TokenType::LITERAL_IDENT: { - if (Extension() == ScriptExtension::TS && !lexer_->GetToken().NewLine()) { - lexer::TokenType keywordType = lexer_->GetToken().KeywordType(); - if (keywordType == lexer::TokenType::KEYW_AS && !(flags & ExpressionParseFlags::EXP_DISALLOW_AS)) { - ir::Expression *asExpression = ParseTsAsExpression(lhsExpression, flags); - return ParseAssignmentExpression(asExpression); - } else if (keywordType == lexer::TokenType::KEYW_SATISFIES) { - ir::Expression *satisfiesExpression = ParseTsSatisfiesExpression(lhsExpression); - return ParseAssignmentExpression(satisfiesExpression); - } + lexer::TokenType keywordType = lexer_->GetToken().KeywordType(); + if (Extension() == ScriptExtension::TS && keywordType == lexer::TokenType::KEYW_AS && + !(flags & ExpressionParseFlags::EXP_DISALLOW_AS) && !lexer_->GetToken().NewLine()) { + ir::Expression *asExpression = ParseTsAsExpression(lhsExpression, flags); + return ParseAssignmentExpression(asExpression); + } else if (Extension() == ScriptExtension::TS && keywordType == lexer::TokenType::KEYW_SATISFIES && + !lexer_->GetToken().NewLine()) { + ir::Expression *satisfiesExpression = ParseTsSatisfiesExpression(lhsExpression); + return ParseAssignmentExpression(satisfiesExpression); } break; } diff --git a/es2panda/parser/parserImpl.cpp b/es2panda/parser/parserImpl.cpp index 02b2ad3a53..1a8d74c040 100644 --- a/es2panda/parser/parserImpl.cpp +++ b/es2panda/parser/parserImpl.cpp @@ -1962,6 +1962,7 @@ static bool IsModifierKind(const lexer::Token &token) case lexer::TokenType::KEYW_DECLARE: case lexer::TokenType::KEYW_READONLY: case lexer::TokenType::KEYW_ACCESSOR: + case lexer::TokenType::KEYW_OVERRIDE: return true; default: return false; @@ -1996,25 +1997,28 @@ ir::ModifierFlags ParserImpl::ParseModifiers() case lexer::TokenType::KEYW_PUBLIC: { actualStatus = ir::ModifierFlags::PUBLIC; nextStatus = ir::ModifierFlags::ASYNC | ir::ModifierFlags::STATIC | ir::ModifierFlags::READONLY | - ir::ModifierFlags::DECLARE | ir::ModifierFlags::ABSTRACT | ir::ModifierFlags::ACCESSOR; + ir::ModifierFlags::DECLARE | ir::ModifierFlags::ABSTRACT | ir::ModifierFlags::ACCESSOR | + ir::ModifierFlags::OVERRIDE; break; } case lexer::TokenType::KEYW_PRIVATE: { actualStatus = ir::ModifierFlags::PRIVATE; nextStatus = ir::ModifierFlags::ASYNC | ir::ModifierFlags::STATIC | ir::ModifierFlags::READONLY | - ir::ModifierFlags::DECLARE | ir::ModifierFlags::ABSTRACT | ir::ModifierFlags::ACCESSOR; + ir::ModifierFlags::DECLARE | ir::ModifierFlags::ABSTRACT | ir::ModifierFlags::ACCESSOR | + ir::ModifierFlags::OVERRIDE; break; } case lexer::TokenType::KEYW_PROTECTED: { actualStatus = ir::ModifierFlags::PROTECTED; nextStatus = ir::ModifierFlags::ASYNC | ir::ModifierFlags::STATIC | ir::ModifierFlags::READONLY | - ir::ModifierFlags::DECLARE | ir::ModifierFlags::ABSTRACT | ir::ModifierFlags::ACCESSOR; + ir::ModifierFlags::DECLARE | ir::ModifierFlags::ABSTRACT | ir::ModifierFlags::ACCESSOR | + ir::ModifierFlags::OVERRIDE; break; } case lexer::TokenType::KEYW_STATIC: { actualStatus = ir::ModifierFlags::STATIC; nextStatus = ir::ModifierFlags::ASYNC | ir::ModifierFlags::READONLY | ir::ModifierFlags::DECLARE | - ir::ModifierFlags::ABSTRACT | ir::ModifierFlags::ACCESSOR; + ir::ModifierFlags::ABSTRACT | ir::ModifierFlags::ACCESSOR | ir::ModifierFlags::OVERRIDE; break; } case lexer::TokenType::KEYW_ASYNC: { @@ -2025,7 +2029,7 @@ ir::ModifierFlags ParserImpl::ParseModifiers() case lexer::TokenType::KEYW_ABSTRACT: { actualStatus = ir::ModifierFlags::ABSTRACT; nextStatus = ir::ModifierFlags::ACCESS | ir::ModifierFlags::ASYNC | ir::ModifierFlags::STATIC | - ir::ModifierFlags::READONLY | ir::ModifierFlags::DECLARE; + ir::ModifierFlags::READONLY | ir::ModifierFlags::DECLARE | ir::ModifierFlags::OVERRIDE; break; } case lexer::TokenType::KEYW_DECLARE: { @@ -2044,6 +2048,11 @@ ir::ModifierFlags ParserImpl::ParseModifiers() nextStatus = ir::ModifierFlags::NONE; break; } + case lexer::TokenType::KEYW_OVERRIDE: { + actualStatus = ir::ModifierFlags::OVERRIDE; + nextStatus = ir::ModifierFlags::ACCESSOR | ir::ModifierFlags::ASYNC | ir::ModifierFlags::READONLY; + break; + } default: { UNREACHABLE(); } @@ -2592,7 +2601,7 @@ ArenaVector ParserImpl::ParseDecorators() ir::Statement *ParserImpl::ParseClassElement(const ArenaVector &properties, ArenaVector *indexSignatures, bool hasSuperClass, - bool isDeclare, bool isAbstractClass) + bool isDeclare, bool isAbstractClass, bool isExtendsFromNull) { ClassElmentDescriptor desc; @@ -2612,6 +2621,11 @@ ir::Statement *ParserImpl::ParseClassElement(const ArenaVector ThrowSyntaxError("Decorators are not available for auto accessor property now."); } + if ((desc.modifiers & ir::ModifierFlags::OVERRIDE) && (!desc.hasSuperClass || isExtendsFromNull)) { + ThrowSyntaxError({"This member cannot have an 'override' modifier because its containing class " + "does not extend another class."}); + } + CheckClassPrivateIdentifier(&desc); CheckClassGeneratorMethod(&desc); ParseClassKeyModifiers(&desc); @@ -2862,11 +2876,14 @@ ir::ClassDefinition *ParserImpl::ParseClassDefinition(bool isDeclaration, bool i // Parse SuperClass ir::Expression *superClass = nullptr; bool hasSuperClass = false; + bool isExtendsFromNull = false; if (lexer_->GetToken().Type() == lexer::TokenType::KEYW_EXTENDS) { lexer_->NextToken(); hasSuperClass = true; superClass = ParseLeftHandSideExpression(); + ASSERT(superClass != nullptr); + isExtendsFromNull = superClass->IsNullLiteral(); } ir::TSTypeParameterInstantiation *superTypeParams = nullptr; @@ -2952,7 +2969,8 @@ ir::ClassDefinition *ParserImpl::ParseClassDefinition(bool isDeclaration, bool i continue; } - ir::Statement *property = ParseClassElement(properties, &indexSignatures, hasSuperClass, isDeclare, isAbstract); + ir::Statement *property = ParseClassElement(properties, &indexSignatures, hasSuperClass, + isDeclare, isAbstract, isExtendsFromNull); if (property->IsEmptyStatement()) { continue; @@ -3668,6 +3686,7 @@ ir::TSParameterProperty *ParserImpl::CreateTsParameterProperty(ir::Expression *p { auto accessibility = ir::AccessibilityOption::NO_OPTS; bool readonly = false; + bool isOverride = false; bool isStatic = false; bool isExport = false; @@ -3683,13 +3702,17 @@ ir::TSParameterProperty *ParserImpl::CreateTsParameterProperty(ir::Expression *p readonly = true; } + if (modifiers & ir::ModifierFlags::OVERRIDE) { + isOverride = true; + } + if (modifiers & ir::ModifierFlags::STATIC) { isStatic = true; } // TODO(Csaba Repasi): Handle export property of TSParameterProperty - return AllocNode(accessibility, parameter, readonly, isStatic, isExport); + return AllocNode(accessibility, parameter, readonly, isOverride, isStatic, isExport); } ir::Expression *ParserImpl::ParseFunctionParameter(bool isDeclare) diff --git a/es2panda/parser/parserImpl.h b/es2panda/parser/parserImpl.h index b0fedd4c98..7b026849e8 100644 --- a/es2panda/parser/parserImpl.h +++ b/es2panda/parser/parserImpl.h @@ -310,7 +310,7 @@ private: ArenaVector ParseDecorators(); ir::Statement *ParseClassElement(const ArenaVector &properties, ArenaVector *indexSignatures, bool hasSuperClass, - bool isDeclare, bool isAbstractClass); + bool isDeclare, bool isAbstractClass, bool isExtendsFromNull); ir::MethodDefinition *CreateImplicitConstructor(ir::Expression *superClass, bool hasSuperClass, bool isDeclare = false); ir::MethodDefinition *CheckClassMethodOverload(ir::Statement *property, ir::MethodDefinition **ctor, bool isDeclare, diff --git a/es2panda/test/compiler/ts/cases/compiler/test-ts-override-1-expected.txt b/es2panda/test/compiler/ts/cases/compiler/test-ts-override-1-expected.txt new file mode 100644 index 0000000000..0cfbf08886 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/compiler/test-ts-override-1-expected.txt @@ -0,0 +1 @@ +2 diff --git a/es2panda/test/compiler/ts/cases/compiler/test-ts-override-1.ts b/es2panda/test/compiler/ts/cases/compiler/test-ts-override-1.ts new file mode 100644 index 0000000000..35f372f4e2 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/compiler/test-ts-override-1.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 Base { + b: number = 1; +} + +class Sub extends Base { + constructor (override b: number){ + super(); + } +} + +let q = new Sub(2); +print(q.b) \ No newline at end of file diff --git a/es2panda/test/parser/ts/test-ts-parameter-property-expected.txt b/es2panda/test/parser/ts/test-ts-parameter-property-expected.txt index 22fa8852a1..6e169c21d8 100644 --- a/es2panda/test/parser/ts/test-ts-parameter-property-expected.txt +++ b/es2panda/test/parser/ts/test-ts-parameter-property-expected.txt @@ -149,6 +149,7 @@ "type": "TSParameterProperty", "accessibility": "public", "readonly": false, + "override": false, "static": false, "export": false, "parameter": { @@ -180,6 +181,7 @@ "type": "TSParameterProperty", "accessibility": "private", "readonly": false, + "override": false, "static": false, "export": false, "parameter": { @@ -211,6 +213,7 @@ "type": "TSParameterProperty", "accessibility": "protected", "readonly": false, + "override": false, "static": false, "export": false, "parameter": { @@ -242,6 +245,7 @@ "type": "TSParameterProperty", "accessibility": "undefined", "readonly": true, + "override": false, "static": false, "export": false, "parameter": { @@ -273,6 +277,7 @@ "type": "TSParameterProperty", "accessibility": "public", "readonly": true, + "override": false, "static": false, "export": false, "parameter": { @@ -304,6 +309,7 @@ "type": "TSParameterProperty", "accessibility": "private", "readonly": true, + "override": false, "static": false, "export": false, "parameter": { @@ -335,6 +341,7 @@ "type": "TSParameterProperty", "accessibility": "protected", "readonly": true, + "override": false, "static": false, "export": false, "parameter": { @@ -366,6 +373,7 @@ "type": "TSParameterProperty", "accessibility": "public", "readonly": true, + "override": false, "static": false, "export": false, "parameter": { @@ -1019,6 +1027,7 @@ "type": "TSParameterProperty", "accessibility": "public", "readonly": false, + "override": false, "static": false, "export": false, "parameter": { @@ -1174,6 +1183,7 @@ "type": "TSParameterProperty", "accessibility": "undefined", "readonly": true, + "override": false, "static": false, "export": false, "parameter": { diff --git a/es2panda/test/parser/ts/test_override-expected.txt b/es2panda/test/parser/ts/test_override-expected.txt new file mode 100644 index 0000000000..942981d301 --- /dev/null +++ b/es2panda/test/parser/ts/test_override-expected.txt @@ -0,0 +1 @@ +SyntaxError: This member cannot have an 'override' modifier because its containing class does not extend another class. [test_override.ts:18:14] diff --git a/es2panda/test/parser/ts/test_override.ts b/es2panda/test/parser/ts/test_override.ts new file mode 100644 index 0000000000..8e71ecd8ca --- /dev/null +++ b/es2panda/test/parser/ts/test_override.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 O1 { + override p1: number = 1 + foo (v: string) {} +} \ No newline at end of file diff --git a/es2panda/test/parser/ts/test_override1-expected.txt b/es2panda/test/parser/ts/test_override1-expected.txt new file mode 100644 index 0000000000..b45e4dcfad --- /dev/null +++ b/es2panda/test/parser/ts/test_override1-expected.txt @@ -0,0 +1,797 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "O2", + "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": 20, + "column": 2 + } + } + }, + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo", + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 8 + } + } + }, + "kind": "method", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "v", + "typeAnnotation": { + "type": "TSStringKeyword", + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 11 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 18, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 23 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 23 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "fooo", + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 9 + } + } + }, + "kind": "method", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "v", + "typeAnnotation": { + "type": "TSStringKeyword", + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 19, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 11 + }, + "end": { + "line": 19, + "column": 12 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 19, + "column": 22 + }, + "end": { + "line": 19, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 10 + }, + "end": { + "line": 19, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 10 + }, + "end": { + "line": 19, + "column": 24 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 24 + } + } + } + ], + "indexSignatures": [], + "loc": { + "start": { + "line": 17, + "column": 10 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "O3", + "loc": { + "start": { + "line": 22, + "column": 7 + }, + "end": { + "line": 22, + "column": 9 + } + } + }, + "superClass": { + "type": "Identifier", + "name": "O2", + "loc": { + "start": { + "line": 22, + "column": 18 + }, + "end": { + "line": 22, + "column": 20 + } + } + }, + "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": 22, + "column": 1 + }, + "end": { + "line": 25, + "column": 2 + } + } + }, + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo", + "loc": { + "start": { + "line": 23, + "column": 14 + }, + "end": { + "line": 23, + "column": 17 + } + } + }, + "kind": "method", + "static": false, + "optional": false, + "override": true, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "v", + "typeAnnotation": { + "type": "TSStringKeyword", + "loc": { + "start": { + "line": 23, + "column": 22 + }, + "end": { + "line": 23, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 19 + }, + "end": { + "line": 23, + "column": 20 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 23, + "column": 30 + }, + "end": { + "line": 23, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 18 + }, + "end": { + "line": 23, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 18 + }, + "end": { + "line": 23, + "column": 32 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 32 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "fooo", + "loc": { + "start": { + "line": 24, + "column": 5 + }, + "end": { + "line": 24, + "column": 9 + } + } + }, + "kind": "method", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "v", + "typeAnnotation": { + "type": "TSStringKeyword", + "loc": { + "start": { + "line": 24, + "column": 14 + }, + "end": { + "line": 24, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 11 + }, + "end": { + "line": 24, + "column": 12 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 24, + "column": 22 + }, + "end": { + "line": 24, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 10 + }, + "end": { + "line": 24, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 10 + }, + "end": { + "line": 24, + "column": 24 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 5 + }, + "end": { + "line": 24, + "column": 24 + } + } + } + ], + "indexSignatures": [], + "loc": { + "start": { + "line": 22, + "column": 21 + }, + "end": { + "line": 25, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 1 + }, + "end": { + "line": 25, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 26, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/ts/test_override1.ts b/es2panda/test/parser/ts/test_override1.ts new file mode 100644 index 0000000000..a7127461fe --- /dev/null +++ b/es2panda/test/parser/ts/test_override1.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. + */ + + +class O2 { + foo (v: string) {} + fooo (v: string) {} +} + +class O3 extends O2 { + override foo (v: string) {} + fooo (v: string) {} +} diff --git a/es2panda/test/parser/ts/test_override2-expected.txt b/es2panda/test/parser/ts/test_override2-expected.txt new file mode 100644 index 0000000000..7329da9504 --- /dev/null +++ b/es2panda/test/parser/ts/test_override2-expected.txt @@ -0,0 +1,685 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "MyOverride", + "loc": { + "start": { + "line": 17, + "column": 16 + }, + "end": { + "line": 17, + "column": 26 + } + } + }, + "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": 10 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo", + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 17 + } + } + }, + "kind": "method", + "abstract": true, + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSUnknownKeyword", + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 18, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 29 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 29 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "bar", + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 19, + "column": 17 + } + } + }, + "kind": "method", + "abstract": true, + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSVoidKeyword", + "loc": { + "start": { + "line": 19, + "column": 21 + }, + "end": { + "line": 19, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 26 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 26 + } + } + } + ], + "indexSignatures": [], + "loc": { + "start": { + "line": 17, + "column": 27 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 10 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "SubOverride", + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 22, + "column": 27 + } + } + }, + "superClass": { + "type": "Identifier", + "name": "MyOverride", + "loc": { + "start": { + "line": 22, + "column": 36 + }, + "end": { + "line": 22, + "column": 46 + } + } + }, + "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": 22, + "column": 10 + }, + "end": { + "line": 25, + "column": 2 + } + } + }, + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo", + "loc": { + "start": { + "line": 23, + "column": 23 + }, + "end": { + "line": 23, + "column": 26 + } + } + }, + "kind": "method", + "abstract": true, + "static": false, + "optional": false, + "override": true, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 23, + "column": 30 + }, + "end": { + "line": 23, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 26 + }, + "end": { + "line": 23, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 26 + }, + "end": { + "line": 23, + "column": 37 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 37 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "bar", + "loc": { + "start": { + "line": 24, + "column": 5 + }, + "end": { + "line": 24, + "column": 8 + } + } + }, + "kind": "method", + "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": 24, + "column": 11 + }, + "end": { + "line": 24, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 8 + }, + "end": { + "line": 24, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 8 + }, + "end": { + "line": 24, + "column": 14 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 5 + }, + "end": { + "line": 24, + "column": 14 + } + } + } + ], + "indexSignatures": [], + "loc": { + "start": { + "line": 22, + "column": 47 + }, + "end": { + "line": 25, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 10 + }, + "end": { + "line": 25, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 25, + "column": 2 + } + } +} diff --git a/es2panda/test/parser/ts/test_override2.ts b/es2panda/test/parser/ts/test_override2.ts new file mode 100644 index 0000000000..8ee1bedb3e --- /dev/null +++ b/es2panda/test/parser/ts/test_override2.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. + */ + + +abstract class MyOverride { + abstract foo(): unknown; + abstract bar(): void; +} + +abstract class SubOverride extends MyOverride { + abstract override foo(): number; + bar() { } +} \ No newline at end of file diff --git a/es2panda/test/parser/ts/test_override3-expected.txt b/es2panda/test/parser/ts/test_override3-expected.txt new file mode 100644 index 0000000000..f702d7cc34 --- /dev/null +++ b/es2panda/test/parser/ts/test_override3-expected.txt @@ -0,0 +1 @@ +SyntaxError: Unexpected modifier [test_override3.ts:23:14] diff --git a/es2panda/test/parser/ts/test_override3.ts b/es2panda/test/parser/ts/test_override3.ts new file mode 100644 index 0000000000..bf2e32b86d --- /dev/null +++ b/es2panda/test/parser/ts/test_override3.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. + */ + + +abstract class MyOverride1 { + abstract foo(): unknown; + abstract bar(): void; +} + +abstract class SubOverride1 extends MyOverride1 { + override abstract foo(): number; + bar() { } +} \ No newline at end of file diff --git a/es2panda/test/parser/ts/test_override4-expected.txt b/es2panda/test/parser/ts/test_override4-expected.txt new file mode 100644 index 0000000000..d2f98bd241 --- /dev/null +++ b/es2panda/test/parser/ts/test_override4-expected.txt @@ -0,0 +1,408 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "BaseOverride", + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "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": 19, + "column": 2 + } + } + }, + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "foo", + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 8 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 18, + "column": 11 + }, + "end": { + "line": 18, + "column": 12 + } + } + }, + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 12 + } + } + } + ], + "indexSignatures": [], + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Sub", + "loc": { + "start": { + "line": 21, + "column": 7 + }, + "end": { + "line": 21, + "column": 10 + } + } + }, + "superClass": { + "type": "Identifier", + "name": "BaseOverride", + "loc": { + "start": { + "line": 21, + "column": 19 + }, + "end": { + "line": 21, + "column": 31 + } + } + }, + "implements": [], + "constructor": { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "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": "TSParameterProperty", + "accessibility": "undefined", + "readonly": false, + "override": true, + "static": false, + "export": false, + "parameter": { + "type": "Identifier", + "name": "foo", + "typeAnnotation": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 22, + "column": 32 + }, + "end": { + "line": 22, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 27 + }, + "end": { + "line": 22, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 18 + }, + "end": { + "line": 22, + "column": 30 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Super", + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 14 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 40 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 17 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 17 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + "body": [], + "indexSignatures": [], + "loc": { + "start": { + "line": 21, + "column": 32 + }, + "end": { + "line": 25, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 25, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 25, + "column": 2 + } + } +} diff --git a/es2panda/test/parser/ts/test_override4.ts b/es2panda/test/parser/ts/test_override4.ts new file mode 100644 index 0000000000..bfd970444c --- /dev/null +++ b/es2panda/test/parser/ts/test_override4.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. + */ + + +class BaseOverride { + foo = 1; +} + +class Sub extends BaseOverride { + constructor (override foo: number) { + super(); + } +} \ No newline at end of file diff --git a/es2panda/test/parser/ts/test_override5-expected.txt b/es2panda/test/parser/ts/test_override5-expected.txt new file mode 100644 index 0000000000..39376b987f --- /dev/null +++ b/es2panda/test/parser/ts/test_override5-expected.txt @@ -0,0 +1 @@ +SyntaxError: Unexpected modifier [test_override5.ts:22:27] diff --git a/es2panda/test/parser/ts/test_override5.ts b/es2panda/test/parser/ts/test_override5.ts new file mode 100644 index 0000000000..65d9643deb --- /dev/null +++ b/es2panda/test/parser/ts/test_override5.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. + */ + + +class BaseOverride1 { + foo = 1; +} + +class Sub1 extends BaseOverride1 { + constructor (override public foo: number) { + super(); + } +} \ No newline at end of file diff --git a/es2panda/test/parser/ts/test_override6-expected.txt b/es2panda/test/parser/ts/test_override6-expected.txt new file mode 100644 index 0000000000..170be50dc5 --- /dev/null +++ b/es2panda/test/parser/ts/test_override6-expected.txt @@ -0,0 +1,73 @@ +{ + "type": "Program", + "statements": [ + { + "type": "FunctionDeclaration", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "override", + "loc": { + "start": { + "line": 17, + "column": 10 + }, + "end": { + "line": 17, + "column": 18 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 17, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 23 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 17, + "column": 23 + } + } +} diff --git a/es2panda/test/parser/ts/test_override6.ts b/es2panda/test/parser/ts/test_override6.ts new file mode 100644 index 0000000000..18fd0f9114 --- /dev/null +++ b/es2panda/test/parser/ts/test_override6.ts @@ -0,0 +1,17 @@ +/* + * 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/LIsCENSE-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 override() {} \ No newline at end of file diff --git a/es2panda/test/parser/ts/test_override7-expected.txt b/es2panda/test/parser/ts/test_override7-expected.txt new file mode 100644 index 0000000000..e191a3dbbd --- /dev/null +++ b/es2panda/test/parser/ts/test_override7-expected.txt @@ -0,0 +1 @@ +SyntaxError: This member cannot have an 'override' modifier because its containing class does not extend another class. [test_override7.ts:18:14] diff --git a/es2panda/test/parser/ts/test_override7.ts b/es2panda/test/parser/ts/test_override7.ts new file mode 100644 index 0000000000..deac4c9f3c --- /dev/null +++ b/es2panda/test/parser/ts/test_override7.ts @@ -0,0 +1,19 @@ +/* + * 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 A1 extends null { + override method1() {} +} \ No newline at end of file diff --git a/es2panda/test/parser/ts/transformed_cases/test-class-constructor4-transformed-expected.txt b/es2panda/test/parser/ts/transformed_cases/test-class-constructor4-transformed-expected.txt index 69d7527d4d..7756d14600 100644 --- a/es2panda/test/parser/ts/transformed_cases/test-class-constructor4-transformed-expected.txt +++ b/es2panda/test/parser/ts/transformed_cases/test-class-constructor4-transformed-expected.txt @@ -188,6 +188,7 @@ "type": "TSParameterProperty", "accessibility": "public", "readonly": false, + "override": false, "static": false, "export": false, "parameter": { -- Gitee