Skip to content

Commit

Permalink
Avoid name conflicts for default exported classes (#4756)
Browse files Browse the repository at this point in the history
* Avoid name conflicts for default exported classes

* Improve coverage
  • Loading branch information
lukastaegert committed Dec 17, 2022
1 parent ffab4cd commit a27cf81
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/ast/variables/ExportDefaultVariable.ts
Expand Up @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions test/function/samples/class-name-conflict-3/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'does not shadow variables when preserving class names'
};
1 change: 1 addition & 0 deletions test/function/samples/class-name-conflict-3/foo.js
@@ -0,0 +1 @@
export default class Foo {}
14 changes: 14 additions & 0 deletions 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();
3 changes: 3 additions & 0 deletions test/function/samples/class-name-conflict-4/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'does not shadow variables when preserving class names'
};
5 changes: 5 additions & 0 deletions test/function/samples/class-name-conflict-4/foo.js
@@ -0,0 +1,5 @@
var Foo = class {};

export default Foo;

Foo = 'reassigned';
14 changes: 14 additions & 0 deletions 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();
2 changes: 2 additions & 0 deletions test/function/samples/class-name-conflict-4/reexport.js
@@ -0,0 +1,2 @@
import Foo from './foo';
export default Foo;

0 comments on commit a27cf81

Please sign in to comment.