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.

Ruby: keep a method in Ruby when a default names a constant (#1396)

jakalijeyb077a0ba1ca5
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c8c7bd8b..1f30f85f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -267,6 +267,10 @@
- PowerShell: a parameter typed `[decimal]`, `[uint64]` or `[ulong]` keeps its function in
PowerShell, with the reasons C# gives for the same types, instead of bridging it as `f64` or
`i64`. `[uint32]`, `[double]` and `[single]` bridge as before (#1341 by @jakalijey).
+- Ruby: a method whose default names a constant (`sep = SEP`, `Core::SEP`, `DEFAULTS[:sep]`)
+ stays in Ruby with the reason, as PHP does: the wrapper and the shadow define the method outside
+ the original's module, where the constant raised `NameError`. Literal defaults are bridged as
+ before (#1380 by @jakalijey).
## 5.1.1 (2026-12-02)
diff --git a/src/lang/ruby.rs b/src/lang/ruby.rs
index 683449bd..9081a56f 100644
--- a/src/lang/ruby.rs
+++ b/src/lang/ruby.rs
@@ -834,6 +834,17 @@ fn doc_above(lines: &[&str], row: usize) -> Vec<String> {
out
}
+/// The first constant a default reads (`SEP`, `Core::SEP`, the `DEFAULTS` of `DEFAULTS[:sep]`),
+/// written as in the source.
+fn first_constant<'a>(node: Node, source: &'a str) -> Option<&'a str> {
+ if matches!(node.kind(), "constant" | "scope_resolution") {
+ return Some(text(node, source));
+ }
+ let mut cursor = node.walk();
+ let children: Vec<Node> = node.named_children(&mut cursor).collect();
+ children.into_iter().find_map(|c| first_constant(c, source))
+}
+
/// The dialect type of a literal default (`"-"`, `0`, `1.5`, `true`, `[]`).
fn literal_type(value: &str) -> Option<&'static str> {
let v = value.trim();
@@ -912,9 +923,15 @@ fn method_fn(
|| text(p, source).to_string(),
|n| text(n, source).to_string(),
);
- let value = p
- .child_by_field_name("value")
- .map(|v| text(v, source).to_string());
+ let value_node = p.child_by_field_name("value");
+ // The wrapper and the shadow define the method outside the original's module, so a
+ // constant the default reads is not found there.
+ if let Some(constant) = value_node.and_then(|v| first_constant(v, source)) {
+ blocker.get_or_insert_with(|| {
+ format!("has a default that names a constant (`{constant}`)")
+ });
+ }
+ let value = value_node.map(|v| text(v, source).to_string());
let (keyword_only, optional) = match p.kind() {
"identifier" => (false, false),
"optional_parameter" => (false, true),
@@ -1488,6 +1505,44 @@ group :test {
assert_eq!(signature(&fns[1]), "text, width = (4 * 2)");
}
+ #[test]
+ fn a_default_that_names_a_constant_stays_in_ruby() {
+ let src = "module Dd\n module Core\n SEP = \"-\"\n DEFAULTS = { sep: \"-\" }\n\
+ def self.slug(text, sep = SEP)\n text.split(\" \").join(sep)\n end\n\
+ def self.scoped(text, sep = Core::SEP)\n text.split(\" \").join(sep)\n end\n\
+ def self.table(text, sep = DEFAULTS[:sep])\n text.split(\" \").join(sep)\n end\n\
+ def self.keyword(text, sep: SEP)\n text.split(\" \").join(sep)\n end\n\
+ def self.plain(text, sep = \"-\")\n text.split(\" \").join(sep)\n end\n\
+ def self.many(text, n = 0, w = (4 * 2), xs = [], on = true, x = nil)\n text * n\n end\n\
+ end\nend\n";
+ let fns = exports("lib/dd/core.rb", src);
+ let blocker = |name: &str| {
+ fns.iter()
+ .find(|f| f.name == name)
+ .unwrap_or_else(|| panic!("{name} not in {fns:?}"))
+ .blocker
+ .clone()
+ };
+ assert_eq!(
+ blocker("slug").as_deref(),
+ Some("has a default that names a constant (`SEP`)")
+ );
+ assert_eq!(
+ blocker("scoped").as_deref(),
+ Some("has a default that names a constant (`Core::SEP`)")
+ );
+ assert_eq!(
+ blocker("table").as_deref(),
+ Some("has a default that names a constant (`DEFAULTS`)")
+ );
+ assert_eq!(
+ blocker("keyword").as_deref(),
+ Some("has a default that names a constant (`SEP`)")
+ );
+ assert_eq!(blocker("plain"), None);
+ assert_eq!(blocker("many"), None);
+ }
+
#[test]
fn gemfile_blocks_that_open_and_close_on_one_line_or_with_begin_keep_the_group() {
let one_line = "source \"https://rubygems.org\"\ngroup :test do\n if ENV[\"CI\"] then gem \"ci_reporter\" end\n gem \"rspec\"\nend\ngem \"rack\"\n";