Skip to content

Commit 0c14e16

Browse files
committed
fix(javascript): preserve strict directive spelling
1 parent 2543f96 commit 0c14e16

10 files changed

Lines changed: 66 additions & 3 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Preserve sloppy mode for parenthesized and escaped "use strict" strings.

‎lib/UseStrictPlugin.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
} = require("./ModuleTypeConstants");
1313
const ConstDependency = require("./dependencies/ConstDependency");
1414

15+
/** @import { Directive } from "estree" */
1516
/** @import { JavascriptParserOptions } from "../declarations/WebpackOptions" */
1617
/** @import Compiler from "./Compiler" */
1718
/** @import { BuildInfo } from "./Module" */
@@ -40,8 +41,7 @@ class UseStrictPlugin {
4041
if (
4142
firstNode &&
4243
firstNode.type === "ExpressionStatement" &&
43-
firstNode.expression.type === "Literal" &&
44-
firstNode.expression.value === "use strict"
44+
/** @type {Directive} */ (firstNode).directive === "use strict"
4545
) {
4646
// Remove "use strict" expression. It will be added later by the renderer again.
4747
// This is necessary in order to not break the strict mode when webpack prepends code.

‎lib/javascript/JavascriptParser.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5901,7 +5901,7 @@ class JavascriptParser extends Parser {
59015901
return;
59025902
}
59035903
const value = /** @type {Literal} */ (statement.expression).value;
5904-
if (value === "use strict") {
5904+
if (/** @type {Directive} */ (statement).directive === "use strict") {
59055905
this.scope.isStrict = true;
59065906
} else if (value === "use asm") {
59075907
this.scope.isAsmJs = true;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use \
2+
strict";
3+
module.exports = (function () { return this === undefined; })();
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use\x20strict";
2+
module.exports = (function () { return this === undefined; })();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
it("should not make parenthesized strings strict directives", () => {
2+
expect(require("./parenthesized")).toBe(false);
3+
});
4+
5+
it("should not make escaped strings strict directives", () => {
6+
expect(require("./escaped")).toBe(false);
7+
expect(require("./continued")).toBe(false);
8+
});
9+
10+
it("should preserve real strict directives", () => {
11+
expect(require("./strict")).toBe(true);
12+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
("use strict");
2+
module.exports = (function () { return this === undefined; })();
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
module.exports = (function () { return this === undefined; })();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
optimization: {
6+
minimize: false
7+
}
8+
};

‎test/unitCases/JavascriptParser.unittest.js‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,35 @@ const {
1212
} = require("../../lib/javascript/syntax");
1313

1414
describe("JavascriptParser", () => {
15+
describe("strict directive spelling", () => {
16+
it.each([
17+
['"use strict";', true],
18+
["'use strict';", true],
19+
['("use strict");', false],
20+
['"use\\x20strict";', false],
21+
['"use \\\nstrict";', false]
22+
])("detects the mode of %s", (directive, expected) => {
23+
for (const source of [
24+
`${directive} probe();`,
25+
`function test() { ${directive} probe(); }`
26+
]) {
27+
const parser = new JavascriptParser("script");
28+
/** @type {boolean[]} */
29+
const modes = [];
30+
parser.hooks.call.for("probe").tap("test", () => {
31+
modes.push(parser.scope.isStrict);
32+
});
33+
parser.parse(
34+
source,
35+
/** @type {import("../../lib/Parser").ParserState} */ (
36+
/** @type {unknown} */ ({})
37+
)
38+
);
39+
expect(modes).toEqual([expected]);
40+
}
41+
});
42+
});
43+
1544
/* eslint-disable no-unused-vars */
1645
/** @type {EXPECTED_ANY} */ let abc;
1746
/** @type {EXPECTED_ANY} */ let cde;

0 commit comments

Comments
 (0)