GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — perf(javascript): keep built-in program taps on objects until the tre… · webpack/webpack@36a02bf

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/javascript
    • UseStrictPlugin.js
  • test/configCases/parsing/program-path-taps
    • webpack.config.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/javascript/UseStrictPlugin.js‎

Copy file name to clipboardExpand all lines: lib/javascript/UseStrictPlugin.js
+9-16Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,16 @@ class UseStrictPlugin {
5959
parserOptions.overrideStrict === "strict";
6060
}
6161
};
62-
63-
const { Field, Part } = require("./syntax-parser");
64-
65-
parser.hooks.program.tap(
66-
{ name: PLUGIN_NAME, path: true },
67-
(path, program) => {
68-
const count = path.fieldCount(Field.body, program);
69-
for (let i = 0; i < count; i++) {
70-
const statement = path.field(i, Field.body, program);
71-
const directive = path.textOf(Part.directive, statement);
72-
if (directive === null) break;
73-
if (directive !== "use strict") continue;
74-
removeUseStrict(path.range(statement), i > 0);
75-
}
76-
applyOverrideStrict();
62+
parser.hooks.program.tap(PLUGIN_NAME, (ast) => {
63+
for (let i = 0; i < ast.body.length; i++) {
64+
const statement = ast.body[i];
65+
const directive = parser.getDirective(statement);
66+
if (directive === undefined) break;
67+
if (directive !== "use strict") continue;
68+
removeUseStrict(/** @type {Range} */ (statement.range), i > 0);
7769
}
78-
);
70+
applyOverrideStrict();
71+
});
7972
};
8073

8174
normalModuleFactory.hooks.parser
Collapse file

‎test/configCases/parsing/program-path-taps/webpack.config.js‎

Copy file name to clipboardExpand all lines: test/configCases/parsing/program-path-taps/webpack.config.js
+15-4Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ const PLUGIN_NAME = "program-path-taps";
1010

1111
/**
1212
* Taps `program` with `{ path: true }` next to a tap without it, and checks
13-
* what the built-in path taps recorded on each module.
13+
* what the built-in taps recorded on each module.
1414
* @param {Compiler} compiler the compiler
1515
* @returns {void}
1616
*/
1717
const programPathTaps = (compiler) => {
1818
compiler.hooks.compilation.tap(
1919
PLUGIN_NAME,
2020
(compilation, { normalModuleFactory }) => {
21-
const { NodeType } = compiler.webpack.javascript.syntax.parser;
21+
const { Field, NodeType, Part } =
22+
compiler.webpack.javascript.syntax.parser;
2223
/** @type {Set<string>} */
2324
const walked = new Set();
2425
for (const type of [
@@ -43,9 +44,19 @@ const programPathTaps = (compiler) => {
4344
(path, program, comments) => {
4445
expect(path.type(program)).toBe(NodeType.Program);
4546
expect(Array.isArray(comments)).toBe(true);
46-
walked.add(
47-
/** @type {NormalModule} */ (parser.state.module).resource
47+
const { resource } = /** @type {NormalModule} */ (
48+
parser.state.module
4849
);
50+
if (/(strict-cjs|custom)\.js$/.test(resource)) {
51+
// `custom.js` comes from a parse marking no directive
52+
const first = path.field(0, Field.body, program);
53+
expect(path.textOf(Part.directive, first)).toBe("use strict");
54+
expect(path.range(first)).toEqual([
55+
path.start(first),
56+
path.end(first)
57+
]);
58+
}
59+
walked.add(resource);
4960
}
5061
);
5162
});

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.