GeezerVison is showing github.com — fix: reject a write to a loop's imported iteration binding · webpack/webpack@bcef6e8
File treeExpand file treeCollapse file tree Open diff view settings configCases/concatenate-modules/assigned-import-binding
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/dependencies/HarmonyImportDependencyParserPlugin.js +27-7Lines changed: 27 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -38,6 +38,7 @@ const { ImportPhaseUtils, createGetImportPhase } = require("./ImportPhase"); | 38 | 38 | * PrivateIdentifier, | 39 | 39 | * Identifier, | 40 | 40 | * MemberExpression, | | 41 | + * Pattern, | 41 | 42 | * UpdateExpression | 42 | 43 | * } from "estree" | 43 | 44 | */ | @@ -52,7 +53,8 @@ const { ImportPhaseUtils, createGetImportPhase } = require("./ImportPhase"); | 52 | 53 | * @import JavascriptParser, { | 53 | 54 | * ImportAttributes, | 54 | 55 | * Range, | 55 | | - * Members | | 56 | + * Members, | | 57 | + * WriteStatement | 56 | 58 | * } from "../javascript/JavascriptParser" | 57 | 59 | */ | 58 | 60 | /** @import { Ids, ExportPresenceMode } from "./HarmonyImportDependency" */ | @@ -405,6 +407,16 @@ class HarmonyImportDependencyParserPlugin { | 405 | 407 | } | 406 | 408 | } | 407 | 409 | ); | | 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 | + | 408 | 420 | /** | 409 | 421 | * Whether a member write lands on the namespace object itself, which is | 410 | 422 | * read-only. A property of an imported object is an ordinary write. | @@ -418,7 +430,7 @@ class HarmonyImportDependencyParserPlugin { | 418 | 430 | /** | 419 | 431 | * Records the write target of an assignment or update to an imported | 420 | 432 | * 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 | 422 | 434 | * @param {Identifier | MemberExpression | undefined} target the written reference, if any | 423 | 435 | * @returns {void} | 424 | 436 | */ | @@ -449,17 +461,25 @@ class HarmonyImportDependencyParserPlugin { | 449 | 461 | expr.left.type === "Identifier" ? expr.left : undefined | 450 | 462 | ); | 451 | 463 | }); | 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; | 454 | 472 | }); | 455 | 473 | // A namespace member is written through the reference its member chain | 456 | 474 | // emits, so both write forms flag that reference rather than the binding. | 457 | | - parser.hooks.updateMemberChain | | 475 | + parser.hooks.writeMemberChain | 458 | 476 | .for(harmonySpecifierTag) | 459 | | - .tap(PLUGIN_NAME, (expr, members) => { | | 477 | + .tap(PLUGIN_NAME, (node, members) => { | 460 | 478 | const settings = /** @type {HarmonySettings} */ (parser.currentTagData); | 461 | 479 | if (isNamespaceMemberWrite(settings, members)) { | 462 | | - assignmentTarget = /** @type {MemberExpression} */ (expr.argument); | | 480 | + assignmentTarget = /** @type {MemberExpression} */ ( | | 481 | + writeTargetOf(node) | | 482 | + ); | 463 | 483 | } | 464 | 484 | }); | 465 | 485 | parser.hooks.expression | |
Collapse file Copy file name to clipboardExpand all lines: lib/javascript/JavascriptParser.js +40-35Lines changed: 40 additions & 35 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -354,6 +354,7 @@ class VariableInfo { | 354 | 354 |
| 355 | 355 | /** @typedef {string[]} CalleeMembers */ | 356 | 356 | /** @typedef {string[]} Members */ | | 357 | +/** @typedef {UpdateExpression | ForInStatement | ForOfStatement} WriteStatement a write that is not an assignment's left side */ | 357 | 358 | /** @typedef {boolean[]} MembersOptionals */ | 358 | 359 | /** @typedef {Range[]} MemberRanges */ | 359 | 360 |
| @@ -1092,11 +1093,11 @@ class JavascriptParser extends Parser { | 1092 | 1093 | rename: new HookMap(() => new SyncBailHook(["initExpression"])), | 1093 | 1094 | /** @type {HookMap<SyncBailHook<[AssignmentExpression], boolean | void>>} */ | 1094 | 1095 | 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"]) | 1100 | 1101 | ), | 1101 | 1102 | /** @type {HookMap<SyncBailHook<[AssignmentExpression, Members], boolean | void>>} */ | 1102 | 1103 | assignMemberChain: new HookMap( | @@ -3029,7 +3030,7 @@ class JavascriptParser extends Parser { | 3029 | 3030 | if (statement.left.type === "VariableDeclaration") { | 3030 | 3031 | this.blockPreWalkVariableDeclaration(statement.left); | 3031 | 3032 | this.walkVariableDeclaration(statement.left); | 3032 | | - } else { | | 3033 | + } else if (!this._walkWriteTarget(statement.left, statement)) { | 3033 | 3034 | this.walkPattern(statement.left); | 3034 | 3035 | } | 3035 | 3036 |
| @@ -3073,7 +3074,7 @@ class JavascriptParser extends Parser { | 3073 | 3074 | if (statement.left.type === "VariableDeclaration") { | 3074 | 3075 | this.blockPreWalkVariableDeclaration(statement.left); | 3075 | 3076 | this.walkVariableDeclaration(statement.left); | 3076 | | - } else { | | 3077 | + } else if (!this._walkWriteTarget(statement.left, statement)) { | 3077 | 3078 | this.walkPattern(statement.left); | 3078 | 3079 | } | 3079 | 3080 |
| @@ -4167,41 +4168,45 @@ class JavascriptParser extends Parser { | 4167 | 4168 | * @param {UpdateExpression} expression the update expression | 4168 | 4169 | */ | 4169 | 4170 | 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 && | 4191 | 4199 | this._hasHooksForInfo( | 4192 | | - this.hooks.updateMemberChain, | | 4200 | + this.hooks.writeMemberChain, | 4193 | 4201 | /** @type {string | VariableInfo} */ (this._memberRootInfo) | 4194 | 4202 | ) && | 4195 | 4203 | this.callHooksForInfo( | 4196 | | - this.hooks.updateMemberChain, | | 4204 | + this.hooks.writeMemberChain, | 4197 | 4205 | /** @type {string | VariableInfo} */ (this._memberRootInfo), | 4198 | | - expression, | | 4206 | + node, | 4199 | 4207 | this._members | 4200 | 4208 | ) | 4201 | | - ) { | 4202 | | - return; | 4203 | | - } | 4204 | | - this.walkExpression(expression.argument); | | 4209 | + ); | 4205 | 4210 | } | 4206 | 4211 |
| 4207 | 4212 | /** | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -9,6 +9,12 @@ import { | 9 | 9 | writeAliased, | 10 | 10 | writeThroughNamespace | 11 | 11 | } from "./quoted-writer"; | | 12 | +import { | | 13 | + loopAwaitIdentifier, | | 14 | + loopInIdentifier, | | 15 | + loopOfIdentifier, | | 16 | + loopOfNamespaceMember | | 17 | +} from "./loops"; | 12 | 18 |
| 13 | 19 | it("should reject a write to an imported binding", () => { | 14 | 20 | expect(writeLet).toThrow(TypeError); | @@ -46,6 +52,16 @@ it("should reject a write to an export no identifier can spell", () => { | 46 | 52 | expect(readQuoted()).toBe(1); | 47 | 53 | }); | 48 | 54 |
| | 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 | + | 49 | 65 | it("should still concatenate every module", () => { | 50 | 66 | const concatenated = __STATS__.modules.filter((m) => m.modules); | 51 | 67 | expect(concatenated).toHaveLength(1); | @@ -54,6 +70,7 @@ it("should still concatenate every module", () => { | 54 | 70 | "./counter.js", | 55 | 71 | "./helper.js", | 56 | 72 | "./index.js", | | 73 | + "./loops.js", | 57 | 74 | "./namespace.js", | 58 | 75 | "./quoted-writer.js", | 59 | 76 | "./quoted.js", | |
Collapse file Copy file name to clipboard +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 Copy file name to clipboardExpand all lines: test/unitCases/JavascriptParser.unittest.js +6-6Lines changed: 6 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1596,10 +1596,10 @@ describe("JavascriptParser", () => { | 1596 | 1596 | }); | 1597 | 1597 | }); | 1598 | 1598 |
| 1599 | | - describe("update hooks", () => { | | 1599 | + describe("write hooks", () => { | 1600 | 1600 | /** | 1601 | 1601 | * @param {string} source source to parse | 1602 | | - * @param {"update" | "updateMemberChain"} hook the hook to tap | | 1602 | + * @param {"write" | "writeMemberChain"} hook the hook to tap | 1603 | 1603 | * @param {boolean} handled what the tap returns | 1604 | 1604 | * @returns {string[]} the names the walk reported as read | 1605 | 1605 | */ | @@ -1627,13 +1627,13 @@ describe("JavascriptParser", () => { | 1627 | 1627 | }; | 1628 | 1628 |
| 1629 | 1629 | 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"]); | 1632 | 1632 | }); | 1633 | 1633 |
| 1634 | 1634 | 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([]); | 1637 | 1637 | }); | 1638 | 1638 | }); | 1639 | 1639 | }); |
Collapse file Copy file name to clipboardExpand all lines: types.d.ts +5-4Lines changed: 5 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13123,9 +13123,9 @@ declare class JavascriptParser extends ParserClass { | 13123 | 13123 | canRename: HookMap<SyncBailHook<[ExpressionEstreeIndex], boolean | void>>; | 13124 | 13124 | rename: HookMap<SyncBailHook<[ExpressionEstreeIndex], boolean | void>>; | 13125 | 13125 | 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> | 13129 | 13129 | >; | 13130 | 13130 | assignMemberChain: HookMap< | 13131 | 13131 | SyncBailHook<[AssignmentExpression, string[]], boolean | void> | @@ -29475,7 +29475,7 @@ declare interface SyncModuleIdsPluginOptions { | 29475 | 29475 | /** | 29476 | 29476 | * operation mode (defaults to merge) | 29477 | 29477 | */ | 29478 | | - mode?: "update" | "read" | "create" | "merge"; | | 29478 | + mode?: "read" | "create" | "merge" | "update"; | 29479 | 29479 | } | 29480 | 29480 | type SyncWasmModuleBuildMeta = KnownBuildMeta & | 29481 | 29481 | Record<string, any> & | @@ -30923,6 +30923,7 @@ type WriteFileOptions = | 30923 | 30923 | declare interface WriteOnlySet<T> { | 30924 | 30924 | add: (item: T) => void; | 30925 | 30925 | } | | 30926 | +type WriteStatement = UpdateExpression | ForInStatement | ForOfStatement; | 30926 | 30927 | type WriteStreamOptions = StreamOptions & { | 30927 | 30928 | fs?: null | CreateWriteStreamFSImplementation; | 30928 | 30929 | flush?: boolean; | |
|