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