MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / declaredTypeParameters

Function declaredTypeParameters

src/resolution/type-parameters.ts:172–215  ·  view source on GitHub ↗

The type parameters a declaration declares, each with the default a C++ head gives it (`typename Parser = TcParser`).

(node: Node, context: ResolutionContext)

Source from the content-addressed store, hash-verified

170
171/** The type parameters a declaration declares, each with the default a C++ head gives it (`typename Parser = TcParser`). */
172function declaredTypeParameters(node: Node, context: ResolutionContext): ReadonlyMap<string, string | undefined> {
173 let memo = memos.get(context);
174 if (!memo) {
175 memo = new Map();
176 memos.set(context, memo);
177 }
178 const hit = memo.get(node.id);
179 if (hit) return hit;
180 const names = new Map<string, string | undefined>();
181 const lines = context.getFileLines?.(node.filePath) ?? context.readFile(node.filePath)?.split(/\r?\n/) ?? null;
182 if (lines) {
183 const head = lines.slice(node.startLine - 1, Math.min(node.endLine, node.startLine - 1 + HEAD_LINES)).join('\n');
184 const at = nameIndex(head, node.name);
185 // (C++ declares them only in a `template <…>` head, below: the `<…>` after
186 // a C++ class's name is a specialization's arguments.)
187 if (at >= 0 && node.language !== 'cpp') {
188 // After the declared name: `Foo<T>`, `f[A]`.
189 let open = at + node.name.length;
190 while (open < head.length && /[ \t]/.test(head[open]!)) open++;
191 if (head[open] === '<' || head[open] === '[') collect(head, open, names);
192 // Before it, detached from any type name: `static <T> T max(`, `fun <T> f(`.
193 const before = head.slice(0, at);
194 for (let i = 0; i < before.length; i++) {
195 if (before[i] === '<' && (i === 0 || /[\s(,]/.test(before[i - 1]!))) collect(before, i, names);
196 }
197 }
198 // Go: a method of a generic type declares the type's parameters again in
199 // its receiver — `func (p *Pool[T]) Get() T`, `func (LazyLoader[K, V]) …`.
200 if (node.language === 'go' && node.kind === 'method') {
201 const receiver = /^\s*func\s*\(\s*(?:[A-Za-z_]\w*\s+)?\*?\s*[A-Za-z_]\w*\s*\[/.exec(head);
202 if (receiver) collect(head, receiver[0].length - 1, names);
203 }
204 // C++: `template <typename T, class U>` on the lines just above. Each
205 // parameter is named last: `int N = 0`, `detail::fixed_string S` and
206 // `std::integral T` declare N, S and T.
207 if (node.language === 'cpp') {
208 const above = lines.slice(Math.max(0, node.startLine - 3), node.startLine).join('\n');
209 const template = /\btemplate\s*</g;
210 for (let m = template.exec(above); m; m = template.exec(above)) collect(above, m.index + m[0].length - 1, names, true);
211 }
212 }
213 memo.set(node.id, names);
214 return names;
215}
216
217const valueMemos = new WeakMap<ResolutionContext, Map<string, ReadonlySet<string>>>();
218

Callers 2

gateTypeParameterFunction · 0.85
typeParametersInScopeFunction · 0.85

Calls 8

nameIndexFunction · 0.85
getFileLinesMethod · 0.80
readFileMethod · 0.80
collectFunction · 0.70
getMethod · 0.65
execMethod · 0.65
setMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected