Repository: rollup/rollup
File: test/chunking-form/index.js (JavaScript)

Code before:
			path.basename(directory) + ': ' + config.description,
			() => {
				if (config.before) {
					before(config.before);
				}
				if (config.after) {
					after(config.after);
				}
				const logs = [];
				after(() => config.logs && compareLogs(logs, config.logs));

				for (const format of config.formats || FORMATS) {
					it('generates ' + format, async () => {
						process.chdir(directory);
						const warnings = [];
						const bundle = await rollup({
							input: [directory + '/main.js'],
							onLog: (level, log) => {
								logs.push({ level, ...log });
								if (level === 'warn' && !config.expectedWarnings?.includes(log.code)) {
									warnings.push(log);
								}
							},
							strictDeprecations: true,
							...config.options
						});
						await generateAndTestBundle(
							bundle,
							{
								dir: `${directory}/_actual/${format}`,
								exports: 'auto',
								format,
								chunkFileNames: 'generated-[name].js',
								validate: true,
								...(config.options || {}).output
							},
							`${directory}/_expected/${format}`,
							config
						);
						if (warnings.length > 0) {
							const codes = new Set();
							for (const { code } of warnings) {
								codes.add(code);
							}
							throw new Error(
								`Unexpected warnings (${[...codes].join(', ')}): \n${warnings
									.map(({ message }) => `${message}\n\n`)
									.join('')}` +
									'If you expect warnings, list their codes in config.expectedWarnings'
							);
						}

Review comment (refactor):
I think this should be reverted and instead we need to fix the underlying issue, which is that the safeVariableNames currently depend on the format. It is an important feature of Rollup that you can generate different formats from the same bundle, and it should be possible to write a test that shows that the current version can cause errors with that.
I think the only way out is to remove all format-specific exceptions in the naming logic and always disallow those identifiers. Here are the places I can find:
- https://github.com/rollup/rollup/blob/9cebb0b6d9f03d11f53a8c3e7e9e3a79f567e3c6/src/Chunk.ts#L1447-L1472
  all names that are potentially added can be consolidated into the initial usedNames set. Also, it might make sense to extract the corresponding array into a global constant to avoid re-creating it on every run.
- https://github.com/rollup/rollup/blob/9cebb0b6d9f03d11f53a8c3e7e9e3a79f567e3c6/src/ast/scopes/ChildScope.ts#L64-L66
  This one is more tricky. Maybe we just add "exports" to RESERVED_NAMES https://github.com/rollup/rollup/blob/9cebb0b6d9f03d11f53a8c3e7e9e3a79f567e3c6/src/utils/RESERVED_NAMES.ts#L1-L1.
- https://github.com/rollup/rollup/blob/9cebb0b6d9f03d11f53a8c3e7e9e3a79f567e3c6/src/Chunk.ts#L1428-L1434 This one is rather tricky. So in some formats, the actual variable name will be deconflicted, but in other formats it will be "exports". To avoid format specific ambiguity, we would somehow need to rework the logic so that the es/system name is always deconflicted, even though the variable is sometimes rendered as `exports.name`

Code after:
	'chunking form',
	path.resolve(__dirname, 'samples'),
	(directory, config) => {
		(config.skip ? describe.skip : config.solo ? describe.only : describe)(
			path.basename(directory) + ': ' + config.description,
			() => {
				let bundle;

				if (config.before) {
					before(config.before);
				}
				if (config.after) {
					after(config.after);
				}
				const logs = [];
				const warnings = [];
				after(() => config.logs && compareLogs(logs, config.logs));

				for (const format of shuffle(config.formats || FORMATS)) {
					it('generates ' + format, async () => {
						process.chdir(directory);
						bundle =
							bundle ||
							(await rollup({
								input: [directory + '/main.js'],
								onLog: (level, log) => {
									logs.push({ level, ...log });
									if (level === 'warn' && !config.expectedWarnings?.includes(log.code)) {
										warnings.push(log);
									}
								},
								strictDeprecations: true,
								...config.options
							}));
						await generateAndTestBundle(
							bundle,
							{
								dir: `${directory}/_actual/${format}`,
								exports: 'auto',
								format,
								chunkFileNames: 'generated-[name].js',
								validate: true,
								...(config.options || {}).output
							},
							`${directory}/_expected/${format}`,
							config
						);
						if (warnings.length > 0) {
							const codes = new Set();
							for (const { code } of warnings) {
								codes.add(code);