Skip to content

Commit c11d62f

Browse files
committed
Tighten module-graph map helpers (drop double-lookup + optional chains)
1 parent a661a21 commit c11d62f

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

packages/knip/src/util/module-graph.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,30 @@ export const createImports = (): ImportMaps => ({
6464
});
6565

6666
export const addValue = (map: IdToFileMap, id: string, value: string) => {
67-
if (map.has(id)) map.get(id)?.add(value);
67+
const existing = map.get(id);
68+
if (existing) existing.add(value);
6869
else map.set(id, new Set([value]));
6970
};
7071

7172
export const addNsValue = (map: IdToNsToFileMap, id: string, ns: string, value: string) => {
72-
if (map.has(id)) {
73-
if (map.get(id)?.has(ns)) map.get(id)?.get(ns)?.add(value);
74-
else map.get(id)?.set(ns, new Set([value]));
75-
} else {
73+
const inner = map.get(id);
74+
if (!inner) {
7675
map.set(id, new Map([[ns, new Set([value])]]));
76+
return;
7777
}
78+
const set = inner.get(ns);
79+
if (set) set.add(value);
80+
else inner.set(ns, new Set([value]));
7881
};
7982

8083
const addValues = (map: IdToFileMap, id: string, values: Set<string>) => {
81-
if (map.has(id)) for (const v of values) map.get(id)?.add(v);
84+
const existing = map.get(id);
85+
if (existing) for (const v of values) existing.add(v);
8286
else map.set(id, values);
8387
};
8488

8589
const addNsValues = (map: IdToNsToFileMap, id: string, value: IdToFileMap) => {
86-
// @ts-expect-error come on
87-
if (map.has(id)) for (const [ns, v] of value) addValues(map.get(id), ns, v);
90+
const existing = map.get(id);
91+
if (existing) for (const [ns, v] of value) addValues(existing, ns, v);
8892
else map.set(id, value);
8993
};

0 commit comments

Comments
 (0)