| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.base.Preconditions; |
| 20 |
import com.google.common.collect.ImmutableList; |
| 21 |
import com.google.javascript.rhino.Node; |
| 22 |
import com.google.javascript.rhino.jstype.FunctionType; |
| 23 |
import com.google.javascript.rhino.jstype.JSType; |
| 24 |
import com.google.javascript.rhino.jstype.JSTypeRegistry; |
| 25 |
import com.google.javascript.rhino.jstype.ObjectType; |
| 26 |
import com.google.javascript.rhino.jstype.StaticScope; |
| 27 |
|
| 28 |
import java.util.Collection; |
| 29 |
import java.util.Collections; |
| 30 |
import java.util.List; |
| 31 |
import java.util.Map; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class CodingConventions { |
| 37 |
|
| 38 |
private CodingConventions() {} |
| 39 |
|
| 40 |
|
| 41 |
public static CodingConvention getDefault() { |
| 42 |
return new DefaultCodingConvention(); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public static class Proxy implements CodingConvention { |
| 56 |
|
| 57 |
protected final CodingConvention nextConvention; |
| 58 |
|
| 59 |
protected Proxy(CodingConvention convention) { |
| 60 |
this.nextConvention = convention; |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public boolean isConstant(String variableName) { |
| 65 |
return nextConvention.isConstant(variableName); |
| 66 |
} |
| 67 |
|
| 68 |
@Override public boolean isConstantKey(String keyName) { |
| 69 |
return nextConvention.isConstantKey(keyName); |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public boolean isValidEnumKey(String key) { |
| 74 |
return nextConvention.isValidEnumKey(key); |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public boolean isOptionalParameter(Node parameter) { |
| 79 |
return nextConvention.isOptionalParameter(parameter); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public boolean isVarArgsParameter(Node parameter) { |
| 84 |
return nextConvention.isVarArgsParameter(parameter); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public boolean isExported(String name, boolean local) { |
| 89 |
return nextConvention.isExported(name, local); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
@Override |
| 94 |
public final boolean isExported(String name) { |
| 95 |
return isExported(name, false) || isExported(name, true); |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public boolean isPrivate(String name) { |
| 100 |
return nextConvention.isPrivate(name); |
| 101 |
} |
| 102 |
|
| 103 |
@Override |
| 104 |
public SubclassRelationship getClassesDefinedByCall(Node callNode) { |
| 105 |
return nextConvention.getClassesDefinedByCall(callNode); |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public boolean isSuperClassReference(String propertyName) { |
| 110 |
return nextConvention.isSuperClassReference(propertyName); |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public String extractClassNameIfProvide(Node node, Node parent) { |
| 115 |
return nextConvention.extractClassNameIfProvide(node, parent); |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public String extractClassNameIfRequire(Node node, Node parent) { |
| 120 |
return nextConvention.extractClassNameIfRequire(node, parent); |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public String getExportPropertyFunction() { |
| 125 |
return nextConvention.getExportPropertyFunction(); |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public String getExportSymbolFunction() { |
| 130 |
return nextConvention.getExportSymbolFunction(); |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public List<String> identifyTypeDeclarationCall(Node n) { |
| 135 |
return nextConvention.identifyTypeDeclarationCall(n); |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public void applySubclassRelationship(FunctionType parentCtor, |
| 140 |
FunctionType childCtor, SubclassType type) { |
| 141 |
nextConvention.applySubclassRelationship( |
| 142 |
parentCtor, childCtor, type); |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
public String getAbstractMethodName() { |
| 147 |
return nextConvention.getAbstractMethodName(); |
| 148 |
} |
| 149 |
|
| 150 |
@Override |
| 151 |
public String getSingletonGetterClassName(Node callNode) { |
| 152 |
return nextConvention.getSingletonGetterClassName(callNode); |
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
public void applySingletonGetter(FunctionType functionType, |
| 157 |
FunctionType getterType, ObjectType objectType) { |
| 158 |
nextConvention.applySingletonGetter( |
| 159 |
functionType, getterType, objectType); |
| 160 |
} |
| 161 |
|
| 162 |
@Override |
| 163 |
public boolean isInlinableFunction(Node n) { |
| 164 |
return nextConvention.isInlinableFunction(n); |
| 165 |
} |
| 166 |
|
| 167 |
@Override |
| 168 |
public DelegateRelationship getDelegateRelationship(Node callNode) { |
| 169 |
return nextConvention.getDelegateRelationship(callNode); |
| 170 |
} |
| 171 |
|
| 172 |
@Override |
| 173 |
public void applyDelegateRelationship( |
| 174 |
ObjectType delegateSuperclass, ObjectType delegateBase, |
| 175 |
ObjectType delegator, FunctionType delegateProxy, |
| 176 |
FunctionType findDelegate) { |
| 177 |
nextConvention.applyDelegateRelationship( |
| 178 |
delegateSuperclass, delegateBase, delegator, |
| 179 |
delegateProxy, findDelegate); |
| 180 |
} |
| 181 |
|
| 182 |
@Override |
| 183 |
public String getDelegateSuperclassName() { |
| 184 |
return nextConvention.getDelegateSuperclassName(); |
| 185 |
} |
| 186 |
|
| 187 |
@Override |
| 188 |
public void checkForCallingConventionDefiningCalls( |
| 189 |
Node n, Map<String, String> delegateCallingConventions) { |
| 190 |
nextConvention.checkForCallingConventionDefiningCalls( |
| 191 |
n, delegateCallingConventions); |
| 192 |
} |
| 193 |
|
| 194 |
@Override |
| 195 |
public void defineDelegateProxyPrototypeProperties( |
| 196 |
JSTypeRegistry registry, StaticScope<JSType> scope, |
| 197 |
List<ObjectType> delegateProxyPrototypes, |
| 198 |
Map<String, String> delegateCallingConventions) { |
| 199 |
nextConvention.defineDelegateProxyPrototypeProperties( |
| 200 |
registry, scope, delegateProxyPrototypes, delegateCallingConventions); |
| 201 |
} |
| 202 |
|
| 203 |
@Override |
| 204 |
public String getGlobalObject() { |
| 205 |
return nextConvention.getGlobalObject(); |
| 206 |
} |
| 207 |
|
| 208 |
@Override |
| 209 |
public Collection<AssertionFunctionSpec> getAssertionFunctions() { |
| 210 |
return nextConvention.getAssertionFunctions(); |
| 211 |
} |
| 212 |
|
| 213 |
@Override |
| 214 |
public Bind describeFunctionBind(Node n) { |
| 215 |
return describeFunctionBind(n, false); |
| 216 |
} |
| 217 |
|
| 218 |
@Override |
| 219 |
public Bind describeFunctionBind(Node n, boolean useTypeInfo) { |
| 220 |
return nextConvention.describeFunctionBind(n, useTypeInfo); |
| 221 |
} |
| 222 |
|
| 223 |
@Override |
| 224 |
public boolean isPropertyTestFunction(Node call) { |
| 225 |
return nextConvention.isPropertyTestFunction(call); |
| 226 |
} |
| 227 |
|
| 228 |
@Override |
| 229 |
public boolean isPrototypeAlias(Node getProp) { |
| 230 |
return false; |
| 231 |
} |
| 232 |
|
| 233 |
@Override |
| 234 |
public ObjectLiteralCast getObjectLiteralCast(Node callNode) { |
| 235 |
return nextConvention.getObjectLiteralCast(callNode); |
| 236 |
} |
| 237 |
|
| 238 |
@Override |
| 239 |
public Collection<String> getIndirectlyDeclaredProperties() { |
| 240 |
return nextConvention.getIndirectlyDeclaredProperties(); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
private static class DefaultCodingConvention implements CodingConvention { |
| 250 |
|
| 251 |
private static final long serialVersionUID = 1L; |
| 252 |
|
| 253 |
@Override |
| 254 |
public boolean isConstant(String variableName) { |
| 255 |
return false; |
| 256 |
} |
| 257 |
|
| 258 |
@Override |
| 259 |
public boolean isConstantKey(String variableName) { |
| 260 |
return false; |
| 261 |
} |
| 262 |
|
| 263 |
@Override |
| 264 |
public boolean isValidEnumKey(String key) { |
| 265 |
return key != null && key.length() > 0; |
| 266 |
} |
| 267 |
|
| 268 |
@Override |
| 269 |
public boolean isOptionalParameter(Node parameter) { |
| 270 |
|
| 271 |
|
| 272 |
return false; |
| 273 |
} |
| 274 |
|
| 275 |
@Override |
| 276 |
public boolean isVarArgsParameter(Node parameter) { |
| 277 |
|
| 278 |
return false; |
| 279 |
} |
| 280 |
|
| 281 |
@Override |
| 282 |
public boolean isExported(String name, boolean local) { |
| 283 |
return local && name.startsWith("$super"); |
| 284 |
} |
| 285 |
|
| 286 |
@Override |
| 287 |
public boolean isExported(String name) { |
| 288 |
return isExported(name, false) || isExported(name, true); |
| 289 |
} |
| 290 |
|
| 291 |
@Override |
| 292 |
public boolean isPrivate(String name) { |
| 293 |
return false; |
| 294 |
} |
| 295 |
|
| 296 |
@Override |
| 297 |
public SubclassRelationship getClassesDefinedByCall(Node callNode) { |
| 298 |
return null; |
| 299 |
} |
| 300 |
|
| 301 |
@Override |
| 302 |
public boolean isSuperClassReference(String propertyName) { |
| 303 |
return false; |
| 304 |
} |
| 305 |
|
| 306 |
@Override |
| 307 |
public String extractClassNameIfProvide(Node node, Node parent) { |
| 308 |
String message = "only implemented in GoogleCodingConvention"; |
| 309 |
throw new UnsupportedOperationException(message); |
| 310 |
} |
| 311 |
|
| 312 |
@Override |
| 313 |
public String extractClassNameIfRequire(Node node, Node parent) { |
| 314 |
String message = "only implemented in GoogleCodingConvention"; |
| 315 |
throw new UnsupportedOperationException(message); |
| 316 |
} |
| 317 |
|
| 318 |
@Override |
| 319 |
public String getExportPropertyFunction() { |
| 320 |
return null; |
| 321 |
} |
| 322 |
|
| 323 |
@Override |
| 324 |
public String getExportSymbolFunction() { |
| 325 |
return null; |
| 326 |
} |
| 327 |
|
| 328 |
@Override |
| 329 |
public List<String> identifyTypeDeclarationCall(Node n) { |
| 330 |
return null; |
| 331 |
} |
| 332 |
|
| 333 |
@Override |
| 334 |
public void applySubclassRelationship(FunctionType parentCtor, |
| 335 |
FunctionType childCtor, SubclassType type) { |
| 336 |
|
| 337 |
} |
| 338 |
|
| 339 |
@Override |
| 340 |
public String getAbstractMethodName() { |
| 341 |
return null; |
| 342 |
} |
| 343 |
|
| 344 |
@Override |
| 345 |
public String getSingletonGetterClassName(Node callNode) { |
| 346 |
return null; |
| 347 |
} |
| 348 |
|
| 349 |
@Override |
| 350 |
public void applySingletonGetter(FunctionType functionType, |
| 351 |
FunctionType getterType, ObjectType objectType) { |
| 352 |
|
| 353 |
} |
| 354 |
|
| 355 |
@Override |
| 356 |
public boolean isInlinableFunction(Node n) { |
| 357 |
Preconditions.checkState(n.isFunction()); |
| 358 |
return true; |
| 359 |
} |
| 360 |
|
| 361 |
@Override |
| 362 |
public DelegateRelationship getDelegateRelationship(Node callNode) { |
| 363 |
return null; |
| 364 |
} |
| 365 |
|
| 366 |
@Override |
| 367 |
public void applyDelegateRelationship( |
| 368 |
ObjectType delegateSuperclass, ObjectType delegateBase, |
| 369 |
ObjectType delegator, FunctionType delegateProxy, |
| 370 |
FunctionType findDelegate) { |
| 371 |
|
| 372 |
} |
| 373 |
|
| 374 |
@Override |
| 375 |
public String getDelegateSuperclassName() { |
| 376 |
return null; |
| 377 |
} |
| 378 |
|
| 379 |
@Override |
| 380 |
public void checkForCallingConventionDefiningCalls(Node n, |
| 381 |
Map<String, String> delegateCallingConventions) { |
| 382 |
|
| 383 |
} |
| 384 |
|
| 385 |
@Override |
| 386 |
public void defineDelegateProxyPrototypeProperties( |
| 387 |
JSTypeRegistry registry, StaticScope<JSType> scope, |
| 388 |
List<ObjectType> delegateProxyPrototypes, |
| 389 |
Map<String, String> delegateCallingConventions) { |
| 390 |
|
| 391 |
} |
| 392 |
|
| 393 |
@Override |
| 394 |
public String getGlobalObject() { |
| 395 |
return "window"; |
| 396 |
} |
| 397 |
|
| 398 |
@Override |
| 399 |
public boolean isPropertyTestFunction(Node call) { |
| 400 |
return false; |
| 401 |
} |
| 402 |
|
| 403 |
@Override |
| 404 |
public boolean isPrototypeAlias(Node getProp) { |
| 405 |
return false; |
| 406 |
} |
| 407 |
|
| 408 |
@Override |
| 409 |
public ObjectLiteralCast getObjectLiteralCast(Node callNode) { |
| 410 |
return null; |
| 411 |
} |
| 412 |
|
| 413 |
@Override |
| 414 |
public Collection<AssertionFunctionSpec> getAssertionFunctions() { |
| 415 |
return Collections.emptySet(); |
| 416 |
} |
| 417 |
|
| 418 |
@Override |
| 419 |
public Bind describeFunctionBind(Node n) { |
| 420 |
return describeFunctionBind(n, false); |
| 421 |
} |
| 422 |
|
| 423 |
@Override |
| 424 |
public Bind describeFunctionBind(Node n, boolean useTypeInfo) { |
| 425 |
if (!n.isCall()) { |
| 426 |
return null; |
| 427 |
} |
| 428 |
|
| 429 |
Node callTarget = n.getFirstChild(); |
| 430 |
String name = callTarget.getQualifiedName(); |
| 431 |
if (name != null) { |
| 432 |
if (name.equals("Function.prototype.bind.call")) { |
| 433 |
|
| 434 |
Node fn = callTarget.getNext(); |
| 435 |
if (fn == null) { |
| 436 |
return null; |
| 437 |
} |
| 438 |
Node thisValue = safeNext(fn); |
| 439 |
Node parameters = safeNext(thisValue); |
| 440 |
return new Bind(fn, thisValue, parameters); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
if (callTarget.isGetProp() |
| 445 |
&& callTarget.getLastChild().getString().equals("bind")) { |
| 446 |
Node maybeFn = callTarget.getFirstChild(); |
| 447 |
JSType maybeFnType = maybeFn.getJSType(); |
| 448 |
FunctionType fnType = null; |
| 449 |
if (useTypeInfo && maybeFnType != null) { |
| 450 |
fnType = maybeFnType.restrictByNotNullOrUndefined() |
| 451 |
.toMaybeFunctionType(); |
| 452 |
} |
| 453 |
|
| 454 |
if (fnType != null || maybeFn.isFunction()) { |
| 455 |
|
| 456 |
Node thisValue = callTarget.getNext(); |
| 457 |
Node parameters = safeNext(thisValue); |
| 458 |
return new Bind(maybeFn, thisValue, parameters); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
return null; |
| 463 |
} |
| 464 |
|
| 465 |
@Override |
| 466 |
public Collection<String> getIndirectlyDeclaredProperties() { |
| 467 |
return ImmutableList.of(); |
| 468 |
} |
| 469 |
|
| 470 |
private Node safeNext(Node n) { |
| 471 |
if (n != null) { |
| 472 |
return n.getNext(); |
| 473 |
} |
| 474 |
return null; |
| 475 |
} |
| 476 |
} |
| 477 |
} |