Skip to content

Commit 496a651

Browse files
Closure Teamcopybara-github
authored andcommitted
Internal change
PiperOrigin-RevId: 515370620
1 parent 0b85eb3 commit 496a651

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ private static void removeDuplicates(Requirement.Builder requirement) {
287287
return new ConformanceRules.BannedDependencyRegex(compiler, requirement);
288288
case BANNED_ENHANCE:
289289
return new ConformanceRules.BannedEnhance(compiler, requirement);
290+
case BANNED_MODS_REGEX:
291+
return new ConformanceRules.BannedModsRegex(compiler, requirement);
290292
case BANNED_NAME:
291293
case BANNED_NAME_CALL:
292294
return new ConformanceRules.BannedName(compiler, requirement);

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,6 +1927,49 @@ public final Precondition getPrecondition() {
19271927
}
19281928
}
19291929

1930+
/** Checks that file does not include an @mods annotation for a banned namespace regex. */
1931+
public static final class BannedModsRegex extends AbstractRule {
1932+
private final Pattern bannedModsRegex;
1933+
1934+
public BannedModsRegex(AbstractCompiler compiler, Requirement requirement)
1935+
throws InvalidRequirementSpec {
1936+
super(compiler, requirement);
1937+
1938+
List<String> bannedModsRegexList = requirement.getValueList();
1939+
if (requirement.getValueCount() == 0) {
1940+
throw new InvalidRequirementSpec("missing value");
1941+
}
1942+
bannedModsRegex = buildPattern(bannedModsRegexList);
1943+
}
1944+
1945+
@Override
1946+
protected ConformanceResult checkConformance(NodeTraversal t, Node n) {
1947+
JSDocInfo docInfo = n.getJSDocInfo();
1948+
1949+
if (docInfo == null || !docInfo.hasMods()) {
1950+
return ConformanceResult.CONFORMANCE;
1951+
}
1952+
1953+
if (bannedModsRegex.matcher(docInfo.getMods()).find()) {
1954+
return ConformanceResult.VIOLATION;
1955+
}
1956+
return ConformanceResult.CONFORMANCE;
1957+
}
1958+
1959+
private static final Precondition IS_SCRIPT_NODE =
1960+
new Precondition() {
1961+
@Override
1962+
public boolean shouldCheck(Node n) {
1963+
return n.isScript();
1964+
}
1965+
};
1966+
1967+
@Override
1968+
public final Precondition getPrecondition() {
1969+
return IS_SCRIPT_NODE;
1970+
}
1971+
}
1972+
19301973
private static final QualifiedName GOOG_DOM = QualifiedName.of("goog.dom");
19311974
private static final QualifiedName GOOG_DOM_TAGNAME = QualifiedName.of("goog.dom.TagName");
19321975

src/com/google/javascript/jscomp/conformance/conformance.proto

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ message Requirement {
178178
// based on the passed in regular expression value.
179179
// example: value: ".*oo.*" in "foobar" would match.
180180
BANNED_STRING_REGEX = 17;
181+
182+
// A banned @mods annotation using regular expressions.
183+
BANNED_MODS_REGEX = 18;
181184
}
182185

183186
enum TypeMatchingStrategy {

test/com/google/javascript/jscomp/CheckConformanceTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,27 @@ public void testBannedEnhance() {
660660
testNoWarning(allow2);
661661
}
662662

663+
@Test
664+
public void testBannedModsRegex() {
665+
configuration =
666+
"requirement: {\n"
667+
+ " type: BANNED_MODS_REGEX\n"
668+
+ " value: '.+_bar$'\n"
669+
+ " error_message: 'Modding namespaces ending in _bar is NOT allowed.'\n"
670+
+ "}";
671+
String violationMessage = "Violation: Modding namespaces ending in _bar is NOT allowed.";
672+
673+
String ban = lines("/**", " * @mods {ns.foo_bar}", " * @modName {ns.foo_bar_baz}", " */");
674+
testWarning(ban, CheckConformance.CONFORMANCE_VIOLATION, violationMessage);
675+
676+
String allow1 = lines("/**", " * @mods {ns.foo}", " * @modName {ns.foo_bar}", " */");
677+
testNoWarning(allow1);
678+
679+
String allow2 =
680+
lines("/**", " * @fileoverview no enhance annotation should always pass.", " */");
681+
testNoWarning(allow2);
682+
}
683+
663684
@Test
664685
public void testBannedNameCall() {
665686
configuration =

0 commit comments

Comments
 (0)