diff --git a/es2panda/parser/statementParser.cpp b/es2panda/parser/statementParser.cpp index 9fc6077eb26c79d53fcf3552a57d5c5a6ba2a426..c1692b906af69508cc64905330e74483dc5e79c0 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 0000000000000000000000000000000000000000..161700ccc6dd6f04a9a65c572412cbb2b13be000 --- /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 0000000000000000000000000000000000000000..2780521981ec2c58dd6f3645d87312a51e5de6b3 --- /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 0000000000000000000000000000000000000000..af92f85af7ce446cc5391ad79ecb59d88fae3560 --- /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 0000000000000000000000000000000000000000..95c7f796d6d4bc33b2934052bc21a63496213a57 --- /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 0b9428c5cebba35f6c520d095617fd3a553475b7..664419ddc479c16b63a63460940c8a98119768bb 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",