Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ This will output:

```
$ bundle exec methodray check app/models/user.rb
app/models/user.rb:4:15: error: undefined method `abs` for String
app/models/user.rb:4:19: error: undefined method `abs` for String
message = name.abs
^
^
```

## Contributing
Expand Down
6 changes: 5 additions & 1 deletion rust/src/analyzer/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,12 @@ pub fn dispatch_needs_child<'a>(node: &Node<'a>, source: &str) -> Option<NeedsCh
if let Some(call_node) = node.as_call_node() {
if let Some(receiver) = call_node.receiver() {
let method_name = String::from_utf8_lossy(call_node.name().as_slice()).to_string();
// Use call_operator_loc (.) for error position, fallback to node location
let prism_location = call_node
.call_operator_loc()
.unwrap_or_else(|| node.location());
let location =
SourceLocation::from_prism_location_with_source(&node.location(), source);
SourceLocation::from_prism_location_with_source(&prism_location, source);

// Get block if present (e.g., `x.each { |i| ... }`)
let block = call_node.block();
Expand Down
45 changes: 45 additions & 0 deletions rust/src/analyzer/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,3 +731,48 @@ fn test_hash_nested() {
"Hash[Symbol, Array[Integer]]"
);
}

// ============================================
// Error Location Tests
// ============================================

#[test]
fn test_error_location_points_to_dot() {
// Test that error location points to the dot operator, not the receiver
// "name.abs" - error should point to column 5 (the `.`)
let source = "name = \"x\"\nname.abs";

let (genv, _lenv) = analyze(source);

assert_eq!(genv.type_errors.len(), 1);
let error = &genv.type_errors[0];

// Location should be present
assert!(error.location.is_some());
let loc = error.location.as_ref().unwrap();

// Line 2, column 5 (1-indexed) - the dot position
assert_eq!(loc.line, 2);
assert_eq!(loc.column, 5); // "name" is 4 chars, "." is at column 5
}

#[test]
fn test_error_location_method_chain() {
// Test error location in method chain: "x.upcase.foo"
// Error should point to the second dot (for undefined method `foo`)
let source = "x = \"hello\"\ny = x.upcase.foo";

let (genv, _lenv) = analyze(source);

assert_eq!(genv.type_errors.len(), 1);
let error = &genv.type_errors[0];
assert_eq!(error.method_name, "foo");

assert!(error.location.is_some());
let loc = error.location.as_ref().unwrap();

// Line 2, the second dot position
assert_eq!(loc.line, 2);
// "y = x.upcase" is 12 chars, "." is at column 13
assert_eq!(loc.column, 13);
}