Skip to content

Commit b7f4b9d

Browse files
kevinoconnor7copybara-github
authored andcommitted
Extract a shared J2clSourceUtils class
This just centralizes the checks for whether we're in a J2CL source file as this check was duplicated in multiple places. PiperOrigin-RevId: 893214710
1 parent 4d12b81 commit b7f4b9d

5 files changed

Lines changed: 47 additions & 5 deletions

File tree

src/com/google/javascript/jscomp/CheckSideEffects.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ public void visit(NodeTraversal t, Node n, Node parent) {
232232

233233
if (target.getString().equals(J2CL_PROTECTOR_FN)) {
234234
// Do not let non-J2CL code abuse it.
235-
checkState(n.getSourceFileName().endsWith(".java.js"), "Only allowed for J2CL code");
235+
checkState(
236+
J2clSourceUtils.isJ2clSource(n.getSourceFileName()), "Only allowed for J2CL code");
236237
NodeUtil.deleteFunctionCall(n, compiler);
237238
return;
238239
}

src/com/google/javascript/jscomp/J2clSourceFileChecker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class J2clSourceFileChecker implements CompilerPass {
3030
private static boolean hasJ2cl(Node root) {
3131
for (Node script = root.getFirstChild(); script != null; script = script.getNext()) {
3232
checkState(script.isScript());
33-
if (script.getSourceFileName() != null && script.getSourceFileName().endsWith(".java.js")) {
33+
if (J2clSourceUtils.isJ2clSource(script)) {
3434
return true;
3535
}
3636
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2026 The Closure Compiler Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (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+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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 com.google.javascript.jscomp;
17+
18+
import static com.google.common.base.Preconditions.checkArgument;
19+
20+
import com.google.javascript.rhino.Node;
21+
22+
final class J2clSourceUtils {
23+
24+
public static boolean isJ2clSource(String sourceFileName) {
25+
return sourceFileName != null && sourceFileName.endsWith(".java.js");
26+
}
27+
28+
public static boolean isJ2clSource(Node scriptNode) {
29+
checkArgument(scriptNode.isScript());
30+
return isJ2clSource(scriptNode.getSourceFileName());
31+
}
32+
33+
public static boolean isInsideJ2clSource(Node node) {
34+
Node scriptNode = node.isScript() ? node : NodeUtil.getEnclosingScript(node);
35+
if (scriptNode == null) {
36+
return false;
37+
}
38+
return isJ2clSource(scriptNode);
39+
}
40+
41+
private J2clSourceUtils() {}
42+
}

src/com/google/javascript/jscomp/J2clSuppressWarningsGuard.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public final class J2clSuppressWarningsGuard extends WarningsGuard {
5454

5555
@Override
5656
public @Nullable CheckLevel level(JSError error) {
57-
if (error.sourceName() == null || !error.sourceName().endsWith(".java.js")) {
57+
if (!J2clSourceUtils.isJ2clSource(error.sourceName())) {
5858
return null;
5959
}
6060

src/com/google/javascript/jscomp/TypeCheck.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,7 @@ public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
557557
// Errors in type summary files are suppressed, so no use traversing them.
558558
return false;
559559
}
560-
String filename = n.getSourceFileName();
561-
if (filename != null && filename.endsWith(".java.js")) {
560+
if (J2clSourceUtils.isJ2clSource(n)) {
562561
this.subtypingMode = SubtypingMode.IGNORE_NULL_UNDEFINED;
563562
} else {
564563
this.subtypingMode = SubtypingMode.NORMAL;

0 commit comments

Comments
 (0)