-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
CodingConventions.java
627 lines (524 loc) · 17.6 KB
/
CodingConventions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
* Copyright 2011 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp;
import static com.google.common.base.Preconditions.checkState;
import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.Immutable;
import com.google.javascript.rhino.FunctionTypeI;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.ObjectTypeI;
import com.google.javascript.rhino.ObjectTypeI.PropertyDeclarer;
import com.google.javascript.rhino.StaticSourceFile;
import com.google.javascript.rhino.jstype.FunctionType;
import com.google.javascript.rhino.jstype.JSTypeRegistry;
import com.google.javascript.rhino.jstype.ObjectType;
import com.google.javascript.rhino.jstype.StaticTypedScope;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* Helper classes for dealing with coding conventions.
* @author nicksantos@google.com (Nick Santos)
*/
public final class CodingConventions {
private CodingConventions() {}
/** Gets the default coding convention. */
public static CodingConvention getDefault() {
return new DefaultCodingConvention();
}
/**
* @param n The last statement of a block to check for an always throws
* function call. Used by CheckMissingReturn.
* @param alwaysThrowsFunctionName The name of a function that always throws.
* @return {@code true} if n is call to alwaysThrowsFunctionName, otherwise
* {@code false}.
*/
public static boolean defaultIsFunctionCallThatAlwaysThrows(
Node n, String alwaysThrowsFunctionName) {
if (n.isExprResult()) {
if (!n.getFirstChild().isCall()) {
return false;
}
} else if (!n.isCall()) {
return false;
}
if (n.isExprResult()) {
n = n.getFirstChild();
}
// n is a call
return n.getFirstChild().matchesQualifiedName(alwaysThrowsFunctionName);
}
static boolean isAliasingGlobalThis(CodingConvention convention, Node n) {
return n.isAssign()
&& n.getFirstChild().matchesQualifiedName(convention.getGlobalObject())
&& n.getLastChild().isThis();
}
/**
* A convention that wraps another.
*
* When you want to support a new library, you should subclass this
* delegate, and override the methods that you want to customize.
*
* This way, a person using jQuery and Closure Library can create a new
* coding convention by creating a jQueryCodingConvention that delegates
* to a ClosureCodingConvention that delegates to a DefaultCodingConvention.
*/
@Immutable
public static class Proxy implements CodingConvention {
protected final CodingConvention nextConvention;
protected Proxy(CodingConvention convention) {
this.nextConvention = convention;
}
@Override
public boolean isConstant(String variableName) {
return nextConvention.isConstant(variableName);
}
@Override public boolean isConstantKey(String keyName) {
return nextConvention.isConstantKey(keyName);
}
@Override
public boolean isValidEnumKey(String key) {
return nextConvention.isValidEnumKey(key);
}
@Override
public boolean isOptionalParameter(Node parameter) {
return nextConvention.isOptionalParameter(parameter);
}
@Override
public boolean isVarArgsParameter(Node parameter) {
return nextConvention.isVarArgsParameter(parameter);
}
@Override
public boolean isFunctionCallThatAlwaysThrows(Node n) {
return nextConvention.isFunctionCallThatAlwaysThrows(n);
}
@Override
public boolean isExported(String name, boolean local) {
return nextConvention.isExported(name, local);
}
@Override
public String getPackageName(StaticSourceFile source) {
return nextConvention.getPackageName(source);
}
@Override
public final boolean isExported(String name) {
return isExported(name, false) || isExported(name, true);
}
@Override
public boolean blockRenamingForProperty(String name) {
return nextConvention.blockRenamingForProperty(name);
}
@Override
public boolean isPrivate(String name) {
return nextConvention.isPrivate(name);
}
@Override
public boolean hasPrivacyConvention() {
return nextConvention.hasPrivacyConvention();
}
@Override
public SubclassRelationship getClassesDefinedByCall(Node callNode) {
return nextConvention.getClassesDefinedByCall(callNode);
}
@Override
public boolean isClassFactoryCall(Node callNode) {
return nextConvention.isClassFactoryCall(callNode);
}
@Override
public boolean isSuperClassReference(String propertyName) {
return nextConvention.isSuperClassReference(propertyName);
}
@Override
public boolean extractIsModuleFile(Node node, Node parent) {
return nextConvention.extractIsModuleFile(node, parent);
}
@Override
public String extractClassNameIfProvide(Node node, Node parent) {
return nextConvention.extractClassNameIfProvide(node, parent);
}
@Override
public String extractClassNameIfRequire(Node node, Node parent) {
return nextConvention.extractClassNameIfRequire(node, parent);
}
@Override
public String getExportPropertyFunction() {
return nextConvention.getExportPropertyFunction();
}
@Override
public String getExportSymbolFunction() {
return nextConvention.getExportSymbolFunction();
}
@Override
public List<String> identifyTypeDeclarationCall(Node n) {
return nextConvention.identifyTypeDeclarationCall(n);
}
@Override
public void applySubclassRelationship(
PropertyDeclarer declarer,
FunctionTypeI parentCtor,
FunctionTypeI childCtor,
SubclassType type) {
nextConvention.applySubclassRelationship(declarer, parentCtor, childCtor, type);
}
@Override
public String getAbstractMethodName() {
return nextConvention.getAbstractMethodName();
}
@Override
public String getSingletonGetterClassName(Node callNode) {
return nextConvention.getSingletonGetterClassName(callNode);
}
@Override
public void applySingletonGetter(PropertyDeclarer declarer,
FunctionTypeI functionType, FunctionTypeI getterType, ObjectTypeI objectType) {
nextConvention.applySingletonGetter(declarer, functionType, getterType, objectType);
}
@Override
public boolean isInlinableFunction(Node n) {
return nextConvention.isInlinableFunction(n);
}
@Override
public DelegateRelationship getDelegateRelationship(Node callNode) {
return nextConvention.getDelegateRelationship(callNode);
}
@Override
public void applyDelegateRelationship(
ObjectType delegateSuperclass, ObjectType delegateBase,
ObjectType delegator, FunctionType delegateProxy,
FunctionType findDelegate) {
nextConvention.applyDelegateRelationship(
delegateSuperclass, delegateBase, delegator,
delegateProxy, findDelegate);
}
@Override
public String getDelegateSuperclassName() {
return nextConvention.getDelegateSuperclassName();
}
@Override
public void checkForCallingConventionDefinitions(
Node n, Map<String, String> delegateCallingConventions) {
nextConvention.checkForCallingConventionDefinitions(
n, delegateCallingConventions);
}
@Override
public void defineDelegateProxyPrototypeProperties(
JSTypeRegistry registry,
StaticTypedScope<com.google.javascript.rhino.jstype.JSType> scope,
List<ObjectType> delegateProxyPrototypes,
Map<String, String> delegateCallingConventions) {
nextConvention.defineDelegateProxyPrototypeProperties(
registry, scope, delegateProxyPrototypes, delegateCallingConventions);
}
@Override
public String getGlobalObject() {
return nextConvention.getGlobalObject();
}
@Override
public boolean isAliasingGlobalThis(Node n) {
return nextConvention.isAliasingGlobalThis(n);
}
@Override
public Collection<AssertionFunctionSpec> getAssertionFunctions() {
return nextConvention.getAssertionFunctions();
}
@Override
public Bind describeFunctionBind(Node n) {
return describeFunctionBind(n, false, false);
}
@Override
public Bind describeFunctionBind(
Node n, boolean callerChecksTypes, boolean iCheckTypes) {
return nextConvention
.describeFunctionBind(n, callerChecksTypes, iCheckTypes);
}
@Override
public Cache describeCachingCall(Node node) {
return nextConvention.describeCachingCall(node);
}
@Override
public boolean isPropertyTestFunction(Node call) {
return nextConvention.isPropertyTestFunction(call);
}
@Override
public boolean isPropertyRenameFunction(String name) {
return nextConvention.isPropertyRenameFunction(name);
}
@Override
public boolean isPrototypeAlias(Node getProp) {
return false;
}
@Override
public ObjectLiteralCast getObjectLiteralCast(Node callNode) {
return nextConvention.getObjectLiteralCast(callNode);
}
@Override
public Collection<String> getIndirectlyDeclaredProperties() {
return nextConvention.getIndirectlyDeclaredProperties();
}
}
/**
* The default coding convention.
* Should be at the bottom of all proxy chains.
*/
@Immutable
private static class DefaultCodingConvention implements CodingConvention {
private static final long serialVersionUID = 1L;
@Override
public boolean isConstant(String variableName) {
return false;
}
@Override
public boolean isConstantKey(String variableName) {
return false;
}
@Override
public boolean isValidEnumKey(String key) {
return key != null && key.length() > 0;
}
@Override
public boolean isOptionalParameter(Node parameter) {
// be as lax as possible, but this must be mutually exclusive from
// var_args parameters.
return parameter.isOptionalArg();
}
@Override
public boolean isVarArgsParameter(Node parameter) {
// be as lax as possible
return parameter.isVarArgs();
}
@Override
public boolean isFunctionCallThatAlwaysThrows(Node n) {
return false;
}
@Override
public String getPackageName(StaticSourceFile source) {
// The package name of a source file is its file path.
String name = source.getName();
int lastSlash = name.lastIndexOf('/');
return lastSlash == -1 ? "" : name.substring(0, lastSlash);
}
@Override
public boolean isExported(String name, boolean local) {
return local && name.startsWith("$super");
}
@Override
public boolean isExported(String name) {
return isExported(name, false) || isExported(name, true);
}
@Override
public boolean blockRenamingForProperty(String name) {
return false;
}
@Override
public boolean isPrivate(String name) {
return false;
}
@Override
public boolean hasPrivacyConvention() {
return false;
}
@Override
public SubclassRelationship getClassesDefinedByCall(Node callNode) {
Node callName = callNode.getFirstChild();
if ((callName.matchesQualifiedName("$jscomp.inherits")
|| callName.matchesQualifiedName("$jscomp$inherits"))
&& callNode.getChildCount() == 3) {
Node subclass = callName.getNext();
Node superclass = subclass.getNext();
// The StripCode pass may create $jscomp.inherits calls with NULL arguments.
if (subclass.isQualifiedName() && superclass.isQualifiedName()) {
return new SubclassRelationship(SubclassType.INHERITS, subclass, superclass);
}
}
return null;
}
@Override
public boolean isClassFactoryCall(Node callNode) {
return false;
}
@Override
public boolean isSuperClassReference(String propertyName) {
return false;
}
@Override
public boolean extractIsModuleFile(Node node, Node parent) {
String message = "only implemented in ClosureCodingConvention";
throw new UnsupportedOperationException(message);
}
@Override
public String extractClassNameIfProvide(Node node, Node parent) {
String message = "only implemented in ClosureCodingConvention";
throw new UnsupportedOperationException(message);
}
@Override
public String extractClassNameIfRequire(Node node, Node parent) {
String message = "only implemented in ClosureCodingConvention";
throw new UnsupportedOperationException(message);
}
@Override
public String getExportPropertyFunction() {
return null;
}
@Override
public String getExportSymbolFunction() {
return null;
}
@Override
public List<String> identifyTypeDeclarationCall(Node n) {
return null;
}
@Override
public void applySubclassRelationship(
PropertyDeclarer declarer,
FunctionTypeI parentCtor,
FunctionTypeI childCtor,
SubclassType type) {
// do nothing
}
@Override
public String getAbstractMethodName() {
return null;
}
@Override
public String getSingletonGetterClassName(Node callNode) {
return null;
}
@Override
public void applySingletonGetter(
PropertyDeclarer declarer, FunctionTypeI functionType,
FunctionTypeI getterType, ObjectTypeI objectType) {
// do nothing.
}
@Override
public boolean isInlinableFunction(Node n) {
checkState(n.isFunction(), n);
return true;
}
@Override
public DelegateRelationship getDelegateRelationship(Node callNode) {
return null;
}
@Override
public void applyDelegateRelationship(
ObjectType delegateSuperclass, ObjectType delegateBase,
ObjectType delegator, FunctionType delegateProxy,
FunctionType findDelegate) {
// do nothing.
}
@Override
public String getDelegateSuperclassName() {
return null;
}
@Override
public void checkForCallingConventionDefinitions(Node n,
Map<String, String> delegateCallingConventions) {
// do nothing.
}
@Override
public void defineDelegateProxyPrototypeProperties(
JSTypeRegistry registry,
StaticTypedScope<com.google.javascript.rhino.jstype.JSType> scope,
List<ObjectType> delegateProxyPrototypes,
Map<String, String> delegateCallingConventions) {
// do nothing.
}
@Override
public String getGlobalObject() {
return "window";
}
@Override
public boolean isAliasingGlobalThis(Node n) {
return CodingConventions.isAliasingGlobalThis(this, n);
}
@Override
public boolean isPropertyTestFunction(Node call) {
return call.getFirstChild().matchesQualifiedName("Array.isArray");
}
@Override
public boolean isPropertyRenameFunction(String name) {
return NodeUtil.JSC_PROPERTY_NAME_FN.equals(name);
}
@Override
public boolean isPrototypeAlias(Node getProp) {
return false;
}
@Override
public ObjectLiteralCast getObjectLiteralCast(Node callNode) {
return null;
}
@Override
public Collection<AssertionFunctionSpec> getAssertionFunctions() {
return Collections.emptySet();
}
@Override
public Bind describeFunctionBind(Node n) {
return describeFunctionBind(n, false, false);
}
@Override
public Bind describeFunctionBind(
Node n, boolean callerChecksTypes, boolean iCheckTypes) {
if (!n.isCall()) {
return null;
}
Node callTarget = n.getFirstChild();
if (callTarget.isQualifiedName()) {
if (callTarget.matchesQualifiedName("Function.prototype.bind.call")) {
// goog.bind(fn, self, args...);
Node fn = callTarget.getNext();
if (fn == null) {
return null;
}
Node thisValue = safeNext(fn);
Node parameters = safeNext(thisValue);
return new Bind(fn, thisValue, parameters);
}
}
if (callTarget.isGetProp()
&& callTarget.getLastChild().getString().equals("bind")) {
Node maybeFn = callTarget.getFirstChild();
com.google.javascript.rhino.jstype.JSType maybeFnType =
maybeFn.getJSType();
FunctionType fnType = null;
if (iCheckTypes && maybeFnType != null) {
fnType = maybeFnType.restrictByNotNullOrUndefined()
.toMaybeFunctionType();
}
if (fnType != null || callerChecksTypes || maybeFn.isFunction()) {
// (function(){}).bind(self, args...);
Node thisValue = callTarget.getNext();
Node parameters = safeNext(thisValue);
return new Bind(maybeFn, thisValue, parameters);
}
}
return null;
}
@Override
public Cache describeCachingCall(Node node) {
return null;
}
@Override
public Collection<String> getIndirectlyDeclaredProperties() {
return ImmutableList.of();
}
private static Node safeNext(Node n) {
if (n != null) {
return n.getNext();
}
return null;
}
}
}