Repository: rollup/rollup
File: src/ast/nodes/ExportDefaultDeclaration.ts (TypeScript)

Code before:
	declare needsBoundaries: true;
	declare scope: ModuleScope;
	declare type: NodeType.tExportDefaultDeclaration;
	declare variable: ExportDefaultVariable;

	declare private declarationName: string | undefined;

	include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
		this.included = true;
		this.declaration.include(context, includeChildrenRecursively);
		if (includeChildrenRecursively) {
			this.scope.context.includeVariableInModule(this.variable, UNKNOWN_PATH, context);
		}
	}

	includePath(path: ObjectPath, context: InclusionContext): void {
		this.included = true;
		this.declaration.includePath(path, context);
	}

	initialise(): void {
		super.initialise();
		const declaration = this.declaration as FunctionDeclaration | ClassDeclaration;
		this.declarationName =
			(declaration.id && declaration.id.name) || (this.declaration as Identifier).name;

		let name = this.declarationName || this.scope.context.getModuleName();

		// check if there's already a variable with the same name in the scope. see no-treeshake-default-export-conflict
		const existingVariable = this.scope.variables.get(name);
		if (existingVariable) {
			name = `${name}_default`;
		}

		this.variable = this.scope.addExportDefaultDeclaration(name, this, this.scope.context);
		this.scope.context.addExport(this);
	}

	removeAnnotations(code: MagicString) {
		this.declaration.removeAnnotations(code);
	}

	render(code: MagicString, options: RenderOptions, nodeRenderOptions?: NodeRenderOptions): void {
		const { start, end } = nodeRenderOptions as { end: number; start: number };
		const declarationStart = getDeclarationStart(code.original, this.start);

		if (this.declaration instanceof FunctionDeclaration) {
			this.renderNamedDeclaration(
				code,
				declarationStart,
				this.declaration.id === null

Review comment (bug):
Can you say a word about this change? How do you trigger a bug without this change? If so, can we add a specific test for this bug?
I am also asking because this will only deconflict the name in case the variable declaration is above the default export. If you switch the order, no deconflicting will happen as then, the scope will not yet contain the variable. Just in case this is important.

Code after:
	declare needsBoundaries: true;
	declare scope: ModuleScope;
	declare type: NodeType.tExportDefaultDeclaration;
	declare variable: ExportDefaultVariable;

	declare private declarationName: string | undefined;

	bind(): void {
		super.bind();
		const name = this.declarationName || this.scope.context.getModuleName();
		// Check if there's already a variable with the same name in the scope. This
		// can cause inconsistencies when using the cache.
		this.variable.name = this.scope.variables.get(name) ? `${name}_default` : name;
	}

	include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
		this.included = true;
		this.declaration.include(context, includeChildrenRecursively);
		if (includeChildrenRecursively) {
			this.scope.context.includeVariableInModule(this.variable, UNKNOWN_PATH, context);
		}
	}

	includePath(path: ObjectPath, context: InclusionContext): void {
		this.included = true;
		this.declaration.includePath(path, context);
	}

	initialise(): void {
		super.initialise();
		const declaration = this.declaration as FunctionDeclaration | ClassDeclaration;
		this.declarationName =
			(declaration.id && declaration.id.name) || (this.declaration as Identifier).name;
		this.variable = this.scope.addExportDefaultDeclaration(this, this.scope.context);
		this.scope.context.addExport(this);
	}

	removeAnnotations(code: MagicString) {
		this.declaration.removeAnnotations(code);
	}

	render(code: MagicString, options: RenderOptions, nodeRenderOptions?: NodeRenderOptions): void {
		const { start, end } = nodeRenderOptions as { end: number; start: number };
		const declarationStart = getDeclarationStart(code.original, this.start);

		if (this.declaration instanceof FunctionDeclaration) {
			this.renderNamedDeclaration(
				code,
				declarationStart,
				this.declaration.id === null
					? getFunctionIdInsertPosition(code.original, declarationStart)