Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,11 @@ public J visitCallNode(Nodes.CallNode node) {
skip(".");
}

// `Foo.(a)` elides the `call` that Prism still reports as the name
if (name.equals("call") && !peekKeywordAt("call", indexOfNextNonWhitespace(cursor))) {
markers = markers.add(new ImplicitCall(randomId()));
}

J.Identifier methodName = identifier(name);
if (name.equals("new")) {
return new J.NewClass(
Expand Down Expand Up @@ -2048,19 +2053,23 @@ public J visitAssocSplatNode(Nodes.AssocSplatNode node) {
private Rb.Hash hash(Space prefix, Nodes.Node[] elements, Nodes.@Nullable Node rest) {
AtomicReference<Markers> markers = new AtomicReference<>(Markers.EMPTY);
Space before = whitespace();
boolean braces = source.startsWith("{", cursor);

List<Nodes.Node> all = new ArrayList<>(Arrays.asList(elements));
if (rest != null) {
all.add(rest);
}

// a brace-less hash whose first key is itself a hash (`eq({} => 0)`) also starts with `{`,
// so the brace only belongs to this hash when it sits ahead of the first pair
boolean braces = source.startsWith("{", cursor) &&
(all.isEmpty() || cursor < charStart(all.get(0)));
Markers hashMarkers = Markers.EMPTY;
if (braces) {
skip("{");
} else {
hashMarkers = hashMarkers.add(new OmitParentheses(randomId()));
}

List<Nodes.Node> all = new ArrayList<>(Arrays.asList(elements));
if (rest != null) {
all.add(rest);
}

List<JRightPadded<Expression>> pairs = new ArrayList<>(all.size());
for (int i = 0; i < all.size(); i++) {
Expression pair = all.get(i) instanceof Nodes.AssocNode ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,11 @@ public J visitMethodInvocation(J.MethodInvocation method, PrintOutputCapture<P>
(method.getMarkers().findFirst(Colon2.class).isPresent() ? "::" : ".");
visitRightPadded(method.getPadding().getSelect(),
JRightPadded.Location.METHOD_SELECT, suffix, p);
visit(method.getName(), p);
// `Foo.()` writes no message, but a recipe that renamed it has to write the new one
if (!method.getMarkers().findFirst(ImplicitCall.class).isPresent() ||
!"call".equals(method.getSimpleName())) {
visit(method.getName(), p);
}

JContainer<Expression> args = method.getPadding().getArguments();
AtomicReference<Rb.Block> blockArg = new AtomicReference<>();
Expand Down Expand Up @@ -1308,7 +1312,8 @@ public J visitNewClass(J.NewClass newClass, PrintOutputCapture<P> p) {
beforeSyntax(newClass, Space.Location.NEW_CLASS_PREFIX, p);
visit(newClass.getClazz(), p);
visitSpace(requireNonNull(newClass.getPadding().getEnclosing()).getAfter(), Space.Location.NEW_CLASS_ENCLOSING_SUFFIX, p);
p.append(newClass.getMarkers().findFirst(SafeNavigation.class).isPresent() ? "&." : ".");
p.append((newClass.getMarkers().findFirst(SafeNavigation.class).isPresent() ? "&" : "") +
(newClass.getMarkers().findFirst(Colon2.class).isPresent() ? "::" : "."));
visitSpace(newClass.getNew(), Space.Location.NEW_PREFIX, p);
p.append("new");
JContainer<Expression> args = newClass.getPadding().getArguments();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2026 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.ruby.marker;

import lombok.Value;
import lombok.With;
import org.openrewrite.marker.Marker;

import java.util.UUID;

/**
* {@code Foo.(a)} is shorthand for {@code Foo.call(a)}, written with the message elided.
*/
@Value
@With
public class ImplicitCall implements Marker {
UUID id;
}
13 changes: 13 additions & 0 deletions rewrite-ruby/src/test/java/org/openrewrite/ruby/tree/HashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,17 @@ void bracketedConstructorWithoutTrailingComma() {
)
);
}

@Test
void hashKey() {
rewriteRun(
ruby(
"""
expect(metrics[0].data).to eq({} => 0)
expect(metrics[0].data).to eq({a: 1} => 0)
x = {{} => 0}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,57 @@ void blockLastArgument() {
);
}

@Test
void callSugar() {
rewriteRun(
ruby(
"""
Sweep.()
MarkForToken.(t)
MarkForToken.(t, 1)
obj&.()
"""
)
);
}

@Test
void callSugarWithBlock() {
rewriteRun(
ruby(
"""
Sweep.() { |a| a }
"""
)
);
}

@Test
void explicitCall() {
rewriteRun(
ruby(
"""
Sweep.call()
Sweep.call
MarkForToken.call(t)
"""
)
);
}

@Test
void colon2Call() {
rewriteRun(
ruby(
"""
Nokogiri::XML(response.body)
WEBrick::Log::new(log_path)
Integer::sqrt(9)
"""
)
);
}

@Test
void noParens() {
rewriteRun(
Expand Down
Loading