1- import { Project , SourceFile } from 'ts-morph' ;
1+ import {
2+ ImportDeclaration ,
3+ Project ,
4+ ReferencedSymbol ,
5+ SourceFile ,
6+ SyntaxKind ,
7+ } from 'ts-morph' ;
28
3- import { getPackageVersion , nonNullish } from './helpers' ;
4- import { FileUsage , PackageUsage , Options } from './types' ;
9+ import {
10+ getJSXElementProps ,
11+ getPackageVersion ,
12+ getProperty ,
13+ isCallExpression ,
14+ isJSXElement ,
15+ isPropertyAccessExpression ,
16+ isValue ,
17+ line ,
18+ nonNullish ,
19+ print ,
20+ } from './helpers' ;
21+
22+ import {
23+ PackageUsage ,
24+ Options ,
25+ JSXElementUsage ,
26+ Import ,
27+ CallExpressionUsage ,
28+ PropertyAccessExpressionUsage ,
29+ FileUsage ,
30+ } from './types' ;
31+
32+ const getDetailsFromImports = ( importDeclaration : ImportDeclaration ) => {
33+ const defaultReferences = importDeclaration
34+ . getDefaultImport ( )
35+ ?. findReferences ( ) ;
36+
37+ const namedReferences = importDeclaration
38+ . getNamedImports ( )
39+ . map ( ( named ) =>
40+ named . getLastChildByKindOrThrow ( SyntaxKind . Identifier ) . findReferences ( )
41+ ) ;
42+
43+ const propsList = [
44+ ...getDetailsFromReferences ( defaultReferences ) ,
45+ ...namedReferences . map ( getDetailsFromReferences ) . flat ( ) ,
46+ ] ;
47+
48+ return propsList ;
49+ } ;
50+
51+ const getDetailsFromReferences = (
52+ references : ReferencedSymbol [ ] = [ ]
53+ ) : Import [ ] =>
54+ references . map ( ( referencedSymbol ) => ( {
55+ name : referencedSymbol . getDefinition ( ) . getNode ( ) . getText ( ) ,
56+ usages : referencedSymbol
57+ . getReferences ( )
58+ . flatMap (
59+ (
60+ reference
61+ ) :
62+ | JSXElementUsage
63+ | CallExpressionUsage
64+ | PropertyAccessExpressionUsage
65+ | never [ ] => {
66+ if ( isValue ( reference ) ) {
67+ return {
68+ line : line ( reference ) ,
69+ text : print ( reference ) ,
70+ } ;
71+ }
72+
73+ if ( isCallExpression ( reference ) ) {
74+ return {
75+ line : line ( reference ) ,
76+ text : print ( reference ) ,
77+ } ;
78+ }
79+
80+ if ( isPropertyAccessExpression ( reference ) ) {
81+ return {
82+ line : line ( reference ) ,
83+ property : getProperty ( reference ) ,
84+ text : print ( reference ) ,
85+ } ;
86+ }
87+
88+ if ( isJSXElement ( reference ) ) {
89+ return {
90+ line : line ( reference ) ,
91+ props : getJSXElementProps ( reference ) ,
92+ text : print ( reference ) ,
93+ } ;
94+ }
95+
96+ return [ ] ;
97+ }
98+ ) ,
99+ } ) ) ;
5100
6101function getPackageUsage (
7102 pkg : string ,
@@ -21,14 +116,12 @@ function getPackageUsage(
21116 return undefined ;
22117 }
23118
24- const namedImports = importDeclaration . getNamedImports ( ) ;
25- const defaultImport = importDeclaration . getDefaultImport ( ) ;
119+ const imports = getDetailsFromImports ( importDeclaration ) ;
26120
27121 return {
28122 name : sourceFile . getBaseName ( ) ,
29123 filePath : sourceFile . getFilePath ( ) ,
30- defaultImport : defaultImport ?. getText ( ) ,
31- namedImports : namedImports . map ( ( a ) => a . getName ( ) ) ,
124+ imports,
32125 } ;
33126}
34127
0 commit comments