Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/ast/nodes/BinaryExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
UNKNOWN_PATH
} from '../utils/PathTracker';
import { getRenderedLiteralValue } from '../utils/renderLiteralValue';
import ExternalVariable from '../variables/ExternalVariable';
import NamespaceVariable from '../variables/NamespaceVariable';
import ExpressionStatement from './ExpressionStatement';
import type { LiteralValue } from './Literal';
Expand Down Expand Up @@ -106,7 +107,9 @@ export default class BinaryExpression extends NodeBase implements DeoptimizableE

// Optimize `'export' in namespace`
if (this.operator === 'in' && this.right.variable instanceof NamespaceVariable) {
return this.right.variable.context.traceExport(String(leftValue))[0] != null;
const [variable] = this.right.variable.context.traceExport(String(leftValue));
if (variable instanceof ExternalVariable) return UnknownValue;
return !!variable;
}

const rightValue = this.right.getLiteralValueAtPath(EMPTY_PATH, recursionTracker, origin);
Expand All @@ -120,7 +123,9 @@ export default class BinaryExpression extends NodeBase implements DeoptimizableE

getRenderedLiteralValue() {
// Only optimize `'export' in ns`
if (this.operator !== 'in' || !this.right.variable?.isNamespace) return UnknownValue;
if (this.operator !== 'in' || !(this.right.variable instanceof NamespaceVariable)) {
return UnknownValue;
}

if (this.renderedLiteralValue !== UNASSIGNED) return this.renderedLiteralValue;
return (this.renderedLiteralValue = getRenderedLiteralValue(
Expand All @@ -147,11 +152,12 @@ export default class BinaryExpression extends NodeBase implements DeoptimizableE
include(
context: InclusionContext,
includeChildrenRecursively: IncludeChildren,
_options?: InclusionOptions
options?: InclusionOptions
) {
this.included = true;
if (!this.included) this.includeNode(context);
if (typeof this.getRenderedLiteralValue() === 'symbol') {
super.include(context, includeChildrenRecursively, _options);
this.left.include(context, includeChildrenRecursively, options);
this.right.include(context, includeChildrenRecursively, options);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
description: 'disables optimization for external namespace when using the in operator',
options: {
external: ['node:crypto']
external: ['node:crypto', './ext.js']
}
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import * as nc from 'node:crypto';
import * as ext from './ext.js';

function _mergeNamespaces(n, m) {
m.forEach(function (e) {
e && typeof e !== 'string' && !Array.isArray(e) && Object.keys(e).forEach(function (k) {
if (k !== 'default' && !(k in n)) {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
});
return Object.freeze(n);
}

var pt = /*#__PURE__*/_mergeNamespaces({
__proto__: null
}, [ext]);

const crypto = 'webcrypto' in nc;
const direct = 'whatever' in ext;
const indirect = 'whatever' in pt;

export { crypto };
export { crypto, direct, indirect };
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const whatever = 1
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
import * as nc from 'node:crypto';
import * as ext from './ext.js';
import * as pt from './passthrough.js';

export const crypto = 'webcrypto' in nc;
export const direct = 'whatever' in ext;
export const indirect = 'whatever' in pt;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ext.js'
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
function c() {}

console.log(c());
console.log(true);
5 changes: 3 additions & 2 deletions test/form/samples/namespace-optimization-in-operator/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as foo from './foo';

if ('d' in foo) console.log(foo.d())
if ('c' in foo) console.log(foo.c())
if ('d' in foo) console.log(foo.d());
if ('c' in foo) console.log(foo.c());
console.log('c' in foo);
3 changes: 3 additions & 0 deletions test/form/samples/optimization-in-operator/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = defineTest({
description: 'keep foo structure'
});
11 changes: 11 additions & 0 deletions test/form/samples/optimization-in-operator/_expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var foo = {
a: 1,
b: 1
};

function check(name) {
if (name in foo) return true;
return false;
}

export { check as default };
4 changes: 4 additions & 0 deletions test/form/samples/optimization-in-operator/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
a: 1,
b: 1
};
6 changes: 6 additions & 0 deletions test/form/samples/optimization-in-operator/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import foo from './foo';

export default function check(name) {
if (name in foo) return true;
return false;
}
Loading