Skip to content

Commit

Permalink
Merge pull request #1503 from ChadKillingsworth/warn-improper-nosidee…
Browse files Browse the repository at this point in the history
…ffects

Add warning when @nosideeffects is used outside of externs.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=114242321
  • Loading branch information
blickly committed Feb 10, 2016
1 parent 78f58a0 commit 0fe75fa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/com/google/javascript/jscomp/CheckJSDoc.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public void visit(NodeTraversal t, Node n, Node parent) {
validateArrowFunction(n);
validateDefaultValue(n, info);
validateTempates(n, info);
validateNoSideEffects(n, info);
}

private void validateTempates(Node n, JSDocInfo info) {
Expand Down Expand Up @@ -365,4 +366,13 @@ private void validateDefaultValue(Node n, JSDocInfo info) {
}
}
}

/**
* Check that @nosideeeffects annotations are only present in externs.
*/
private void validateNoSideEffects(Node n, JSDocInfo info) {
if (info != null && info.isNoSideEffects() && !n.isFromExterns()) {
reportMisplaced(n, "nosideeffects", "@nosideeffects is only supported in externs.");
}
}
}
6 changes: 6 additions & 0 deletions test/com/google/javascript/jscomp/CheckJsDocTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,10 @@ public void testBadTemplate3() {
testBadTemplate("/** @template T */ var f = function() {};");
testBadTemplate("/** @template T */ Foo.prototype.f = function() {};");
}

public void testNoSideEffectsInSrc() {
testSame("/** @nosideeffects */ function foo() {}; foo();", MISPLACED_ANNOTATION);

testSame("/** @nosideeffects */ function foo() {};", "foo();", null);
}
}

0 comments on commit 0fe75fa

Please sign in to comment.