mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 05:51:06 +00:00
refactor(electron): share ESM native binding projection (#16397)
This commit is contained in:
1 parent
7629b1aada
commit
8eb8ea6873
1 file changed
+48
-83
@@ -7,10 +7,9 @@ import {
|
||||
import { stripQueryAndFragment } from "../artifactPathSyntax.js";
|
||||
import type { ElectronNativeAddonBindingFinding } from "./electronStaticAnalysisTypes.js";
|
||||
import {
|
||||
isTypeOnlyExportDeclaration,
|
||||
isTypeOnlyExportSpecifier,
|
||||
isTypeOnlyImportDeclaration,
|
||||
isTypeOnlyImportSpecifier,
|
||||
semanticStaticModuleSource,
|
||||
} from "./javascriptModuleDeclaration.js";
|
||||
import { addLocatedFinding } from "./javascriptStaticAnalysisFindings.js";
|
||||
import { argumentNode, range } from "./javascriptStaticAnalysisHelpers.js";
|
||||
@@ -47,9 +46,12 @@ export const inspectElectronNativeNode = (
|
||||
node: t.Node,
|
||||
context: JavaScriptFindingContext,
|
||||
): void => {
|
||||
if (t.isImportDeclaration(node)) inspectImport(node, context);
|
||||
else if (t.isExportNamedDeclaration(node)) inspectNamedExport(node, context);
|
||||
else if (t.isExportAllDeclaration(node)) inspectExportAll(node, context);
|
||||
if (
|
||||
t.isImportDeclaration(node) ||
|
||||
t.isExportNamedDeclaration(node) ||
|
||||
t.isExportAllDeclaration(node)
|
||||
)
|
||||
inspectStaticBinding(node, context);
|
||||
else if (t.isVariableDeclarator(node) || t.isAssignmentExpression(node)) {
|
||||
const value = nativeCallReceiver(
|
||||
t.isVariableDeclarator(node) ? node.init : node.right,
|
||||
@@ -88,91 +90,54 @@ export function* finishElectronNativeBindingsSteps(
|
||||
pending.length = 0;
|
||||
}
|
||||
|
||||
const inspectImport = (
|
||||
node: t.ImportDeclaration,
|
||||
const inspectStaticBinding = (
|
||||
node: t.ImportDeclaration | t.ExportNamedDeclaration | t.ExportAllDeclaration,
|
||||
context: JavaScriptFindingContext,
|
||||
): void => {
|
||||
if (!isNativeSpecifier(node.source.value, "import")) return;
|
||||
// TypeScript erases `import type` and per-binding `type` imports; they never
|
||||
// request a runtime native-addon member.
|
||||
if (isTypeOnlyImportDeclaration(node)) return;
|
||||
const valueSpecifiers = node.specifiers.filter(
|
||||
(specifier) =>
|
||||
!(t.isImportSpecifier(specifier) && isTypeOnlyImportSpecifier(specifier)),
|
||||
);
|
||||
if (node.specifiers.length > 0 && valueSpecifiers.length === 0) return;
|
||||
const members = valueSpecifiers.flatMap((specifier) => {
|
||||
if (t.isImportDefaultSpecifier(specifier)) return ["default"];
|
||||
if (t.isImportNamespaceSpecifier(specifier)) return [];
|
||||
return [propertyName(specifier.imported)];
|
||||
});
|
||||
if (node.source == null || !isNativeSpecifier(node.source.value, "import"))
|
||||
return;
|
||||
const source = semanticStaticModuleSource(node);
|
||||
if (source === null) return;
|
||||
|
||||
const members: string[] = [];
|
||||
let namespaceAccess = false;
|
||||
if (t.isExportAllDeclaration(node)) namespaceAccess = true;
|
||||
else {
|
||||
// Type-only bindings do not request native-addon members.
|
||||
let hasRuntimeSpecifier = node.specifiers.length === 0;
|
||||
for (const specifier of node.specifiers) {
|
||||
if (
|
||||
(t.isImportSpecifier(specifier) &&
|
||||
isTypeOnlyImportSpecifier(specifier)) ||
|
||||
(t.isExportSpecifier(specifier) && isTypeOnlyExportSpecifier(specifier))
|
||||
)
|
||||
continue;
|
||||
hasRuntimeSpecifier = true;
|
||||
if (t.isImportSpecifier(specifier))
|
||||
members.push(propertyName(specifier.imported));
|
||||
else if (t.isExportSpecifier(specifier))
|
||||
members.push(propertyName(specifier.local));
|
||||
else if (
|
||||
t.isImportDefaultSpecifier(specifier) ||
|
||||
t.isExportDefaultSpecifier(specifier)
|
||||
)
|
||||
members.push("default");
|
||||
else if (
|
||||
t.isImportNamespaceSpecifier(specifier) ||
|
||||
t.isExportNamespaceSpecifier(specifier)
|
||||
)
|
||||
namespaceAccess = true;
|
||||
}
|
||||
if (!hasRuntimeSpecifier) return;
|
||||
}
|
||||
addBinding({
|
||||
context,
|
||||
node,
|
||||
specifier: node.source.value,
|
||||
kind: "import",
|
||||
specifier: source.value,
|
||||
kind: t.isImportDeclaration(node) ? "import" : "re-export",
|
||||
moduleKind: "import",
|
||||
members,
|
||||
namespaceAccess: valueSpecifiers.some((value) =>
|
||||
t.isImportNamespaceSpecifier(value),
|
||||
),
|
||||
dynamicMemberAccess: false,
|
||||
});
|
||||
};
|
||||
|
||||
const inspectNamedExport = (
|
||||
node: t.ExportNamedDeclaration,
|
||||
context: JavaScriptFindingContext,
|
||||
): void => {
|
||||
if (
|
||||
node.source === null ||
|
||||
node.source === undefined ||
|
||||
!isNativeSpecifier(node.source.value, "import")
|
||||
)
|
||||
return;
|
||||
if (isTypeOnlyExportDeclaration(node)) return;
|
||||
const valueSpecifiers = node.specifiers.filter(
|
||||
(specifier) =>
|
||||
!(t.isExportSpecifier(specifier) && isTypeOnlyExportSpecifier(specifier)),
|
||||
);
|
||||
if (node.specifiers.length > 0 && valueSpecifiers.length === 0) return;
|
||||
const members = valueSpecifiers.flatMap((specifier) => {
|
||||
if (t.isExportSpecifier(specifier)) return [propertyName(specifier.local)];
|
||||
if (t.isExportDefaultSpecifier(specifier)) return ["default"];
|
||||
return [];
|
||||
});
|
||||
addBinding({
|
||||
context,
|
||||
node,
|
||||
specifier: node.source.value,
|
||||
kind: "re-export",
|
||||
moduleKind: "import",
|
||||
members,
|
||||
namespaceAccess: valueSpecifiers.some((value) =>
|
||||
t.isExportNamespaceSpecifier(value),
|
||||
),
|
||||
dynamicMemberAccess: false,
|
||||
});
|
||||
};
|
||||
|
||||
const inspectExportAll = (
|
||||
node: t.ExportAllDeclaration,
|
||||
context: JavaScriptFindingContext,
|
||||
): void => {
|
||||
if (
|
||||
node.source === undefined ||
|
||||
!isNativeSpecifier(node.source.value, "import") ||
|
||||
isTypeOnlyExportDeclaration(node)
|
||||
)
|
||||
return;
|
||||
addBinding({
|
||||
context,
|
||||
node,
|
||||
specifier: node.source.value,
|
||||
kind: "re-export",
|
||||
moduleKind: "import",
|
||||
members: [],
|
||||
namespaceAccess: true,
|
||||
namespaceAccess,
|
||||
dynamicMemberAccess: false,
|
||||
});
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user