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 — feat(javascript): let every parser hook take path taps (#23238) · webpack/webpack@e6e091a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • .changeset
    • 046-javascript-program-path-taps.md
  • lib/javascript
    • JavascriptParser.js
  • test/configCases/parsing
    • hook-path-taps
      • dep.js
      • index.js
      • webpack.config.js
    • logical-chain-evaluation
      • webpack.config.js
  • types.d.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎.changeset/046-javascript-program-path-taps.md‎

Copy file name to clipboardExpand all lines: .changeset/046-javascript-program-path-taps.md
+1-1Lines changed: 1 addition & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": minor
33
---
44

5-
Let a `program` parser tap read the program through the path with `path: true`.
5+
Let JavaScript parser hook taps read nodes through the path with `path: true`.
Collapse file

‎lib/javascript/JavascriptParser.js‎

Copy file name to clipboardExpand all lines: lib/javascript/JavascriptParser.js
+535-240Lines changed: 535 additions & 240 deletions
Large diffs are not rendered by default.
Collapse file

‎test/configCases/parsing/hook-path-taps/dep.js‎

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const value = 1;
Collapse file

‎test/configCases/parsing/hook-path-taps/index.js‎

Copy file name to clipboard
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { value } from "./dep";
2+
3+
global.magicCall = (text) => text.length;
4+
5+
class Shape {
6+
area() {
7+
return 0;
8+
}
9+
}
10+
11+
const declared = magicCall("hello");
12+
const lazy = import("./dep");
13+
14+
it("should build what path taps read", () => {
15+
expect(value).toBe(1);
16+
expect(declared).toBe(5);
17+
expect(new Shape().area()).toBe(0);
18+
expect(lazy).toBeInstanceOf(Promise);
19+
return import("./dep").then((dep) => {
20+
expect(dep.value).toBe(1);
21+
});
22+
});
Collapse file

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

Copy file name to clipboard
+144Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
"use strict";
2+
3+
/** @typedef {import("../../../../lib/Compiler")} Compiler */
4+
/** @import JavascriptParser from "../../../../lib/javascript/JavascriptParser" */
5+
6+
const PLUGIN_NAME = "hook-path-taps";
7+
8+
/**
9+
* Taps one hook of each family with `{ path: true }` and checks the ids each
10+
* tap gets read as the nodes the walk passed.
11+
* @param {Compiler} compiler the compiler
12+
* @returns {void}
13+
*/
14+
const hookPathTaps = (compiler) => {
15+
compiler.hooks.compilation.tap(
16+
PLUGIN_NAME,
17+
(compilation, { normalModuleFactory }) => {
18+
const { Field, NodeType } = compiler.webpack.javascript.syntax.parser;
19+
/** @type {Set<string>} */
20+
const seen = new Set();
21+
/** @type {JavascriptParser | undefined} */
22+
let lastParser;
23+
// a build restored from the cache parses nothing
24+
let parsedIndex = false;
25+
normalModuleFactory.hooks.parser
26+
.for("javascript/auto")
27+
.tap(PLUGIN_NAME, (moduleParser) => {
28+
const parser = /** @type {JavascriptParser} */ (moduleParser);
29+
lastParser = parser;
30+
// before the built-in taps, which bail on the hooks they handle
31+
const opts = {
32+
name: PLUGIN_NAME,
33+
path: /** @type {true} */ (true),
34+
stage: -100
35+
};
36+
parser.hooks.statement.tap(opts, (path, statement) => {
37+
expect(path.type(statement)).not.toBe(0);
38+
seen.add("statement");
39+
});
40+
// a tap without `path` on the same hook still gets the object
41+
parser.hooks.call.for("magicCall").tap(PLUGIN_NAME, (expr) => {
42+
expect(expr.type).toBe("CallExpression");
43+
seen.add("call object");
44+
});
45+
parser.hooks.call.for("magicCall").tap(opts, (path, call) => {
46+
expect(path.type(call)).toBe(NodeType.CallExpression);
47+
// a parse inside the tap has a view of its own, and this one's
48+
// ids read the same after it
49+
if (!seen.has("nested parse")) {
50+
seen.add("nested parse");
51+
parser.parse("1;", parser.state);
52+
}
53+
expect(path.type(call)).toBe(NodeType.CallExpression);
54+
const argument = path.field(0, Field.arguments, call);
55+
expect(path.value(argument)).toBe("hello");
56+
seen.add("call");
57+
});
58+
parser.hooks.evaluateIdentifier
59+
.for("magicCall")
60+
.tap(opts, (path, identifier) => {
61+
expect(path.name(identifier)).toBe("magicCall");
62+
seen.add("evaluateIdentifier");
63+
});
64+
parser.hooks.declarator.tap(opts, (path, declarator, statement) => {
65+
expect(path.type(declarator)).toBe(NodeType.VariableDeclarator);
66+
expect(path.type(statement)).toBe(NodeType.VariableDeclaration);
67+
seen.add("declarator");
68+
});
69+
parser.hooks.classBodyElement.tap(opts, (path, element, owner) => {
70+
expect(path.type(element)).toBe(NodeType.MethodDefinition);
71+
expect(path.type(owner)).toBe(NodeType.ClassDeclaration);
72+
seen.add("classBodyElement");
73+
});
74+
parser.hooks.importSpecifier.tap(
75+
opts,
76+
(path, statement, source, exportName, identifierName) => {
77+
expect(path.type(statement)).toBe(NodeType.ImportDeclaration);
78+
expect([source, exportName, identifierName]).toEqual([
79+
"./dep",
80+
"value",
81+
"value"
82+
]);
83+
seen.add("importSpecifier");
84+
}
85+
);
86+
// a node the walk passes none for, as an import no `.then` reads, is 0
87+
parser.hooks.importCall.tap(opts, (path, expr, then) => {
88+
expect(path.type(expr)).toBe(NodeType.ImportExpression);
89+
if (then === 0) {
90+
seen.add("importCall without then");
91+
} else {
92+
expect(path.type(then)).toBe(NodeType.CallExpression);
93+
seen.add("importCall");
94+
}
95+
});
96+
parser.hooks.finish.tap(opts, (path, program) => {
97+
expect(path.type(program)).toBe(NodeType.Program);
98+
if (/index\.js$/.test(parser.state.module.resource)) {
99+
parsedIndex = true;
100+
}
101+
seen.add("finish");
102+
});
103+
});
104+
compilation.hooks.finishModules.tap(PLUGIN_NAME, () => {
105+
if (!parsedIndex) return;
106+
expect([...seen].sort()).toEqual([
107+
"call",
108+
"call object",
109+
"classBodyElement",
110+
"declarator",
111+
"evaluateIdentifier",
112+
"finish",
113+
"importCall",
114+
"importCall without then",
115+
"importSpecifier",
116+
"nested parse",
117+
"statement"
118+
]);
119+
// called outside a parse, a path tap reads through a view of its own
120+
let name = "";
121+
const parser = /** @type {JavascriptParser} */ (lastParser);
122+
parser.hooks.evaluateIdentifier
123+
.for("outside")
124+
.tap({ name: PLUGIN_NAME, path: true }, (path, identifier) => {
125+
name = path.name(identifier);
126+
});
127+
parser.hooks.evaluateIdentifier.for("outside").call(
128+
/** @type {import("estree").Identifier} */ ({
129+
type: "Identifier",
130+
name: "outside",
131+
start: 0,
132+
end: 7
133+
})
134+
);
135+
expect(name).toBe("outside");
136+
});
137+
}
138+
);
139+
};
140+
141+
/** @type {import("../../../../").Configuration} */
142+
module.exports = {
143+
plugins: [hookPathTaps]
144+
};
Collapse file

‎test/configCases/parsing/logical-chain-evaluation/webpack.config.js‎

Copy file name to clipboardExpand all lines: test/configCases/parsing/logical-chain-evaluation/webpack.config.js
+17-6Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,23 @@ module.exports = [
4545
}
4646
});
4747
} else {
48-
hook.tap(
49-
{ name: "ObserveLogicalChainPlugin", stage: -100 },
50-
() => {
51-
hookCalls++;
52-
}
53-
);
48+
const options = {
49+
name: "ObserveLogicalChainPlugin",
50+
stage: -100
51+
};
52+
const observe = () => {
53+
hookCalls++;
54+
return undefined;
55+
};
56+
// each hook's `tap` takes path taps too, so they are tapped apart
57+
if (hookType === "walk") {
58+
parser.hooks.expressionLogicalOperator.tap(
59+
options,
60+
observe
61+
);
62+
} else {
63+
parser.hooks.evaluate.for(hookType).tap(options, observe);
64+
}
5465
}
5566
if (!intercept && hookType !== "walk") {
5667
parser.hooks.evaluate

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.