From 2fcb631af454c6e4b9ceb99621430462eead78a3 Mon Sep 17 00:00:00 2001 From: songqi Date: Thu, 10 Aug 2023 20:52:29 +0800 Subject: [PATCH] Fix parser of importDeclaration Issue: I7SATW Tests: test_es2abc Signed-off-by: songqi Change-Id: I00a694b519deba78640cba3dcce48608c2326d51 --- es2panda/parser/statementParser.cpp | 6 + .../script/test-ts-import1-expected.txt | 1 + .../test/parser/script/test-ts-import1.ts | 17 ++ .../script/test-ts-importEqual1-expected.txt | 200 ++++++++++++++++++ .../parser/script/test-ts-importEqual1.ts | 21 ++ es2panda/test/runner.py | 1 + 6 files changed, 246 insertions(+) create mode 100644 es2panda/test/parser/script/test-ts-import1-expected.txt create mode 100644 es2panda/test/parser/script/test-ts-import1.ts create mode 100644 es2panda/test/parser/script/test-ts-importEqual1-expected.txt create mode 100644 es2panda/test/parser/script/test-ts-importEqual1.ts diff --git a/es2panda/parser/statementParser.cpp b/es2panda/parser/statementParser.cpp index 9fc6077eb2..c1692b906a 100644 --- a/es2panda/parser/statementParser.cpp +++ b/es2panda/parser/statementParser.cpp @@ -3004,9 +3004,15 @@ ir::Statement *ParserImpl::ParseImportDeclaration(StatementParsingFlags flags) ConsumeSemicolon(astNode->AsTSImportEqualsDeclaration()); return astNode->AsTSImportEqualsDeclaration(); } + if (Extension() == ScriptExtension::TS && !context_.IsModule()) { + ThrowSyntaxError("'import' and 'export' may appear only with 'sourceType: module'"); + } source = ParseFromClause(true); AddImportEntryItem(source, &specifiers, isType); } else { + if (Extension() == ScriptExtension::TS && !context_.IsModule()) { + ThrowSyntaxError("'import' and 'export' may appear only with 'sourceType: module'"); + } // import 'source' source = ParseFromClause(false); AddImportEntryItem(source, nullptr, isType); diff --git a/es2panda/test/parser/script/test-ts-import1-expected.txt b/es2panda/test/parser/script/test-ts-import1-expected.txt new file mode 100644 index 0000000000..161700ccc6 --- /dev/null +++ b/es2panda/test/parser/script/test-ts-import1-expected.txt @@ -0,0 +1 @@ +SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' [test-ts-import1.ts:17:12] diff --git a/es2panda/test/parser/script/test-ts-import1.ts b/es2panda/test/parser/script/test-ts-import1.ts new file mode 100644 index 0000000000..2780521981 --- /dev/null +++ b/es2panda/test/parser/script/test-ts-import1.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/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. + */ + + +import {x} from './test'; diff --git a/es2panda/test/parser/script/test-ts-importEqual1-expected.txt b/es2panda/test/parser/script/test-ts-importEqual1-expected.txt new file mode 100644 index 0000000000..af92f85af7 --- /dev/null +++ b/es2panda/test/parser/script/test-ts-importEqual1-expected.txt @@ -0,0 +1,200 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSModuleDeclaration", + "id": { + "type": "Identifier", + "name": "A", + "loc": { + "start": { + "line": 17, + "column": 11 + }, + "end": { + "line": 17, + "column": 12 + } + } + }, + "body": { + "type": "TSModuleBlock", + "body": [ + { + "type": "ExportNamedDeclaration", + "declaration": { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "b", + "loc": { + "start": { + "line": 18, + "column": 16 + }, + "end": { + "line": 18, + "column": 17 + } + } + }, + "init": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 18, + "column": 20 + }, + "end": { + "line": 18, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 16 + }, + "end": { + "line": 18, + "column": 21 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 22 + } + } + }, + "source": null, + "specifiers": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 22 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "declare": false, + "global": false, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 21, + "column": 7 + } + } + }, + { + "type": "TSImportEqualsDeclaration", + "id": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 21, + "column": 8 + }, + "end": { + "line": 21, + "column": 9 + } + } + }, + "moduleReference": { + "type": "TSQualifiedName", + "left": { + "type": "Identifier", + "name": "A", + "loc": { + "start": { + "line": 21, + "column": 12 + }, + "end": { + "line": 21, + "column": 13 + } + } + }, + "right": { + "type": "Identifier", + "name": "b", + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 21, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 12 + }, + "end": { + "line": 21, + "column": 16 + } + } + }, + "isExport": false, + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 21, + "column": 16 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 22, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/script/test-ts-importEqual1.ts b/es2panda/test/parser/script/test-ts-importEqual1.ts new file mode 100644 index 0000000000..95c7f796d6 --- /dev/null +++ b/es2panda/test/parser/script/test-ts-importEqual1.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. + */ + + +namespace A { + export var b = 1; +} + +import a = A.b; diff --git a/es2panda/test/runner.py b/es2panda/test/runner.py index 0b9428c5ce..664419ddc4 100755 --- a/es2panda/test/runner.py +++ b/es2panda/test/runner.py @@ -1450,6 +1450,7 @@ def main(): runner = RegressionRunner(args) runner.add_directory("parser/concurrent", "js", ["--module", "--dump-ast"]) runner.add_directory("parser/js", "js", ["--parse-only", "--dump-ast"]) + runner.add_directory("parser/script", "ts", ["--parse-only", "--dump-ast"]) runner.add_directory("parser/ts", "ts", ["--parse-only", "--module", "--dump-ast"]) runner.add_directory("parser/ts/type_checker", "ts", -- Gitee