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

Function matchRubyConstantPathRef

src/resolution/ruby-constant-paths.ts:66–113  ·  view source on GitHub ↗
(ref: UnresolvedRef, context: ResolutionContext)

Source from the content-addressed store, hash-verified

64 * of the name.
65 */
66export function matchRubyConstantPathRef(ref: UnresolvedRef, context: ResolutionContext): ResolvedRef | null | undefined {
67 if (ref.language !== 'ruby') return undefined;
68 if (ref.referenceKind === 'references') {
69 const m = CONSTANT_PATH.exec(ref.referenceName);
70 if (!m || !isNamespaced(m)) return undefined;
71 const target = constantsNamed(m[2]!, m[1] !== undefined, ref, context)[0];
72 return target ? { original: ref, targetNodeId: target.id, confidence: 0.9, resolvedBy: 'qualified-name' } : null;
73 }
74 if (ref.referenceKind === 'calls') {
75 // A call on an instance `Foo.new(…)` made, bare or namespaced: an instance
76 // method of the class Ruby's lookup finds for `Foo` there, its own or one
77 // its ancestry declares — never a namesake's by the method name alone. A
78 // class the project doesn't declare (rack-protection's `Request.new(env)`
79 // is rack's) has no method here.
80 const made = CONSTRUCTED_CALL.exec(ref.referenceName);
81 if (made) {
82 const cls = constantsNamed(made[2]!, made[1] !== undefined, ref, context)[0];
83 const method = cls?.kind === 'class' ? methodOf(cls.qualifiedName, made[3]!, ref, context, 'instance') : null;
84 return method ? { original: ref, targetNodeId: method.node.id, confidence: method.own ? 0.9 : 0.85, resolvedBy: 'instance-method' } : null;
85 }
86 // A call on an instance variable, typed by the class's `initialize`: an
87 // instance method of the class it is given there, or nothing when that
88 // class is not the project's. A variable `initialize` doesn't type is
89 // left to the other strategies, as before.
90 const ivar = IVAR_CALL.exec(ref.referenceName);
91 if (ivar) {
92 const cls = ivarClass(ivar[1]!, ref, context);
93 if (cls === undefined) return undefined;
94 const method = cls ? methodOf(cls.qualifiedName, ivar[2]!, ref, context, 'instance') : null;
95 return method ? { original: ref, targetNodeId: method.node.id, confidence: method.own ? 0.85 : 0.8, resolvedBy: 'instance-method' } : null;
96 }
97 // A call on what a method of the class around it hands back. A name the
98 // calling method binds itself, or a method that says nothing of its value,
99 // is left to the other strategies.
100 const bare = BARE_NAME_CALL.exec(ref.referenceName);
101 if (bare) {
102 const cls = accessorClass(bare[1]!, ref, context);
103 const method = cls ? methodOf(cls.qualifiedName, bare[2]!, ref, context, 'instance') : null;
104 return method ? { original: ref, targetNodeId: method.node.id, confidence: method.own ? 0.85 : 0.8, resolvedBy: 'instance-method' } : undefined;
105 }
106 const m = CONSTANT_PATH_CALL.exec(ref.referenceName);
107 if (!m || !isNamespaced(m)) return undefined;
108 const owner = constantsNamed(m[2]!, m[1] !== undefined, ref, context)[0];
109 const method = owner && NAMESPACE_KINDS.has(owner.kind) ? methodOf(owner.qualifiedName, m[3]!, ref, context) : null;
110 return method ? { original: ref, targetNodeId: method.node.id, confidence: method.own ? 0.85 : 0.8, resolvedBy: 'qualified-name' } : null;
111 }
112 return undefined;
113}
114
115/** Whether a matched path is written with a `::`, which a bare `Foo` is not. */
116function isNamespaced(m: RegExpExecArray): boolean {

Callers 1

resolveOneInnerMethod · 0.90

Calls 7

isNamespacedFunction · 0.85
constantsNamedFunction · 0.85
ivarClassFunction · 0.85
accessorClassFunction · 0.85
hasMethod · 0.80
methodOfFunction · 0.70
execMethod · 0.65

Tested by

no test coverage detected