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