From 19b4f0ad53e823ae0fad468fda056830e5407b39 Mon Sep 17 00:00:00 2001 From: Eric Eldredge Date: Wed, 7 Sep 2022 12:02:53 -0400 Subject: [PATCH 1/4] Extract _findAssetNode util --- packages/dev/query/src/cli.js | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/packages/dev/query/src/cli.js b/packages/dev/query/src/cli.js index a759ce4af1f..a18e026b2b5 100644 --- a/packages/dev/query/src/cli.js +++ b/packages/dev/query/src/cli.js @@ -121,22 +121,29 @@ function getAsset(v: string) { } } -function findAsset(v: string) { +function _findAssetNode(v: string) { let assetRegex = new RegExp(v); for (let node of assetGraph.nodes.values()) { if ( node.type === 'asset' && assetRegex.test(fromProjectPathRelative(node.value.filePath)) ) { - try { - console.log( - `${bundleGraph.getAssetPublicId( - bundleGraph.getAssetById(node.id), - )} ${fromProjectPathRelative(node.value.filePath)}`, - ); - } catch (e) { - console.log(fromProjectPathRelative(node.value.filePath)); - } + return node; + } + } +} + +function findAsset(v: string) { + let node = _findAssetNode(v); + if (node) { + try { + console.log( + `${bundleGraph.getAssetPublicId( + bundleGraph.getAssetById(node.id), + )} ${fromProjectPathRelative(node.value.filePath)}`, + ); + } catch (e) { + console.log(fromProjectPathRelative(node.value.filePath)); } } } From 8bf046efec049cf0b8096af748dc7151055f3e00 Mon Sep 17 00:00:00 2001 From: Eric Eldredge Date: Wed, 7 Sep 2022 12:04:37 -0400 Subject: [PATCH 2/4] Add findAssetWithSymbol --- packages/dev/query/src/cli.js | 82 +++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/packages/dev/query/src/cli.js b/packages/dev/query/src/cli.js index a18e026b2b5..914065bc0f1 100644 --- a/packages/dev/query/src/cli.js +++ b/packages/dev/query/src/cli.js @@ -148,6 +148,81 @@ function findAsset(v: string) { } } +function findAssetWithSymbol(local: string) { + let [, assetId, binding, ref] = nullthrows( + local.match(/^\$([^$]+)\$([^$]+)\$(.*)$/), + `symbol ${local} could not be resolved`, + ); + + let asset = bundleGraph._graph.getNodeByContentKey(assetId); + + // If the asset couldn't be found by extracting a content key from the name, + // search for the local name in asset used symbols. + if (asset == null) { + outer: for (let node of assetGraph.nodes.values()) { + if (node.type === 'asset' && node.value.symbols) { + for (let symbol of node.value.symbols.values()) { + if (symbol.local === local) { + asset = node; + break outer; + } + } + } + } + } + + invariant(asset, `An asset for ${assetId} could not be found`); + invariant( + asset.type === 'asset', + `Expected ${assetId} to be an asset, but found a ${asset.type}`, + ); + + try { + console.log( + `${bundleGraph.getAssetPublicId( + bundleGraph.getAssetById(asset.id), + )} ${fromProjectPathRelative(asset.value.filePath)}`, + ); + } catch (e) { + console.log(fromProjectPathRelative(asset.value.filePath)); + } + if (binding === 'export' && asset.value.symbols) { + for (let [symbolName, symbol] of asset.value.symbols) { + if (symbol.local === local) { + if (symbol.loc) { + let locPath = symbol.loc.filePath; + let locAsset = _findAssetNode(String(locPath)); + if (locAsset != null) { + try { + console.log( + `${bundleGraph.getAssetPublicId( + bundleGraph.getAssetById(locAsset.id), + )} ${fromProjectPathRelative(locAsset.value.filePath)}`, + ); + } catch (e) { + console.log( + `imported as ${symbolName} from ${fromProjectPathRelative( + locAsset.value.filePath, + )}`, + ); + } + } else { + console.log( + `imported as ${symbolName} from ${fromProjectPathRelative( + locPath, + )}`, + ); + } + } else { + console.log(`imported as ${symbolName}`); + } + } + } + } else if (ref) { + console.log(`possibly defined as ${ref}`); + } +} + function getNodeAssetGraph(v: string) { console.log(assetGraph.getNodeByContentKey(v)); } @@ -673,6 +748,13 @@ if (initialCmd != null) { action: findAsset, }, ], + [ + 'findAssetWithSymbol', + { + help: 'args: . Get the asset that defines the symbol with the given local name', + action: findAssetWithSymbol, + }, + ], ])) { // $FlowFixMe server.context[name] = cmd.action; From 67b7a8fde456390d49fbfbb72bb158749fefc095 Mon Sep 17 00:00:00 2001 From: Eric Eldredge Date: Thu, 8 Sep 2022 19:33:54 -0400 Subject: [PATCH 3/4] Search for assets using id known to JSTransformer --- packages/dev/query/src/cli.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/dev/query/src/cli.js b/packages/dev/query/src/cli.js index 914065bc0f1..e531b88bcc7 100644 --- a/packages/dev/query/src/cli.js +++ b/packages/dev/query/src/cli.js @@ -154,13 +154,17 @@ function findAssetWithSymbol(local: string) { `symbol ${local} could not be resolved`, ); - let asset = bundleGraph._graph.getNodeByContentKey(assetId); - - // If the asset couldn't be found by extracting a content key from the name, - // search for the local name in asset used symbols. - if (asset == null) { - outer: for (let node of assetGraph.nodes.values()) { - if (node.type === 'asset' && node.value.symbols) { + let asset; + outer: for (let node of assetGraph.nodes.values()) { + if (node.type === 'asset') { + // Search against the id used by the JSTransformer and ScopeHoistingPackager, + // not the final asset id, as it may have changed with further transformation. + if (node.value.meta.id === assetId) { + asset = node; + break; + } else if (node.value.symbols) { + // If the asset couldn't be found by searching for the id, + // search for the local name in asset used symbols. for (let symbol of node.value.symbols.values()) { if (symbol.local === local) { asset = node; From 518a398ef0a6248a77403f29a517ecc7b02b6e8e Mon Sep 17 00:00:00 2001 From: Eric Eldredge Date: Thu, 8 Sep 2022 19:38:50 -0400 Subject: [PATCH 4/4] Search for asset in two passes First pass optimistically looks for a matching id. Second pass will examine asset symbols, which could be much slower if there are a lot of assets and symbols. --- packages/dev/query/src/cli.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/dev/query/src/cli.js b/packages/dev/query/src/cli.js index e531b88bcc7..edec0020a5c 100644 --- a/packages/dev/query/src/cli.js +++ b/packages/dev/query/src/cli.js @@ -155,16 +155,20 @@ function findAssetWithSymbol(local: string) { ); let asset; - outer: for (let node of assetGraph.nodes.values()) { - if (node.type === 'asset') { - // Search against the id used by the JSTransformer and ScopeHoistingPackager, - // not the final asset id, as it may have changed with further transformation. - if (node.value.meta.id === assetId) { - asset = node; - break; - } else if (node.value.symbols) { - // If the asset couldn't be found by searching for the id, - // search for the local name in asset used symbols. + // Search against the id used by the JSTransformer and ScopeHoistingPackager, + // not the final asset id, as it may have changed with further transformation. + for (let node of assetGraph.nodes.values()) { + if (node.type === 'asset' && node.value.meta.id === assetId) { + asset = node; + break; + } + } + + // If the asset couldn't be found by searching for the id, + // search for the local name in asset used symbols. + if (asset == null) { + outer: for (let node of assetGraph.nodes.values()) { + if (node.type === 'asset' && node.value.symbols) { for (let symbol of node.value.symbols.values()) { if (symbol.local === local) { asset = node;