GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: reject a write to a loop's imported iteration binding · webpack/webpack@bcef6e8

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib
    • dependencies
      • HarmonyImportDependencyParserPlugin.js
    • javascript
      • JavascriptParser.js
  • test
    • configCases/concatenate-modules/assigned-import-binding
      • index.js
      • loops.js
    • unitCases
      • JavascriptParser.unittest.js
  • types.d.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/dependencies/HarmonyImportDependencyParserPlugin.js‎

Copy file name to clipboardExpand all lines: lib/dependencies/HarmonyImportDependencyParserPlugin.js
+27-7Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const { ImportPhaseUtils, createGetImportPhase } = require("./ImportPhase");
3838
* PrivateIdentifier,
3939
* Identifier,
4040
* MemberExpression,
41+
* Pattern,
4142
* UpdateExpression
4243
* } from "estree"
4344
*/
@@ -52,7 +53,8 @@ const { ImportPhaseUtils, createGetImportPhase } = require("./ImportPhase");
5253
* @import JavascriptParser, {
5354
* ImportAttributes,
5455
* Range,
55-
* Members
56+
* Members,
57+
* WriteStatement
5658
* } from "../javascript/JavascriptParser"
5759
*/
5860
/** @import { Ids, ExportPresenceMode } from "./HarmonyImportDependency" */
@@ -405,6 +407,16 @@ class HarmonyImportDependencyParserPlugin {
405407
}
406408
}
407409
);
410+
/**
411+
* The reference an update or a loop writes to.
412+
* @param {WriteStatement} node the construct performing the write
413+
* @returns {Pattern | MemberExpression} the written reference
414+
*/
415+
const writeTargetOf = (node) =>
416+
/** @type {Pattern | MemberExpression} */ (
417+
node.type === "UpdateExpression" ? node.argument : node.left
418+
);
419+
408420
/**
409421
* Whether a member write lands on the namespace object itself, which is
410422
* read-only. A property of an imported object is an ordinary write.
@@ -418,7 +430,7 @@ class HarmonyImportDependencyParserPlugin {
418430
/**
419431
* Records the write target of an assignment or update to an imported
420432
* binding, so the reference walked next renders as a throwing write.
421-
* @param {AssignmentExpression | UpdateExpression} expr the write
433+
* @param {AssignmentExpression | WriteStatement} expr the write
422434
* @param {Identifier | MemberExpression | undefined} target the written reference, if any
423435
* @returns {void}
424436
*/
@@ -449,17 +461,25 @@ class HarmonyImportDependencyParserPlugin {
449461
expr.left.type === "Identifier" ? expr.left : undefined
450462
);
451463
});
452-
parser.hooks.update.for(harmonySpecifierTag).tap(PLUGIN_NAME, (expr) => {
453-
markImportedBindingWrite(expr, /** @type {Identifier} */ (expr.argument));
464+
parser.hooks.write.for(harmonySpecifierTag).tap(PLUGIN_NAME, (node) => {
465+
const target = /** @type {Identifier} */ (writeTargetOf(node));
466+
markImportedBindingWrite(node, target);
467+
// A loop's binding walks nothing on its own, so the reference the
468+
// write renders as has to be walked here to exist at all.
469+
if (node.type === "UpdateExpression") return;
470+
parser.walkExpression(target);
471+
return true;
454472
});
455473
// A namespace member is written through the reference its member chain
456474
// emits, so both write forms flag that reference rather than the binding.
457-
parser.hooks.updateMemberChain
475+
parser.hooks.writeMemberChain
458476
.for(harmonySpecifierTag)
459-
.tap(PLUGIN_NAME, (expr, members) => {
477+
.tap(PLUGIN_NAME, (node, members) => {
460478
const settings = /** @type {HarmonySettings} */ (parser.currentTagData);
461479
if (isNamespaceMemberWrite(settings, members)) {
462-
assignmentTarget = /** @type {MemberExpression} */ (expr.argument);
480+
assignmentTarget = /** @type {MemberExpression} */ (
481+
writeTargetOf(node)
482+
);
463483
}
464484
});
465485
parser.hooks.expression
Collapse file

‎lib/javascript/JavascriptParser.js‎

Copy file name to clipboardExpand all lines: lib/javascript/JavascriptParser.js
+40-35Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ class VariableInfo {
354354

355355
/** @typedef {string[]} CalleeMembers */
356356
/** @typedef {string[]} Members */
357+
/** @typedef {UpdateExpression | ForInStatement | ForOfStatement} WriteStatement a write that is not an assignment's left side */
357358
/** @typedef {boolean[]} MembersOptionals */
358359
/** @typedef {Range[]} MemberRanges */
359360

@@ -1092,11 +1093,11 @@ class JavascriptParser extends Parser {
10921093
rename: new HookMap(() => new SyncBailHook(["initExpression"])),
10931094
/** @type {HookMap<SyncBailHook<[AssignmentExpression], boolean | void>>} */
10941095
assign: new HookMap(() => new SyncBailHook(["expression"])),
1095-
/** @type {HookMap<SyncBailHook<[UpdateExpression], boolean | void>>} */
1096-
update: new HookMap(() => new SyncBailHook(["expression"])),
1097-
/** @type {HookMap<SyncBailHook<[UpdateExpression, Members], boolean | void>>} */
1098-
updateMemberChain: new HookMap(
1099-
() => new SyncBailHook(["expression", "members"])
1096+
/** @type {HookMap<SyncBailHook<[WriteStatement], boolean | void>>} */
1097+
write: new HookMap(() => new SyncBailHook(["node"])),
1098+
/** @type {HookMap<SyncBailHook<[WriteStatement, Members], boolean | void>>} */
1099+
writeMemberChain: new HookMap(
1100+
() => new SyncBailHook(["node", "members"])
11001101
),
11011102
/** @type {HookMap<SyncBailHook<[AssignmentExpression, Members], boolean | void>>} */
11021103
assignMemberChain: new HookMap(
@@ -3029,7 +3030,7 @@ class JavascriptParser extends Parser {
30293030
if (statement.left.type === "VariableDeclaration") {
30303031
this.blockPreWalkVariableDeclaration(statement.left);
30313032
this.walkVariableDeclaration(statement.left);
3032-
} else {
3033+
} else if (!this._walkWriteTarget(statement.left, statement)) {
30333034
this.walkPattern(statement.left);
30343035
}
30353036

@@ -3073,7 +3074,7 @@ class JavascriptParser extends Parser {
30733074
if (statement.left.type === "VariableDeclaration") {
30743075
this.blockPreWalkVariableDeclaration(statement.left);
30753076
this.walkVariableDeclaration(statement.left);
3076-
} else {
3077+
} else if (!this._walkWriteTarget(statement.left, statement)) {
30773078
this.walkPattern(statement.left);
30783079
}
30793080

@@ -4167,41 +4168,45 @@ class JavascriptParser extends Parser {
41674168
* @param {UpdateExpression} expression the update expression
41684169
*/
41694170
walkUpdateExpression(expression) {
4170-
if (expression.argument.type === "Identifier") {
4171-
if (this._strictInModuleOutput) {
4172-
this._checkStrictModeAssignTarget(expression.argument.name, expression);
4173-
}
4174-
// An update reads and writes its target, so a tap that cares about the
4175-
// write gets its own hook: `assign` carries an AssignmentExpression.
4176-
if (
4177-
this._callHooksForName1(
4178-
this.hooks.update,
4179-
expression.argument.name,
4180-
expression
4181-
)
4182-
) {
4183-
return;
4184-
}
4185-
} else if (
4186-
expression.argument.type === "MemberExpression" &&
4187-
this._resolveMemberExpression(
4188-
expression.argument,
4189-
ALLOWED_MEMBER_TYPES_EXPRESSION
4190-
) === 1 &&
4171+
if (
4172+
this._strictInModuleOutput &&
4173+
expression.argument.type === "Identifier"
4174+
) {
4175+
this._checkStrictModeAssignTarget(expression.argument.name, expression);
4176+
}
4177+
if (this._walkWriteTarget(expression.argument, expression)) return;
4178+
this.walkExpression(expression.argument);
4179+
}
4180+
4181+
/**
4182+
* Offers a write that is not an assignment's left side — an update's target
4183+
* or a loop's binding — to the taps that care, `assign` carrying only an
4184+
* AssignmentExpression.
4185+
* @param {Expression | Pattern} target the written reference
4186+
* @param {WriteStatement} node the construct performing the write
4187+
* @returns {boolean} true when a tap rendered the write itself
4188+
*/
4189+
_walkWriteTarget(target, node) {
4190+
if (target.type === "Identifier") {
4191+
return Boolean(
4192+
this._callHooksForName1(this.hooks.write, target.name, node)
4193+
);
4194+
}
4195+
return Boolean(
4196+
target.type === "MemberExpression" &&
4197+
this._resolveMemberExpression(target, ALLOWED_MEMBER_TYPES_EXPRESSION) ===
4198+
1 &&
41914199
this._hasHooksForInfo(
4192-
this.hooks.updateMemberChain,
4200+
this.hooks.writeMemberChain,
41934201
/** @type {string | VariableInfo} */ (this._memberRootInfo)
41944202
) &&
41954203
this.callHooksForInfo(
4196-
this.hooks.updateMemberChain,
4204+
this.hooks.writeMemberChain,
41974205
/** @type {string | VariableInfo} */ (this._memberRootInfo),
4198-
expression,
4206+
node,
41994207
this._members
42004208
)
4201-
) {
4202-
return;
4203-
}
4204-
this.walkExpression(expression.argument);
4209+
);
42054210
}
42064211

42074212
/**
Collapse file

‎test/configCases/concatenate-modules/assigned-import-binding/index.js‎

Copy file name to clipboardExpand all lines: test/configCases/concatenate-modules/assigned-import-binding/index.js
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import {
99
writeAliased,
1010
writeThroughNamespace
1111
} from "./quoted-writer";
12+
import {
13+
loopAwaitIdentifier,
14+
loopInIdentifier,
15+
loopOfIdentifier,
16+
loopOfNamespaceMember
17+
} from "./loops";
1218

1319
it("should reject a write to an imported binding", () => {
1420
expect(writeLet).toThrow(TypeError);
@@ -46,6 +52,16 @@ it("should reject a write to an export no identifier can spell", () => {
4652
expect(readQuoted()).toBe(1);
4753
});
4854

55+
it("should reject a write to a loop's iteration binding", () => {
56+
expect(loopOfIdentifier).toThrow(TypeError);
57+
expect(loopInIdentifier).toThrow(TypeError);
58+
expect(loopOfNamespaceMember).toThrow(TypeError);
59+
expect(readCount()).toBe(0);
60+
});
61+
62+
it("should reject it in an async iteration too", () =>
63+
expect(loopAwaitIdentifier()).rejects.toThrow(TypeError));
64+
4965
it("should still concatenate every module", () => {
5066
const concatenated = __STATS__.modules.filter((m) => m.modules);
5167
expect(concatenated).toHaveLength(1);
@@ -54,6 +70,7 @@ it("should still concatenate every module", () => {
5470
"./counter.js",
5571
"./helper.js",
5672
"./index.js",
73+
"./loops.js",
5774
"./namespace.js",
5875
"./quoted-writer.js",
5976
"./quoted.js",
Collapse file

‎test/configCases/concatenate-modules/assigned-import-binding/loops.js‎

Copy file name to clipboard
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as counter from "./counter";
2+
import { count } from "./counter";
3+
4+
export function loopOfIdentifier() {
5+
for (count of [7]) {
6+
// the write happens before the body runs
7+
}
8+
}
9+
10+
export function loopInIdentifier() {
11+
for (count in { seven: 7 }) {
12+
// same, for the other iteration form
13+
}
14+
}
15+
16+
export function loopOfNamespaceMember() {
17+
for (counter.count of [8]) {
18+
// a namespace member is read-only like the binding itself
19+
}
20+
}
21+
22+
export async function loopAwaitIdentifier() {
23+
for await (count of [Promise.resolve(9)]) {
24+
// `for await` walks the same path as `for of`
25+
}
26+
}
Collapse file

‎test/unitCases/JavascriptParser.unittest.js‎

Copy file name to clipboardExpand all lines: test/unitCases/JavascriptParser.unittest.js
+6-6Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,10 +1596,10 @@ describe("JavascriptParser", () => {
15961596
});
15971597
});
15981598

1599-
describe("update hooks", () => {
1599+
describe("write hooks", () => {
16001600
/**
16011601
* @param {string} source source to parse
1602-
* @param {"update" | "updateMemberChain"} hook the hook to tap
1602+
* @param {"write" | "writeMemberChain"} hook the hook to tap
16031603
* @param {boolean} handled what the tap returns
16041604
* @returns {string[]} the names the walk reported as read
16051605
*/
@@ -1627,13 +1627,13 @@ describe("JavascriptParser", () => {
16271627
};
16281628

16291629
it("reports an update and still walks the target it reads", () => {
1630-
expect(readsWith("x++;", "update", false)).toEqual(["x"]);
1631-
expect(readsWith("x.y++;", "updateMemberChain", false)).toEqual(["x"]);
1630+
expect(readsWith("x++;", "write", false)).toEqual(["x"]);
1631+
expect(readsWith("x.y++;", "writeMemberChain", false)).toEqual(["x"]);
16321632
});
16331633

16341634
it("lets a tap that rendered the whole update stop the walk", () => {
1635-
expect(readsWith("x++;", "update", true)).toEqual([]);
1636-
expect(readsWith("x.y++;", "updateMemberChain", true)).toEqual([]);
1635+
expect(readsWith("x++;", "write", true)).toEqual([]);
1636+
expect(readsWith("x.y++;", "writeMemberChain", true)).toEqual([]);
16371637
});
16381638
});
16391639
});
Collapse file

‎types.d.ts‎

Copy file name to clipboardExpand all lines: types.d.ts
+5-4Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13123,9 +13123,9 @@ declare class JavascriptParser extends ParserClass {
1312313123
canRename: HookMap<SyncBailHook<[ExpressionEstreeIndex], boolean | void>>;
1312413124
rename: HookMap<SyncBailHook<[ExpressionEstreeIndex], boolean | void>>;
1312513125
assign: HookMap<SyncBailHook<[AssignmentExpression], boolean | void>>;
13126-
update: HookMap<SyncBailHook<[UpdateExpression], boolean | void>>;
13127-
updateMemberChain: HookMap<
13128-
SyncBailHook<[UpdateExpression, string[]], boolean | void>
13126+
write: HookMap<SyncBailHook<[WriteStatement], boolean | void>>;
13127+
writeMemberChain: HookMap<
13128+
SyncBailHook<[WriteStatement, string[]], boolean | void>
1312913129
>;
1313013130
assignMemberChain: HookMap<
1313113131
SyncBailHook<[AssignmentExpression, string[]], boolean | void>
@@ -29475,7 +29475,7 @@ declare interface SyncModuleIdsPluginOptions {
2947529475
/**
2947629476
* operation mode (defaults to merge)
2947729477
*/
29478-
mode?: "update" | "read" | "create" | "merge";
29478+
mode?: "read" | "create" | "merge" | "update";
2947929479
}
2948029480
type SyncWasmModuleBuildMeta = KnownBuildMeta &
2948129481
Record<string, any> &
@@ -30923,6 +30923,7 @@ type WriteFileOptions =
3092330923
declare interface WriteOnlySet<T> {
3092430924
add: (item: T) => void;
3092530925
}
30926+
type WriteStatement = UpdateExpression | ForInStatement | ForOfStatement;
3092630927
type WriteStreamOptions = StreamOptions & {
3092730928
fs?: null | CreateWriteStreamFSImplementation;
3092830929
flush?: boolean;

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.