@@ -1777,13 +1777,13 @@ const nodeIdOf = (view, node) => |
1777 | 1777 | * A node argument a `{ path: true }` tap gets while the parse walks the tree: |
1778 | 1778 | * an id the walk passed as it is, a lazy object of the tree as its id. |
1779 | 1779 | * @param {SyntaxTree} tree the tree |
1780 | | - * @param {unknown} node the argument |
| 1780 | + * @param {Node | number | null | undefined} node the argument |
1781 | 1781 | * @returns {number} its id, `0` for none, `-1` for an object of no tree's |
1782 | 1782 | */ |
1783 | 1783 | const treeIdOf = (tree, node) => { |
1784 | 1784 | if (typeof node === "number") return node; |
1785 | 1785 | if (node === null || node === undefined) return 0; |
1786 | | - return tree.nodeOf(/** @type {Node} */ (node)) || -1; |
| 1786 | + return tree.nodeOf(node) || -1; |
1787 | 1787 | }; |
1788 | 1788 |
|
1789 | 1789 | // The ESTree type of each `NodeType`, the key `hooks.evaluate` is read by. |
@@ -1797,11 +1797,11 @@ for (const name of Object.keys(NodeType)) { |
1797 | 1797 | /** |
1798 | 1798 | * A node's id where a hook or a method takes the node: in the tree walk the id |
1799 | 1799 | * stands for the node, and a tap reads it through the path or as its object. |
1800 | | - * @template {Node} [T=Node] |
| 1800 | + * @template [T=Node] |
1801 | 1801 | * @param {number} id the node's id |
1802 | 1802 | * @returns {T} the id, typed as the node it stands for |
1803 | 1803 | */ |
1804 | | -const asNode = (id) => /** @type {T} */ (/** @type {unknown} */ (id)); |
| 1804 | +const asNode = (id) => /** @type {T} */ (id); |
1805 | 1805 |
|
1806 | 1806 | /** |
1807 | 1807 | * A tap without `path` on a hook the id walk calls: each node argument it |
@@ -1811,10 +1811,9 @@ const asNode = (id) => /** @type {T} */ (/** @type {unknown} */ (id)); |
1811 | 1811 | * @param {(...args: A) => R} fn the tap's function |
1812 | 1812 | * @param {number} nodes a bit for each argument that is a node |
1813 | 1813 | * @param {JavascriptParser} parser the parser |
1814 | | - * @param {number} count how many arguments the hook passes, all the tap gets |
1815 | 1814 | * @returns {(...args: A) => R} the function to tap instead |
1816 | 1815 | */ |
1817 | | -const objectTapOf = (fn, nodes, parser, count) => { |
| 1816 | +const objectTapOf = (fn, nodes, parser) => { |
1818 | 1817 | /** |
1819 | 1818 | * @param {A[number]} arg an argument |
1820 | 1819 | * @param {number} bit its bit in `nodes` |
@@ -1826,55 +1825,40 @@ const objectTapOf = (fn, nodes, parser, count) => { |
1826 | 1825 | /** @type {SyntaxTree} */ (parser._tree).estree(arg) |
1827 | 1826 | ) |
1828 | 1827 | : arg; |
1829 | | - // WHY: a fixed parameter list per hook arity keeps a rest array off the hot |
1830 | | - // path and passes the tap no more arguments than the hook does; the casts |
1831 | | - // restate the tap's own arguments. |
1832 | | - const call = /** @type {(...args: A[number][]) => R} */ ( |
1833 | | - /** @type {unknown} */ (fn) |
1834 | | - ); |
1835 | | - /** @type {(...args: A[number][]) => R} */ |
1836 | | - let wrapper; |
1837 | | - switch (count) { |
1838 | | - case 1: |
1839 | | - wrapper = (a) => call(objectOf(a, 1)); |
1840 | | - break; |
1841 | | - case 2: |
1842 | | - wrapper = (a, b) => call(objectOf(a, 1), objectOf(b, 2)); |
1843 | | - break; |
1844 | | - case 3: |
1845 | | - wrapper = (a, b, c) => |
1846 | | - call(objectOf(a, 1), objectOf(b, 2), objectOf(c, 4)); |
1847 | | - break; |
1848 | | - case 4: |
1849 | | - wrapper = (a, b, c, d) => |
1850 | | - call(objectOf(a, 1), objectOf(b, 2), objectOf(c, 4), d); |
1851 | | - break; |
1852 | | - default: |
1853 | | - wrapper = (a, b, c, d, e) => |
1854 | | - call(objectOf(a, 1), objectOf(b, 2), objectOf(c, 4), d, e); |
| 1828 | + // a tap declaring no parameter may read them as a rest: it gets exactly |
| 1829 | + // the hook's arguments, each tap declaring some gets them without an array |
| 1830 | + if (fn.length === 0) { |
| 1831 | + return (/** @type {A} */ ...args) => { |
| 1832 | + for (let i = 0; i < 3 && i < args.length; i++) { |
| 1833 | + args[i] = objectOf(args[i], 1 << i); |
| 1834 | + } |
| 1835 | + return fn(...args); |
| 1836 | + }; |
1855 | 1837 | } |
1856 | | - return /** @type {(...args: A) => R} */ (/** @type {unknown} */ (wrapper)); |
| 1838 | + const call = /** @type {(...args: A[number][]) => R} */ (fn); |
| 1839 | + return /** @type {(...args: A) => R} */ ( |
| 1840 | + /** @type {(...args: A[number][]) => R} */ ( |
| 1841 | + ( |
| 1842 | + /** @type {A[number]} */ a, |
| 1843 | + /** @type {A[number]} */ b, |
| 1844 | + /** @type {A[number]} */ c, |
| 1845 | + /** @type {A[number]} */ d, |
| 1846 | + /** @type {A[number]} */ e |
| 1847 | + ) => call(objectOf(a, 1), objectOf(b, 2), objectOf(c, 4), d, e) |
| 1848 | + ) |
| 1849 | + ); |
1857 | 1850 | }; |
1858 | 1851 |
|
1859 | | -/** |
1860 | | - * How many arguments a hook passes its taps: as many as it was made with names for. |
1861 | | - * @param {{ isUsed: () => boolean }} hook the hook |
1862 | | - * @returns {number} the count |
1863 | | - */ |
1864 | | -const argumentCountOf = (hook) => |
1865 | | - /** @type {{ _args: string[] }} */ (/** @type {unknown} */ (hook))._args |
1866 | | - .length; |
1867 | | - |
1868 | 1852 | /** |
1869 | 1853 | * Lets a hook take `{ path: true }` taps: such a tap gets the path, then each |
1870 | | - * node argument as its id. Other taps are tapped as they are, and the hook |
| 1854 | + * node argument as its id. Other taps get the nodes' objects, and the hook |
1871 | 1855 | * gets no interceptor, so a call to it compiles as before. |
1872 | 1856 | * @template T |
1873 | 1857 | * @template R |
1874 | 1858 | * @param {SyncBailHook<T, R>} hook the hook |
1875 | 1859 | * @param {number} nodes a bit for each argument that is a node |
1876 | 1860 | * @param {JavascriptParser} parser the parser whose parse the path reads |
1877 | | - * @returns {void} |
| 1861 | + * @returns {SyncBailHook<T, R>["tap"]} the hook's own tap method, which takes a tap as it is |
1878 | 1862 | */ |
1879 | 1863 | const takePathTaps = (hook, nodes, parser) => { |
1880 | 1864 | const tap = hook.tap; |
@@ -1890,9 +1874,7 @@ const takePathTaps = (hook, nodes, parser) => { |
1890 | 1874 | options, |
1891 | 1875 | parser._idHooks && |
1892 | 1876 | /** @type {{ ids?: boolean }} */ (options).ids !== true |
1893 | | - ? /** @type {(...args: AsArray<T>) => R} */ ( |
1894 | | - objectTapOf(fn, nodes, parser, argumentCountOf(hook)) |
1895 | | - ) |
| 1877 | + ? objectTapOf(fn, nodes, parser) |
1896 | 1878 | : fn |
1897 | 1879 | ); |
1898 | 1880 | } |
@@ -1910,9 +1892,18 @@ const takePathTaps = (hook, nodes, parser) => { |
1910 | 1892 | ) => { |
1911 | 1893 | const tree = parser._tree; |
1912 | 1894 | if (tree !== undefined) { |
1913 | | - const ia = (nodes & 1) !== 0 ? treeIdOf(tree, a) : a; |
1914 | | - const ib = (nodes & 2) !== 0 ? treeIdOf(tree, b) : b; |
1915 | | - const ic = (nodes & 4) !== 0 ? treeIdOf(tree, c) : c; |
| 1895 | + const ia = |
| 1896 | + (nodes & 1) !== 0 |
| 1897 | + ? treeIdOf(tree, /** @type {Node | number | null} */ (a)) |
| 1898 | + : a; |
| 1899 | + const ib = |
| 1900 | + (nodes & 2) !== 0 |
| 1901 | + ? treeIdOf(tree, /** @type {Node | number | null} */ (b)) |
| 1902 | + : b; |
| 1903 | + const ic = |
| 1904 | + (nodes & 4) !== 0 |
| 1905 | + ? treeIdOf(tree, /** @type {Node | number | null} */ (c)) |
| 1906 | + : c; |
1916 | 1907 | if (ia !== -1 && ib !== -1 && ic !== -1) { |
1917 | 1908 | return pathFn( |
1918 | 1909 | /** @type {JavascriptNodeReader} */ (tree), |
@@ -1965,6 +1956,7 @@ const takePathTaps = (hook, nodes, parser) => { |
1965 | 1956 | /** @type {(...args: AsArray<T>) => R} */ (wrapper) |
1966 | 1957 | ); |
1967 | 1958 | }; |
| 1959 | + return tap; |
1968 | 1960 | }; |
1969 | 1961 |
|
1970 | 1962 | /** |
@@ -2414,8 +2406,6 @@ class JavascriptParser extends Parser { |
2414 | 2406 | // the tree the walk reads by id, while `_walkTree` parses into one |
2415 | 2407 | /** @type {SyntaxTree | undefined} */ |
2416 | 2408 | this._tree = undefined; |
2417 | | - // whether the hooks the id walk calls hand a tap without `path` objects |
2418 | | - this._idHooks = false; |
2419 | 2409 | // The view `{ path: true }` taps read the parse's nodes through, made on |
2420 | 2410 | // the first such tap of a parse, and one kept for the next parse. |
2421 | 2411 | /** @type {EstreeView | undefined} */ |
@@ -2425,6 +2415,12 @@ class JavascriptParser extends Parser { |
2425 | 2415 | // The program being parsed, whose directives the view marks. |
2426 | 2416 | /** @type {Program | undefined} */ |
2427 | 2417 | this._pathProgram = undefined; |
| 2418 | + // whether the hooks the id walk calls hand a tap without `path` objects |
| 2419 | + this._idHooks = false; |
| 2420 | + // the hooks that take path taps, each with its node bits and the tap |
| 2421 | + // method it had, to wrap the taps it holds once the walk reads ids |
| 2422 | + /** @type {{ hook: PathHook, nodes: number, tap: PathHook["tap"] }[]} */ |
| 2423 | + this._pathHooks = []; |
2428 | 2424 | for (const name of /** @type {(keyof PathHooks)[]} */ ( |
2429 | 2425 | Object.keys(PATH_HOOK_NODES) |
2430 | 2426 | )) { |
@@ -2436,13 +2432,15 @@ class JavascriptParser extends Parser { |
2436 | 2432 | hook.intercept( |
2437 | 2433 | /** @type {{ factory: (key: string, made: PathHook) => PathHook }} */ ({ |
2438 | 2434 | factory: (key, made) => { |
2439 | | - takePathTaps(made, nodes, this); |
| 2435 | + const tap = takePathTaps(made, nodes, this); |
| 2436 | + this._pathHooks.push({ hook: made, nodes, tap }); |
2440 | 2437 | return made; |
2441 | 2438 | } |
2442 | 2439 | }) |
2443 | 2440 | ); |
2444 | 2441 | } else { |
2445 | | - takePathTaps(hook, nodes, this); |
| 2442 | + const tap = takePathTaps(hook, nodes, this); |
| 2443 | + this._pathHooks.push({ hook, nodes, tap }); |
2446 | 2444 | } |
2447 | 2445 | } |
2448 | 2446 | // the scope last hoisted into, whose statements its block pass reads next, |
@@ -3028,7 +3026,8 @@ class JavascriptParser extends Parser { |
3028 | 3026 | .for("UnaryExpression") |
3029 | 3027 | .tap( |
3030 | 3028 | /** @type {{ name: string }} */ ({ name: CLASS_NAME, ids: true }), |
3031 | | - (_node) => { |
| 3029 | + (expression) => { |
| 3030 | + const _node = /** @type {UnaryExpression} */ (expression); |
3032 | 3031 | const operator = this._operatorOf(_node); |
3033 | 3032 | if (operator === "typeof") { |
3034 | 3033 | const tree = this._tree; |
@@ -3047,9 +3046,7 @@ class JavascriptParser extends Parser { |
3047 | 3046 | typeof argument === "number" |
3048 | 3047 | ? /** @type {SyntaxTree} */ (tree).name(argument) |
3049 | 3048 | : /** @type {Identifier} */ (argument).name, |
3050 | | - typeof _node === "number" |
3051 | | - ? asNode(_node) |
3052 | | - : /** @type {UnaryExpression} */ (_node) |
| 3049 | + typeof _node === "number" ? asNode(_node) : _node |
3053 | 3050 | ); |
3054 | 3051 | if (res !== undefined) return res; |
3055 | 3052 | break; |
@@ -3869,7 +3866,7 @@ class JavascriptParser extends Parser { |
3869 | 3866 | const range = this._rangeOf(expr); |
3870 | 3867 | if (!range) return; |
3871 | 3868 | const expressions = this._childrenOf( |
3872 | | - expr, |
| 3869 | + /** @type {SequenceExpression} */ (expr), |
3873 | 3870 | Field.expressions, |
3874 | 3871 | "expressions" |
3875 | 3872 | ); |
@@ -9916,40 +9913,38 @@ class JavascriptParser extends Parser { |
9916 | 9913 | /** |
9917 | 9914 | * A child of a node to evaluate or walk: its id when the node is a lazy |
9918 | 9915 | * object of the tree, so the child gets no object, else the child itself. |
9919 | | - * @param {object | number} node the node, or its id |
| 9916 | + * @template {Node} N |
| 9917 | + * @template {keyof N} K |
| 9918 | + * @param {N | number} node the node, or its id |
9920 | 9919 | * @param {number} field the child's `Field` |
9921 | | - * @param {string} key the child's ESTree property |
9922 | | - * @returns {Expression} the child, or its id standing for it |
| 9920 | + * @param {K} key the child's ESTree property |
| 9921 | + * @returns {N[K]} the child, or its id standing for it |
9923 | 9922 | */ |
9924 | 9923 | _childOf(node, field, key) { |
9925 | 9924 | const tree = this._tree; |
9926 | 9925 | if (tree !== undefined) { |
9927 | | - const id = |
9928 | | - typeof node === "number" |
9929 | | - ? node |
9930 | | - : tree.nodeOf(/** @type {Node} */ (node)); |
| 9926 | + const id = typeof node === "number" ? node : tree.nodeOf(node); |
9931 | 9927 | if (id !== 0) { |
9932 | 9928 | return asNode(tree.field(0, field, id)); |
9933 | 9929 | } |
9934 | 9930 | } |
9935 | | - return /** @type {Record<string, Expression>} */ (node)[key]; |
| 9931 | + return /** @type {N} */ (node)[key]; |
9936 | 9932 | } |
9937 | 9933 |
|
9938 | 9934 | /** |
9939 | 9935 | * The children in a list field of a node to evaluate: their ids when the |
9940 | 9936 | * node is a lazy object of the tree, so they get no object, else the children. |
9941 | | - * @param {object | number} node the node, or its id |
| 9937 | + * @template {Node} N |
| 9938 | + * @template {keyof N} K |
| 9939 | + * @param {N | number} node the node, or its id |
9942 | 9940 | * @param {number} field the list's `Field` |
9943 | | - * @param {string} key the list's ESTree property |
| 9941 | + * @param {K} key the list's ESTree property |
9944 | 9942 | * @returns {(Expression | SpreadElement)[]} the children, or their ids standing for them |
9945 | 9943 | */ |
9946 | 9944 | _childrenOf(node, field, key) { |
9947 | 9945 | const tree = this._tree; |
9948 | 9946 | if (tree !== undefined) { |
9949 | | - const id = |
9950 | | - typeof node === "number" |
9951 | | - ? node |
9952 | | - : tree.nodeOf(/** @type {Node} */ (node)); |
| 9947 | + const id = typeof node === "number" ? node : tree.nodeOf(node); |
9953 | 9948 | if (id !== 0) { |
9954 | 9949 | const count = tree.fieldCount(field, id); |
9955 | 9950 | /** @type {(Expression | SpreadElement)[]} */ |
@@ -9960,9 +9955,9 @@ class JavascriptParser extends Parser { |
9960 | 9955 | return children; |
9961 | 9956 | } |
9962 | 9957 | } |
9963 | | - return /** @type {Record<string, (Expression | SpreadElement)[]>} */ (node)[ |
9964 | | - key |
9965 | | - ]; |
| 9958 | + return /** @type {(Expression | SpreadElement)[]} */ ( |
| 9959 | + /** @type {N} */ (node)[key] |
| 9960 | + ); |
9966 | 9961 | } |
9967 | 9962 |
|
9968 | 9963 | /** |
@@ -10383,43 +10378,31 @@ class JavascriptParser extends Parser { |
10383 | 10378 | */ |
10384 | 10379 | _takeIdHooks() { |
10385 | 10380 | this._idHooks = true; |
10386 | | - for (const name of /** @type {(keyof PathHooks)[]} */ ( |
10387 | | - Object.keys(PATH_HOOK_NODES) |
10388 | | - )) { |
10389 | | - const nodes = /** @type {number} */ (PATH_HOOK_NODES[name]); |
10390 | | - const hookOrMap = /** @type {PathHook | HookMap<PathHook>} */ ( |
10391 | | - this.hooks[name] |
10392 | | - ); |
10393 | | - const hooks = |
10394 | | - hookOrMap instanceof HookMap |
10395 | | - ? /** @type {{ _map: Map<string, PathHook> }} */ ( |
10396 | | - /** @type {unknown} */ (hookOrMap) |
10397 | | - )._map.values() |
10398 | | - : [hookOrMap]; |
10399 | | - for (const hook of hooks) { |
10400 | | - let changed = false; |
10401 | | - for (const tap of hook.taps) { |
10402 | | - // a path tap reads ids, as do the parser's own `ids` taps |
10403 | | - if ( |
10404 | | - /** @type {{ path?: boolean, ids?: boolean }} */ (tap).path === |
10405 | | - true || |
10406 | | - /** @type {{ ids?: boolean }} */ (tap).ids === true |
10407 | | - ) { |
10408 | | - continue; |
10409 | | - } |
10410 | | - tap.fn = objectTapOf( |
10411 | | - /** @type {(node: Node) => boolean | void} */ (tap.fn), |
10412 | | - nodes, |
10413 | | - this, |
10414 | | - argumentCountOf(hook) |
10415 | | - ); |
10416 | | - changed = true; |
10417 | | - } |
10418 | | - if (changed) { |
10419 | | - /** @type {{ _resetCompilation: () => void }} */ ( |
10420 | | - /** @type {unknown} */ (hook) |
10421 | | - )._resetCompilation(); |
10422 | | - } |
| 10381 | + for (const { hook, nodes, tap } of this._pathHooks) { |
| 10382 | + const taps = hook.taps; |
| 10383 | + // a path tap reads ids, as do the parser's own `ids` taps |
| 10384 | + if ( |
| 10385 | + taps.every( |
| 10386 | + (item) => |
| 10387 | + /** @type {{ path?: boolean, ids?: boolean }} */ (item).path === |
| 10388 | + true || /** @type {{ ids?: boolean }} */ (item).ids === true |
| 10389 | + ) |
| 10390 | + ) { |
| 10391 | + continue; |
| 10392 | + } |
| 10393 | + // tapped again in order, as `tap` sorts them, which also recompiles |
| 10394 | + // the hook's call |
| 10395 | + const previous = taps.splice(0); |
| 10396 | + for (const { fn, type, ...options } of previous) { |
| 10397 | + const tapFn = /** @type {(node: Node) => boolean | void} */ (fn); |
| 10398 | + tap.call( |
| 10399 | + hook, |
| 10400 | + options, |
| 10401 | + /** @type {{ path?: boolean }} */ (options).path === true || |
| 10402 | + /** @type {{ ids?: boolean }} */ (options).ids === true |
| 10403 | + ? tapFn |
| 10404 | + : objectTapOf(tapFn, nodes, this) |
| 10405 | + ); |
10423 | 10406 | } |
10424 | 10407 | } |
10425 | 10408 | } |
|