From d67e8df6bf2d8f668ffc208657ea56a05d8bd980 Mon Sep 17 00:00:00 2001 From: ctw-ian Date: Mon, 29 Aug 2022 16:56:48 +0800 Subject: [PATCH] Fix incorrect class parsing in constructor Issue:I5OMOH Signed-off-by: ctw-ian Change-Id: Ic1294e50cc7d09368657cd39032c598b97bee1b6 --- es2panda/parser/parserImpl.cpp | 1 + .../js/test-class-constructor-1-expected.txt | 1 + .../parser/js/test-class-constructor-1.js | 19 + .../js/test-class-constructor-2-expected.txt | 413 ++++++++++++++++++ .../parser/js/test-class-constructor-2.js | 23 + 5 files changed, 457 insertions(+) create mode 100644 es2panda/test/parser/js/test-class-constructor-1-expected.txt create mode 100644 es2panda/test/parser/js/test-class-constructor-1.js create mode 100644 es2panda/test/parser/js/test-class-constructor-2-expected.txt create mode 100644 es2panda/test/parser/js/test-class-constructor-2.js diff --git a/es2panda/parser/parserImpl.cpp b/es2panda/parser/parserImpl.cpp index b1bab02173..63e822f8d4 100644 --- a/es2panda/parser/parserImpl.cpp +++ b/es2panda/parser/parserImpl.cpp @@ -2623,6 +2623,7 @@ ir::ClassDefinition *ParserImpl::ParseClassDefinition(bool isDeclaration, bool i // Parse ClassBody auto savedStatus = context_.Status(); context_.Status() |= ParserStatus::IN_CLASS_BODY; + context_.Status() &= ~(ParserStatus::CONSTRUCTOR_FUNCTION); lexer::SourcePosition classBodyStartLoc = lexer_->GetToken().Start(); lexer_->NextToken(lexer::LexerNextTokenFlags::KEYWORD_TO_IDENT); diff --git a/es2panda/test/parser/js/test-class-constructor-1-expected.txt b/es2panda/test/parser/js/test-class-constructor-1-expected.txt new file mode 100644 index 0000000000..4f33b991ef --- /dev/null +++ b/es2panda/test/parser/js/test-class-constructor-1-expected.txt @@ -0,0 +1 @@ +SyntaxError: 'static' modifier cannot appear on a parameter. [test-class-constructor.js:18:17] \ No newline at end of file diff --git a/es2panda/test/parser/js/test-class-constructor-1.js b/es2panda/test/parser/js/test-class-constructor-1.js new file mode 100644 index 0000000000..b93a7bdf44 --- /dev/null +++ b/es2panda/test/parser/js/test-class-constructor-1.js @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +class A { + constructor(static a) {} +} diff --git a/es2panda/test/parser/js/test-class-constructor-2-expected.txt b/es2panda/test/parser/js/test-class-constructor-2-expected.txt new file mode 100644 index 0000000000..624c42cf4f --- /dev/null +++ b/es2panda/test/parser/js/test-class-constructor-2-expected.txt @@ -0,0 +1,413 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "column": 8 + } + } + }, + "superClass": null, + "implements": [], + "constructor": { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "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": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 13 + } + } + }, + "property": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 19, + "column": 15 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 15 + } + } + }, + "right": { + "type": "ClassExpression", + "definition": { + "id": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 24 + }, + "end": { + "line": 19, + "column": 25 + } + } + }, + "superClass": null, + "implements": [], + "constructor": { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "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": 19, + "column": 18 + }, + "end": { + "line": 21, + "column": 10 + } + } + }, + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "b", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 20 + }, + "end": { + "line": 20, + "column": 21 + } + } + }, + "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": 20, + "column": 24 + }, + "end": { + "line": 20, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 21 + }, + "end": { + "line": 20, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 21 + }, + "end": { + "line": 20, + "column": 26 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 20, + "column": 26 + } + } + } + ], + "indexSignatures": [], + "loc": { + "start": { + "line": 19, + "column": 26 + }, + "end": { + "line": 21, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 21, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 21, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 21, + "column": 10 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 22, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 16 + }, + "end": { + "line": 22, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 16 + }, + "end": { + "line": 22, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 22, + "column": 6 + } + } + }, + "body": [], + "indexSignatures": [], + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 23, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 24, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/js/test-class-constructor-2.js b/es2panda/test/parser/js/test-class-constructor-2.js new file mode 100644 index 0000000000..18bcd5ecc1 --- /dev/null +++ b/es2panda/test/parser/js/test-class-constructor-2.js @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +class A { + constructor() { + this.a = class B { + static b() {} + } + } +} -- Gitee