Repository: parcel-bundler/parcel
File: packages/shared/scope-hoisting/src/link.js (JavaScript)

Code before:
    }

    // if the symbol is in the scope there is no need to remap it
    if (symbol && path.scope.getProgramParent().hasBinding(symbol)) {
      return t.identifier(symbol);
    }

    return null;
  }

  function interop(mod, originalName, path, node) {
    // Handle interop for default imports of CommonJS modules.
    if (mod.meta.isCommonJS && originalName === 'default') {
      let name = getName(mod, '$interop$default');
      if (!path.scope.getBinding(name)) {
        // Hoist to the nearest path with the same scope as the exports is declared in
        let binding = path.scope.getBinding(
          assertString(mod.meta.exportsIdentifier),
        );
        let parent;
        if (binding) {
          parent = path.findParent(p => t.isProgram(p.parent));
        }

        if (!parent) {
          parent = path.getStatementParent();
        }

        let [decl] = parent.insertBefore(
          DEFAULT_INTEROP_TEMPLATE({
            NAME: t.identifier(name),
            MODULE: node,
          }),
        );

        if (binding) {
          binding.reference(
            decl.get<NodePath<Identifier>>('declarations.0.init'),
          );
        }

        getScopeBefore(parent).registerDeclaration(decl);
      }

      return t.identifier(name);
    }

    // if there is a CommonJS export return $id$exports.name
    if (originalName !== '*') {
      return t.memberExpression(node, t.identifier(originalName));
    }

Review comment (bug):
Not sure if Will forwarded this to you, but can you please add a invariant here to make sure that `binding.path.parentPath.isProgram()` is true to prevent a broken bundle output in the future.

Code after:
    }

    // if the symbol is in the scope there is no need to remap it
    if (symbol && path.scope.getProgramParent().hasBinding(symbol)) {
      return t.identifier(symbol);
    }

    return null;
  }

  function interop(mod, originalName, path, node) {
    // Handle interop for default imports of CommonJS modules.
    if (mod.meta.isCommonJS && originalName === 'default') {
      let name = getName(mod, '$interop$default');
      if (!path.scope.getBinding(name)) {
        // Hoist to the nearest path with the same scope as the exports is declared in
        let binding = path.scope.getBinding(
          assertString(mod.meta.exportsIdentifier),
        );
        let parent;
        if (binding) {
          invariant(
            binding.path.getStatementParent().parentPath.isProgram(),
            "Expected binding declaration's parent to be the program",
          );
          parent = path.findParent(p => t.isProgram(p.parent));
        }

        if (!parent) {
          parent = path.getStatementParent();
        }

        let [decl] = parent.insertBefore(
          DEFAULT_INTEROP_TEMPLATE({
            NAME: t.identifier(name),
            MODULE: node,
          }),
        );

        if (binding) {
          binding.reference(
            decl.get<NodePath<Identifier>>('declarations.0.init'),
          );
        }

        getScopeBefore(parent).registerDeclaration(decl);
      }

      return t.identifier(name);
    }