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 — refactor(javascript): drop the walk's remaining unknown and object casts · webpack/webpack@6d9b7be

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/javascript
    • JavascriptParser.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/javascript/JavascriptParser.js‎

Copy file name to clipboardExpand all lines: lib/javascript/JavascriptParser.js
+96-113Lines changed: 96 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,13 +1777,13 @@ const nodeIdOf = (view, node) =>
17771777
* A node argument a `{ path: true }` tap gets while the parse walks the tree:
17781778
* an id the walk passed as it is, a lazy object of the tree as its id.
17791779
* @param {SyntaxTree} tree the tree
1780-
* @param {unknown} node the argument
1780+
* @param {Node | number | null | undefined} node the argument
17811781
* @returns {number} its id, `0` for none, `-1` for an object of no tree's
17821782
*/
17831783
const treeIdOf = (tree, node) => {
17841784
if (typeof node === "number") return node;
17851785
if (node === null || node === undefined) return 0;
1786-
return tree.nodeOf(/** @type {Node} */ (node)) || -1;
1786+
return tree.nodeOf(node) || -1;
17871787
};
17881788

17891789
// The ESTree type of each `NodeType`, the key `hooks.evaluate` is read by.
@@ -1797,11 +1797,11 @@ for (const name of Object.keys(NodeType)) {
17971797
/**
17981798
* A node's id where a hook or a method takes the node: in the tree walk the id
17991799
* 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]
18011801
* @param {number} id the node's id
18021802
* @returns {T} the id, typed as the node it stands for
18031803
*/
1804-
const asNode = (id) => /** @type {T} */ (/** @type {unknown} */ (id));
1804+
const asNode = (id) => /** @type {T} */ (id);
18051805

18061806
/**
18071807
* 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));
18111811
* @param {(...args: A) => R} fn the tap's function
18121812
* @param {number} nodes a bit for each argument that is a node
18131813
* @param {JavascriptParser} parser the parser
1814-
* @param {number} count how many arguments the hook passes, all the tap gets
18151814
* @returns {(...args: A) => R} the function to tap instead
18161815
*/
1817-
const objectTapOf = (fn, nodes, parser, count) => {
1816+
const objectTapOf = (fn, nodes, parser) => {
18181817
/**
18191818
* @param {A[number]} arg an argument
18201819
* @param {number} bit its bit in `nodes`
@@ -1826,55 +1825,40 @@ const objectTapOf = (fn, nodes, parser, count) => {
18261825
/** @type {SyntaxTree} */ (parser._tree).estree(arg)
18271826
)
18281827
: 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+
};
18551837
}
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+
);
18571850
};
18581851

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-
18681852
/**
18691853
* 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
18711855
* gets no interceptor, so a call to it compiles as before.
18721856
* @template T
18731857
* @template R
18741858
* @param {SyncBailHook<T, R>} hook the hook
18751859
* @param {number} nodes a bit for each argument that is a node
18761860
* @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
18781862
*/
18791863
const takePathTaps = (hook, nodes, parser) => {
18801864
const tap = hook.tap;
@@ -1890,9 +1874,7 @@ const takePathTaps = (hook, nodes, parser) => {
18901874
options,
18911875
parser._idHooks &&
18921876
/** @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)
18961878
: fn
18971879
);
18981880
}
@@ -1910,9 +1892,18 @@ const takePathTaps = (hook, nodes, parser) => {
19101892
) => {
19111893
const tree = parser._tree;
19121894
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;
19161907
if (ia !== -1 && ib !== -1 && ic !== -1) {
19171908
return pathFn(
19181909
/** @type {JavascriptNodeReader} */ (tree),
@@ -1965,6 +1956,7 @@ const takePathTaps = (hook, nodes, parser) => {
19651956
/** @type {(...args: AsArray<T>) => R} */ (wrapper)
19661957
);
19671958
};
1959+
return tap;
19681960
};
19691961

19701962
/**
@@ -2414,8 +2406,6 @@ class JavascriptParser extends Parser {
24142406
// the tree the walk reads by id, while `_walkTree` parses into one
24152407
/** @type {SyntaxTree | undefined} */
24162408
this._tree = undefined;
2417-
// whether the hooks the id walk calls hand a tap without `path` objects
2418-
this._idHooks = false;
24192409
// The view `{ path: true }` taps read the parse's nodes through, made on
24202410
// the first such tap of a parse, and one kept for the next parse.
24212411
/** @type {EstreeView | undefined} */
@@ -2425,6 +2415,12 @@ class JavascriptParser extends Parser {
24252415
// The program being parsed, whose directives the view marks.
24262416
/** @type {Program | undefined} */
24272417
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 = [];
24282424
for (const name of /** @type {(keyof PathHooks)[]} */ (
24292425
Object.keys(PATH_HOOK_NODES)
24302426
)) {
@@ -2436,13 +2432,15 @@ class JavascriptParser extends Parser {
24362432
hook.intercept(
24372433
/** @type {{ factory: (key: string, made: PathHook) => PathHook }} */ ({
24382434
factory: (key, made) => {
2439-
takePathTaps(made, nodes, this);
2435+
const tap = takePathTaps(made, nodes, this);
2436+
this._pathHooks.push({ hook: made, nodes, tap });
24402437
return made;
24412438
}
24422439
})
24432440
);
24442441
} else {
2445-
takePathTaps(hook, nodes, this);
2442+
const tap = takePathTaps(hook, nodes, this);
2443+
this._pathHooks.push({ hook, nodes, tap });
24462444
}
24472445
}
24482446
// the scope last hoisted into, whose statements its block pass reads next,
@@ -3028,7 +3026,8 @@ class JavascriptParser extends Parser {
30283026
.for("UnaryExpression")
30293027
.tap(
30303028
/** @type {{ name: string }} */ ({ name: CLASS_NAME, ids: true }),
3031-
(_node) => {
3029+
(expression) => {
3030+
const _node = /** @type {UnaryExpression} */ (expression);
30323031
const operator = this._operatorOf(_node);
30333032
if (operator === "typeof") {
30343033
const tree = this._tree;
@@ -3047,9 +3046,7 @@ class JavascriptParser extends Parser {
30473046
typeof argument === "number"
30483047
? /** @type {SyntaxTree} */ (tree).name(argument)
30493048
: /** @type {Identifier} */ (argument).name,
3050-
typeof _node === "number"
3051-
? asNode(_node)
3052-
: /** @type {UnaryExpression} */ (_node)
3049+
typeof _node === "number" ? asNode(_node) : _node
30533050
);
30543051
if (res !== undefined) return res;
30553052
break;
@@ -3869,7 +3866,7 @@ class JavascriptParser extends Parser {
38693866
const range = this._rangeOf(expr);
38703867
if (!range) return;
38713868
const expressions = this._childrenOf(
3872-
expr,
3869+
/** @type {SequenceExpression} */ (expr),
38733870
Field.expressions,
38743871
"expressions"
38753872
);
@@ -9916,40 +9913,38 @@ class JavascriptParser extends Parser {
99169913
/**
99179914
* A child of a node to evaluate or walk: its id when the node is a lazy
99189915
* 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
99209919
* @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
99239922
*/
99249923
_childOf(node, field, key) {
99259924
const tree = this._tree;
99269925
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);
99319927
if (id !== 0) {
99329928
return asNode(tree.field(0, field, id));
99339929
}
99349930
}
9935-
return /** @type {Record<string, Expression>} */ (node)[key];
9931+
return /** @type {N} */ (node)[key];
99369932
}
99379933

99389934
/**
99399935
* The children in a list field of a node to evaluate: their ids when the
99409936
* 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
99429940
* @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
99449942
* @returns {(Expression | SpreadElement)[]} the children, or their ids standing for them
99459943
*/
99469944
_childrenOf(node, field, key) {
99479945
const tree = this._tree;
99489946
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);
99539948
if (id !== 0) {
99549949
const count = tree.fieldCount(field, id);
99559950
/** @type {(Expression | SpreadElement)[]} */
@@ -9960,9 +9955,9 @@ class JavascriptParser extends Parser {
99609955
return children;
99619956
}
99629957
}
9963-
return /** @type {Record<string, (Expression | SpreadElement)[]>} */ (node)[
9964-
key
9965-
];
9958+
return /** @type {(Expression | SpreadElement)[]} */ (
9959+
/** @type {N} */ (node)[key]
9960+
);
99669961
}
99679962

99689963
/**
@@ -10383,43 +10378,31 @@ class JavascriptParser extends Parser {
1038310378
*/
1038410379
_takeIdHooks() {
1038510380
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+
);
1042310406
}
1042410407
}
1042510408
}

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.