Repository: parcel-bundler/parcel
File: packages/transformers/js/src/JSTransformer.js (JavaScript)

Code before:
        asset.addDependency({
          specifier: dep.specifier,
          specifierType: dep.kind === 'Require' ? 'commonjs' : 'esm',
          loc: convertLoc(dep.loc),
          priority: dep.kind === 'DynamicImport' ? 'lazy' : 'sync',
          isOptional: dep.is_optional,
          meta,
          resolveFrom: isHelper ? __filename : undefined,
          env,
        });
      }
    }

    asset.meta.id = asset.id;
    if (hoist_result) {
      asset.symbols.ensure();
      for (let {exported, local, loc} of hoist_result.exported_symbols) {
        asset.symbols.set(exported, local, convertLoc(loc));
      }

      let deps = new Map();
      for (let dep of asset.getDependencies()) {
        if (!deps.has(dep.meta.placeholder ?? dep.specifier)) {
          deps.set(dep.meta.placeholder ?? dep.specifier, []);
        }
        deps.get(dep.meta.placeholder ?? dep.specifier)?.push(dep);
      }
      for (let depArr of deps.values()) {
        for (let dep of depArr) {
          dep.symbols.ensure();
        }
      }

      for (let {
        source,
        local,
        imported,
        loc,
        kind,
      } of hoist_result.imported_symbols) {
        for (let dep of nullthrows(deps.get(source))) {
          if (
            (dep.meta.kind === 'Require' ||
              dep.meta.kind === 'Import' ||
              dep.meta.kind === 'DynamicImport') &&
            dep.meta.kind !== kind
          ) {
            continue;
          }
          dep.symbols.set(imported, local, convertLoc(loc));
        }

Review comment (suggestion):
A possible alternative way of implementing this could be to make the map keyed by `(dep.meta.placeholder ?? dep.specifier) + dep.specifierType`, rather than an array of dependencies per specifier.

Code after:
            includeNodeModules: true,
          };
        }

        // Add required version range for helpers.
        let range;
        if (isHelper) {
          let idx = dep.specifier.indexOf('/');
          if (dep.specifier[0] === '@') {
            idx = dep.specifier.indexOf('/', idx + 1);
          }
          let module = idx >= 0 ? dep.specifier.slice(0, idx) : dep.specifier;
          range = pkg.dependencies[module];
        }

        asset.addDependency({
          specifier: dep.specifier,
          specifierType: dep.kind === 'Require' ? 'commonjs' : 'esm',
          loc: convertLoc(dep.loc),
          priority: dep.kind === 'DynamicImport' ? 'lazy' : 'sync',
          isOptional: dep.is_optional,
          meta,
          resolveFrom: isHelper ? __filename : undefined,
          range,
          env,
        });
      }
    }

    asset.meta.id = asset.id;
    if (hoist_result) {
      asset.symbols.ensure();
      for (let {exported, local, loc} of hoist_result.exported_symbols) {
        asset.symbols.set(exported, local, convertLoc(loc));
      }

      // deps is a map of dependencies that are keyed by placeholder or specifier
      // If a placeholder is present, that is used first since placeholders are
      // hashed with DependencyKind's.
      // If not, the specifier is used along with its specifierType appended to
      // it to separate dependencies with the same specifier.
      let deps = new Map(
        asset
          .getDependencies()
          .map(dep => [
            dep.meta.placeholder ??
              dep.specifier +
                (dep.specifierType === 'esm' ? dep.specifierType : ''),
            dep,
          ]),
      );