Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle self-referencing namespaces #4991

Merged
merged 1 commit into from May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/ast/variables/NamespaceVariable.ts
Expand Up @@ -135,15 +135,15 @@ export default class NamespaceVariable extends Variable {
const memberVariables = this.getMemberVariables();
const members: [key: string | null, value: string][] = Object.entries(memberVariables)
.filter(([_, variable]) => variable.included)
.map(([name, original]) => {
if (this.referencedEarly || original.isReassigned) {
.map(([name, variable]) => {
if (this.referencedEarly || variable.isReassigned || variable === this) {
return [
null,
`get ${name}${_}()${_}{${_}return ${original.getName(getPropertyAccess)}${s}${_}}`
`get ${name}${_}()${_}{${_}return ${variable.getName(getPropertyAccess)}${s}${_}}`
];
}

return [name, original.getName(getPropertyAccess)];
return [name, variable.getName(getPropertyAccess)];
});
members.unshift([null, `__proto__:${_}null`]);

Expand Down
23 changes: 23 additions & 0 deletions test/function/samples/self-referencing-namespace/_config.js
@@ -0,0 +1,23 @@
const assert = require('node:assert');
const path = require('node:path');

const ID_MAIN = path.join(__dirname, 'main.js');

module.exports = defineTest({
description: 'supports dynamic namespaces that reference themselves',
options: {
output: { generatedCode: { constBindings: true } }
},
exports(exports) {
assert.strictEqual(exports.foo, 'foo');
assert.strictEqual(exports.ns.foo, 'foo');
assert.strictEqual(exports.ns.ns.foo, 'foo');
},
warnings: [
{
code: 'CIRCULAR_DEPENDENCY',
ids: [ID_MAIN, ID_MAIN],
message: 'Circular dependency: main.js -> main.js'
}
]
});
2 changes: 2 additions & 0 deletions test/function/samples/self-referencing-namespace/main.js
@@ -0,0 +1,2 @@
export * as ns from './main.js';
export const foo = 'foo';