Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

fix(ruby): match module_function symbols exactly (#1245)

Fabián Reyesbd2bbc878358
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 43617c07..5f46a0ae 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,12 @@
## Unreleased
+- Ruby: `bridge` no longer stops with "`soundex` is defined twice" on a module that declares
+ `module_function :soundex` and then `module_function :soundex_str`. A `module_function` call
+ now matches the symbols it names exactly, `?`, `!` and `=` suffixes and operator names
+ included (`:save!` names `save!`, not `save`; `:+` and `:[]=` stay listed); before,
+ `:soundex_str` also matched `soundex` as a substring, so the export was listed twice (found
+ porting the `text` gem's `soundex.rb`, by @cfreyes7).
- The Claude directory copy of the plugin (`directory/`) no longer tells Claude to add the local MCP
server (`rust-anything mcp`, `rust-anything setup`), which the directory refuses for plugins
that are not from a Verified organization. It uses the CLI only; the plugin in `plugin/` keeps
diff --git a/src/lang/ruby.rs b/src/lang/ruby.rs
index 85f3aaac..683449bd 100644
--- a/src/lang/ruby.rs
+++ b/src/lang/ruby.rs
@@ -1121,12 +1121,13 @@ fn namespace(
private_names.retain(|n| !public.contains(n));
}
"call" if is_module && text(item, source).starts_with("module_function") => {
- let call = text(item, source);
+ // Exact symbols: `:soundex_str` must not also name `soundex`.
+ let named = symbols(text(item, source));
for m in &instance {
let name = m
.child_by_field_name("name")
.map_or("", |n| text(n, source));
- if call.contains(&format!(":{name}")) {
+ if named.iter().any(|s| s == name) {
found.extend(method_fn(path, &receiver, *m, source, lines));
}
}
@@ -1172,12 +1173,56 @@ fn namespace(
scope.pop();
}
-/// The `:name` symbols a call names (`private :a, :b`).
+/// Ruby's operator method names, longest first so `:[]=` is not read as `[]`.
+const OPERATORS: [&str; 28] = [
+ "[]=", "===", "<=>", "[]", "==", "=~", "!=", "!~", "<=", ">=", "<<", ">>", "**", "+@", "-@",
+ "+", "-", "*", "/", "%", "<", ">", "!", "~", "&", "|", "^", "`",
+];
+
+/// The symbol literals a call names (`private :a, :b`), with a method name's `?`, `!` or `=`
+/// suffix kept (`:save!` is `save!`) and operator names whole (`:+`, `:[]=`). Skips `Foo::Bar`
+/// and `key:`, and a hash rocket (`:a=>1`) ends at `a`.
fn symbols(call: &str) -> Vec<String> {
- call.split(|c: char| !(c.is_alphanumeric() || matches!(c, '_' | ':' | '?')))
- .filter_map(|w| w.strip_prefix(':'))
- .map(str::to_string)
- .collect()
+ let word = |c: char| c.is_alphanumeric() || c == '_';
+ let chars: Vec<char> = call.chars().collect();
+ let mut out = Vec::new();
+ let mut i = 0;
+ while i < chars.len() {
+ let colon = chars[i] == ':' && (i == 0 || !(chars[i - 1] == ':' || word(chars[i - 1])));
+ if !colon {
+ i += 1;
+ continue;
+ }
+ if !chars
+ .get(i + 1)
+ .is_some_and(|&c| c.is_alphabetic() || c == '_')
+ {
+ let rest: String = chars[i + 1..].iter().take(3).collect();
+ match OPERATORS.iter().find(|op| rest.starts_with(**op)) {
+ Some(op) => {
+ out.push((*op).to_string());
+ i += 1 + op.chars().count();
+ }
+ None => i += 1,
+ }
+ continue;
+ }
+ let mut end = i + 1;
+ while chars.get(end).is_some_and(|&c| word(c)) {
+ end += 1;
+ }
+ let suffix = match chars.get(end) {
+ Some('?' | '!') => true,
+ Some('=') => !matches!(chars.get(end + 1), Some('>' | '=' | '~')),
+ _ => false,
+ };
+ if suffix {
+ end += 1;
+ }
+ out.push(chars[i + 1..end].iter().collect());
+ i = end;
+ }
+ out
}
/// `extend self` or `extend(self)`, not `extend OtherModule`.
@@ -1367,6 +1412,46 @@ group :test {
assert_eq!(fns[1].params[0].ts_type, "number");
}
+ #[test]
+ fn module_function_symbol_that_extends_another_name_is_not_a_second_export() {
+ // text gem's soundex.rb: `:soundex_str` used to match `soundex` as a substring.
+ let src = "module Soundex\n def soundex(s)\n s\n end\n module_function :soundex\n\n \
+ private\n\n def soundex_str(s)\n s\n end\n module_function :soundex_str\nend\n";
+ let fns = exports("lib/soundex.rb", src);
+ let names: Vec<&str> = fns.iter().map(|f| f.name.as_str()).collect();
+ assert_eq!(names, ["soundex"]);
+ }
+
+ #[test]
+ fn module_function_symbols_keep_their_bang_and_setter_suffixes() {
+ // `:save!` names `save!`, not `save`: exporting `save` again would stop `bridge`.
+ let src = "module Store\n def save(x)\n x\n end\n module_function :save\n\n \
+ def save!(x)\n x\n end\n module_function :save!\nend\n";
+ let fns = exports("lib/store.rb", src);
+ let names: Vec<&str> = fns.iter().map(|f| f.name.as_str()).collect();
+ assert_eq!(names, ["save", "save!"]);
+ assert_eq!(
+ symbols("module_function :a, :b?, :c!, :d=, :e"),
+ ["a", "b?", "c!", "d=", "e"]
+ );
+ assert_eq!(symbols("private :x=>1, Foo::Bar, key: :y"), ["x", "y"]);
+ }
+
+ #[test]
+ fn module_function_operator_symbols_stay_listed() {
+ // `def +(x)` exported with `module_function :+` is listed (and kept in Ruby by
+ // `method_fn`'s name check), not dropped because `:+` is not a word.
+ let src = "module Ops\n def +(x)\n x\n end\n module_function :+\n\n \
+ def [](i)\n i\n end\n module_function :[]\nend\n";
+ let fns = exports("lib/ops.rb", src);
+ let names: Vec<&str> = fns.iter().map(|f| f.name.as_str()).collect();
+ assert_eq!(names, ["+", "[]"]);
+ assert_eq!(
+ symbols("module_function :+, :[]=, :<=>, :-@, :==, :!, :a"),
+ ["+", "[]=", "<=>", "-@", "==", "!", "a"]
+ );
+ }
+
#[test]
fn extend_self_exports_the_public_methods() {
let src = "module Slug\n def size(text) = text.length\n\n extend self\n\n \