27
27
import com .google .javascript .rhino .JSDocInfo .Visibility ;
28
28
import com .google .javascript .rhino .Node ;
29
29
import java .util .ArrayList ;
30
- import java .util .HashSet ;
30
+ import java .util .LinkedHashSet ;
31
31
import java .util .List ;
32
32
import java .util .Set ;
33
33
@@ -42,9 +42,9 @@ public class CheckUnusedPrivateProperties implements CompilerPass, NodeTraversal
42
42
DiagnosticType .disabled ("JSC_UNUSED_PRIVATE_PROPERTY" , "Private property {0} is never read" );
43
43
44
44
private final AbstractCompiler compiler ;
45
- private final Set <String > used = new HashSet <>();
45
+ private final Set <String > used = new LinkedHashSet <>();
46
46
private final List <Node > candidates = new ArrayList <>();
47
- private final HashSet <String > constructorsAndInterfaces = new HashSet <>();
47
+ private final LinkedHashSet <String > constructorsAndInterfaces = new LinkedHashSet <>();
48
48
49
49
public CheckUnusedPrivateProperties (AbstractCompiler compiler ) {
50
50
this .compiler = compiler ;
@@ -90,55 +90,59 @@ public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
90
90
@ Override
91
91
public void visit (NodeTraversal t , Node n , Node parent ) {
92
92
switch (n .getToken ()) {
93
- case SCRIPT : {
94
- // exiting the script, report any privates not used in the file.
95
- reportUnused (t );
96
- break ;
97
- }
93
+ case SCRIPT :
94
+ {
95
+ // exiting the script, report any privates not used in the file.
96
+ reportUnused (t );
97
+ break ;
98
+ }
98
99
99
- case GETPROP : {
100
+ case GETPROP :
101
+ {
100
102
String propName = n .getString ();
101
103
if (isPinningPropertyUse (n ) || !isCandidatePropertyDefinition (n )) {
102
- used .add (propName );
103
- } else {
104
- // Only consider "private" properties.
105
- if (isCheckablePrivatePropDecl (n )) {
106
- candidates .add (n );
107
- }
108
- }
109
- break ;
110
- }
111
-
112
- case MEMBER_FUNCTION_DEF : {
113
- // Only consider "private" methods.
114
- if (isCheckablePrivatePropDecl (n )) {
115
- candidates .add (n );
116
- }
117
- break ;
118
- }
119
-
120
- case OBJECTLIT : {
104
+ used .add (propName );
105
+ } else {
106
+ // Only consider "private" properties.
107
+ if (isCheckablePrivatePropDecl (n )) {
108
+ candidates .add (n );
109
+ }
110
+ }
111
+ break ;
112
+ }
113
+
114
+ case MEMBER_FUNCTION_DEF :
115
+ {
116
+ // Only consider "private" methods.
117
+ if (isCheckablePrivatePropDecl (n )) {
118
+ candidates .add (n );
119
+ }
120
+ break ;
121
+ }
122
+
123
+ case OBJECTLIT :
124
+ {
121
125
// Assume any object literal definition might be a reflection on the
122
126
// class property.
123
127
for (Node c = n .getFirstChild (); c != null ; c = c .getNext ()) {
124
128
if (c .isStringKey () || c .isGetterDef () || c .isSetterDef () || c .isMemberFunctionDef ()) {
125
129
used .add (c .getString ());
126
130
}
127
- }
128
- break ;
129
- }
131
+ }
132
+ break ;
133
+ }
130
134
131
135
case CALL :
132
136
// Look for properties referenced through a property rename function.
133
137
Node target = n .getFirstChild ();
134
138
if (n .hasMoreThanOneChild ()
135
139
&& compiler .getCodingConvention ().isPropertyRenameFunction (target )) {
136
- Node propName = target .getNext ();
140
+ Node propName = target .getNext ();
137
141
if (propName .isStringLit ()) {
138
142
used .add (propName .getString ());
139
- }
140
- }
141
- break ;
143
+ }
144
+ }
145
+ break ;
142
146
143
147
case FUNCTION :
144
148
JSDocInfo info = NodeUtil .getBestJSDocInfo (n );
@@ -205,8 +209,7 @@ private static boolean isPinningPropertyUse(Node n) {
205
209
} else if (parent .isAssign ()) {
206
210
// A simple assignment doesn't pin the property.
207
211
return false ;
208
- } else if (NodeUtil .isAssignmentOp (parent )
209
- || parent .isInc () || parent .isDec ()) {
212
+ } else if (NodeUtil .isAssignmentOp (parent ) || parent .isInc () || parent .isDec ()) {
210
213
// In general, compound assignments are both reads and writes, but
211
214
// if the property is never otherwise read we can consider it simply
212
215
// a write.
0 commit comments