Skip to content

Commit

Permalink
Return correct values for unkown namespace members (#4684)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Oct 18, 2022
1 parent 1619ef2 commit 088c877
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/ast/variables/NamespaceVariable.ts
@@ -1,11 +1,14 @@
import type { AstContext } from '../../Module';
import type Module from '../../Module';
import type { AstContext } from '../../Module';
import { getToStringTagValue, MERGE_NAMESPACES_VARIABLE } from '../../utils/interopHelpers';
import type { RenderOptions } from '../../utils/renderHelpers';
import { getSystemExportStatement } from '../../utils/systemJsRendering';
import type Identifier from '../nodes/Identifier';
import type { LiteralValueOrUnknown } from '../nodes/shared/Expression';
import { UnknownValue } from '../nodes/shared/Expression';
import type ChildScope from '../scopes/ChildScope';
import type { ObjectPath } from '../utils/PathTracker';
import { SymbolToStringTag } from '../utils/PathTracker';
import Variable from './Variable';

export default class NamespaceVariable extends Variable {
Expand All @@ -29,9 +32,11 @@ export default class NamespaceVariable extends Variable {
this.name = identifier.name;
}

getLiteralValueAtPath(): LiteralValueOrUnknown {
// This can only happen for Symbol.toStringTag right now
return 'Module';
getLiteralValueAtPath(path: ObjectPath): LiteralValueOrUnknown {
if (path[0] === SymbolToStringTag) {
return 'Module';
}
return UnknownValue;
}

getMemberVariables(): { [name: string]: Variable } {
Expand Down
9 changes: 9 additions & 0 deletions test/function/samples/namespace-literal-value/_config.js
@@ -0,0 +1,9 @@
const assert = require('node:assert');

module.exports = {
description: 'does not simplify accessing unknown properties from namespaces',
exports({ isNull }) {
assert.strictEqual(isNull('a'), true);
assert.strictEqual(isNull('b'), false);
}
};
3 changes: 3 additions & 0 deletions test/function/samples/namespace-literal-value/main.js
@@ -0,0 +1,3 @@
import * as ns from './namespace';

export const isNull = prop => (ns[prop] === null ? true : false);
2 changes: 2 additions & 0 deletions test/function/samples/namespace-literal-value/namespace.js
@@ -0,0 +1,2 @@
export const a = null;
export const b = true;

0 comments on commit 088c877

Please sign in to comment.