From 255f306eca80af1479493fc87c269c5feef73535 Mon Sep 17 00:00:00 2001 From: songqi Date: Fri, 17 Feb 2023 16:12:04 +0800 Subject: [PATCH] Fix parser of super in class Issue: I6FVDA Test: parser/compiler/tsc/test262 Signed-off-by: songqi Change-Id: I087bb653398f5feb4c1a60b3a7b151892b0f0428 --- es2panda/parser/parserImpl.cpp | 4 + .../test-ts-class-super-1-expected.txt | 2 + .../classes/test-ts-class-super-1.ts | 33 + .../parser/ts/test-class-super-1-expected.txt | 662 ++++++++++++++++++ es2panda/test/parser/ts/test-class-super-1.ts | 25 + 5 files changed, 726 insertions(+) create mode 100644 es2panda/test/compiler/ts/cases/conformance/classes/test-ts-class-super-1-expected.txt create mode 100644 es2panda/test/compiler/ts/cases/conformance/classes/test-ts-class-super-1.ts create mode 100644 es2panda/test/parser/ts/test-class-super-1-expected.txt create mode 100644 es2panda/test/parser/ts/test-class-super-1.ts diff --git a/es2panda/parser/parserImpl.cpp b/es2panda/parser/parserImpl.cpp index 8188ab23d0..b03ce00620 100644 --- a/es2panda/parser/parserImpl.cpp +++ b/es2panda/parser/parserImpl.cpp @@ -2441,7 +2441,11 @@ ir::Statement *ParserImpl::ParseClassProperty(ClassElmentDescriptor *desc, if (isDeclare) { ThrowSyntaxError("Initializers are not allowed in ambient contexts."); } + // TODO(songqi):static classProperty's value can use super keyword in TypeScript4.4. + // Currently only Parser is supported, Compiler support requires Transformer. + context_.Status() |= ParserStatus::ALLOW_SUPER; value = ParseExpression(); + context_.Status() &= ~ParserStatus::ALLOW_SUPER; propEnd = value->End(); } diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-class-super-1-expected.txt b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-class-super-1-expected.txt new file mode 100644 index 0000000000..15c2ac3d64 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-class-super-1-expected.txt @@ -0,0 +1,2 @@ +100 +200 diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-class-super-1.ts b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-class-super-1.ts new file mode 100644 index 0000000000..a4ce3e29a5 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-class-super-1.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 Base { + test1() { + return 100; + } + test2() { + return 200; + } +} + +class C extends Base { + public childTest1 = super.test1; + public childTest2 = super.test2(); +} + +var c = new C(); +print(c.childTest1()); +print(c.childTest2); diff --git a/es2panda/test/parser/ts/test-class-super-1-expected.txt b/es2panda/test/parser/ts/test-class-super-1-expected.txt new file mode 100644 index 0000000000..58c0306efe --- /dev/null +++ b/es2panda/test/parser/ts/test-class-super-1-expected.txt @@ -0,0 +1,662 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Base", + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "column": 11 + } + } + }, + "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": "prop", + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 16 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 100, + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 18, + "column": 22 + } + } + }, + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 22 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "test2", + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 17 + } + } + }, + "kind": "method", + "static": true, + "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": 19, + "column": 20 + }, + "end": { + "line": 19, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 23 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 23 + } + } + } + ], + "indexSignatures": [], + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "C", + "loc": { + "start": { + "line": 22, + "column": 7 + }, + "end": { + "line": 22, + "column": 8 + } + } + }, + "superClass": { + "type": "Identifier", + "name": "Base", + "loc": { + "start": { + "line": 22, + "column": 17 + }, + "end": { + "line": 22, + "column": 21 + } + } + }, + "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": "ClassProperty", + "key": { + "type": "Identifier", + "name": "childTest1", + "loc": { + "start": { + "line": 23, + "column": 12 + }, + "end": { + "line": 23, + "column": 22 + } + } + }, + "value": { + "type": "MemberExpression", + "object": { + "type": "Super", + "loc": { + "start": { + "line": 23, + "column": 25 + }, + "end": { + "line": 23, + "column": 30 + } + } + }, + "property": { + "type": "Identifier", + "name": "prop", + "loc": { + "start": { + "line": 23, + "column": 31 + }, + "end": { + "line": 23, + "column": 35 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 23, + "column": 25 + }, + "end": { + "line": 23, + "column": 35 + } + } + }, + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 35 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "childTest2", + "loc": { + "start": { + "line": 24, + "column": 12 + }, + "end": { + "line": 24, + "column": 22 + } + } + }, + "value": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "Super", + "loc": { + "start": { + "line": 24, + "column": 25 + }, + "end": { + "line": 24, + "column": 30 + } + } + }, + "property": { + "type": "Identifier", + "name": "test2", + "loc": { + "start": { + "line": 24, + "column": 31 + }, + "end": { + "line": 24, + "column": 36 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 24, + "column": 25 + }, + "end": { + "line": 24, + "column": 36 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 24, + "column": 25 + }, + "end": { + "line": 24, + "column": 38 + } + } + }, + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 5 + }, + "end": { + "line": 24, + "column": 38 + } + } + } + ], + "indexSignatures": [], + "loc": { + "start": { + "line": 22, + "column": 22 + }, + "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-class-super-1.ts b/es2panda/test/parser/ts/test-class-super-1.ts new file mode 100644 index 0000000000..cfcd134cd8 --- /dev/null +++ b/es2panda/test/parser/ts/test-class-super-1.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 Base { + static prop = 100; + static test2() { } +} + +class C extends Base { + static childTest1 = super.prop; + static childTest2 = super.test2(); +} -- Gitee