@@ -121,23 +121,113 @@ function getAsset(v: string) {
121121 }
122122}
123123
124- function findAsset ( v : string ) {
124+ function _findAssetNode ( v : string ) {
125125 let assetRegex = new RegExp ( v ) ;
126126 for ( let node of assetGraph . nodes . values ( ) ) {
127127 if (
128128 node . type === 'asset' &&
129129 assetRegex . test ( fromProjectPathRelative ( node . value . filePath ) )
130130 ) {
131- try {
132- console . log (
133- `${ bundleGraph . getAssetPublicId (
134- bundleGraph . getAssetById ( node . id ) ,
135- ) } ${ fromProjectPathRelative ( node . value . filePath ) } `,
136- ) ;
137- } catch ( e ) {
138- console . log ( fromProjectPathRelative ( node . value . filePath ) ) ;
131+ return node ;
132+ }
133+ }
134+ }
135+
136+ function findAsset ( v : string ) {
137+ let node = _findAssetNode ( v ) ;
138+ if ( node ) {
139+ try {
140+ console . log (
141+ `${ bundleGraph . getAssetPublicId (
142+ bundleGraph . getAssetById ( node . id ) ,
143+ ) } ${ fromProjectPathRelative ( node . value . filePath ) } `,
144+ ) ;
145+ } catch ( e ) {
146+ console . log ( fromProjectPathRelative ( node . value . filePath ) ) ;
147+ }
148+ }
149+ }
150+
151+ function findAssetWithSymbol ( local : string ) {
152+ let [ , assetId , binding , ref ] = nullthrows (
153+ local . match ( / ^ \$ ( [ ^ $ ] + ) \$ ( [ ^ $ ] + ) \$ ( .* ) $ / ) ,
154+ `symbol ${ local } could not be resolved` ,
155+ ) ;
156+
157+ let asset ;
158+ // Search against the id used by the JSTransformer and ScopeHoistingPackager,
159+ // not the final asset id, as it may have changed with further transformation.
160+ for ( let node of assetGraph . nodes . values ( ) ) {
161+ if ( node . type === 'asset' && node . value . meta . id === assetId ) {
162+ asset = node ;
163+ break ;
164+ }
165+ }
166+
167+ // If the asset couldn't be found by searching for the id,
168+ // search for the local name in asset used symbols.
169+ if ( asset == null ) {
170+ outer: for ( let node of assetGraph . nodes . values ( ) ) {
171+ if ( node . type === 'asset' && node . value . symbols ) {
172+ for ( let symbol of node . value . symbols . values ( ) ) {
173+ if ( symbol . local === local ) {
174+ asset = node ;
175+ break outer;
176+ }
177+ }
178+ }
179+ }
180+ }
181+
182+ invariant ( asset , `An asset for ${ assetId } could not be found` ) ;
183+ invariant (
184+ asset . type === 'asset' ,
185+ `Expected ${ assetId } to be an asset, but found a ${ asset . type } ` ,
186+ ) ;
187+
188+ try {
189+ console . log (
190+ `${ bundleGraph . getAssetPublicId (
191+ bundleGraph . getAssetById ( asset . id ) ,
192+ ) } ${ fromProjectPathRelative ( asset . value . filePath ) } `,
193+ ) ;
194+ } catch ( e ) {
195+ console . log ( fromProjectPathRelative ( asset . value . filePath ) ) ;
196+ }
197+ if ( binding === 'export' && asset . value . symbols ) {
198+ for ( let [ symbolName , symbol ] of asset . value . symbols ) {
199+ if ( symbol . local === local ) {
200+ if ( symbol . loc ) {
201+ let locPath = symbol . loc . filePath ;
202+ let locAsset = _findAssetNode ( String ( locPath ) ) ;
203+ if ( locAsset != null ) {
204+ try {
205+ console . log (
206+ `${ bundleGraph . getAssetPublicId (
207+ bundleGraph . getAssetById ( locAsset . id ) ,
208+ ) } ${ fromProjectPathRelative ( locAsset . value . filePath ) } `,
209+ ) ;
210+ } catch ( e ) {
211+ console . log (
212+ `imported as ${ symbolName } from ${ fromProjectPathRelative (
213+ locAsset . value . filePath ,
214+ ) } `,
215+ ) ;
216+ }
217+ } else {
218+ console . log (
219+ `imported as ${ symbolName } from ${ fromProjectPathRelative (
220+ locPath ,
221+ ) } `,
222+ ) ;
223+ }
224+ } else {
225+ console . log ( `imported as ${ symbolName } ` ) ;
226+ }
139227 }
140228 }
229+ } else if ( ref ) {
230+ console . log ( `possibly defined as ${ ref } ` ) ;
141231 }
142232}
143233
@@ -666,6 +756,13 @@ if (initialCmd != null) {
666756 action : findAsset ,
667757 } ,
668758 ] ,
759+ [
760+ 'findAssetWithSymbol' ,
761+ {
762+ help : 'args: <local>. Get the asset that defines the symbol with the given local name' ,
763+ action : findAssetWithSymbol ,
764+ } ,
765+ ] ,
669766 ] ) ) {
670767 // $FlowFixMe
671768 server . context [ name ] = cmd . action ;
0 commit comments