View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.ecmascript.rule;
5   
6   import java.util.List;
7   
8   import net.sourceforge.pmd.RuleContext;
9   import net.sourceforge.pmd.lang.LanguageRegistry;
10  import net.sourceforge.pmd.lang.ParserOptions;
11  import net.sourceforge.pmd.lang.ast.Node;
12  import net.sourceforge.pmd.lang.ecmascript.EcmascriptLanguageModule;
13  import net.sourceforge.pmd.lang.ecmascript.EcmascriptParserOptions;
14  import net.sourceforge.pmd.lang.ecmascript.ast.ASTArrayComprehension;
15  import net.sourceforge.pmd.lang.ecmascript.ast.ASTArrayComprehensionLoop;
16  import net.sourceforge.pmd.lang.ecmascript.ast.ASTArrayLiteral;
17  import net.sourceforge.pmd.lang.ecmascript.ast.ASTAssignment;
18  import net.sourceforge.pmd.lang.ecmascript.ast.ASTAstRoot;
19  import net.sourceforge.pmd.lang.ecmascript.ast.ASTBlock;
20  import net.sourceforge.pmd.lang.ecmascript.ast.ASTBreakStatement;
21  import net.sourceforge.pmd.lang.ecmascript.ast.ASTCatchClause;
22  import net.sourceforge.pmd.lang.ecmascript.ast.ASTComment;
23  import net.sourceforge.pmd.lang.ecmascript.ast.ASTConditionalExpression;
24  import net.sourceforge.pmd.lang.ecmascript.ast.ASTContinueStatement;
25  import net.sourceforge.pmd.lang.ecmascript.ast.ASTDoLoop;
26  import net.sourceforge.pmd.lang.ecmascript.ast.ASTElementGet;
27  import net.sourceforge.pmd.lang.ecmascript.ast.ASTEmptyExpression;
28  import net.sourceforge.pmd.lang.ecmascript.ast.ASTExpressionStatement;
29  import net.sourceforge.pmd.lang.ecmascript.ast.ASTForInLoop;
30  import net.sourceforge.pmd.lang.ecmascript.ast.ASTForLoop;
31  import net.sourceforge.pmd.lang.ecmascript.ast.ASTFunctionCall;
32  import net.sourceforge.pmd.lang.ecmascript.ast.ASTFunctionNode;
33  import net.sourceforge.pmd.lang.ecmascript.ast.ASTIfStatement;
34  import net.sourceforge.pmd.lang.ecmascript.ast.ASTInfixExpression;
35  import net.sourceforge.pmd.lang.ecmascript.ast.ASTKeywordLiteral;
36  import net.sourceforge.pmd.lang.ecmascript.ast.ASTLabel;
37  import net.sourceforge.pmd.lang.ecmascript.ast.ASTLabeledStatement;
38  import net.sourceforge.pmd.lang.ecmascript.ast.ASTLetNode;
39  import net.sourceforge.pmd.lang.ecmascript.ast.ASTName;
40  import net.sourceforge.pmd.lang.ecmascript.ast.ASTNewExpression;
41  import net.sourceforge.pmd.lang.ecmascript.ast.ASTNumberLiteral;
42  import net.sourceforge.pmd.lang.ecmascript.ast.ASTObjectLiteral;
43  import net.sourceforge.pmd.lang.ecmascript.ast.ASTObjectProperty;
44  import net.sourceforge.pmd.lang.ecmascript.ast.ASTParenthesizedExpression;
45  import net.sourceforge.pmd.lang.ecmascript.ast.ASTPropertyGet;
46  import net.sourceforge.pmd.lang.ecmascript.ast.ASTRegExpLiteral;
47  import net.sourceforge.pmd.lang.ecmascript.ast.ASTReturnStatement;
48  import net.sourceforge.pmd.lang.ecmascript.ast.ASTScope;
49  import net.sourceforge.pmd.lang.ecmascript.ast.ASTStringLiteral;
50  import net.sourceforge.pmd.lang.ecmascript.ast.ASTSwitchCase;
51  import net.sourceforge.pmd.lang.ecmascript.ast.ASTSwitchStatement;
52  import net.sourceforge.pmd.lang.ecmascript.ast.ASTThrowStatement;
53  import net.sourceforge.pmd.lang.ecmascript.ast.ASTTryStatement;
54  import net.sourceforge.pmd.lang.ecmascript.ast.ASTUnaryExpression;
55  import net.sourceforge.pmd.lang.ecmascript.ast.ASTVariableDeclaration;
56  import net.sourceforge.pmd.lang.ecmascript.ast.ASTVariableInitializer;
57  import net.sourceforge.pmd.lang.ecmascript.ast.ASTWhileLoop;
58  import net.sourceforge.pmd.lang.ecmascript.ast.ASTWithStatement;
59  import net.sourceforge.pmd.lang.ecmascript.ast.ASTXmlDotQuery;
60  import net.sourceforge.pmd.lang.ecmascript.ast.ASTXmlExpression;
61  import net.sourceforge.pmd.lang.ecmascript.ast.ASTXmlMemberGet;
62  import net.sourceforge.pmd.lang.ecmascript.ast.ASTXmlString;
63  import net.sourceforge.pmd.lang.ecmascript.ast.EcmascriptNode;
64  import net.sourceforge.pmd.lang.ecmascript.ast.EcmascriptParserVisitor;
65  import net.sourceforge.pmd.lang.rule.AbstractRule;
66  import net.sourceforge.pmd.lang.rule.ImmutableLanguage;
67  import net.sourceforge.pmd.lang.rule.properties.BooleanProperty;
68  import net.sourceforge.pmd.lang.rule.properties.EnumeratedProperty;
69  
70  public abstract class AbstractEcmascriptRule extends AbstractRule implements EcmascriptParserVisitor, ImmutableLanguage {
71  
72      private static final BooleanProperty RECORDING_COMMENTS_DESCRIPTOR = EcmascriptParserOptions.RECORDING_COMMENTS_DESCRIPTOR;
73      private static final BooleanProperty RECORDING_LOCAL_JSDOC_COMMENTS_DESCRIPTOR = EcmascriptParserOptions.RECORDING_LOCAL_JSDOC_COMMENTS_DESCRIPTOR;
74      private static final EnumeratedProperty<EcmascriptParserOptions.Version> RHINO_LANGUAGE_VERSION = EcmascriptParserOptions.RHINO_LANGUAGE_VERSION;
75  
76      public AbstractEcmascriptRule() {
77  	super.setLanguage(LanguageRegistry.getLanguage(EcmascriptLanguageModule.NAME));
78  	definePropertyDescriptor(RECORDING_COMMENTS_DESCRIPTOR);
79  	definePropertyDescriptor(RECORDING_LOCAL_JSDOC_COMMENTS_DESCRIPTOR);
80  	definePropertyDescriptor(RHINO_LANGUAGE_VERSION);
81      }
82  
83      @Override
84      public ParserOptions getParserOptions() {
85  	return new EcmascriptParserOptions(this);
86      }
87  
88      public void apply(List<? extends Node> nodes, RuleContext ctx) {
89  	visitAll(nodes, ctx);
90      }
91  
92      protected void visitAll(List<? extends Node> nodes, RuleContext ctx) {
93  	for (Object element : nodes) {
94  	    ASTAstRoot node = (ASTAstRoot) element;
95  	    visit(node, ctx);
96  	}
97      }
98  
99      //
100     // The following APIs are identical to those in EcmascriptParserVisitorAdapter.
101     // Due to Java single inheritance, it preferred to extend from the more
102     // complex Rule base class instead of from relatively simple Visitor.
103     //
104 
105     public Object visit(EcmascriptNode<?> node, Object data) {
106 	node.childrenAccept(this, data);
107 	return null;
108     }
109 
110     public Object visit(ASTArrayComprehension node, Object data) {
111 	return visit((EcmascriptNode<?>) node, data);
112     }
113 
114     public Object visit(ASTArrayComprehensionLoop node, Object data) {
115 	return visit((EcmascriptNode<?>) node, data);
116     }
117 
118     public Object visit(ASTArrayLiteral node, Object data) {
119 	return visit((EcmascriptNode<?>) node, data);
120     }
121 
122     public Object visit(ASTAssignment node, Object data) {
123 	return visit((EcmascriptNode<?>) node, data);
124     }
125 
126     public Object visit(ASTAstRoot node, Object data) {
127 	return visit((EcmascriptNode<?>) node, data);
128     }
129 
130     public Object visit(ASTBlock node, Object data) {
131 	return visit((EcmascriptNode<?>) node, data);
132     }
133 
134     public Object visit(ASTBreakStatement node, Object data) {
135 	return visit((EcmascriptNode<?>) node, data);
136     }
137 
138     public Object visit(ASTCatchClause node, Object data) {
139 	return visit((EcmascriptNode<?>) node, data);
140     }
141 
142     public Object visit(ASTComment node, Object data) {
143 	return visit((EcmascriptNode<?>) node, data);
144     }
145 
146     public Object visit(ASTConditionalExpression node, Object data) {
147 	return visit((EcmascriptNode<?>) node, data);
148     }
149 
150     public Object visit(ASTContinueStatement node, Object data) {
151 	return visit((EcmascriptNode<?>) node, data);
152     }
153 
154     public Object visit(ASTDoLoop node, Object data) {
155 	return visit((EcmascriptNode<?>) node, data);
156     }
157 
158     public Object visit(ASTElementGet node, Object data) {
159 	return visit((EcmascriptNode<?>) node, data);
160     }
161 
162     public Object visit(ASTEmptyExpression node, Object data) {
163 	return visit((EcmascriptNode<?>) node, data);
164     }
165 
166     public Object visit(ASTExpressionStatement node, Object data) {
167 	return visit((EcmascriptNode<?>) node, data);
168     }
169 
170     public Object visit(ASTForInLoop node, Object data) {
171 	return visit((EcmascriptNode<?>) node, data);
172     }
173 
174     public Object visit(ASTForLoop node, Object data) {
175 	return visit((EcmascriptNode<?>) node, data);
176     }
177 
178     public Object visit(ASTFunctionCall node, Object data) {
179 	return visit((EcmascriptNode<?>) node, data);
180     }
181 
182     public Object visit(ASTFunctionNode node, Object data) {
183 	return visit((EcmascriptNode<?>) node, data);
184     }
185 
186     public Object visit(ASTIfStatement node, Object data) {
187 	return visit((EcmascriptNode<?>) node, data);
188     }
189 
190     public Object visit(ASTInfixExpression node, Object data) {
191 	return visit((EcmascriptNode<?>) node, data);
192     }
193 
194     public Object visit(ASTKeywordLiteral node, Object data) {
195 	return visit((EcmascriptNode<?>) node, data);
196     }
197 
198     public Object visit(ASTLabel node, Object data) {
199 	return visit((EcmascriptNode<?>) node, data);
200     }
201 
202     public Object visit(ASTLabeledStatement node, Object data) {
203 	return visit((EcmascriptNode<?>) node, data);
204     }
205 
206     public Object visit(ASTLetNode node, Object data) {
207 	return visit((EcmascriptNode<?>) node, data);
208     }
209 
210     public Object visit(ASTName node, Object data) {
211 	return visit((EcmascriptNode<?>) node, data);
212     }
213 
214     public Object visit(ASTNewExpression node, Object data) {
215 	return visit((EcmascriptNode<?>) node, data);
216     }
217 
218     public Object visit(ASTNumberLiteral node, Object data) {
219 	return visit((EcmascriptNode<?>) node, data);
220     }
221 
222     public Object visit(ASTObjectLiteral node, Object data) {
223 	return visit((EcmascriptNode<?>) node, data);
224     }
225 
226     public Object visit(ASTObjectProperty node, Object data) {
227 	return visit((EcmascriptNode<?>) node, data);
228     }
229 
230     public Object visit(ASTParenthesizedExpression node, Object data) {
231 	return visit((EcmascriptNode<?>) node, data);
232     }
233 
234     public Object visit(ASTPropertyGet node, Object data) {
235 	return visit((EcmascriptNode<?>) node, data);
236     }
237 
238     public Object visit(ASTRegExpLiteral node, Object data) {
239 	return visit((EcmascriptNode<?>) node, data);
240     }
241 
242     public Object visit(ASTReturnStatement node, Object data) {
243 	return visit((EcmascriptNode<?>) node, data);
244     }
245 
246     public Object visit(ASTScope node, Object data) {
247 	return visit((EcmascriptNode<?>) node, data);
248     }
249 
250     public Object visit(ASTStringLiteral node, Object data) {
251 	return visit((EcmascriptNode<?>) node, data);
252     }
253 
254     public Object visit(ASTSwitchCase node, Object data) {
255 	return visit((EcmascriptNode<?>) node, data);
256     }
257 
258     public Object visit(ASTSwitchStatement node, Object data) {
259 	return visit((EcmascriptNode<?>) node, data);
260     }
261 
262     public Object visit(ASTThrowStatement node, Object data) {
263 	return visit((EcmascriptNode<?>) node, data);
264     }
265 
266     public Object visit(ASTTryStatement node, Object data) {
267 	return visit((EcmascriptNode<?>) node, data);
268     }
269 
270     public Object visit(ASTUnaryExpression node, Object data) {
271 	return visit((EcmascriptNode<?>) node, data);
272     }
273 
274     public Object visit(ASTVariableDeclaration node, Object data) {
275 	return visit((EcmascriptNode<?>) node, data);
276     }
277 
278     public Object visit(ASTVariableInitializer node, Object data) {
279 	return visit((EcmascriptNode<?>) node, data);
280     }
281 
282     public Object visit(ASTWhileLoop node, Object data) {
283 	return visit((EcmascriptNode<?>) node, data);
284     }
285 
286     public Object visit(ASTWithStatement node, Object data) {
287 	return visit((EcmascriptNode<?>) node, data);
288     }
289 
290     public Object visit(ASTXmlDotQuery node, Object data) {
291 	return visit((EcmascriptNode<?>) node, data);
292     }
293 
294     public Object visit(ASTXmlExpression node, Object data) {
295 	return visit((EcmascriptNode<?>) node, data);
296     }
297 
298     public Object visit(ASTXmlMemberGet node, Object data) {
299 	return visit((EcmascriptNode<?>) node, data);
300     }
301 
302     public Object visit(ASTXmlString node, Object data) {
303 	return visit((EcmascriptNode<?>) node, data);
304     }
305 }