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

Remove import bindings after rewriting live references #10640

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions packages/babel-helper-module-transforms/src/index.ts
Expand Up @@ -25,6 +25,8 @@ import normalizeModuleAndLoadMetadata, {
hasExports,
isSideEffectImport,
validateImportInteropOption,
removeExportDeclarations,
removeImportDeclarations,
} from "./normalize-and-load-metadata";
import type {
InteropType,
Expand Down Expand Up @@ -91,7 +93,9 @@ export function rewriteModuleStatementsAndPrepareHeader(
rewriteThis(path);
}

removeExportDeclarations(path);
rewriteLiveReferences(path, meta);
removeImportDeclarations(path);

if (strictMode !== false) {
const hasStrict = path.node.directives.some(directive => {
Expand Down
Expand Up @@ -123,8 +123,6 @@ export default function normalizeModuleAndLoadMetadata(
stringSpecifiers,
);

removeModuleDeclarations(programPath);

// Reuse the imported namespace name if there is one.
for (const [, metadata] of source) {
if (metadata.importsNamespace.size > 0) {
Expand Down Expand Up @@ -535,11 +533,9 @@ function nameAnonymousExports(programPath: NodePath<t.Program>) {
});
}

function removeModuleDeclarations(programPath: NodePath<t.Program>) {
export function removeExportDeclarations(programPath: NodePath<t.Program>) {
programPath.get("body").forEach(child => {
if (child.isImportDeclaration()) {
child.remove();
} else if (child.isExportNamedDeclaration()) {
if (child.isExportNamedDeclaration()) {
if (child.node.declaration) {
// @ts-expect-error todo(flow->ts): avoid mutations
child.node.declaration._blockHoist = child.node._blockHoist;
Expand Down Expand Up @@ -568,3 +564,11 @@ function removeModuleDeclarations(programPath: NodePath<t.Program>) {
}
});
}

export function removeImportDeclarations(programPath: NodePath<t.Program>) {
programPath.get("body").forEach(child => {
if (child.isImportDeclaration()) {
child.remove();
}
});
}
Expand Up @@ -250,11 +250,14 @@ const rewriteReferencesVisitor: Visitor<RewriteReferencesVisitorState> = {
);
}

const localBinding = path.scope.getBinding(localName);
const rootBinding = scope.getBinding(localName);

// redeclared in this scope
if (rootBinding !== localBinding) return;
if (process.env.BABEL_8_BREAKING) {
if (!scope.isSameBinding(path.scope, localName)) return;
} else {
if (path.scope.getBinding(localName) !== scope.getBinding(localName)) {
return;
}
}

const ref = buildImportReference(importData, path.node);

Expand Down Expand Up @@ -315,8 +318,14 @@ const rewriteReferencesVisitor: Visitor<RewriteReferencesVisitorState> = {
const localName = left.node.name;

// redeclared in this scope
if (scope.getBinding(localName) !== path.scope.getBinding(localName)) {
return;
if (process.env.BABEL_8_BREAKING) {
if (!scope.isSameBinding(path.scope, localName)) return;
} else {
if (
scope.getBinding(localName) !== path.scope.getBinding(localName)
) {
return;
}
}

const exportedNames = exported.get(localName);
Expand Down Expand Up @@ -347,8 +356,11 @@ const rewriteReferencesVisitor: Visitor<RewriteReferencesVisitorState> = {
} else {
const ids = left.getOuterBindingIdentifiers();
const programScopeIds = Object.keys(ids).filter(
localName =>
scope.getBinding(localName) === path.scope.getBinding(localName),
process.env.BABEL_8_BREAKING
? localName => scope.isSameBinding(path.scope, localName)
: localName =>
scope.getBinding(localName) ===
path.scope.getBinding(localName),
);
const id = programScopeIds.find(localName => imported.has(localName));

Expand Down
5 changes: 5 additions & 0 deletions packages/babel-traverse/src/scope/index.ts
Expand Up @@ -1212,4 +1212,9 @@ export default class Scope {
}
} while ((scope = scope.parent));
}

isSameBinding(scope2: Scope, name: string) {
const binding = this.getBinding(name);
return !!binding && binding === scope2.getBinding(name);
}
}