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

Code before:
      node = interop(mod, symbol, path, node);
      return node;
    }

    return node;
  }

  function findSymbol(path, symbol) {
    if (symbol && replacements.has(symbol)) {
      symbol = replacements.get(symbol);
    }

    // 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)) {
        let binding = nullthrows(
          path.scope.getBinding(
            bundle.hasAsset(mod)
              ? assertString(mod.meta.exportsIdentifier)
              : // If this bundle doesn't have the asset, use the binding for
                // the `parcelRequire`d init function.
                getIdentifier(mod, 'init').name,
          ),
        );

        invariant(
          binding.path.getStatementParent().parentPath.isProgram(),
          "Expected binding declaration's parent to be the program",
        );

        // Hoist to the nearest path with the same scope as the exports is declared in.
        let parent = nullthrows(path.findParent(p => t.isProgram(p.parent)));
        let [decl] = parent.insertBefore(
          DEFAULT_INTEROP_TEMPLATE({
            NAME: t.identifier(name),
            MODULE: node,
          }),
        );

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

Review comment (suggestion):
@mischnic I believe this `nullthrows` is a safe assumption to make. Let me know if you know of a case where the binding may not exist.

Code after:
      node = interop(mod, symbol, path, node);
      return node;
    }

    return node;
  }

  function findSymbol(path, symbol) {
    if (symbol && replacements.has(symbol)) {
      symbol = replacements.get(symbol);
    }

    // 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)) {
        let binding = nullthrows(
          path.scope.getBinding(
            bundle.hasAsset(mod)
              ? assertString(mod.meta.exportsIdentifier)
              : // If this bundle doesn't have the asset, use the binding for
                // the `parcelRequire`d init function.
                getName(mod, 'init'),
          ),
        );

        invariant(
          binding.path.getStatementParent().parentPath.isProgram(),
          "Expected binding declaration's parent to be the program",
        );

        // Hoist to the nearest path with the same scope as the exports is declared in.
        let parent = nullthrows(path.findParent(p => t.isProgram(p.parent)));
        let [decl] = parent.insertBefore(
          DEFAULT_INTEROP_TEMPLATE({
            NAME: t.identifier(name),
            MODULE: node,
          }),
        );

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