mirror of
https://github.com/abhigyanpatwari/GitNexus.git
synced 2026-11-29 23:37:41 +00:00
* fix(ruby): a block parameter stops the receiver type walk — it no longer takes a same-named outer local's type (RUBY-03)
The shared receiver walk stops at the nearest scope that owns the
receiver's name, and a scope owns a name it holds no type for only through
Scope.lexicalNames. Ruby published none, so an untyped parameter was
invisible to the walk, which climbed past it to a same-named outer binding:
`user = User.new; items.each { |user| user.save }` resolved the block's
`user.save` to User#save.
- emitRubyScopeCaptures attaches @scope.lexical-names to every scope that
declares parameters: a do/end or brace block (its |...| names, block
locals included; this covers lambda { |x| }, proc { |x| } and
Proc.new { |x| }), the body block of a stabby lambda (its parameter list
is a sibling of the body), and a method or singleton method. Blocks
already had scopes; none is added. Numbered and `it` parameters are not
declared names and are not listed.
- The YARD @param decline (RUBY-01) no longer fires for a block, lambda or
nested-def parameter of the tagged name: the shadow now stops the walk on
its own scope, so the tagged use beside it keeps its type. A stabby lambda
whose parameter default reads the name still declines, because the
parameter list sits outside the lambda body.
Fixed shapes, each pinned against exact owner IDs with a decoy: block and
lambda/proc parameters shadowing a typed local or a YARD-typed parameter,
nested blocks, every block-parameter form, an untyped method parameter named
like a typed reader, an attr_reader @return type or a top-level local, a
nested def parameter under a tagged method, and a bare read of a parameter
(a read, not a call). Recall cost: an explicit target() / target(1) /
target 1 beside a same-named block parameter now resolves to nothing, as it
already did beside a same-named method parameter.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
* test(bench): gate Ruby block-parameter stop correctness and scaling (RUBY-03)
Block, lambda and method parameters are published as lexical names on the
scope that declares them, so the receiver walk stops at a block parameter
instead of taking a same-named outer local's type.
bench/ruby-block-param-stop pins what that produces and checks how its
cost grows, in the format of bench/ruby-gem-resolution.
Each file holds class Svc<i> with m(items): `user = User.new`, K blocks
rotating `|x| user.run` (the nearest `user`), `|user| user.save` and
`|user, i| user.save` (no edge), side by side or nested, then `user.save`.
Each method's CALLS must equal the expected multiset; main gives the
rebinding blocks wrong User#save edges. One class per file, because Ruby
resolution on main is already super-linear in the classes of ONE file
(measured with `--shape crowded`, not gated).
--check runs a CI profile (about 15 s locally); --full keeps min of 15
after 2 warmups and a fresh process per shape. Every budget is 1.5.
Re-running the scope query over the method for every block scope fails
the nested block gate (2.242 > 1.5).
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
* test(bench): ruby-block-param-stop — fixed-width indent, input bytes per cell, 16x side-by-side blocks
Three defects the scaling-bench review found in the other benches apply
here too:
- the nested generator indented each level two spaces deeper, so the
input grew 6.1x for 4x depth; every line now has a fixed-width indent
and each shape pins its input_bytes beside its groups and edges;
- the gate could not see a known O(blocks^2) edit: walking the enclosing
method once per block scope measured 1.410 on the 4x side-by-side
range and passed. Side-by-side blocks now grow 16x (6 -> 96), where
the same edit fails sibling_capture_block_scaling_ratio 3.596 > 1.5
(nested 1.778) with unchanged fingerprints;
- the ratio divides by each shape's own growth factor.
Nesting depth stays 4x: over 6 -> 96 it measures 2.57 (capture) and 2.46
(resolution) from two terms that predate #3905 (the shared callable-flow
fallback that searches a block-taking call's subtree for an argument
list, and the shared lexical lookup climbing from each reference to its
binder); baseline.json `_nested_depth_note` records both. Three CI-profile
runs and one --full run pass, every ratio at most 1.202.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
* fix(ruby): resolve PR #3905 feedback
Preserve outer YARD types across lambda defaults and explicit self calls beside parameters. Add parameter-order regressions and a four-size scaling sweep.
Note: pre-existing failure in scenario-map/consolidation.test.ts (jscpd 5.4.2 installed; 5.4.3 expected) not addressed by this PR.
---------
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
114 lines
4.2 KiB
TypeScript
114 lines
4.2 KiB
TypeScript
/**
|
|
* RUBY-03 — what Ruby publishes for the receiver walk to stop on.
|
|
*
|
|
* A scope that declares parameters carries `@scope.lexical-names`: the names it
|
|
* binds, typed or not. Each binder lands on the scope that declares it — a
|
|
* block's `|…|` names (block locals included) on that block, a stabby lambda's
|
|
* `->(…)` names on its body block (the parameter list sits outside the body),
|
|
* a method's parameters on the method — and never on the enclosing body. A
|
|
* scope that declares no parameter carries no capture.
|
|
*/
|
|
import { describe, expect, it } from 'vitest';
|
|
import type { CaptureMatch } from 'gitnexus-shared';
|
|
import { emitRubyScopeCaptures } from '../../../../src/core/ingestion/languages/ruby/index.js';
|
|
|
|
const SOURCE = `class Svc
|
|
def run(user, *rest, key:, opt: 1, **kw, &blk)
|
|
items.each do |item, (a, b); tmp|
|
|
[1].each { |inner| inner }
|
|
end
|
|
check = ->(x, y = 2) { x }
|
|
bare = -> z { z }
|
|
loose = lambda { |w| w }
|
|
nothing = -> { 1 }
|
|
items.map { _1 }
|
|
items.map { it }
|
|
end
|
|
|
|
def self.build(cfg)
|
|
cfg
|
|
end
|
|
|
|
def none
|
|
[1].each { 2 }
|
|
end
|
|
end
|
|
`;
|
|
|
|
/** `startLine:startCol-endLine:endCol` of a capture. */
|
|
const span = (match: CaptureMatch, tag: string): string => {
|
|
const r = match[tag]?.range;
|
|
return r === undefined ? `<no ${tag}>` : `${r.startLine}:${r.startCol}-${r.endLine}:${r.endCol}`;
|
|
};
|
|
|
|
/** Every function or block scope, keyed `kind span`, with the names it lists. */
|
|
function scopeNames(matches: readonly CaptureMatch[]): Record<string, string[]> {
|
|
const out: Record<string, string[]> = {};
|
|
for (const match of matches) {
|
|
const tag = ['@scope.function', '@scope.block'].find((t) => match[t] !== undefined);
|
|
if (tag === undefined) continue;
|
|
const raw = match['@scope.lexical-names']?.text;
|
|
out[`${tag.slice('@scope.'.length)} ${span(match, tag)}`] =
|
|
raw === undefined ? [] : (JSON.parse(raw) as string[]);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
describe('Ruby lexical names (RUBY-03)', () => {
|
|
it('preserves callable .() and .call invocations while dropping the false name() invocation', () => {
|
|
const matches = emitRubyScopeCaptures(
|
|
'class Host; def run(f); f.(); f.call; f(); end; end',
|
|
'calls.rb',
|
|
);
|
|
expect(
|
|
matches
|
|
.filter((match) => match['@callable-flow.invoke'])
|
|
.map((match) => match['@callable-flow.invoke']?.text),
|
|
).toEqual(['f.()', 'f.call']);
|
|
const call = matches.find((match) => match['@reference.call.member']?.text === 'f()');
|
|
expect(call?.['@reference.receiver']?.text).toBe('self');
|
|
expect(call?.['@reference.arity']?.text).toBe('0');
|
|
});
|
|
|
|
it('lambda defaults claim only referenced names declared before the expression', () => {
|
|
const matches = emitRubyScopeCaptures(
|
|
'def run(user)\n f = ->(x = user.save, user: nil, y: user.save) { user.save }\nend\n',
|
|
'defaults.rb',
|
|
);
|
|
expect(
|
|
matches
|
|
.filter((match) => match['@scope.block'])
|
|
.map((match) => [
|
|
match['@scope.block']?.text,
|
|
JSON.parse(match['@scope.lexical-names']?.text ?? '[]'),
|
|
]),
|
|
).toEqual([
|
|
['user.save', ['user']],
|
|
['{ user.save }', ['x', 'user', 'y']],
|
|
]);
|
|
});
|
|
|
|
it('each scope lists the parameters IT declares; nested blocks keep their own', () => {
|
|
expect(scopeNames(emitRubyScopeCaptures(SOURCE, 'svc.rb'))).toEqual({
|
|
// a method: positional, splat, keyword, optional keyword, double splat, block
|
|
'function 2:2-12:5': ['user', 'rest', 'key', 'opt', 'kw', 'blk'],
|
|
// do … end: positional, destructured, block local — not the inner block's name
|
|
'block 3:15-5:7': ['item', 'a', 'b', 'tmp'],
|
|
'block 4:15-4:32': ['inner'],
|
|
// a stabby lambda's parameters bind on its body block
|
|
'block 6:25-6:30': ['x', 'y'],
|
|
'block 7:16-7:21': ['z'],
|
|
// lambda { |w| … } is an ordinary block
|
|
'block 8:19-8:28': ['w'],
|
|
// no declared parameter: numbered and `it` parameters are not declared
|
|
// names (the grammar has no node for them), so nothing is listed
|
|
'block 9:17-9:22': [],
|
|
'block 10:14-10:20': [],
|
|
'block 11:14-11:20': [],
|
|
// a singleton method
|
|
'function 14:2-16:5': ['cfg'],
|
|
'function 18:2-20:5': [],
|
|
'block 19:13-19:18': [],
|
|
});
|
|
});
|
|
});
|