Debian Codegraph @

src/com/google/javascript/jscomp/CheckSuspiciousCode.java

raw symbols SCIP
1 /*
2 * Copyright 2012 The Closure Compiler Authors.
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
17 package com.google.javascript.jscomp;
18
19 import com.google.common.base.Preconditions;
20 import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
21 import com.google.javascript.rhino.Node;
22 import com.google.javascript.rhino.Token;
23
24 /**
25 * Checks for common errors, such as misplaced semicolons:
26 * <pre>
27 * if (x); act_now();
28 * </pre>
29 * or comparison against NaN:
30 * <pre>
31 * if (x === NaN) act();
32 * </pre>
33 * and generates warnings.
34 *
35 * @author [email protected] (John Lenz)
36 */
37 final class CheckSuspiciousCode extends AbstractPostOrderCallback {
38
39 static final DiagnosticType SUSPICIOUS_SEMICOLON = DiagnosticType.warning(
40 "JSC_SUSPICIOUS_SEMICOLON",
41 "If this if/for/while really shouldn't have a body, use {}");
42
43 static final DiagnosticType SUSPICIOUS_COMPARISON_WITH_NAN =
44 DiagnosticType.warning(
45 "JSC_SUSPICIOUS_NAN",
46 "Comparison again NaN is always false. Did you mean isNaN()?");
47
48 CheckSuspiciousCode() {
49 }
50
51 @Override
52 public void visit(NodeTraversal t, Node n, Node parent) {
53 checkMissingSemicolon(t, n);
54 checkNaN(t, n);
55 }
56
57 private void checkMissingSemicolon(NodeTraversal t, Node n) {
58 switch (n.getType()) {
59 case Token.IF:
60 Node trueCase = n.getFirstChild().getNext();
61 reportIfWasEmpty(t, trueCase);
62 Node elseCase = trueCase.getNext();
63 if (elseCase != null) {
64 reportIfWasEmpty(t, elseCase);
65 }
66 break;
67
68 case Token.WHILE:
69 case Token.FOR:
70 reportIfWasEmpty(t, NodeUtil.getLoopCodeBlock(n));
71 break;
72 }
73 }
74
75 private void reportIfWasEmpty(NodeTraversal t, Node block) {
76 Preconditions.checkState(block.isBlock());
77
78 // A semicolon is distinguished from a block without children by
79 // annotating it with EMPTY_BLOCK. Blocks without children are
80 // usually intentional, especially with loops.
81 if (!block.hasChildren() && block.wasEmptyNode()) {
82 t.getCompiler().report(
83 t.makeError(block, SUSPICIOUS_SEMICOLON));
84 }
85 }
86
87 private void checkNaN(NodeTraversal t, Node n) {
88 switch (n.getType()) {
89 case Token.EQ:
90 case Token.GE:
91 case Token.GT:
92 case Token.LE:
93 case Token.LT:
94 case Token.NE:
95 case Token.SHEQ:
96 case Token.SHNE:
97 reportIfNaN(t, n.getFirstChild());
98 reportIfNaN(t, n.getLastChild());
99 }
100 }
101
102 private void reportIfNaN(NodeTraversal t, Node n) {
103 if (NodeUtil.isNaN(n)) {
104 t.getCompiler().report(
105 t.makeError(n.getParent(), SUSPICIOUS_COMPARISON_WITH_NAN));
106 }
107 }
108 }