Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parcel-query function to locate a symbol by local name #8452

Merged
merged 4 commits into from Dec 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
115 changes: 106 additions & 9 deletions packages/dev/query/src/cli.js
Expand Up @@ -121,23 +121,113 @@ 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));
}
}
}

function findAssetWithSymbol(local: string) {
let [, assetId, binding, ref] = nullthrows(
local.match(/^\$([^$]+)\$([^$]+)\$(.*)$/),
`symbol ${local} could not be resolved`,
);

let asset;
// 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you actually run into such a case? I can't think of a situation where the first search would fail

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right that this shouldn't be possible, but if there is an undefined symbol at runtime (due to a bug in Parcel), it likely won't be discoverable via the asset graph, but might still be discoverable in used symbols. (I have one such case that I am investigating and can share).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the case i mentioned: #8468

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}`);
}
}

Expand Down Expand Up @@ -666,6 +756,13 @@ if (initialCmd != null) {
action: findAsset,
},
],
[
'findAssetWithSymbol',
{
help: 'args: <local>. Get the asset that defines the symbol with the given local name',
action: findAssetWithSymbol,
},
],
])) {
// $FlowFixMe
server.context[name] = cmd.action;
Expand Down