Repository: rollup/rollup
File: src/utils/systemJsRendering.ts (TypeScript)

Code before:
import type MagicString from 'magic-string';
import type Variable from '../ast/variables/Variable';
import type { RenderOptions } from './renderHelpers';

export function getSystemExportStatement(
	exportedVariables: readonly Variable[],
	{ exportNamesByVariable, snippets: { _, getObject, getPropertyAccess } }: RenderOptions,
	modifier = ''
): string {
	if (
		exportedVariables.length === 1 &&
		exportNamesByVariable.get(exportedVariables[0])!.length === 1
	) {
		const variable = exportedVariables[0];
		return `exports(${JSON.stringify(
			exportNamesByVariable.get(variable)![0]
		)},${_}${variable.getName(getPropertyAccess)}${modifier})`;
	} else {
		const fields: [key: string | null, value: string][] = [];
		fields.push([null, `__proto__:${_}null`]);
		for (const variable of exportedVariables) {
			for (const exportName of exportNamesByVariable.get(variable)!) {
				fields.push([exportName, variable.getName(getPropertyAccess) + modifier]);
			}
		}
		return `exports(${getObject(fields, { lineBreakIndent: null })})`;
	}
}

// This is only invoked if there is exactly one export name
export function renderSystemExportExpression(
	exportedVariable: Variable,
	expressionStart: number,
	expressionEnd: number,
	code: MagicString,
	{ exportNamesByVariable, snippets: { _ } }: RenderOptions
): void {
	code.prependRight(
		expressionStart,
		`exports(${JSON.stringify(exportNamesByVariable.get(exportedVariable)![0])},${_}`
	);
	code.appendLeft(expressionEnd, ')');
}

export function renderSystemExportFunction(

Review comment (suggestion):
I do not unterstand why we need this. This generates a lot of noise and if we set a potential __proto__ variable via `["__proto__"]`, this should not have any effect.

Code after:
import type MagicString from 'magic-string';
import type Variable from '../ast/variables/Variable';
import type { RenderOptions } from './renderHelpers';

export function getSystemExportStatement(
	exportedVariables: readonly Variable[],
	{ exportNamesByVariable, snippets: { _, getObject, getPropertyAccess } }: RenderOptions,
	modifier = ''
): string {
	if (
		exportedVariables.length === 1 &&
		exportNamesByVariable.get(exportedVariables[0])!.length === 1
	) {
		const variable = exportedVariables[0];
		return `exports(${JSON.stringify(
			exportNamesByVariable.get(variable)![0]
		)},${_}${variable.getName(getPropertyAccess)}${modifier})`;
	} else {
		const fields: [key: string, value: string][] = [];
		for (const variable of exportedVariables) {
			for (const exportName of exportNamesByVariable.get(variable)!) {
				fields.push([exportName, variable.getName(getPropertyAccess) + modifier]);
			}
		}
		return `exports(${getObject(fields, { lineBreakIndent: null })})`;
	}
}

// This is only invoked if there is exactly one export name
export function renderSystemExportExpression(
	exportedVariable: Variable,
	expressionStart: number,
	expressionEnd: number,
	code: MagicString,
	{ exportNamesByVariable, snippets: { _ } }: RenderOptions
): void {
	code.prependRight(
		expressionStart,
		`exports(${JSON.stringify(exportNamesByVariable.get(exportedVariable)![0])},${_}`
	);
	code.appendLeft(expressionEnd, ')');
}

export function renderSystemExportFunction(
	exportedVariables: readonly Variable[],