Skip to content

Commit 91590d3

Browse files
fix(javascript): preserve strict directive spelling (#22185)
* fix(javascript): preserve strict directive spelling * fix(javascript): detect strict mode throughout directive prologues * fix(javascript): read a directive's spelling from an AST that states none An AST webpack did not parse — a preparsed one, or whatever a custom `module.parser.javascript.parse` returns — may report a directive as a plain expression statement, so requiring the optional `directive` field stopped the prologue scan there and left the module non-strict. Recover the spelling from the literal itself for those trees: a range that does not start where the statement does is a parenthesized expression, and the raw text keeps an escape unexpanded. --------- Co-authored-by: Alexander Akait <[email protected]>
1 parent 69fe24f commit 91590d3

21 files changed

Lines changed: 294 additions & 12 deletions
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+
Recognize strict mode only from valid directives throughout the prologue.

‎lib/UseStrictPlugin.js‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,18 @@ class UseStrictPlugin {
3636
*/
3737
const handler = (parser, parserOptions) => {
3838
parser.hooks.program.tap(PLUGIN_NAME, (ast) => {
39-
const firstNode = ast.body[0];
40-
if (
41-
firstNode &&
42-
firstNode.type === "ExpressionStatement" &&
43-
firstNode.expression.type === "Literal" &&
44-
firstNode.expression.value === "use strict"
45-
) {
39+
for (const statement of ast.body) {
40+
const directive = parser.getDirective(statement);
41+
if (directive === undefined) break;
42+
if (directive !== "use strict") continue;
4643
// Remove "use strict" expression. It will be added later by the renderer again.
4744
// This is necessary in order to not break the strict mode when webpack prepends code.
4845
// @see https://github.com/webpack/webpack/issues/1970
4946
const dep = new ConstDependency(
5047
"",
51-
/** @type {Range} */ (firstNode.range)
48+
/** @type {Range} */ (statement.range)
5249
);
53-
dep.loc = parser.getLocation(firstNode);
50+
dep.loc = parser.getLocation(statement);
5451
parser.state.module.addPresentationalDependency(dep);
5552
/** @type {BuildInfo} */
5653
(parser.state.module.buildInfo).strict = true;

‎lib/javascript/JavascriptParser.js‎

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5896,6 +5896,37 @@ class JavascriptParser extends Parser {
58965896
return oldScope;
58975897
}
58985898

5899+
/**
5900+
* The directive a statement spells, or `undefined` when it is not one. An
5901+
* AST webpack did not parse may leave the metadata off a directive, so the
5902+
* spelling is recovered from the literal itself there.
5903+
* @param {Directive | Statement | ModuleDeclaration} statement statement
5904+
* @returns {string | undefined} the spelling between the quotes
5905+
*/
5906+
getDirective(statement) {
5907+
if (statement.type !== "ExpressionStatement") return undefined;
5908+
const directive = /** @type {Directive} */ (statement).directive;
5909+
if (directive !== undefined || this._hasParserRecord) return directive;
5910+
const expression = statement.expression;
5911+
if (expression.type !== "Literal" || typeof expression.value !== "string") {
5912+
return undefined;
5913+
}
5914+
// a parenthesized string is an expression rather than a directive, and
5915+
// only the statement says so — the literal starts inside the parentheses
5916+
if (
5917+
statement.range !== undefined &&
5918+
expression.range !== undefined &&
5919+
statement.range[0] !== expression.range[0]
5920+
) {
5921+
return undefined;
5922+
}
5923+
// the raw text keeps an escape unexpanded, which is what separates a
5924+
// directive from a string that merely reads like one
5925+
return expression.raw === undefined
5926+
? expression.value
5927+
: expression.raw.slice(1, -1);
5928+
}
5929+
58995930
/**
59005931
* Processes the provided statement.
59015932
* @param {(Directive | Statement | ModuleDeclaration)[]} statements statements
@@ -5910,11 +5941,17 @@ class JavascriptParser extends Parser {
59105941
return;
59115942
}
59125943
const value = /** @type {Literal} */ (statement.expression).value;
5913-
if (value === "use strict") {
5914-
this.scope.isStrict = true;
5915-
} else if (value === "use asm") {
5944+
if (value === "use asm") {
59165945
this.scope.isAsmJs = true;
59175946
}
5947+
for (const directiveStatement of statements) {
5948+
const directive = this.getDirective(directiveStatement);
5949+
if (directive === undefined) break;
5950+
if (directive === "use strict") {
5951+
this.scope.isStrict = true;
5952+
break;
5953+
}
5954+
}
59185955
}
59195956

59205957
/**
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use\x20strict";
2+
module.exports = [provided, (function () { return this === undefined; })()];
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
it("should detect a strict directive an AST does not mark as one", () => {
2+
expect(require("./strict")).toEqual([42, true]);
3+
});
4+
5+
it("should keep rejecting strings that only read like a directive", () => {
6+
expect(require("./escaped")).toEqual([42, false]);
7+
expect(require("./parenthesized")).toEqual([42, false]);
8+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
("use strict");
2+
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;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
module.exports = [provided, (function () { return this === undefined; })()];
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"use strict";
2+
3+
const acorn = require("acorn");
4+
const webpack = require("../../../../");
5+
6+
/**
7+
* @import {
8+
* ParseOptions,
9+
* ParseResult
10+
* } from "../../../../lib/javascript/JavascriptParser"
11+
*/
12+
13+
/**
14+
* Drops the optional directive metadata, as a parser that reports a directive
15+
* as a plain expression statement does.
16+
* @param {EXPECTED_ANY} node node to strip
17+
* @returns {void}
18+
*/
19+
const stripDirectives = (node) => {
20+
if (Array.isArray(node)) {
21+
for (const item of node) stripDirectives(item);
22+
return;
23+
}
24+
if (node === null || typeof node !== "object") return;
25+
if (node.type === "ExpressionStatement") node.directive = undefined;
26+
for (const key of Object.keys(node)) stripDirectives(node[key]);
27+
};
28+
29+
/** @type {import("../../../../").Configuration} */
30+
module.exports = {
31+
plugins: [
32+
new webpack.ProvidePlugin({
33+
provided: [require.resolve("./provided"), "value"]
34+
})
35+
],
36+
module: {
37+
parser: {
38+
javascript: {
39+
/**
40+
* A parser whose AST states no directive, so webpack reads the
41+
* spelling out of the literal itself.
42+
* @param {string} code source code
43+
* @param {ParseOptions} options parse options
44+
* @returns {ParseResult} parse result
45+
*/
46+
parse: (code, options) => {
47+
/** @type {ParseResult["comments"]} */
48+
const comments = [];
49+
const ast = acorn.parse(code, {
50+
sourceType: options.sourceType,
51+
ecmaVersion: "latest",
52+
locations: false,
53+
ranges: true,
54+
allowHashBang: true,
55+
allowReturnOutsideFunction: options.allowReturnOutsideFunction,
56+
onComment: /** @type {EXPECTED_ANY} */ (comments)
57+
});
58+
stripDirectives(ast);
59+
return {
60+
ast: /** @type {ParseResult["ast"]} */ (ast),
61+
comments
62+
};
63+
}
64+
}
65+
}
66+
},
67+
optimization: {
68+
minimize: false
69+
}
70+
};
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; })();

0 commit comments

Comments
 (0)