Skip to content

Commit 3f64a52

Browse files
authored
Merge branch 'main' into tim/issue-7548
2 parents d26e556 + a731874 commit 3f64a52

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

rewrite-java/src/main/java/org/openrewrite/java/InlineMethodCalls.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
@Value
3636
public class InlineMethodCalls extends Recipe {
3737
private static final Pattern TEMPLATE_IDENTIFIER = Pattern.compile("#\\{(\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*):any\\(.*?\\)}");
38+
private static final Pattern BARE_METHOD_CALL = Pattern.compile("^\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*\\(");
3839

3940
@Option(displayName = "Method pattern",
4041
description = "A method pattern that is used to find matching method invocations.",
@@ -199,6 +200,11 @@ private String createTemplateString(MethodCall original, JavaType.Method methodT
199200
((J.MethodInvocation) original).getSelect() == null &&
200201
replacement.startsWith("this.")) {
201202
templateString = replacement.substring(5);
203+
} else if (original instanceof J.MethodInvocation &&
204+
((J.MethodInvocation) original).getSelect() != null &&
205+
BARE_METHOD_CALL.matcher(replacement).find()) {
206+
// Original had a select but replacement is a bare method call; preserve the select
207+
templateString = "#{this:any()}." + replacement;
202208
} else {
203209
templateString = replacement.replaceAll("\\bthis\\b", "#{this:any()}");
204210
}

rewrite-java/src/test/java/org/openrewrite/java/InlineMethodCallsTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,53 @@ void foo(Builder builder) {
508508
);
509509
}
510510

511+
@Test
512+
void preserveSelectWhenReplacementIsBareMethodCall() {
513+
//language=java
514+
rewriteRun(
515+
spec -> spec.recipe(new InlineMethodCalls(
516+
"Lib getTotalHits()",
517+
"getTotalHitsCount()",
518+
null,
519+
null,
520+
null)),
521+
java(
522+
"""
523+
class Lib {
524+
@Deprecated
525+
public int getTotalHits() {
526+
return getTotalHitsCount();
527+
}
528+
529+
public int getTotalHitsCount() {
530+
return 0;
531+
}
532+
533+
static int usage(Lib lib) {
534+
return lib.getTotalHits();
535+
}
536+
}
537+
""",
538+
"""
539+
class Lib {
540+
@Deprecated
541+
public int getTotalHits() {
542+
return getTotalHitsCount();
543+
}
544+
545+
public int getTotalHitsCount() {
546+
return 0;
547+
}
548+
549+
static int usage(Lib lib) {
550+
return lib.getTotalHitsCount();
551+
}
552+
}
553+
"""
554+
)
555+
);
556+
}
557+
511558
@Test
512559
void sameArgumentUsedTwice() {
513560
//language=java

rewrite-javascript/build.gradle.kts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,24 @@ val npmPublish = tasks.register<NpmTask>("npmPublish") {
253253
}
254254

255255
workingDir.set(file("rewrite"))
256+
257+
// npm has no `--skip-duplicate`; the daily scheduled publish would fail with a 403
258+
// whenever no new commit had landed since the previous run (the snapshot version is
259+
// derived from the latest commit timestamp). Skip the task if the version already exists.
260+
onlyIf {
261+
val versionToCheck = extractVersionFromJar() ?: datedSnapshotVersion
262+
val process = ProcessBuilder("npm", "view", "@openrewrite/rewrite@$versionToCheck", "version")
263+
.directory(file("rewrite"))
264+
.redirectErrorStream(true)
265+
.start()
266+
process.waitFor()
267+
val output = process.inputStream.bufferedReader().readText().trim()
268+
val alreadyPublished = output.contains(versionToCheck)
269+
if (alreadyPublished) {
270+
logger.lifecycle("Skipping npmPublish: @openrewrite/rewrite@$versionToCheck already published")
271+
}
272+
!alreadyPublished
273+
}
256274
}
257275

258276
tasks.named("publish") {

0 commit comments

Comments
 (0)