Skip to content

Commit f4edc4c

Browse files
Closure Teamcopybara-github
authored andcommitted
Internal change
PiperOrigin-RevId: 512657926
1 parent 455a3c6 commit f4edc4c

File tree

5 files changed

+128
-8
lines changed

5 files changed

+128
-8
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ enum Annotation {
5555
MIXIN_CLASS,
5656
MIXIN_FUNCTION,
5757
MODIFIES,
58+
MODS,
5859
NG_INJECT,
5960
NO_COLLAPSE,
6061
NO_COMPILE,
@@ -139,6 +140,7 @@ enum Annotation {
139140
.put("mixinClass", Annotation.MIXIN_CLASS)
140141
.put("mixinFunction", Annotation.MIXIN_FUNCTION)
141142
.put("modifies", Annotation.MODIFIES)
143+
.put("mods", Annotation.MODS)
142144
.put("nocollapse", Annotation.NO_COLLAPSE)
143145
.put("nocompile", Annotation.NO_COMPILE)
144146
.put("nodts", Annotation.NO_DTS)

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,17 @@ private JsDocToken parseAnnotation(JsDocToken token, List<ExtendedTypeInfo> exte
549549
jsdocBuilder.recordEnhance(enhance);
550550
return token;
551551

552+
case MODS:
553+
if (jsdocBuilder.isModsRecorded()) {
554+
addParserWarning(Msg.JSDOC_MODS_EXTRA);
555+
} else {
556+
token = parseModsTag(next());
557+
}
558+
if (token != JsDocToken.EOC && token != JsDocToken.EOF) {
559+
token = eatUntilEOLIfNotAnnotation();
560+
}
561+
return token;
562+
552563
case ENUM:
553564
token = next();
554565
lineno = stream.getLineno();
@@ -1339,6 +1350,31 @@ private JsDocToken parseSuppressTag(JsDocToken token) {
13391350
}
13401351
}
13411352

1353+
/**
1354+
* Parse a {@code @mods} tag of the form {@code @mods &#123;google3.path.to.file&#125;}.
1355+
*
1356+
* @param token The current token.
1357+
*/
1358+
private JsDocToken parseModsTag(JsDocToken token) {
1359+
if (token != JsDocToken.LEFT_CURLY) {
1360+
addParserWarning(Msg.JSDOC_MODS);
1361+
return token;
1362+
}
1363+
if (!match(JsDocToken.STRING)) {
1364+
addParserWarning(Msg.JSDOC_MODS);
1365+
return token;
1366+
}
1367+
String namespace = stream.getString();
1368+
token = next();
1369+
if (!match(JsDocToken.RIGHT_CURLY)) {
1370+
addParserWarning(Msg.JSDOC_MODS);
1371+
} else {
1372+
token = next();
1373+
jsdocBuilder.recordMods(namespace);
1374+
}
1375+
return token;
1376+
}
1377+
13421378
/**
13431379
* Parse a {@code @closurePrimitive} tag
13441380
*

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

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import com.google.common.collect.ImmutableMap;
5151
import com.google.common.collect.ImmutableSet;
5252
import com.google.common.collect.Iterables;
53+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
5354
import java.io.Serializable;
5455
import java.util.ArrayList;
5556
import java.util.Collection;
@@ -486,6 +487,7 @@ private enum IdGenerator {
486487
new Property<>("fileoverviewDescription");
487488
private static final Property<String> RETURN_DESCRIPTION = new Property<>("returnDescription");
488489
private static final Property<String> ENHANCED_NAMESPACE = new Property<>("enhance");
490+
private static final Property<String> MODS = new Property<>("mods");
489491
private static final Property<List<String>> TS_TYPES = new Property<>("tsType");
490492

491493
private static final Property<List<String>> AUTHORS = new Property<>("authors");
@@ -1373,6 +1375,16 @@ public String getEnhance() {
13731375
return ENHANCED_NAMESPACE.get(this);
13741376
}
13751377

1378+
/** Returns whether this has a modded namespace. */
1379+
public boolean hasMods() {
1380+
return MODS.get(this) != null;
1381+
}
1382+
1383+
/** Returns the modded namespace or null if none is specified. */
1384+
public String getMods() {
1385+
return MODS.get(this);
1386+
}
1387+
13761388
/** Gets the list of all markers for the documentation in this JSDoc. */
13771389
public Collection<Marker> getMarkers() {
13781390
ArrayList<Marker> markers = MARKERS.get(this);
@@ -1624,13 +1636,14 @@ public void recordOriginalCommentPosition(int position) {
16241636
*/
16251637
public boolean isPopulatedWithFileOverview() {
16261638
return populated
1627-
&& (bits
1628-
& (Bit.FILEOVERVIEW.mask
1629-
| Bit.EXTERNS.mask
1630-
| Bit.NOCOMPILE.mask
1631-
| Bit.TYPE_SUMMARY.mask
1632-
| Bit.ENHANCED_NAMESPACE.mask))
1633-
!= 0;
1639+
&& ((bits
1640+
& (Bit.FILEOVERVIEW.mask
1641+
| Bit.EXTERNS.mask
1642+
| Bit.NOCOMPILE.mask
1643+
| Bit.TYPE_SUMMARY.mask
1644+
| Bit.ENHANCED_NAMESPACE.mask))
1645+
!= 0
1646+
|| isModsRecorded());
16341647
}
16351648

16361649
/** Returns whether this builder recorded a description. */
@@ -2207,6 +2220,21 @@ public boolean recordEnhance(String namespace) {
22072220
return populateProp(ENHANCED_NAMESPACE, namespace);
22082221
}
22092222

2223+
/** Returns whether this builder recorded a modded namespace. */
2224+
public boolean isModsRecorded() {
2225+
return props.get(MODS) != null;
2226+
}
2227+
2228+
/**
2229+
* Records modded namespace.
2230+
*
2231+
* @return {@code true} If the modded namespace was recorded.
2232+
*/
2233+
@CanIgnoreReturnValue
2234+
public boolean recordMods(String namespace) {
2235+
return populateProp(MODS, RhinoStringPool.addOrGet(namespace));
2236+
}
2237+
22102238
public boolean recordLicense(String license) {
22112239
setProp(LICENSE, RhinoStringPool.addOrGet(license));
22122240
populated = true;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ public enum Msg {
102102
JSDOC_MODIFIES("malformed @modifies tag"),
103103
JSDOC_MODIFIES_DUPLICATE("conflicting @modifies tag"),
104104
JSDOC_MODIFIES_UNKNOWN("unknown @modifies parameter: {0}"),
105+
JSDOC_MODS("malformed @mods tag"),
106+
JSDOC_MODS_EXTRA("extra @mods tag"),
105107
JSDOC_NAME_SYNTAX("name not recognized due to syntax error."),
106108
JSDOC_NGINJECT_EXTRA("extra @ngInject tag"),
107109
JSDOC_NOCOLLAPSE("extra @nocollapse tag"),

test/com/google/javascript/jscomp/parsing/JsDocInfoParserTest.java

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2996,6 +2996,59 @@ public void testBadSuppress8() {
29962996
parse("@suppress */", "malformed @suppress tag");
29972997
}
29982998

2999+
@Test
3000+
public void testMods1() {
3001+
JSDocInfo info = parseFileOverview("@mods {ns.Foo} */");
3002+
assertThat(info.getMods()).isEqualTo("ns.Foo");
3003+
}
3004+
3005+
@Test
3006+
public void testMods2() {
3007+
JSDocInfo info = parseFileOverview("@mods {google3.path.to.file} */");
3008+
assertThat(info.getMods()).isEqualTo("google3.path.to.file");
3009+
}
3010+
3011+
@Test
3012+
public void testJsDocAfterMods() {
3013+
JSDocInfo info = parseFileOverview("@mods {ns.Foo} @modName {new_feature_flag} */");
3014+
assertThat(info.getMods()).isEqualTo("ns.Foo");
3015+
}
3016+
3017+
@Test
3018+
public void testMultipleModsTags() {
3019+
parseFileOverview("@mods {ns.Foo} \n * @mods {ns.Bar} */", "extra @mods tag");
3020+
}
3021+
3022+
@Test
3023+
public void testBadMods1() {
3024+
parseFileOverview("@mods {} */", "malformed @mods tag");
3025+
}
3026+
3027+
@Test
3028+
public void testBadMods2() {
3029+
parseFileOverview("@mods { */", "malformed @mods tag");
3030+
}
3031+
3032+
@Test
3033+
public void testBadMods3() {
3034+
parseFileOverview("@mods } */", "malformed @mods tag");
3035+
}
3036+
3037+
@Test
3038+
public void testBadMods4() {
3039+
parseFileOverview("@mods ns.Foo */", "malformed @mods tag");
3040+
}
3041+
3042+
@Test
3043+
public void testBadMods5() {
3044+
parseFileOverview("@mods {ns.Foo */", "malformed @mods tag");
3045+
}
3046+
3047+
@Test
3048+
public void testBadMods6() {
3049+
parseFileOverview("@mods ns.Foo} */", "malformed @mods tag");
3050+
}
3051+
29993052
@Test
30003053
public void testModifies1() {
30013054
JSDocInfo info = parse("@modifies {this} */");
@@ -5161,7 +5214,6 @@ public void testAllowlistedAnnotations() {
51615214
+ "* @member \n"
51625215
+ "* @memberOf \n"
51635216
+ "* @modName \n"
5164-
+ "* @mods \n"
51655217
+ "* @name \n"
51665218
+ "* @namespace \n"
51675219
+ "* @ngInject \n"

0 commit comments

Comments
 (0)