From 54371f3057b8e6abaeac115456402584426a92cf Mon Sep 17 00:00:00 2001 From: songqi Date: Thu, 1 Dec 2022 12:03:23 +0800 Subject: [PATCH] Fix bug of find importName failed during runtime Issue: I67XKR Tests: test262,parser,compiler,tsc Signed-off-by: songqi Change-Id: I794187ffdc128890a6e0a1421e586fedb8158cc9 --- es2panda/parser/statementParser.cpp | 4 +- .../test/compiler/ts/cases/projects/.gitkeep | 0 .../test-ts-export-type.ts | 20 ++++ .../test-ts-import-type-exec-expected.txt | 3 + .../test-ts-import-type-exec.ts | 27 +++++ .../test-ts-export-type.ts | 22 ++++ .../test-ts-import-type-exec-expected.txt | 3 + .../test-ts-import-type-exec.ts | 27 +++++ .../dest-exec-expected.txt | 2 + .../ts_test_compiler_project/dest-exec.ts | 21 ++++ .../ts_test_compiler_project/dir/dir_src.ts | 19 ++++ .../projects/ts_test_compiler_project/src.ts | 17 +++ es2panda/test/runner.py | 103 ++++++++++++++++-- 13 files changed, 260 insertions(+), 8 deletions(-) delete mode 100644 es2panda/test/compiler/ts/cases/projects/.gitkeep create mode 100644 es2panda/test/compiler/ts/projects/ts_import_type_project_1/test-ts-export-type.ts create mode 100644 es2panda/test/compiler/ts/projects/ts_import_type_project_1/test-ts-import-type-exec-expected.txt create mode 100644 es2panda/test/compiler/ts/projects/ts_import_type_project_1/test-ts-import-type-exec.ts create mode 100644 es2panda/test/compiler/ts/projects/ts_import_type_project_2/test-ts-export-type.ts create mode 100644 es2panda/test/compiler/ts/projects/ts_import_type_project_2/test-ts-import-type-exec-expected.txt create mode 100644 es2panda/test/compiler/ts/projects/ts_import_type_project_2/test-ts-import-type-exec.ts create mode 100644 es2panda/test/compiler/ts/projects/ts_test_compiler_project/dest-exec-expected.txt create mode 100644 es2panda/test/compiler/ts/projects/ts_test_compiler_project/dest-exec.ts create mode 100644 es2panda/test/compiler/ts/projects/ts_test_compiler_project/dir/dir_src.ts create mode 100644 es2panda/test/compiler/ts/projects/ts_test_compiler_project/src.ts diff --git a/es2panda/parser/statementParser.cpp b/es2panda/parser/statementParser.cpp index d9f1d33c0c..e8f67909e3 100644 --- a/es2panda/parser/statementParser.cpp +++ b/es2panda/parser/statementParser.cpp @@ -2930,7 +2930,9 @@ ir::Statement *ParserImpl::ParseImportDeclaration(StatementParsingFlags flags) return astNode->AsTSImportEqualsDeclaration(); } source = ParseFromClause(true); - AddImportEntryItem(source, &specifiers); + if (!isType) { + AddImportEntryItem(source, &specifiers); + } } else { // import 'source' source = ParseFromClause(false); diff --git a/es2panda/test/compiler/ts/cases/projects/.gitkeep b/es2panda/test/compiler/ts/cases/projects/.gitkeep deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/es2panda/test/compiler/ts/projects/ts_import_type_project_1/test-ts-export-type.ts b/es2panda/test/compiler/ts/projects/ts_import_type_project_1/test-ts-export-type.ts new file mode 100644 index 0000000000..830fbfd5de --- /dev/null +++ b/es2panda/test/compiler/ts/projects/ts_import_type_project_1/test-ts-export-type.ts @@ -0,0 +1,20 @@ +/* + * 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. + */ + + +export interface I { + name:string; + age:number; +}; diff --git a/es2panda/test/compiler/ts/projects/ts_import_type_project_1/test-ts-import-type-exec-expected.txt b/es2panda/test/compiler/ts/projects/ts_import_type_project_1/test-ts-import-type-exec-expected.txt new file mode 100644 index 0000000000..dc52409456 --- /dev/null +++ b/es2panda/test/compiler/ts/projects/ts_import_type_project_1/test-ts-import-type-exec-expected.txt @@ -0,0 +1,3 @@ +interface +24 +HelloWorld! diff --git a/es2panda/test/compiler/ts/projects/ts_import_type_project_1/test-ts-import-type-exec.ts b/es2panda/test/compiler/ts/projects/ts_import_type_project_1/test-ts-import-type-exec.ts new file mode 100644 index 0000000000..df2459ae41 --- /dev/null +++ b/es2panda/test/compiler/ts/projects/ts_import_type_project_1/test-ts-import-type-exec.ts @@ -0,0 +1,27 @@ +/* + * 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. + */ + + +import type {I} from './test-ts-export-type'; +export type {I}; + +function foo() { + let a:I = {name:"interface", age:24}; + print(a.name); + print(a.age); +} + +foo(); +print("HelloWorld!"); diff --git a/es2panda/test/compiler/ts/projects/ts_import_type_project_2/test-ts-export-type.ts b/es2panda/test/compiler/ts/projects/ts_import_type_project_2/test-ts-export-type.ts new file mode 100644 index 0000000000..39e005f4e1 --- /dev/null +++ b/es2panda/test/compiler/ts/projects/ts_import_type_project_2/test-ts-export-type.ts @@ -0,0 +1,22 @@ +/* + * 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. + */ + + +interface I { + name:string; + age:number; +}; + +export type {I}; diff --git a/es2panda/test/compiler/ts/projects/ts_import_type_project_2/test-ts-import-type-exec-expected.txt b/es2panda/test/compiler/ts/projects/ts_import_type_project_2/test-ts-import-type-exec-expected.txt new file mode 100644 index 0000000000..dc52409456 --- /dev/null +++ b/es2panda/test/compiler/ts/projects/ts_import_type_project_2/test-ts-import-type-exec-expected.txt @@ -0,0 +1,3 @@ +interface +24 +HelloWorld! diff --git a/es2panda/test/compiler/ts/projects/ts_import_type_project_2/test-ts-import-type-exec.ts b/es2panda/test/compiler/ts/projects/ts_import_type_project_2/test-ts-import-type-exec.ts new file mode 100644 index 0000000000..df2459ae41 --- /dev/null +++ b/es2panda/test/compiler/ts/projects/ts_import_type_project_2/test-ts-import-type-exec.ts @@ -0,0 +1,27 @@ +/* + * 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. + */ + + +import type {I} from './test-ts-export-type'; +export type {I}; + +function foo() { + let a:I = {name:"interface", age:24}; + print(a.name); + print(a.age); +} + +foo(); +print("HelloWorld!"); diff --git a/es2panda/test/compiler/ts/projects/ts_test_compiler_project/dest-exec-expected.txt b/es2panda/test/compiler/ts/projects/ts_test_compiler_project/dest-exec-expected.txt new file mode 100644 index 0000000000..ae34f30f55 --- /dev/null +++ b/es2panda/test/compiler/ts/projects/ts_test_compiler_project/dest-exec-expected.txt @@ -0,0 +1,2 @@ +HelloWorld +1 diff --git a/es2panda/test/compiler/ts/projects/ts_test_compiler_project/dest-exec.ts b/es2panda/test/compiler/ts/projects/ts_test_compiler_project/dest-exec.ts new file mode 100644 index 0000000000..57b56eb0e2 --- /dev/null +++ b/es2panda/test/compiler/ts/projects/ts_test_compiler_project/dest-exec.ts @@ -0,0 +1,21 @@ +/* + * 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. + */ + + +import {a} from './src'; +import {A} from './dir/dir_src'; + +print(a); +print(A.prop); diff --git a/es2panda/test/compiler/ts/projects/ts_test_compiler_project/dir/dir_src.ts b/es2panda/test/compiler/ts/projects/ts_test_compiler_project/dir/dir_src.ts new file mode 100644 index 0000000000..5cd4e0484e --- /dev/null +++ b/es2panda/test/compiler/ts/projects/ts_test_compiler_project/dir/dir_src.ts @@ -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. + */ + + +export class A { + static prop = 1; +} diff --git a/es2panda/test/compiler/ts/projects/ts_test_compiler_project/src.ts b/es2panda/test/compiler/ts/projects/ts_test_compiler_project/src.ts new file mode 100644 index 0000000000..60fdeccce2 --- /dev/null +++ b/es2panda/test/compiler/ts/projects/ts_test_compiler_project/src.ts @@ -0,0 +1,17 @@ +/* + * 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. + */ + + +export var a = "HelloWorld"; diff --git a/es2panda/test/runner.py b/es2panda/test/runner.py index 836804a905..d63d438438 100755 --- a/es2panda/test/runner.py +++ b/es2panda/test/runner.py @@ -22,6 +22,7 @@ import fnmatch import multiprocessing import os import re +import shutil import subprocess import sys import test262util @@ -783,12 +784,19 @@ class CompilerRunner(Runner): Runner.__init__(self, args, "Compiler") def add_directory(self, directory, extension, flags): - glob_expression = path.join( - self.test_root, directory, "**/*.%s" % (extension)) - files = glob(glob_expression, recursive=True) - files = fnmatch.filter(files, self.test_root + '**' + self.args.filter) - - self.tests += list(map(lambda f: CompilerTest(f, flags), files)) + if directory.endswith("projects"): + projects_path = path.join(self.test_root, directory) + for project in os.listdir(projects_path): + glob_expression = path.join(projects_path, project, "**/*.%s" % (extension)) + files = glob(glob_expression, recursive=True) + files = fnmatch.filter(files, self.test_root + '**' + self.args.filter) + self.tests.append(CompilerProjectTest(projects_path, project, files, flags)) + else: + glob_expression = path.join( + self.test_root, directory, "**/*.%s" % (extension)) + files = glob(glob_expression, recursive=True) + files = fnmatch.filter(files, self.test_root + '**' + self.args.filter) + self.tests += list(map(lambda f: CompilerTest(f, flags), files)) def test_path(self, src): return src @@ -839,6 +847,86 @@ class CompilerTest(Test): return self +class CompilerProjectTest(Test): + def __init__(self, projects_path, project, test_paths, flags): + Test.__init__(self, "", flags) + self.projects_path = projects_path + self.project = project + self.test_paths = test_paths + + def remove_project(self, runner): + project_path = runner.build_dir + "/" + self.project + if path.exists(project_path): + shutil.rmtree(project_path) + + def get_file_absolute_path_and_name(self, runner): + sub_path = self.path[len(self.projects_path):] + file_relative_path = path.split(sub_path)[0] + file_name = path.split(sub_path)[1] + file_absolute_path = runner.build_dir + "/" + file_relative_path + return [file_absolute_path, file_name] + + def run(self, runner): + # Compile all ts source files in the project to abc files. + for test_path in self.test_paths: + self.path = test_path + [file_absolute_path, file_name] = self.get_file_absolute_path_and_name(runner) + if not path.exists(file_absolute_path): + os.makedirs(file_absolute_path) + + test_abc_name = ("%s.abc" % (path.splitext(file_name)[0])) + test_abc_path = path.join(file_absolute_path, test_abc_name) + es2abc_cmd = runner.cmd_prefix + [runner.es2panda] + es2abc_cmd.extend(self.flags) + es2abc_cmd.extend(["--output=" + test_abc_path]) + es2abc_cmd.append(self.path) + self.log_cmd(es2abc_cmd) + + process = subprocess.Popen(es2abc_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out, err = process.communicate() + if err: + self.passed = False + self.error = err.decode("utf-8", errors="ignore") + self.remove_project(runner) + return self + + # Run test files that need to be executed in the project. + for test_path in self.test_paths: + self.path = test_path + if self.path.endswith("-exec.ts"): + [file_absolute_path, file_name] = self.get_file_absolute_path_and_name(runner) + + test_abc_name = ("%s.abc" % (path.splitext(file_name)[0])) + test_abc_path = path.join(file_absolute_path, test_abc_name) + + ld_library_path = runner.ld_library_path + os.environ.setdefault("LD_LIBRARY_PATH", ld_library_path) + run_abc_cmd = [runner.ark_js_vm] + run_abc_cmd.extend([test_abc_path]) + self.log_cmd(run_abc_cmd) + + process = subprocess.Popen(run_abc_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out, err = process.communicate() + self.output = out.decode("utf-8", errors="ignore") + err.decode("utf-8", errors="ignore") + expected_path = self.get_path_to_expected() + try: + with open(expected_path, 'r') as fp: + expected = fp.read() + self.passed = expected == self.output and process.returncode in [0, 1] + except Exception: + self.passed = False + + if not self.passed: + self.error = err.decode("utf-8", errors="ignore") + self.remove_project(runner) + return self + + self.passed = True + + self.remove_project(runner) + return self + + class TSDeclarationTest(Test): def get_path_to_expected(self): file_name = self.path[:self.path.find(".d.ts")] @@ -927,7 +1015,8 @@ def main(): if args.compiler: runner = CompilerRunner(args) runner.add_directory("compiler/js", "js", []) - runner.add_directory("compiler/ts", "ts", ["--extension=ts"]) + runner.add_directory("compiler/ts/cases", "ts", ["--extension=ts"]) + runner.add_directory("compiler/ts/projects", "ts", ["--module", "--extension=ts"]) runner.add_directory("compiler/dts", "d.ts", ["--module", "--extension=ts"]) runners.append(runner) -- Gitee