Skip to content

Commit a067e66

Browse files
authored
fix(javascript): preserve strict mode during module fallback (#22465)
1 parent 4d46ce4 commit a067e66

13 files changed

Lines changed: 104 additions & 5 deletions

File tree

‎lib/javascript/syntax-parser.js‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13054,10 +13054,9 @@ class WebpackParser extends BaseParser {
1305413054
}
1305513055

1305613056
/**
13057-
* In `auto` source type a top-level `return` is script-only syntax. When the
13058-
* strict module parse reaches it with no module construct seen yet, downgrade
13059-
* to a sloppy script in place: the prefix parsed under stricter rules stays a
13060-
* valid sloppy prefix, so the second full parse is avoided.
13057+
* In `auto` source type a top-level `return` is script-only syntax. With no
13058+
* module construct seen yet, switch to script in place and retain strictness
13059+
* required by the source or parser options.
1306113060
* @returns {boolean} true when the parse was downgraded to script
1306213061
*/
1306313062
_tryModuleFallback() {
@@ -13071,7 +13070,7 @@ class WebpackParser extends BaseParser {
1307113070
this.options.allowReturnOutsideFunction = true;
1307213071
// acorn stamps Program.sourceType from this at parseTopLevel's end
1307313072
this.options.sourceType = "script";
13074-
this.strict = false;
13073+
this.strict = this.strictDirective(0);
1307513074
this.inModule = false;
1307613075
this._moduleFallback = false;
1307713076
return true;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
0;
2+
"use strict";
3+
4+
if (typeof module === "undefined") return;
5+
6+
with ({ value: 42 }) {
7+
module.exports = value;
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
3+
module.exports = [
4+
[{ message: /Invalid number/, moduleName: /octal\.js/ }],
5+
[{ message: /'with' in strict mode/, moduleName: /with\.js/ }]
6+
];
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use\x20strict";
2+
3+
if (typeof module === "undefined") return;
4+
5+
with ({ value: 42 }) {
6+
module.exports = value;
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function strictFunction() {
2+
"use strict";
3+
}
4+
5+
if (typeof module === "undefined") return;
6+
7+
with ({ value: 42 }) {
8+
module.exports = value;
9+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
it("should reject a with statement after a return in strict code", () => {
2+
expect(() => require("./with")).toThrow(/'with' in strict mode/);
3+
});
4+
5+
it("should reject a legacy octal literal after a return in strict code", () => {
6+
expect(() => require("./octal")).toThrow(/Invalid number/);
7+
});
8+
9+
it("should preserve strict mode when a top-level return selects script mode", () => {
10+
expect(require("./strict")).toBe(true);
11+
});
12+
13+
it("should keep a script without a strict directive non-strict", () => {
14+
expect(require("./loose")).toBe(42);
15+
});
16+
17+
it("should keep a script whose strict-looking string is no directive non-strict", () => {
18+
expect(require("./escaped")).toBe(42);
19+
expect(require("./parenthesized")).toBe(42);
20+
expect(require("./after-expression")).toBe(42);
21+
expect(require("./function-directive")).toBe(42);
22+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"use strict";
2+
3+
module.exports = (options) => {
4+
if (options.cache && options.cache.type === "filesystem") {
5+
// Modules with parse errors are rebuilt on each run.
6+
return [/Pack got invalid because of write to/];
7+
}
8+
9+
return [];
10+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if (typeof module === "undefined") return;
2+
3+
with ({ value: 42 }) {
4+
module.exports = value;
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"another directive";
2+
"use strict";
3+
4+
return;
5+
module.exports = 010;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
("use strict");
2+
3+
if (typeof module === "undefined") return;
4+
5+
with ({ value: 42 }) {
6+
module.exports = value;
7+
}

0 commit comments

Comments
 (0)