Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1409)

Unified Diff: Source/devtools/scripts/jsdoc-validator/src/org/chromium/devtools/jsdoc/checks/RequiredThisAnnotationCheck.java

Issue 137553005: DevTools: [JsDocValidator] Refactor JsDoc annotation checkers (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Limit the thread count by the number of validated files Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/devtools/scripts/jsdoc-validator/src/org/chromium/devtools/jsdoc/checks/RequiredThisAnnotationCheck.java
diff --git a/Source/devtools/scripts/jsdoc-validator/src/org/chromium/devtools/jsdoc/checks/RequiredThisAnnotationCheck.java b/Source/devtools/scripts/jsdoc-validator/src/org/chromium/devtools/jsdoc/checks/RequiredThisAnnotationCheck.java
deleted file mode 100644
index edacb34779d37555c8da829a7f87f8a25137044b..0000000000000000000000000000000000000000
--- a/Source/devtools/scripts/jsdoc-validator/src/org/chromium/devtools/jsdoc/checks/RequiredThisAnnotationCheck.java
+++ /dev/null
@@ -1,76 +0,0 @@
-package org.chromium.devtools.jsdoc.checks;
-
-import com.google.javascript.rhino.head.Token;
-import com.google.javascript.rhino.head.ast.Assignment;
-import com.google.javascript.rhino.head.ast.AstNode;
-import com.google.javascript.rhino.head.ast.FunctionNode;
-
-import java.util.ArrayDeque;
-import java.util.Deque;
-import java.util.HashSet;
-import java.util.Set;
-
-public final class RequiredThisAnnotationCheck extends ValidationCheck {
-
- private final Deque<AstNode> functionStack = new ArrayDeque<>(16);
- private final Set<AstNode> thisReferencingFunctions = new HashSet<>();
-
- @Override
- public void doVisit(AstNode node) {
- if (node.getType() == Token.THIS) {
- if (!functionStack.isEmpty()) {
- // Non-global scope.
- thisReferencingFunctions.add(functionStack.peekFirst());
- }
- return;
- }
-
- if (node.getType() != Token.FUNCTION) {
- return;
- }
-
- functionStack.push(node);
- }
-
- @Override
- public void didVisit(AstNode node) {
- if (node.getType() != Token.FUNCTION) {
- return;
- }
-
- FunctionNode functionNode = (FunctionNode) functionStack.remove();
- if (thisReferencingFunctions.contains(functionNode) && !functionStack.isEmpty()) {
- // If the stack is not empty, then it's a nested function.
- String jsDoc = getJsDoc(functionNode);
- AstNode functionNameNode = AstUtil.getFunctionNameNode(functionNode);
- if (functionNameNode != null && shouldAddThisAnnotation(jsDoc)) {
- getContext().reportErrorInNode(functionNameNode, 0,
- "@this annotation is required for functions referencing 'this'");
- }
- }
- thisReferencingFunctions.remove(functionNode);
- }
-
- private boolean shouldAddThisAnnotation(String jsDoc) {
- return jsDoc == null || (!jsDoc.contains("@this") && !jsDoc.contains("@constructor"));
- }
-
- private String getJsDoc(FunctionNode functionNode) {
- String jsDoc = functionNode.getJsDoc();
- if (jsDoc != null) {
- return jsDoc;
- }
-
- // reader.onloadend = function() {...}
- if (AstUtil.hasParentOfType(functionNode, Token.ASSIGN)) {
- Assignment assignment = (Assignment) functionNode.getParent();
- if (assignment.getRight() == functionNode) {
- jsDoc = assignment.getJsDoc();
- if (jsDoc != null) {
- return jsDoc;
- }
- }
- }
- return null;
- }
-}

Powered by Google App Engine
This is Rietveld 408576698