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

Function scalaMemberInScope

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

* A bare Scala name reaches a member of another type — `FreeT.FlatMapped`'s * `type A`, a test suite's `val P` — only from inside that type, a type that * extends it, or a file importing its members (`import Owner._`, * `Owner.{A}`, `Owner.*`). cats' bare type parameters landed on such members *

(result: ResolvedRef, ref: UnresolvedRef, context: ResolutionContext)

Source from the content-addressed store, hash-verified

115 * across the library once the test-local classes stopped catching them.
116 */
117function scalaMemberInScope(result: ResolvedRef, ref: UnresolvedRef, context: ResolutionContext): boolean {
118 if (!/^[A-Za-z_]\w*$/.test(ref.referenceName) || ref.referenceKind !== 'references') return true;
119 // Bare in the SOURCE: the index keeps `SttpClient.Service` by its last
120 // segment, and a qualified name reaches any member.
121 const line = context.getFileLines?.(ref.filePath)?.[ref.line - 1] ?? context.readFile(ref.filePath)?.split(/\r?\n/)[ref.line - 1];
122 if (line === undefined || !line.startsWith(ref.referenceName, ref.column) || /\.\s*$/.test(line.slice(0, ref.column))) return true;
123 const target = context.getNodeById?.(result.targetNodeId);
124 if (!target || target.language !== 'scala') return true;
125 const cut = target.qualifiedName.lastIndexOf('::');
126 if (cut < 0) return true;
127 const ownerQn = target.qualifiedName.slice(0, cut);
128 const containers = context
129 .getNodesByQualifiedName(ownerQn)
130 .filter((o) => o.filePath === target.filePath && o.startLine <= target.startLine && o.endLine >= target.endLine);
131 const inside = (o: Node): boolean => ref.filePath === o.filePath && ref.line >= o.startLine && ref.line <= o.endLine;
132 // A def's local (`case class B()` in a test method): only in there.
133 const def = containers.find((o) => o.kind === 'function' || o.kind === 'method');
134 if (def) return inside(def);
135 const owner = containers.find((o) => SCALA_OWNER_KINDS.has(o.kind));
136 if (!owner) return true; // a package member
137 if (inside(owner)) return true;
138 // Inherited: the reference sits in a type that extends the owner.
139 for (const around of enclosingDeclarations(ref, context)) {
140 if (!SCALA_OWNER_KINDS.has(around.kind)) continue;
141 if (around.name === owner.name) return true;
142 if (context.getSupertypes?.(around.name, 'scala').includes(owner.name)) return true;
143 }
144 // Imported: `import cats.data.FreeT.FlatMapped._`, `Owner.{A, B}`, `Owner.*`.
145 const text = context.readFile(ref.filePath) ?? '';
146 const imports = new RegExp(
147 String.raw`\bimport\s+[\w.]*\b${escapeRegExp(owner.name)}\.(?:_|\*|given\b|\{[^}]*\b(?:_|\*|${escapeRegExp(target.name)})\b[^}]*\}|${escapeRegExp(target.name)}\b)`,
148 );
149 return /^[A-Za-z_]\w*$/.test(owner.name) && /^[A-Za-z_]\w*$/.test(target.name) ? imports.test(text) : true;
150}
151
152function enclosingDeclarations(at: { filePath: string; line: number }, context: ResolutionContext): Node[] {
153 return context

Callers 1

gateTypeParameterFunction · 0.85

Calls 9

escapeRegExpFunction · 0.90
enclosingDeclarationsFunction · 0.85
getFileLinesMethod · 0.80
readFileMethod · 0.80
hasMethod · 0.80
getSupertypesMethod · 0.80
insideFunction · 0.70
getNodeByIdMethod · 0.65

Tested by

no test coverage detected