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

Code before:

  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'),
        );

        getScopeBefore(parent).registerDeclaration(decl);
      }

      return t.identifier(name);

Review comment (suggestion):
```suggestion
                getName(mod, 'init'),
```

The binding name is actually already in the `node` parameter, but it's either an Identifier or a CallExpression. Not really cleaner.

Code after:

  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'),
        );

        getScopeBefore(parent).registerDeclaration(decl);
      }

      return t.identifier(name);