JSAnnotatingVisitor.java

1    /* 
2     * Copyright 2000-2005 JetBrains s.r.o. 
3     * 
4     * Licensed under the Apache License, Version 2.0 (the "License"); 
5     * you may not use this file except in compliance with the License. 
6     * You may obtain a copy of the License at 
7     * 
8     * http://www.apache.org/licenses/LICENSE-2.0 
9     * 
10    * Unless required by applicable law or agreed to in writing, software 
11    * distributed under the License is distributed on an "AS IS" BASIS, 
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13    * See the License for the specific language governing permissions and 
14    * limitations under the License. 
15    */ 
16   package com.intellij.lang.javascript.validation; 
17    
18   import com.intellij.lang.ASTNode; 
19   import com.intellij.lang.annotation.AnnotationHolder; 
20   import com.intellij.lang.annotation.Annotator; 
21   import com.intellij.lang.javascript.psi.*; 
22   import com.intellij.lang.javascript.psi.util.JSUtils; 
23   import com.intellij.psi.PsiElement; 
24   import com.intellij.psi.PsiSubstitutor; 
25   import com.intellij.psi.util.PsiTreeUtil; 
26   import com.intellij.psi.xml.XmlTagChild; 
27    
28   /** 
29    * Created by IntelliJ IDEA. 
30    * User: max 
31    * Date: Feb 3, 2005 
32    * Time: 3:11:41 PM 
33    * To change this template use File | Settings | File Templates. 
34    */ 
35   public class JSAnnotatingVisitor extends JSElementVisitor implements Annotator { 
36     private AnnotationHolder myHolder; 
37    
38     public void annotate(PsiElement psiElement, AnnotationHolder holder) { 
39       myHolder = holder; 
40       psiElement.accept(this); 
41     } 
42    
43     /* TODO: disabled temporary until we get meaninfgful way to resolve external declarations. 
44     public void visitJSReferenceExpression(final JSReferenceExpression node) { 
45       if (node.getQualifier() == null) { 
46         if (node.resolve() == null) { 
47           ASTNode nameIdentifier = node.getNode().findChildByType(JSTokenTypes.IDENTIFIER); 
48           final Annotation ann = myHolder.createErrorAnnotation(nameIdentifier, "cannot resolve '" + nameIdentifier.getText() + "'"); 
49           ann.setHighlightType(ProblemHighlightType.LIKE_UNKNOWN_SYMBOL); 
50         } 
51       } 
52     } 
53     */ 
54    
55     @Override public void visitJSFunctionDeclaration(final JSFunction node) { 
56       final String name = node.getName(); 
57       if (name == null) return; 
58       final ASTNode nameIdentifier = node.findNameIdentifier(); 
59    
60       checkForDuplicateDeclaration(name, node, nameIdentifier); 
61     } 
62    
63     private void checkForDuplicateDeclaration(final String name, final PsiElement decl, final ASTNode nameIdentifier) { 
64       final JSResolveUtil.ResolveProcessor processor = new JSResolveUtil.ResolveProcessor(name) { 
65         public boolean execute(PsiElement element, PsiSubstitutor substitutor) { 
66           if (element == decl) return true; 
67           if (decl instanceof JSParameter && decl.getParent() != element.getParent()) return false; 
68           return super.execute(element, substitutor); 
69         } 
70    
71         public <T> T getHint(Class<T> hintClass) { 
72           return null; 
73         } 
74    
75         public void handleEvent(Event event, Object associated) { 
76         } 
77       }; 
78    
79       JSResolveUtil.treeWalkUp(processor, decl, null, decl); 
80    
81       if (processor.getResult() != null) { 
82         myHolder.createWarningAnnotation(nameIdentifier, "Duplicate declaration"); 
83       } 
84     } 
85    
86     public void visitJSAssignmentExpression(final JSAssignmentExpression expression) { 
87       final JSExpression lExpr = expression.getLOperand(); 
88       if (lExpr instanceof JSReferenceExpression) { 
89         PsiElement resolved = ((JSReferenceExpression)lExpr).resolve(); 
90         if (resolved instanceof JSVariable && ((JSVariable)resolved).isConst()) { 
91           myHolder.createErrorAnnotation(lExpr, "Attempt to assign to const variable"); 
92         } 
93       } 
94    
95       if (!JSUtils.isLHSExpression(lExpr)) { 
96         myHolder.createErrorAnnotation(lExpr, "Must be lvalue"); 
97       } 
98     } 
99    
100    public void visitJSVariable(final JSVariable var) { 
101      if (var.isConst() && var.getInitializer() == null) { 
102        myHolder.createWarningAnnotation(var, "const variable without initializer. It won't be possible to assign meaningful value later."); 
103      } 
104   
105      final String name = var.getName(); 
106      if (name != null) { 
107        checkForDuplicateDeclaration(name, var, var.findNameIdentifier()); 
108      } 
109    } 
110   
111    public void visitJSContinueStatement(final JSContinueStatement node) { 
112      if (node.getStatementToContinue() == null) { 
113        myHolder.createErrorAnnotation(node, "Cannot determine target for 'continue'"); 
114      } 
115    } 
116   
117    public void visitJSBreakStatement(final JSBreakStatement node) { 
118      if (node.getStatementToBreak() == null) { 
119        myHolder.createErrorAnnotation(node, "Cannot determine target for 'break'"); 
120      } 
121    } 
122   
123    public void visitJSReturnStatement(final JSReturnStatement node) { 
124      if (PsiTreeUtil.getParentOfType(node, JSFunction.class, XmlTagChild.class) == null) { 
125        myHolder.createErrorAnnotation(node, "'return' outside function definition"); 
126      } 
127    } 
128   
129    public void visitJSLabeledStatement(final JSLabeledStatement node) { 
130      final String label = node.getLabel(); 
131      if (label != null) { 
132        PsiElement run = node.getParent(); 
133        while(run != null) { 
134          if (run instanceof JSLabeledStatement) { 
135            if (label.equals(((JSLabeledStatement)run).getLabel())) { 
136              myHolder.createErrorAnnotation(node.getLabelIdentifier(), "Duplicate label"); 
137              break; 
138            } 
139          } 
140   
141          if (run instanceof JSFunction) break; 
142          run = run.getParent(); 
143        } 
144      } 
145    } 
146  } 
147