Skip to content

Commit b095e8d

Browse files
committed
remove arguments, replace whole expression
1 parent 4fe01b2 commit b095e8d

File tree

11 files changed

+63
-51
lines changed

11 files changed

+63
-51
lines changed

lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js

+13-21
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,9 @@ const HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDepend
2121
* a.x !== undefined; // if x value statically analyzable
2222
*/
2323
class HarmonyEvaluatedImportSpecifierDependency extends HarmonyImportSpecifierDependency {
24-
constructor(
25-
request,
26-
sourceOrder,
27-
ids,
28-
name,
29-
range,
30-
assertions,
31-
operator,
32-
value,
33-
importSpecifierRange
34-
) {
24+
constructor(request, sourceOrder, ids, name, range, assertions, operator) {
3525
super(request, sourceOrder, ids, name, range, false, assertions);
3626
this.operator = operator;
37-
this.value = value;
38-
this.importSpecifierRange = importSpecifierRange;
3927
}
4028

4129
get type() {
@@ -46,16 +34,12 @@ class HarmonyEvaluatedImportSpecifierDependency extends HarmonyImportSpecifierDe
4634
super.serialize(context);
4735
const { write } = context;
4836
write(this.operator);
49-
write(this.value);
50-
write(this.importSpecifierRange);
5137
}
5238

5339
deserialize(context) {
5440
super.deserialize(context);
5541
const { read } = context;
5642
this.operator = read();
57-
this.value = read();
58-
this.importSpecifierRange = read();
5943
}
6044
}
6145

@@ -89,12 +73,20 @@ HarmonyEvaluatedImportSpecifierDependency.Template = class HarmonyEvaluatedImpor
8973
if (typeof value === "boolean") {
9074
source.replace(dep.range[0], dep.range[1] - 1, `${value}`);
9175
} else {
92-
this._applyForIds(
93-
dependency,
76+
const usedName = exportsInfo.getUsedName(ids, runtime);
77+
78+
const code = this._getCodeForIds(
79+
dep,
9480
source,
9581
templateContext,
96-
ids.slice(0, -1),
97-
dep.importSpecifierRange
82+
ids.slice(0, -1)
83+
);
84+
source.replace(
85+
dep.range[0],
86+
dep.range[1] - 1,
87+
`${
88+
usedName ? JSON.stringify(usedName[usedName.length - 1]) : '""'
89+
} in ${code}`
9890
);
9991
}
10092
}

lib/dependencies/HarmonyImportDependencyParserPlugin.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,7 @@ module.exports = class HarmonyImportDependencyParserPlugin {
159159
settings.name,
160160
expression.range,
161161
settings.assertions,
162-
"in",
163-
leftPart,
164-
rightPart.range
162+
"in"
165163
);
166164
dep.directImport = members.length === 0;
167165
dep.asiSafe = !parser.isAsiPosition(expression.range[0]);

lib/dependencies/HarmonyImportSpecifierDependency.js

+17-9
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,23 @@ HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependen
267267
if (connection && !connection.isTargetActive(runtime)) return;
268268

269269
const ids = dep.getIds(moduleGraph);
270-
this._applyForIds(dependency, source, templateContext, ids, dep.range);
270+
const exportExpr = this._getCodeForIds(dep, source, templateContext, ids);
271+
const range = dep.range;
272+
if (dep.shorthand) {
273+
source.insert(range[1], `: ${exportExpr}`);
274+
} else {
275+
source.replace(range[0], range[1] - 1, exportExpr);
276+
}
271277
}
272278

273-
_applyForIds(dependency, source, templateContext, ids, range) {
274-
const dep = /** @type {HarmonyImportSpecifierDependency} */ (dependency);
279+
/**
280+
* @param {HarmonyImportSpecifierDependency} dep dependency
281+
* @param {ReplaceSource} source source
282+
* @param {DependencyTemplateContext} templateContext context
283+
* @param {string[]} ids ids
284+
* @returns {string} generated code
285+
*/
286+
_getCodeForIds(dep, source, templateContext, ids) {
275287
const { moduleGraph, module, runtime, concatenationScope } =
276288
templateContext;
277289
const connection = moduleGraph.getConnection(dep);
@@ -305,7 +317,7 @@ HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependen
305317
);
306318
}
307319
} else {
308-
super.apply(dependency, source, templateContext);
320+
super.apply(dep, source, templateContext);
309321

310322
const { runtimeTemplate, initFragments, runtimeRequirements } =
311323
templateContext;
@@ -326,11 +338,7 @@ HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependen
326338
runtimeRequirements
327339
});
328340
}
329-
if (dep.shorthand) {
330-
source.insert(range[1], `: ${exportExpr}`);
331-
} else {
332-
source.replace(range[0], range[1] - 1, exportExpr);
333-
}
341+
return exportExpr;
334342
}
335343
};
336344

test/cases/parsing/harmony-export-import-specifier/index.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import { h } from "./h.js";
88
import * as m from "./m";
99
import * as o from "./o";
1010
import * as p from "./p";
11+
import * as q from "./q";
12+
import * as so from "./side-effect-free/o";
13+
import * as sm from "./side-effect-free/m";
1114

1215
it("namespace export as from commonjs should override named export", function () {
1316
expect(x).toBe(1);
@@ -40,15 +43,20 @@ it("complex case should work correctly", () => {
4043
});
4144

4245
it("should handle 'm in n' case", () => {
43-
const obj = { a: "a" in m };
44-
expect(obj.a).toBe(true);
45-
expect("a" in o).toBe(true);
46-
expect("a" in p).toBe(false);
47-
expect("c" in m).toBe(false);
48-
expect("c" in (false ? ({}) : m.d)).toBe(true);
49-
expect("d" in m.d).toBe(false);
46+
const obj = { aaa: "aaa" in m };
47+
expect(obj.aaa).toBe(true);
48+
expect("aaa" in o).toBe(true);
49+
expect("aaa" in p).toBe(false);
50+
expect("ccc" in m).toBe(false);
51+
expect("aaa" in q).toBe(true);
52+
expect("aaa" in so).toBe(true);
53+
expect("ccc" in sm).toBe(false);
54+
expect("ccc" in (false ? {} : m.ddd)).toBe(true);
55+
expect("ccc" in (false ? {} : sm.ddd)).toBe(true);
56+
expect("ddd" in m.ddd).toBe(false);
57+
expect("ddd" in sm.ddd).toBe(false);
5058
if (process.env.NODE_ENV === "production") {
51-
expect(m.d.usedA).toBe(false);
59+
expect(m.ddd.usedA).toBe(false);
5260
expect(m.usedB).toBe(false);
5361
expect(m.usedA).toBe(true);
5462
expect(m.canMangleA).toBe(true);
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export const a = 1;
2-
export const b = 2;
3-
export * as d from "./n";
4-
export const usedA = __webpack_exports_info__.a.used;
5-
export const canMangleA = __webpack_exports_info__.c.canMangle;
6-
export const usedB = __webpack_exports_info__.b.used;
1+
export const aaa = 1;
2+
export const bbb = 2;
3+
export * as ddd from "./n";
4+
export const usedA = __webpack_exports_info__.aaa.used;
5+
export const canMangleA = __webpack_exports_info__.ccc.canMangle;
6+
export const usedB = __webpack_exports_info__.bbb.used;
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export const c = 3;
2-
export const m = () => ({});
3-
export const a = 1;
1+
export const ccc = 3;
2+
export const mmm = () => ({});
3+
export const aaa = 1;
44
export const usedA = __webpack_exports_info__.a.used;
55
export const canMangleC = __webpack_exports_info__.c.canMangle;
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
const exports_ = { a: 1, b: 2 };
1+
const exports_ = { aaa: 1, bbb: 2 };
22
module.exports = exports_;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./o";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "../m";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "../o";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"sideEffects": false
3+
}

0 commit comments

Comments
 (0)