MCPcopy Index your code
hub / github.com/react/react / generateHookMap

Function generateHookMap

packages/react-devtools-shared/src/hooks/generateHookMap.js:62–95  ·  view source on GitHub ↗
(sourceAST: File)

Source from the content-addressed store, hash-verified

60 * }
61 */
62export function generateHookMap(sourceAST: File): HookMap {
63 const hookNamesMapping = getHookNamesMappingFromAST(sourceAST);
64 const namesMap: Map<string, number> = new Map();
65 const names = [];
66 const mappings: Array<HookMapLine> = [];
67
68 let currentLine: $FlowFixMe | null = null;
69 hookNamesMapping.forEach(({name, start}) => {
70 let nameIndex = namesMap.get(name);
71 if (nameIndex == null) {
72 names.push(name);
73 nameIndex = names.length - 1;
74 namesMap.set(name, nameIndex);
75 }
76
77 // TODO: We add a -1 at the end of the entry so we can later
78 // encode/decode the mappings by reusing the encode/decode functions
79 // from the `sourcemap-codec` library. This library expects segments
80 // of specific sizes (i.e. of size 4) in order to encode them correctly.
81 // In the future, when we implement our own encoding, we will not
82 // need this restriction and can remove the -1 at the end.
83 const entry = [start.line, start.column, nameIndex, -1];
84
85 if (currentLine !== start.line) {
86 currentLine = start.line;
87 mappings.push([entry]);
88 } else {
89 const current = mappings[mappings.length - 1];
90 current.push(entry);
91 }
92 });
93
94 return {names, mappings};
95}
96
97/**
98 * Returns encoded version of a Hook Map that is returned

Calls 5

setMethod · 0.80
forEachMethod · 0.65
getMethod · 0.65
pushMethod · 0.65

Tested by

no test coverage detected