Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2009 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 package com.google.javascript.jscomp;
17
18 import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
19 import com.google.javascript.rhino.Node;
20 import java.util.regex.Matcher;
21 import java.util.regex.Pattern;
22
23 /**
24 * Ensures string literals matching certain patterns are only used as
25 * goog.getCssName parameters.
26 *
27 * @author [email protected] (Martin Kretzschmar)
28 */
29 class CheckMissingGetCssName
30 extends AbstractPostOrderCallback implements CompilerPass {
31 private final AbstractCompiler compiler;
32 private final CheckLevel level;
33 private final Matcher blacklist;
34
35 static final String GET_CSS_NAME_FUNCTION = "goog.getCssName";
36 static final String GET_UNIQUE_ID_FUNCTION = ".getUniqueId";
37
38 static final DiagnosticType MISSING_GETCSSNAME =
39 DiagnosticType.disabled(
40 "JSC_MISSING_GETCSSNAME",
41 "missing goog.getCssName around literal ''{0}''");
42
43 CheckMissingGetCssName(AbstractCompiler compiler, CheckLevel level,
44 String blacklistRegex) {
45 this.compiler = compiler;
46 this.level = level;
47 this.blacklist =
48 Pattern.compile("\\b(?:" + blacklistRegex + ")").matcher("");
49 }
50
51 @Override
52 public void process(Node externs, Node root) {
53 NodeTraversal.traverse(compiler, root, this);
54 }
55
56 @Override
57 public void visit(NodeTraversal t, Node n, Node parent) {
58 if (n.isString() &&
59 !parent.isGetProp() &&
60 !parent.isRegExp()) {
61 String s = n.getString();
62
63 for (blacklist.reset(s); blacklist.find();) {
64 if (insideGetCssNameCall(n)) {
65 continue;
66 }
67 if (insideGetUniqueIdCall(n)) {
68 continue;
69 }
70 if (insideAssignmentToIdConstant(n)) {
71 continue;
72 }
73 compiler.report(t.makeError(n, level, MISSING_GETCSSNAME,
74 blacklist.group()));
75 }
76 }
77 }
78
79 /** Returns whether the node is an argument of a goog.getCssName call. */
80 private boolean insideGetCssNameCall(Node n) {
81 Node parent = n.getParent();
82 return parent.isCall() &&
83 GET_CSS_NAME_FUNCTION.equals(
84 parent.getFirstChild().getQualifiedName());
85 }
86
87 /**
88 * Returns whether the node is an argument of a function that returns
89 * a unique id (the last part of the qualified name matches
90 * GET_UNIQUE_ID_FUNCTION).
91 */
92 private boolean insideGetUniqueIdCall(Node n) {
93 Node parent = n.getParent();
94 String name = parent.isCall() ?
95 parent.getFirstChild().getQualifiedName() : null;
96
97 return name != null && name.endsWith(GET_UNIQUE_ID_FUNCTION);
98 }
99
100 /**
101 * Returns whether the node is the right hand side of an assignment or
102 * initialization of a variable named *_ID of *_ID_.
103 */
104 private boolean insideAssignmentToIdConstant(Node n) {
105 Node parent = n.getParent();
106 if (parent.isAssign()) {
107 String qname = parent.getFirstChild().getQualifiedName();
108 return qname != null && isIdName(qname);
109 } else if (parent.isName()) {
110 Node grandParent = parent.getParent();
111 if (grandParent != null && grandParent.isVar()) {
112 String name = parent.getString();
113 return isIdName(name);
114 } else {
115 return false;
116 }
117 } else {
118 return false;
119 }
120 }
121
122 private boolean isIdName(String name) {
123 return name.endsWith("ID") || name.endsWith("ID_");
124 }
125 }