Skip to content

Commit 16dc1da

Browse files
authored
Add JavascriptFileChecker to selectively skip JavaScript files (#787)
* Add JavascriptFileChecker to selectively skip JavaScript files * Add test for skipping typescript
1 parent c1712c2 commit 16dc1da

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ dependencies {
2121

2222
// Limit transitive dependencies for downstream projects like rewrite-spring
2323
provided("org.openrewrite:rewrite-groovy:${rewriteVersion}")
24+
provided("org.openrewrite:rewrite-javascript:${rewriteVersion}")
2425
provided("org.openrewrite:rewrite-kotlin:${rewriteVersion}")
2526
provided("org.openrewrite:rewrite-csharp:${rewriteVersion}")
2627

src/main/java/org/openrewrite/staticanalysis/RemoveUnusedLocalVariables.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.openrewrite.java.MethodMatcher;
2828
import org.openrewrite.java.service.AnnotationService;
2929
import org.openrewrite.java.tree.*;
30+
import org.openrewrite.staticanalysis.javascript.JavascriptFileChecker;
3031
import org.openrewrite.staticanalysis.kotlin.KotlinFileChecker;
3132

3233
import java.util.Arrays;
@@ -36,6 +37,8 @@
3637
import java.util.concurrent.atomic.AtomicBoolean;
3738

3839
import static java.util.Collections.singleton;
40+
import static org.openrewrite.Preconditions.and;
41+
import static org.openrewrite.Preconditions.not;
3942

4043
@EqualsAndHashCode(callSuper = false)
4144
@SuppressWarnings("ConstantConditions")
@@ -107,7 +110,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
107110
ignoreVariableNames.addAll(Arrays.asList(ignoreVariablesNamed));
108111
}
109112

110-
return Preconditions.check(Preconditions.not(new KotlinFileChecker<>()), new JavaIsoVisitor<ExecutionContext>() {
113+
TreeVisitor<?, ExecutionContext> notJsNorKt = and(not(new JavascriptFileChecker<>()), not(new KotlinFileChecker<>()));
114+
return Preconditions.check(notJsNorKt, new JavaIsoVisitor<ExecutionContext>() {
111115
private Cursor getCursorToParentScope(Cursor cursor) {
112116
return cursor.dropParentUntil(is ->
113117
is instanceof J.ClassDeclaration ||
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Moderne Source Available License (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://docs.moderne.io/licensing/moderne-source-available-license
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.staticanalysis.javascript;
17+
18+
import org.jspecify.annotations.Nullable;
19+
import org.openrewrite.Tree;
20+
import org.openrewrite.TreeVisitor;
21+
import org.openrewrite.internal.ReflectionUtils;
22+
import org.openrewrite.javascript.tree.JS;
23+
import org.openrewrite.marker.SearchResult;
24+
25+
/**
26+
* Add a search marker if visiting a JS file
27+
*/
28+
public class JavascriptFileChecker<P> extends TreeVisitor<Tree, P> {
29+
private static final boolean IS_AVAILABLE = ReflectionUtils.isClassAvailable("org.openrewrite.javascript.tree.JS");
30+
31+
@Override
32+
public @Nullable Tree visit(@Nullable Tree tree, P p) {
33+
if (IS_AVAILABLE && tree instanceof JS.CompilationUnit) {
34+
return SearchResult.found(tree);
35+
}
36+
return tree;
37+
}
38+
}

src/test/java/org/openrewrite/staticanalysis/RemoveUnusedLocalVariablesTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import static org.openrewrite.java.Assertions.java;
2626
import static org.openrewrite.java.Assertions.version;
27+
import static org.openrewrite.javascript.Assertions.typescript;
2728
import static org.openrewrite.kotlin.Assertions.kotlin;
2829

2930
@SuppressWarnings({
@@ -1306,4 +1307,20 @@ fun initializerRemoved() : String {
13061307
);
13071308
}
13081309
}
1310+
1311+
@Nested
1312+
class Typescript {
1313+
@Test
1314+
void noChange() {
1315+
rewriteRun(
1316+
typescript(
1317+
"""
1318+
class Foo {
1319+
bar: string;
1320+
}
1321+
"""
1322+
)
1323+
);
1324+
}
1325+
}
13091326
}

0 commit comments

Comments
 (0)