diff --git a/cli/logging.ts b/cli/logging.ts index aeb1f9e3770..c58bab77d41 100644 --- a/cli/logging.ts +++ b/cli/logging.ts @@ -8,32 +8,35 @@ export const stderr = (...parameters: readonly unknown[]) => process.stderr.write(`${parameters.join('')}\n`); export function handleError(error: RollupError, recover = false): void { - const name = error.name || error.cause?.name; + const name = error.name || (error.cause as Error)?.name; const nameSection = name ? `${name}: ` : ''; const pluginSection = error.plugin ? `(plugin ${error.plugin}) ` : ''; const message = `${pluginSection}${nameSection}${error.message}`; - stderr(bold(red(`[!] ${bold(message.toString())}`))); + const outputLines = [bold(red(`[!] ${bold(message.toString())}`))]; if (error.url) { - stderr(cyan(error.url)); + outputLines.push(cyan(error.url)); } if (error.loc) { - stderr(`${relativeId((error.loc.file || error.id)!)} (${error.loc.line}:${error.loc.column})`); + outputLines.push( + `${relativeId((error.loc.file || error.id)!)} (${error.loc.line}:${error.loc.column})` + ); } else if (error.id) { - stderr(relativeId(error.id)); + outputLines.push(relativeId(error.id)); } if (error.frame) { - stderr(dim(error.frame)); + outputLines.push(dim(error.frame)); } if (error.stack) { - stderr(dim(error.stack)); + outputLines.push(dim(error.stack?.replace(`${nameSection}${error.message}\n`, ''))); } - stderr(''); + outputLines.push('', ''); + stderr(outputLines.join('\n')); // eslint-disable-next-line unicorn/no-process-exit if (!recover) process.exit(1); diff --git a/src/ast/variables/ExportDefaultVariable.ts b/src/ast/variables/ExportDefaultVariable.ts index e2ecada0204..7d31cefab03 100644 --- a/src/ast/variables/ExportDefaultVariable.ts +++ b/src/ast/variables/ExportDefaultVariable.ts @@ -37,6 +37,15 @@ export default class ExportDefaultVariable extends LocalVariable { } } + forbidName(name: string) { + const original = this.getOriginalVariable(); + if (original === this) { + super.forbidName(name); + } else { + original.forbidName(name); + } + } + getAssignedVariableName(): string | null { return (this.originalId && this.originalId.name) || null; } diff --git a/src/rollup/types.d.ts b/src/rollup/types.d.ts index 1c35370a821..103e6b6e9e5 100644 --- a/src/rollup/types.d.ts +++ b/src/rollup/types.d.ts @@ -19,7 +19,7 @@ export type RollupWarning = RollupLog; export interface RollupLog { binding?: string; - cause?: Error; + cause?: unknown; code?: string; exporter?: string; frame?: string; diff --git a/test/cli/samples/custom-frame-with-pos/_config.js b/test/cli/samples/custom-frame-with-pos/_config.js index e778c6418a4..2af0a903e4d 100644 --- a/test/cli/samples/custom-frame-with-pos/_config.js +++ b/test/cli/samples/custom-frame-with-pos/_config.js @@ -7,9 +7,6 @@ module.exports = { stderr: stderr => assertIncludes( stderr, - '[!] (plugin at position 1) Error: My error.\n' + - 'main.js (1:5)\n' + - 'custom code frame\n' + - 'Error: My error.' + '[!] (plugin at position 1) Error: My error.\n' + 'main.js (1:5)\n' + 'custom code frame\n' ) }; diff --git a/test/cli/samples/custom-frame/_config.js b/test/cli/samples/custom-frame/_config.js index 6754f50bfe2..bd3f7c35c69 100644 --- a/test/cli/samples/custom-frame/_config.js +++ b/test/cli/samples/custom-frame/_config.js @@ -8,7 +8,7 @@ module.exports = { assertIncludes( stderr, '[!] (plugin at position 1) Error: My error.\n' + - 'main.js\ncustom code frame\nError: My error.\n' + + 'main.js\ncustom code frame\n' + ' at Object.' ); assertIncludes(stderr, 'rollup.config.js:9:19'); diff --git a/test/cli/samples/watch/bundle-error/_config.js b/test/cli/samples/watch/bundle-error/_config.js index f8a2ebeab1a..c00b2e0161b 100644 --- a/test/cli/samples/watch/bundle-error/_config.js +++ b/test/cli/samples/watch/bundle-error/_config.js @@ -16,7 +16,7 @@ module.exports = { setTimeout(() => unlinkSync(mainFile), 300); }, abortOnStderr(data) { - if (data.includes('Error: Unexpected token')) { + if (data.includes('[!] RollupError: Unexpected token')) { setTimeout(() => atomicWriteFileSync(mainFile, 'export default 42;'), 500); return false; } diff --git a/test/function/samples/class-name-conflict2/_config.js b/test/function/samples/class-name-conflict-2/_config.js similarity index 100% rename from test/function/samples/class-name-conflict2/_config.js rename to test/function/samples/class-name-conflict-2/_config.js diff --git a/test/function/samples/class-name-conflict2/bar.js b/test/function/samples/class-name-conflict-2/bar.js similarity index 100% rename from test/function/samples/class-name-conflict2/bar.js rename to test/function/samples/class-name-conflict-2/bar.js diff --git a/test/function/samples/class-name-conflict2/declaration.js b/test/function/samples/class-name-conflict-2/declaration.js similarity index 100% rename from test/function/samples/class-name-conflict2/declaration.js rename to test/function/samples/class-name-conflict-2/declaration.js diff --git a/test/function/samples/class-name-conflict2/expression.js b/test/function/samples/class-name-conflict-2/expression.js similarity index 100% rename from test/function/samples/class-name-conflict2/expression.js rename to test/function/samples/class-name-conflict-2/expression.js diff --git a/test/function/samples/class-name-conflict2/foo.js b/test/function/samples/class-name-conflict-2/foo.js similarity index 100% rename from test/function/samples/class-name-conflict2/foo.js rename to test/function/samples/class-name-conflict-2/foo.js diff --git a/test/function/samples/class-name-conflict2/main.js b/test/function/samples/class-name-conflict-2/main.js similarity index 100% rename from test/function/samples/class-name-conflict2/main.js rename to test/function/samples/class-name-conflict-2/main.js diff --git a/test/function/samples/class-name-conflict-3/_config.js b/test/function/samples/class-name-conflict-3/_config.js new file mode 100644 index 00000000000..922cad01591 --- /dev/null +++ b/test/function/samples/class-name-conflict-3/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'does not shadow variables when preserving class names' +}; diff --git a/test/function/samples/class-name-conflict-3/foo.js b/test/function/samples/class-name-conflict-3/foo.js new file mode 100644 index 00000000000..7804111002d --- /dev/null +++ b/test/function/samples/class-name-conflict-3/foo.js @@ -0,0 +1 @@ +export default class Foo {} diff --git a/test/function/samples/class-name-conflict-3/main.js b/test/function/samples/class-name-conflict-3/main.js new file mode 100644 index 00000000000..22b9b90b33a --- /dev/null +++ b/test/function/samples/class-name-conflict-3/main.js @@ -0,0 +1,14 @@ +import Bar from './foo'; + +const wrapper = () => { + class Foo extends Bar { + static assertName() { + assert.strictEqual(this.name, 'Foo'); + assert.strictEqual(super.name, 'Foo'); + } + } + + return Foo; +}; + +wrapper().assertName(); diff --git a/test/function/samples/class-name-conflict-4/_config.js b/test/function/samples/class-name-conflict-4/_config.js new file mode 100644 index 00000000000..922cad01591 --- /dev/null +++ b/test/function/samples/class-name-conflict-4/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'does not shadow variables when preserving class names' +}; diff --git a/test/function/samples/class-name-conflict-4/foo.js b/test/function/samples/class-name-conflict-4/foo.js new file mode 100644 index 00000000000..03e6dcc163e --- /dev/null +++ b/test/function/samples/class-name-conflict-4/foo.js @@ -0,0 +1,5 @@ +var Foo = class {}; + +export default Foo; + +Foo = 'reassigned'; diff --git a/test/function/samples/class-name-conflict-4/main.js b/test/function/samples/class-name-conflict-4/main.js new file mode 100644 index 00000000000..70152e988cc --- /dev/null +++ b/test/function/samples/class-name-conflict-4/main.js @@ -0,0 +1,14 @@ +import Bar from './reexport'; + +const wrapper = () => { + class Foo extends Bar { + static assertName() { + assert.strictEqual(this.name, 'Foo'); + assert.strictEqual(super.name, 'Foo'); + } + } + + return Foo; +}; + +wrapper().assertName(); diff --git a/test/function/samples/class-name-conflict-4/reexport.js b/test/function/samples/class-name-conflict-4/reexport.js new file mode 100644 index 00000000000..ee90f7e3084 --- /dev/null +++ b/test/function/samples/class-name-conflict-4/reexport.js @@ -0,0 +1,2 @@ +import Foo from './foo'; +export default Foo;