Skip to content

Commit 017450d

Browse files
test: cover CommonJS require-binding re-exports across forms, targets and build modes
Only accept a descriptor whose other properties are constant attributes: the rest is kept as written but not walked, so a variable it names was dropped as unused.
1 parent ef177c2 commit 017450d

46 files changed

Lines changed: 835 additions & 6 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎lib/dependencies/commonjs/CommonJsExportsParserPlugin.js‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,27 @@ const getValueOfPropertyDescription = (expr) => {
106106
}
107107
};
108108

109+
const DESCRIPTOR_ATTRIBUTES = new Set([
110+
"enumerable",
111+
"configurable",
112+
"writable"
113+
]);
114+
115+
/**
116+
* Whether an attribute value names no binding, like `true` or a minifier's `!0`.
117+
* @param {Expression} expr attribute value
118+
* @returns {boolean} true, when the value is a literal, possibly under a unary operator
119+
*/
120+
const isConstantAttribute = (expr) =>
121+
expr.type === "Literal" ||
122+
(expr.type === "UnaryExpression" && expr.argument.type === "Literal");
123+
109124
/**
110125
* Extracts the re-exportable expression from a property descriptor, handling
111126
* both the eager `{ value: <expr> }` form and the lazy
112127
* `{ get: () => <expr> }` / `{ get() { return <expr>; } }` accessor form used
113-
* by barrel files (e.g. webpack's own `lib/index.js`). Descriptors with a `set`
114-
* accessor are left to the generic handler.
128+
* by barrel files (e.g. webpack's own `lib/index.js`). Any other property must
129+
* be a literal attribute, since the descriptor is kept as written but not walked.
115130
* @param {Expression} expr property descriptor expression
116131
* @returns {{ expr: Expression, getter: boolean } | undefined} the value expression and whether it is a lazy getter
117132
*/
@@ -122,15 +137,19 @@ const getReexportOfPropertyDescriptor = (expr) => {
122137
/** @type {Expression | undefined} */
123138
let getFn;
124139
for (const property of expr.properties) {
125-
if (property.type === "SpreadElement" || property.computed) continue;
140+
if (property.type === "SpreadElement" || property.computed) return;
126141
const key = property.key;
127-
if (key.type !== "Identifier") continue;
128-
// A setter means the property isn't a plain re-export — bail out.
129-
if (key.name === "set") return;
142+
if (key.type !== "Identifier") return;
130143
if (key.name === "value") {
131144
valueExpr = /** @type {Expression} */ (property.value);
132145
} else if (key.name === "get") {
133146
getFn = /** @type {Expression} */ (property.value);
147+
} else if (
148+
!DESCRIPTOR_ATTRIBUTES.has(key.name) ||
149+
!isConstantAttribute(/** @type {Expression} */ (property.value))
150+
) {
151+
// a setter, an unknown key or a computed attribute: not a plain re-export
152+
return;
134153
}
135154
}
136155
if (valueExpr) return { expr: valueExpr, getter: false };
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"use strict";
2+
3+
const ns = require("../targets/cjs?descriptor-shapes");
4+
const base = { enumerable: true };
5+
const flag = true;
6+
const key = "configurable";
7+
Object.defineProperty(exports, "computed", { ["value"]: ns, enumerable: true });
8+
Object.defineProperty(exports, "spread", { ...base, value: ns });
9+
Object.defineProperty(exports, "spreadGetter", { ...base, get: () => ns });
10+
Object.defineProperty(exports, "flag", { enumerable: flag, value: ns });
11+
Object.defineProperty(exports, "flagGetter", { enumerable: flag, get: () => ns });
12+
Object.defineProperty(exports, "computedAttribute", {
13+
enumerable: true,
14+
[key]: true,
15+
value: ns
16+
});
17+
const descriptor = { enumerable: true, value: ns };
18+
Object.defineProperty(exports, "variable", descriptor);
19+
Object.defineProperty(exports, "stringKey", { "value": ns, enumerable: true });
20+
Object.defineProperty(exports, "extraKey", {
21+
enumerable: true,
22+
value: ns,
23+
unknown: ns.b
24+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict";
2+
3+
const { a, nested } = require("../targets/cjs?destructured");
4+
exports.a = a;
5+
exports.nested = nested;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
3+
const ns = require("../targets/cjs?escaping");
4+
exports.ns = ns;
5+
// the binding escapes as a whole here, so nothing may be shaken
6+
exports.keys = Object.keys(ns).sort();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"use strict";
2+
3+
const ns = require("../targets/cjs?getter-shapes");
4+
Object.defineProperty(exports, "named", {
5+
enumerable: true,
6+
get: function ns() {
7+
return ns;
8+
}
9+
});
10+
Object.defineProperty(exports, "param", {
11+
enumerable: true,
12+
get(ns = "param-default") {
13+
return ns;
14+
}
15+
});
16+
Object.defineProperty(exports, "asyncArrow", {
17+
enumerable: true,
18+
get: async () => ns
19+
});
20+
Object.defineProperty(exports, "asyncMethod", {
21+
enumerable: true,
22+
async get() {
23+
return ns;
24+
}
25+
});
26+
Object.defineProperty(exports, "generator", {
27+
enumerable: true,
28+
get: function* () {
29+
return ns;
30+
}
31+
});
32+
Object.defineProperty(exports, "twoStatements", {
33+
enumerable: true,
34+
get() {
35+
const local = ns;
36+
return local;
37+
}
38+
});
39+
let stored;
40+
Object.defineProperty(exports, "withSetter", {
41+
enumerable: true,
42+
get: () => (stored === undefined ? ns : stored),
43+
set: (value) => {
44+
stored = value;
45+
}
46+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
// the function body is walked before the binding below is declared
4+
function early() {
5+
exports.early = ns;
6+
}
7+
const ns = require("../targets/cjs?hoisted");
8+
early();
9+
exports.late = ns;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
// only a `const` binding is tracked; reassignable ones are plain values
4+
let viaLet = require("../targets/cjs?let");
5+
var viaVar = require("../targets/cjs?var");
6+
exports.viaLet = viaLet;
7+
exports.viaVar = viaVar;
8+
viaLet = { reassigned: true };
9+
exports.afterReassign = viaLet;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use strict";
2+
3+
// a named `define` registers a local module, which `require` reads without
4+
// resolving a request, so the binding must not become a re-export
5+
define("matrix-local", [], () => ({ local: "amd" }));
6+
const local = require("matrix-local");
7+
exports.local = local;
8+
exports.localMember = local.local;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
3+
const ns = require("../targets/cjs?nested-call");
4+
exports.called = ns.fn();
5+
exports.fn = ns.fn;
6+
exports.computed = ns["a"];
7+
exports.dynamicKey = ns[["b"][0]];
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
3+
const ns = require("../targets/cjs?scope-shadow");
4+
exports.outer = ns;
5+
function inner() {
6+
const ns = { local: "function" };
7+
exports.fromFunction = ns;
8+
exports.fromFunctionMember = ns.local;
9+
}
10+
inner();
11+
{
12+
const ns = { local: "block" };
13+
exports.fromBlock = ns;
14+
}
15+
const arrow = (ns) => {
16+
exports.fromParam = ns;
17+
};
18+
arrow({ local: "param" });

0 commit comments

Comments
 (0)