|
| 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 | +}; |