Skip to content

Commit

Permalink
add finding type from imported module (#150)
Browse files Browse the repository at this point in the history
Signed-off-by: Su Yihan <[email protected]>
  • Loading branch information
yviansu authored Feb 27, 2024
1 parent a3d4138 commit d0b8cd8
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/import_resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ export class ImportResolver {
importModuleScope,
);
}
if (
!globalScope.importGlobalScopeList.includes(
importModuleScope,
)
) {
globalScope.importGlobalScopeList.push(importModuleScope);
}
break;
}
case ts.SyntaxKind.ExportDeclaration: {
Expand Down
10 changes: 10 additions & 0 deletions src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -609,6 +618,7 @@ export class GlobalScope extends Scope {

isCircularImport = false;
importStartFuncNameList: string[] = [];
importGlobalScopeList: GlobalScope[] = [];

constructor(parent: Scope | null = null) {
super(parent);
Expand Down
5 changes: 5 additions & 0 deletions src/variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions tests/samples/export_implicit_type.ts
Original file line number Diff line number Diff line change
@@ -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];
12 changes: 12 additions & 0 deletions tests/samples/import_implicit_type.ts
Original file line number Diff line number Diff line change
@@ -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);
}
10 changes: 10 additions & 0 deletions tools/validate/wamr/validation.json
Original file line number Diff line number Diff line change
Expand Up @@ -2737,6 +2737,16 @@
}
]
},
{
"module": "import_implicit_type",
"entries": [
{
"name": "getObjTypeFromImport",
"args": [],
"result": "1\nhi"
}
]
},
{
"module": "import_namespace",
"entries": [
Expand Down

0 comments on commit d0b8cd8

Please sign in to comment.