From acd624036eafe88400c1cd7239cb6fb8c6f59548 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Tue, 18 Oct 2022 06:22:09 +0200 Subject: [PATCH] Return correct values for unkown namespace members --- src/ast/variables/NamespaceVariable.ts | 13 +++++++++---- .../samples/namespace-literal-value/_config.js | 9 +++++++++ .../samples/namespace-literal-value/main.js | 3 +++ .../samples/namespace-literal-value/namespace.js | 2 ++ 4 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 test/function/samples/namespace-literal-value/_config.js create mode 100644 test/function/samples/namespace-literal-value/main.js create mode 100644 test/function/samples/namespace-literal-value/namespace.js diff --git a/src/ast/variables/NamespaceVariable.ts b/src/ast/variables/NamespaceVariable.ts index 207bbec7b13..a410983d5bf 100644 --- a/src/ast/variables/NamespaceVariable.ts +++ b/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 { @@ -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 } { diff --git a/test/function/samples/namespace-literal-value/_config.js b/test/function/samples/namespace-literal-value/_config.js new file mode 100644 index 00000000000..8ad52a0cd78 --- /dev/null +++ b/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); + } +}; diff --git a/test/function/samples/namespace-literal-value/main.js b/test/function/samples/namespace-literal-value/main.js new file mode 100644 index 00000000000..8eee5b022ea --- /dev/null +++ b/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); diff --git a/test/function/samples/namespace-literal-value/namespace.js b/test/function/samples/namespace-literal-value/namespace.js new file mode 100644 index 00000000000..1d944cff982 --- /dev/null +++ b/test/function/samples/namespace-literal-value/namespace.js @@ -0,0 +1,2 @@ +export const a = null; +export const b = true;