Skip to content

Commit d75c2ac

Browse files
concavelenzcopybara-github
authored andcommitted
Automated Code Change
PiperOrigin-RevId: 624250151
1 parent 80407e1 commit d75c2ac

File tree

5 files changed

+79
-71
lines changed

5 files changed

+79
-71
lines changed

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

+48-50
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,29 @@ final class CheckJSDoc extends AbstractPostOrderCallback implements CompilerPass
4040
+ "\nMessage constants must be prefixed with 'MSG_'.");
4141

4242
public static final DiagnosticType MISPLACED_ANNOTATION =
43-
DiagnosticType.warning("JSC_MISPLACED_ANNOTATION",
44-
"Misplaced {0} annotation. {1}");
43+
DiagnosticType.warning("JSC_MISPLACED_ANNOTATION", "Misplaced {0} annotation. {1}");
4544

4645
public static final DiagnosticType ANNOTATION_DEPRECATED =
47-
DiagnosticType.warning("JSC_ANNOTATION_DEPRECATED",
48-
"The {0} annotation is deprecated. {1}");
46+
DiagnosticType.warning("JSC_ANNOTATION_DEPRECATED", "The {0} annotation is deprecated. {1}");
4947

5048
public static final DiagnosticType DISALLOWED_MEMBER_JSDOC =
51-
DiagnosticType.warning("JSC_DISALLOWED_MEMBER_JSDOC",
49+
DiagnosticType.warning(
50+
"JSC_DISALLOWED_MEMBER_JSDOC",
5251
"Class level JSDocs (@interface, @extends, etc.) are not allowed on class members");
5352

54-
static final DiagnosticType ARROW_FUNCTION_AS_CONSTRUCTOR = DiagnosticType.error(
55-
"JSC_ARROW_FUNCTION_AS_CONSTRUCTOR",
56-
"Arrow functions cannot be used as constructors");
53+
static final DiagnosticType ARROW_FUNCTION_AS_CONSTRUCTOR =
54+
DiagnosticType.error(
55+
"JSC_ARROW_FUNCTION_AS_CONSTRUCTOR", "Arrow functions cannot be used as constructors");
5756

5857
static final DiagnosticType BAD_REST_PARAMETER_ANNOTATION =
5958
DiagnosticType.warning(
6059
"JSC_BAD_REST_PARAMETER_ANNOTATION",
6160
"Missing \"...\" in type annotation for rest parameter.");
6261

63-
static final DiagnosticType DEFAULT_PARAM_MUST_BE_MARKED_OPTIONAL = DiagnosticType.error(
64-
"JSC_DEFAULT_PARAM_MUST_BE_MARKED_OPTIONAL",
65-
"Inline JSDoc on default parameters must be marked as optional");
62+
static final DiagnosticType DEFAULT_PARAM_MUST_BE_MARKED_OPTIONAL =
63+
DiagnosticType.error(
64+
"JSC_DEFAULT_PARAM_MUST_BE_MARKED_OPTIONAL",
65+
"Inline JSDoc on default parameters must be marked as optional");
6666

6767
public static final DiagnosticType INVALID_NO_SIDE_EFFECT_ANNOTATION =
6868
DiagnosticType.error(
@@ -256,9 +256,11 @@ private void validateTemplates(Node n, JSDocInfo info) {
256256
&& !isClassDecl(n)
257257
&& !info.containsFunctionDeclaration()
258258
&& getFunctionDecl(n) == null) {
259-
reportMisplaced(n, "template",
260-
"@template is only allowed in class, constructor, interface, function "
261-
+ "or method declarations");
259+
reportMisplaced(
260+
n,
261+
"template",
262+
"@template is only allowed in class, constructor, interface, function "
263+
+ "or method declarations");
262264
}
263265
}
264266

@@ -283,7 +285,8 @@ && getFunctionDecl(n) == null) {
283285
return n.getLastChild();
284286
}
285287

286-
if (n.isStringKey() && n.getGrandparent() != null
288+
if (n.isStringKey()
289+
&& n.getGrandparent() != null
287290
&& ClosureRewriteClass.isGoogDefineClass(n.getGrandparent())
288291
&& n.getFirstChild().isFunction()) {
289292
return n.getFirstChild();
@@ -312,8 +315,7 @@ private boolean isNameInitializeWithClass(Node n) {
312315
}
313316

314317
private boolean isClass(Node n) {
315-
return n.isClass()
316-
|| (n.isCall() && compiler.getCodingConvention().isClassFactoryCall(n));
318+
return n.isClass() || (n.isCall() && compiler.getCodingConvention().isClassFactoryCall(n));
317319
}
318320

319321
private static boolean isPrototypeOrInstanceDecl(Node n) {
@@ -331,8 +333,7 @@ private static boolean isPrototypeOrInstanceDecl(Node n) {
331333
* Checks that class-level annotations like @interface/@extends are not used on member functions.
332334
*/
333335
private void validateClassLevelJsDoc(Node n, JSDocInfo info) {
334-
if (info != null && n.isMemberFunctionDef()
335-
&& hasClassLevelJsDoc(info)) {
336+
if (info != null && n.isMemberFunctionDef() && hasClassLevelJsDoc(info)) {
336337
report(n, DISALLOWED_MEMBER_JSDOC);
337338
}
338339
}
@@ -359,7 +360,10 @@ private void validateAbstractJsDoc(Node n, JSDocInfo info) {
359360

360361
if (!info.isConstructor() && NodeUtil.getFunctionBody(functionNode).hasChildren()) {
361362
// @abstract annotation on a function with a non-empty body
362-
report(n, MISPLACED_ANNOTATION, "@abstract",
363+
report(
364+
n,
365+
MISPLACED_ANNOTATION,
366+
"@abstract",
363367
"function with a non-empty body cannot be abstract");
364368
return;
365369
}
@@ -406,8 +410,8 @@ private static boolean hasClassLevelJsDoc(JSDocInfo info) {
406410
}
407411

408412
/**
409-
* Warns when nocollapse annotations are present on nodes
410-
* which are not eligible for property collapsing.
413+
* Warns when nocollapse annotations are present on nodes which are not eligible for property
414+
* collapsing.
411415
*/
412416
private void validateNoCollapse(Node n, JSDocInfo info) {
413417
if (info == null || !info.isNoCollapse()) {
@@ -552,19 +556,17 @@ private static boolean isValidMsgName(Node nameNode) {
552556
}
553557
}
554558

555-
/**
556-
* Check that JSDoc with a {@code @type} annotation is in a valid place.
557-
*/
559+
/** Check that JSDoc with a {@code @type} annotation is in a valid place. */
558560
private void validateTypeAnnotations(Node n, JSDocInfo info) {
559561
if (info != null && info.hasType()) {
560562
boolean valid = false;
561563
switch (n.getToken()) {
562-
// Function declarations are valid
564+
// Function declarations are valid
563565
case FUNCTION:
564566
valid = NodeUtil.isFunctionDeclaration(n);
565567
break;
566-
// Object literal properties, catch declarations and variable
567-
// initializers are valid.
568+
// Object literal properties, catch declarations and variable
569+
// initializers are valid.
568570
case NAME:
569571
valid = isTypeAnnotationAllowedForName(n);
570572
break;
@@ -575,7 +577,7 @@ private void validateTypeAnnotations(Node n, JSDocInfo info) {
575577
// function f(/** !Array */ [x]) {}
576578
valid = n.getParent().isParamList();
577579
break;
578-
// Casts, exports, and Object literal properties are valid.
580+
// Casts, exports, and Object literal properties are valid.
579581
case CAST:
580582
case EXPORT:
581583
case STRING_KEY:
@@ -585,22 +587,23 @@ private void validateTypeAnnotations(Node n, JSDocInfo info) {
585587
case COMPUTED_FIELD_DEF:
586588
valid = true;
587589
break;
588-
// Declarations are valid iff they only contain simple names
589-
// /** @type {number} */ var x = 3; // ok
590-
// /** @type {number} */ var {x} = obj; // forbidden
590+
// Declarations are valid iff they only contain simple names
591+
// /** @type {number} */ var x = 3; // ok
592+
// /** @type {number} */ var {x} = obj; // forbidden
591593
case VAR:
592594
case LET:
593595
case CONST:
594596
valid = !NodeUtil.isDestructuringDeclaration(n);
595597
break;
596-
// Property assignments are valid, if at the root of an expression.
597-
case ASSIGN: {
598-
Node lvalue = n.getFirstChild();
598+
// Property assignments are valid, if at the root of an expression.
599+
case ASSIGN:
600+
{
601+
Node lvalue = n.getFirstChild();
599602
valid =
600603
n.getParent().isExprResult()
601604
&& (lvalue.isGetProp() || lvalue.isGetElem() || lvalue.matchesName("exports"));
602-
break;
603-
}
605+
break;
606+
}
604607
case GETPROP:
605608
valid = n.getParent().isExprResult() && n.isQualifiedName();
606609
break;
@@ -612,8 +615,8 @@ private void validateTypeAnnotations(Node n, JSDocInfo info) {
612615
}
613616

614617
if (!valid) {
615-
reportMisplaced(n, "type", "Type annotations are not allowed here. "
616-
+ "Are you missing parentheses?");
618+
reportMisplaced(
619+
n, "type", "Type annotations are not allowed here. " + "Are you missing parentheses?");
617620
}
618621
}
619622
}
@@ -632,17 +635,14 @@ private static boolean isTypeAnnotationAllowedForName(Node n) {
632635
}
633636

634637
private void reportMisplaced(Node n, String annotationName, String note) {
635-
compiler.report(JSError.make(n, MISPLACED_ANNOTATION,
636-
annotationName, note));
638+
compiler.report(JSError.make(n, MISPLACED_ANNOTATION, annotationName, note));
637639
}
638640

639641
private void report(Node n, DiagnosticType type, String... arguments) {
640642
compiler.report(JSError.make(n, type, arguments));
641643
}
642644

643-
/**
644-
* Check that an arrow function is not annotated with {@constructor}.
645-
*/
645+
/** Check that an arrow function is not annotated with {@constructor}. */
646646
private void validateArrowFunction(Node n) {
647647
if (n.isArrowFunction()) {
648648
JSDocInfo info = NodeUtil.getBestJSDocInfo(n);
@@ -686,8 +686,8 @@ private void validateRestParameter(Node restParam) {
686686
}
687687

688688
/**
689-
* Check that a parameter with a default value is marked as optional.
690-
* TODO(bradfordcsmith): This is redundant. We shouldn't require it.
689+
* Check that a parameter with a default value is marked as optional. TODO(bradfordcsmith): This
690+
* is redundant. We shouldn't require it.
691691
*/
692692
private void validateDefaultValue(Node n) {
693693
if (n.isDefaultValue() && n.getParent().isParamList()) {
@@ -725,9 +725,7 @@ private void validateNoSideEffects(Node n, JSDocInfo info) {
725725
}
726726
}
727727

728-
/**
729-
* Check that a let declaration is not used with {@defines}
730-
*/
728+
/** Check that a let declaration is not used with {@defines} */
731729
private void validateDefinesDeclaration(Node n, JSDocInfo info) {
732730
if (info != null && info.isDefine() && n.isLet()) {
733731
report(n, INVALID_DEFINE_ON_LET);
@@ -775,7 +773,7 @@ private void validateTsType(Node n, JSDocInfo info) {
775773
}
776774

777775
private static boolean isFromTs(Node n) {
778-
return n.getSourceFileName().endsWith(".closure.js");
776+
return n.getStaticSourceFile().isTypeScriptSource();
779777
}
780778

781779
private final CheckJsdocTypes checkJsDocTypesVisitor = new CheckJsdocTypes();

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,15 @@ private static class AllowList {
178178
* Returns true if the given path matches one of the prefixes or regexps, and false otherwise
179179
*/
180180
boolean matches(String path) {
181-
// If the path ends with .closure.js, it is probably a tsickle-generated file, and there may
182-
// be entries in the allow list for the TypeScript path
181+
// If the path ends with .closure.js or .tsx.cl.js, it is probably a tsickle-generated file,
182+
// and there may be entries in the allow list for the TypeScript path
183183
String tsPath =
184184
path.endsWith(".closure.js")
185185
? path.substring(0, path.length() - ".closure.js".length()) + ".ts"
186-
: null;
186+
// TSX
187+
: (path.endsWith(".tsx.cl.js")
188+
? path.substring(0, path.length() - ".cl.js".length())
189+
: null);
187190
if (prefixes != null) {
188191
for (String prefix : prefixes) {
189192
if (!path.isEmpty()
@@ -1951,7 +1954,6 @@ protected ConformanceResult checkConformance(NodeTraversal t, Node n) {
19511954
return ConformanceResult.CONFORMANCE;
19521955
}
19531956

1954-
19551957
@Override
19561958
public final Precondition getPrecondition() {
19571959
return Node::isScript;
@@ -1987,7 +1989,6 @@ protected ConformanceResult checkConformance(NodeTraversal t, Node n) {
19871989
return ConformanceResult.CONFORMANCE;
19881990
}
19891991

1990-
19911992
@Override
19921993
public final Precondition getPrecondition() {
19931994
return Node::isScript;

src/com/google/javascript/jscomp/ijs/ConvertToTypedInterface.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ public ConvertToTypedInterface(AbstractCompiler compiler) {
8989
private static void maybeReport(
9090
AbstractCompiler compiler, Node node, DiagnosticType diagnostic, String... fillers) {
9191
String sourceName = NodeUtil.getSourceName(node);
92-
if (sourceName.endsWith("_test.js") || sourceName.endsWith("_test.closure.js")) {
92+
if (sourceName.endsWith("_test.js")
93+
|| sourceName.endsWith("_test.closure.js")
94+
|| sourceName.endsWith("_test.tsx.cl.js")) {
9395
// Allow _test.js files and their tsickle generated
9496
// equivalents to avoid emitting errors at .i.js generation time.
9597
// We expect these files to not be consumed by any other downstream libraries.

src/com/google/javascript/jscomp/parsing/IRFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public static IRFactory transformTree(
327327
ErrorReporter errorReporter,
328328
SourceFile file) {
329329
JsDocInfoParser.JsDocSourceKind jsDocSourceKind =
330-
sourceFile.getName().endsWith(".closure.js")
330+
(sourceFile.isTypeScriptSource())
331331
? JsDocInfoParser.JsDocSourceKind.TSICKLE
332332
: JsDocInfoParser.JsDocSourceKind.NORMAL;
333333
IRFactory irFactory =

src/com/google/javascript/rhino/StaticSourceFile.java

+21-14
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ public enum SourceKind {
5757
NON_CODE,
5858
}
5959

60-
/**
61-
* The name of the file. Must be unique across all files in the compilation.
62-
*/
60+
/** The name of the file. Must be unique across all files in the compilation. */
6361
String getName();
6462

6563
/** The source kind. */
@@ -80,36 +78,45 @@ default boolean isExtern() {
8078
return getKind() == SourceKind.EXTERN;
8179
}
8280

81+
/** Whether the source kind is NON_CODE. */
82+
default boolean isNonCode() {
83+
return getKind() == SourceKind.NON_CODE;
84+
}
85+
86+
default boolean isTypeScriptSource() {
87+
String sourceName = getName();
88+
return sourceName.endsWith(".closure.js") || sourceName.endsWith(".tsx.cl.js");
89+
}
90+
8391
/**
84-
* Returns the offset of the given line number relative to the file start.
85-
* Line number should be 1-based.
92+
* Returns the offset of the given line number relative to the file start. Line number should be
93+
* 1-based.
8694
*
87-
* If the source file doesn't have line information, it should return
88-
* Integer.MIN_VALUE. The negative offsets will make it more obvious
89-
* what happened.
95+
* <p>If the source file doesn't have line information, it should return Integer.MIN_VALUE. The
96+
* negative offsets will make it more obvious what happened.
9097
*
9198
* @param lineNumber the line of the input to get the absolute offset of.
9299
* @return the absolute offset of the start of the provided line.
93-
* @throws IllegalArgumentException if lineno is less than 1 or greater than
94-
* the number of lines in the source.
100+
* @throws IllegalArgumentException if lineno is less than 1 or greater than the number of lines
101+
* in the source.
95102
*/
96103
int getLineOffset(int lineNumber);
97104

98105
/**
99106
* Gets the 1-based line number of the given source offset.
100107
*
101108
* @param offset An absolute file offset.
102-
* @return The 1-based line number of that offset. The behavior is
103-
* undefined if this offset does not exist in the source file.
109+
* @return The 1-based line number of that offset. The behavior is undefined if this offset does
110+
* not exist in the source file.
104111
*/
105112
int getLineOfOffset(int offset);
106113

107114
/**
108115
* Gets the 0-based column number of the given source offset.
109116
*
110117
* @param offset An absolute file offset.
111-
* @return The 0-based column number of that offset. The behavior is
112-
* undefined if this offset does not exist in the source file.
118+
* @return The 0-based column number of that offset. The behavior is undefined if this offset does
119+
* not exist in the source file.
113120
*/
114121
int getColumnOfOffset(int offset);
115122
}

0 commit comments

Comments
 (0)