Skip to content

Commit 724f3ae

Browse files
committed
Output module imports
1 parent ef00872 commit 724f3ae

File tree

18 files changed

+62
-1
lines changed

18 files changed

+62
-1
lines changed

src/models/module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ export class ModuleNode {
44
constructor(
55
public name: string,
66
public exports: ExportNode[],
7+
public imports: string[] | undefined = undefined
78
) {}
89
}

src/parsers/moduleParser.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,15 @@ export function parseModule(sourceFile: ts.SourceFile, context: ParserContext):
3737
.relative(compilerOptions.rootDir!, JSON.parse(sourceFileSymbol.name))
3838
.replace(/\\/g, '/');
3939

40-
return new ModuleNode(relativeModulePath, parsedModuleExports);
40+
const imports: string[] = sourceFile.statements
41+
.filter(s => ts.isImportDeclaration(s) && s.moduleSpecifier)
42+
.map((statement) => {
43+
const importDeclaraion = statement as ts.ImportDeclaration;
44+
const text = importDeclaraion.moduleSpecifier.getText();
45+
return text.substring(1, text.length - 1); // Remove quotes
46+
});
47+
48+
return new ModuleNode(relativeModulePath, parsedModuleExports, imports.length > 0 ? imports : undefined);
4149
} catch (error) {
4250
if (!(error instanceof ParserError)) {
4351
throw new ParserError(error, parsedSymbolStack);

test/base-ui-component/output.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3073,5 +3073,8 @@
30733073
]
30743074
}
30753075
}
3076+
],
3077+
"imports": [
3078+
"react"
30763079
]
30773080
}

test/callable-types-with-other-properties/output.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,8 @@
4141
]
4242
}
4343
}
44+
],
45+
"imports": [
46+
"react"
4447
]
4548
}

test/component-function-variable/output.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,5 +236,8 @@
236236
]
237237
}
238238
}
239+
],
240+
"imports": [
241+
"react"
239242
]
240243
}

test/forwardRef/output.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,5 +161,8 @@
161161
"tags": []
162162
}
163163
}
164+
],
165+
"imports": [
166+
"react"
164167
]
165168
}

test/imports/input.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import * as React from 'react';
2+
import { ExportNode } from '../../src/models/export';

test/imports/output.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "test/imports/input",
3+
"exports": [],
4+
"imports": [
5+
"react",
6+
"../../src/models/export"
7+
]
8+
}

test/jsdocs/output.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,5 +171,8 @@
171171
]
172172
}
173173
}
174+
],
175+
"imports": [
176+
"react"
174177
]
175178
}

test/memo/output.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,8 @@
100100
"tags": []
101101
}
102102
}
103+
],
104+
"imports": [
105+
"react"
103106
]
104107
}

0 commit comments

Comments
 (0)