Skip to content

Commit fc21b55

Browse files
committed
fix(javascript): detect strict mode throughout directive prologues
1 parent 0c14e16 commit fc21b55

10 files changed

Lines changed: 63 additions & 12 deletions

File tree

‎.changeset/012-strict-directive-spelling.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": patch
33
---
44

5-
Preserve sloppy mode for parenthesized and escaped "use strict" strings.
5+
Recognize strict mode only from valid directives throughout the prologue.

‎lib/UseStrictPlugin.js‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,26 @@ class UseStrictPlugin {
3737
*/
3838
const handler = (parser, parserOptions) => {
3939
parser.hooks.program.tap(PLUGIN_NAME, (ast) => {
40-
const firstNode = ast.body[0];
41-
if (
42-
firstNode &&
43-
firstNode.type === "ExpressionStatement" &&
44-
/** @type {Directive} */ (firstNode).directive === "use strict"
45-
) {
40+
for (const statement of ast.body) {
41+
if (
42+
statement.type !== "ExpressionStatement" ||
43+
/** @type {Directive} */ (statement).directive === undefined
44+
) {
45+
break;
46+
}
47+
if (
48+
/** @type {Directive} */ (statement).directive !== "use strict"
49+
) {
50+
continue;
51+
}
4652
// Remove "use strict" expression. It will be added later by the renderer again.
4753
// This is necessary in order to not break the strict mode when webpack prepends code.
4854
// @see https://github.com/webpack/webpack/issues/1970
4955
const dep = new ConstDependency(
5056
"",
51-
/** @type {Range} */ (firstNode.range)
57+
/** @type {Range} */ (statement.range)
5258
);
53-
dep.loc = parser.getLocation(firstNode);
59+
dep.loc = parser.getLocation(statement);
5460
parser.state.module.addPresentationalDependency(dep);
5561
/** @type {BuildInfo} */
5662
(parser.state.module.buildInfo).strict = true;

‎lib/javascript/JavascriptParser.js‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5901,11 +5901,23 @@ class JavascriptParser extends Parser {
59015901
return;
59025902
}
59035903
const value = /** @type {Literal} */ (statement.expression).value;
5904-
if (/** @type {Directive} */ (statement).directive === "use strict") {
5905-
this.scope.isStrict = true;
5906-
} else if (value === "use asm") {
5904+
if (value === "use asm") {
59075905
this.scope.isAsmJs = true;
59085906
}
5907+
for (const directiveStatement of statements) {
5908+
if (
5909+
directiveStatement.type !== "ExpressionStatement" ||
5910+
/** @type {Directive} */ (directiveStatement).directive === undefined
5911+
) {
5912+
break;
5913+
}
5914+
if (
5915+
/** @type {Directive} */ (directiveStatement).directive === "use strict"
5916+
) {
5917+
this.scope.isStrict = true;
5918+
break;
5919+
}
5920+
}
59095921
}
59105922

59115923
/**
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
0;
2+
"use strict";
3+
module.exports = (function () { return this === undefined; })();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
("custom");
2+
"use strict";
3+
module.exports = (function () { return this === undefined; })();

‎test/configCases/parsing/strict-directive-spelling/index.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,12 @@ it("should not make escaped strings strict directives", () => {
1010
it("should preserve real strict directives", () => {
1111
expect(require("./strict")).toBe(true);
1212
});
13+
14+
it("should preserve strict mode after other directives when prepending code", () => {
15+
expect(require("./prologue")).toEqual([42, true]);
16+
});
17+
18+
it("should not recognize strict directives after the prologue ends", () => {
19+
expect(require("./after-expression")).toBe(false);
20+
expect(require("./after-parenthesized")).toBe(false);
21+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"";
2+
"custom";
3+
"use strict";
4+
"use strict";
5+
module.exports = [provided, (function () { return this === undefined; })()];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const value = 42;

‎test/configCases/parsing/strict-directive-spelling/webpack.config.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
"use strict";
22

3+
const webpack = require("../../../../");
4+
35
/** @type {import("../../../../").Configuration} */
46
module.exports = {
7+
plugins: [
8+
new webpack.ProvidePlugin({
9+
provided: [require.resolve("./provided"), "value"]
10+
})
11+
],
512
optimization: {
613
minimize: false
714
}

‎test/unitCases/JavascriptParser.unittest.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ describe("JavascriptParser", () => {
1616
it.each([
1717
['"use strict";', true],
1818
["'use strict';", true],
19+
['"custom"; "use strict";', true],
20+
['""; "custom"; "use strict"; "use strict";', true],
21+
['0; "use strict";', false],
22+
['("custom"); "use strict";', false],
23+
['"custom"; "use\\x20strict";', false],
1924
['("use strict");', false],
2025
['"use\\x20strict";', false],
2126
['"use \\\nstrict";', false]

0 commit comments

Comments
 (0)