diff --git a/src/import_resolve.ts b/src/import_resolve.ts index 2e26bd6..9f23a9f 100644 --- a/src/import_resolve.ts +++ b/src/import_resolve.ts @@ -98,6 +98,13 @@ export class ImportResolver { importModuleScope, ); } + if ( + !globalScope.importGlobalScopeList.includes( + importModuleScope, + ) + ) { + globalScope.importGlobalScopeList.push(importModuleScope); + } break; } case ts.SyntaxKind.ExportDeclaration: { diff --git a/src/scope.ts b/src/scope.ts index a1b1923..fcbd43a 100644 --- a/src/scope.ts +++ b/src/scope.ts @@ -259,6 +259,15 @@ export class Scope { } } + if (!res && searchType === importSearchTypes.Type) { + for (const _importGlobalScope of scope.importGlobalScopeList) { + if (_importGlobalScope.namedTypeMap.has(name)) { + res = _importGlobalScope.namedTypeMap.get(name); + break; + } + } + } + return res; } @@ -609,6 +618,7 @@ export class GlobalScope extends Scope { isCircularImport = false; importStartFuncNameList: string[] = []; + importGlobalScopeList: GlobalScope[] = []; constructor(parent: Scope | null = null) { super(parent); diff --git a/src/variable.ts b/src/variable.ts index 52edd0b..452de40 100644 --- a/src/variable.ts +++ b/src/variable.ts @@ -250,6 +250,11 @@ export class VariableScanner { const variableName = variableDeclarationNode.name.getText(); const typeName = this.typeResolver.getTsTypeName(node); let variableType = currentScope.findType(typeName); + if (!variableType) { + throw new Error( + `should get variableType for variable ${variableName}`, + ); + } if (variableType instanceof TSEnum) { variableType = variableType.memberType; diff --git a/tests/samples/export_implicit_type.ts b/tests/samples/export_implicit_type.ts new file mode 100644 index 0000000..5b15229 --- /dev/null +++ b/tests/samples/export_implicit_type.ts @@ -0,0 +1,7 @@ +/* + * Copyright (C) 2023 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +const obj_literal: {idx: number, newId: string} = {idx: 1, newId: 'hi'}; +export const obj_array = [obj_literal]; \ No newline at end of file diff --git a/tests/samples/import_implicit_type.ts b/tests/samples/import_implicit_type.ts new file mode 100644 index 0000000..6af7e4b --- /dev/null +++ b/tests/samples/import_implicit_type.ts @@ -0,0 +1,12 @@ +/* + * Copyright (C) 2023 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +import { obj_array } from "./export_implicit_type" + +export function getObjTypeFromImport() { + const obj = obj_array[0]; + console.log(obj.idx); + console.log(obj.newId); +} \ No newline at end of file diff --git a/tools/validate/wamr/validation.json b/tools/validate/wamr/validation.json index cbb8fd8..f5cd45c 100644 --- a/tools/validate/wamr/validation.json +++ b/tools/validate/wamr/validation.json @@ -2737,6 +2737,16 @@ } ] }, + { + "module": "import_implicit_type", + "entries": [ + { + "name": "getObjTypeFromImport", + "args": [], + "result": "1\nhi" + } + ] + }, { "module": "import_namespace", "entries": [