Skip to content

Commit

Permalink
Handle self-referencing namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed May 14, 2023
1 parent 9a4c9e1 commit b030892
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
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';

0 comments on commit b030892

Please sign in to comment.