From 5667519ea6010f1a7208ca0eea9ec0d685304b24 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Fri, 16 Dec 2022 13:33:37 +0100 Subject: [PATCH 1/2] Avoid name conflicts for default exported classes --- src/ast/variables/ExportDefaultVariable.ts | 8 ++++++++ .../_config.js | 0 .../bar.js | 0 .../declaration.js | 0 .../expression.js | 0 .../foo.js | 0 .../main.js | 0 .../samples/class-name-conflict-3/_config.js | 3 +++ test/function/samples/class-name-conflict-3/foo.js | 1 + .../function/samples/class-name-conflict-3/main.js | 14 ++++++++++++++ 10 files changed, 26 insertions(+) rename test/function/samples/{class-name-conflict2 => class-name-conflict-2}/_config.js (100%) rename test/function/samples/{class-name-conflict2 => class-name-conflict-2}/bar.js (100%) rename test/function/samples/{class-name-conflict2 => class-name-conflict-2}/declaration.js (100%) rename test/function/samples/{class-name-conflict2 => class-name-conflict-2}/expression.js (100%) rename test/function/samples/{class-name-conflict2 => class-name-conflict-2}/foo.js (100%) rename test/function/samples/{class-name-conflict2 => class-name-conflict-2}/main.js (100%) create mode 100644 test/function/samples/class-name-conflict-3/_config.js create mode 100644 test/function/samples/class-name-conflict-3/foo.js create mode 100644 test/function/samples/class-name-conflict-3/main.js diff --git a/src/ast/variables/ExportDefaultVariable.ts b/src/ast/variables/ExportDefaultVariable.ts index e2ecada0204..977bf32904d 100644 --- a/src/ast/variables/ExportDefaultVariable.ts +++ b/src/ast/variables/ExportDefaultVariable.ts @@ -37,6 +37,14 @@ export default class ExportDefaultVariable extends LocalVariable { } } + forbidName(name: string) { + super.forbidName(name); + const original = this.getOriginalVariable(); + if (original !== this) { + original.forbidName(name); + } + } + getAssignedVariableName(): string | null { return (this.originalId && this.originalId.name) || null; } 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(); From a1da1513e99be8dd67db4f5d71025a47c1badfe9 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Fri, 16 Dec 2022 14:15:05 +0100 Subject: [PATCH 2/2] Improve coverage --- src/ast/variables/ExportDefaultVariable.ts | 5 +++-- .../samples/class-name-conflict-4/_config.js | 3 +++ test/function/samples/class-name-conflict-4/foo.js | 5 +++++ .../function/samples/class-name-conflict-4/main.js | 14 ++++++++++++++ .../samples/class-name-conflict-4/reexport.js | 2 ++ 5 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 test/function/samples/class-name-conflict-4/_config.js create mode 100644 test/function/samples/class-name-conflict-4/foo.js create mode 100644 test/function/samples/class-name-conflict-4/main.js create mode 100644 test/function/samples/class-name-conflict-4/reexport.js diff --git a/src/ast/variables/ExportDefaultVariable.ts b/src/ast/variables/ExportDefaultVariable.ts index 977bf32904d..7d31cefab03 100644 --- a/src/ast/variables/ExportDefaultVariable.ts +++ b/src/ast/variables/ExportDefaultVariable.ts @@ -38,9 +38,10 @@ export default class ExportDefaultVariable extends LocalVariable { } forbidName(name: string) { - super.forbidName(name); const original = this.getOriginalVariable(); - if (original !== this) { + if (original === this) { + super.forbidName(name); + } else { original.forbidName(name); } } 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;