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

Function methodsOf

scripts/gen-go-stdlib-methods.mjs:100–140  ·  view source on GitHub ↗

Method name → `params/results` for the type `name` the package at `importPath` declares.

(importPath, name, seen = new Set())

Source from the content-addressed store, hash-verified

98
99/** Method name → `params/results` for the type `name` the package at `importPath` declares. */
100function methodsOf(importPath, name, seen = new Set()) {
101 const methods = new Map();
102 const key = `${importPath}.${name}`;
103 if (seen.has(key)) return methods;
104 seen.add(key);
105 const p = pkg(importPath);
106 for (const [m, arity] of p.methods.get(name) ?? []) methods.set(m, arity);
107 const declared = p.types.get(name);
108 if (!declared) return methods;
109 const take = (more) => {
110 for (const [m, arity] of more) if (!methods.has(m)) methods.set(m, arity);
111 };
112 const embedded = (type, imports) => {
113 if (type?.type === 'pointer_type') type = type.namedChildren[0];
114 if (type?.type === 'generic_type') type = type.childForFieldName('type');
115 if (type?.type === 'qualified_type') {
116 take(methodsOf(imports.get(type.childForFieldName('package').text), type.childForFieldName('name').text, seen));
117 } else if (type?.type === 'type_identifier') {
118 if (type.text === 'error') take(new Map([['Error', '0/1']]));
119 else take(methodsOf(importPath, type.text, seen));
120 }
121 };
122 const type = declared.type;
123 if (type?.type === 'interface_type') {
124 for (const element of type.namedChildren) {
125 if (element.type === 'method_elem') methods.set(element.childForFieldName('name').text, goSignatureArity(signature(element)));
126 else if (element.type === 'type_elem') {
127 const terms = element.namedChildren.filter((c) => c.type !== 'comment');
128 if (terms.length === 1) embedded(terms[0], declared.imports);
129 }
130 }
131 } else if (type?.type === 'struct_type') {
132 const fields = type.namedChildren.find((c) => c.type === 'field_declaration_list');
133 for (const field of fields?.namedChildren.filter((c) => c.type === 'field_declaration') ?? []) {
134 if (!field.namedChildren.some((c) => c.type === 'field_identifier')) embedded(field.childForFieldName('type'), declared.imports);
135 }
136 } else if (declared.alias) {
137 embedded(type, declared.imports);
138 }
139 return methods;
140}
141
142const EXPORTED = /^\p{Lu}/u;
143const generated = [];

Callers 2

embeddedFunction · 0.70

Calls 8

goSignatureArityFunction · 0.85
signatureFunction · 0.85
embeddedFunction · 0.85
hasMethod · 0.80
pkgFunction · 0.70
getMethod · 0.65
addMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected