| 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.HashMultimap; |
| 21 |
import com.google.common.collect.Multimap; |
| 22 |
import com.google.javascript.jscomp.NodeTraversal.ScopedCallback; |
| 23 |
import com.google.javascript.jscomp.Scope.Var; |
| 24 |
import com.google.javascript.rhino.JSDocInfo; |
| 25 |
import com.google.javascript.rhino.JSDocInfo.Visibility; |
| 26 |
import com.google.javascript.rhino.Node; |
| 27 |
import com.google.javascript.rhino.Token; |
| 28 |
import com.google.javascript.rhino.jstype.FunctionType; |
| 29 |
import com.google.javascript.rhino.jstype.JSType; |
| 30 |
import com.google.javascript.rhino.jstype.ObjectType; |
| 31 |
import com.google.javascript.rhino.jstype.StaticSourceFile; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
class CheckAccessControls implements ScopedCallback, HotSwapCompilerPass { |
| 47 |
|
| 48 |
static final DiagnosticType DEPRECATED_NAME = DiagnosticType.disabled( |
| 49 |
"JSC_DEPRECATED_VAR", |
| 50 |
"Variable {0} has been deprecated."); |
| 51 |
|
| 52 |
static final DiagnosticType DEPRECATED_NAME_REASON = DiagnosticType.disabled( |
| 53 |
"JSC_DEPRECATED_VAR_REASON", |
| 54 |
"Variable {0} has been deprecated: {1}"); |
| 55 |
|
| 56 |
static final DiagnosticType DEPRECATED_PROP = DiagnosticType.disabled( |
| 57 |
"JSC_DEPRECATED_PROP", |
| 58 |
"Property {0} of type {1} has been deprecated."); |
| 59 |
|
| 60 |
static final DiagnosticType DEPRECATED_PROP_REASON = DiagnosticType.disabled( |
| 61 |
"JSC_DEPRECATED_PROP_REASON", |
| 62 |
"Property {0} of type {1} has been deprecated: {2}"); |
| 63 |
|
| 64 |
static final DiagnosticType DEPRECATED_CLASS = DiagnosticType.disabled( |
| 65 |
"JSC_DEPRECATED_CLASS", |
| 66 |
"Class {0} has been deprecated."); |
| 67 |
|
| 68 |
static final DiagnosticType DEPRECATED_CLASS_REASON = DiagnosticType.disabled( |
| 69 |
"JSC_DEPRECATED_CLASS_REASON", |
| 70 |
"Class {0} has been deprecated: {1}"); |
| 71 |
|
| 72 |
static final DiagnosticType BAD_PRIVATE_GLOBAL_ACCESS = |
| 73 |
DiagnosticType.disabled( |
| 74 |
"JSC_BAD_PRIVATE_GLOBAL_ACCESS", |
| 75 |
"Access to private variable {0} not allowed outside file {1}."); |
| 76 |
|
| 77 |
static final DiagnosticType BAD_PRIVATE_PROPERTY_ACCESS = |
| 78 |
DiagnosticType.disabled( |
| 79 |
"JSC_BAD_PRIVATE_PROPERTY_ACCESS", |
| 80 |
"Access to private property {0} of {1} not allowed here."); |
| 81 |
|
| 82 |
static final DiagnosticType BAD_PROTECTED_PROPERTY_ACCESS = |
| 83 |
DiagnosticType.disabled( |
| 84 |
"JSC_BAD_PROTECTED_PROPERTY_ACCESS", |
| 85 |
"Access to protected property {0} of {1} not allowed here."); |
| 86 |
|
| 87 |
static final DiagnosticType PRIVATE_OVERRIDE = |
| 88 |
DiagnosticType.disabled( |
| 89 |
"JSC_PRIVATE_OVERRIDE", |
| 90 |
"Overriding private property of {0}."); |
| 91 |
|
| 92 |
static final DiagnosticType EXTEND_FINAL_CLASS = |
| 93 |
DiagnosticType.error( |
| 94 |
"JSC_EXTEND_FINAL_CLASS", |
| 95 |
"{0} is not allowed to extend final class {1}."); |
| 96 |
|
| 97 |
static final DiagnosticType VISIBILITY_MISMATCH = |
| 98 |
DiagnosticType.disabled( |
| 99 |
"JSC_VISIBILITY_MISMATCH", |
| 100 |
"Overriding {0} property of {1} with {2} property."); |
| 101 |
|
| 102 |
static final DiagnosticType CONST_PROPERTY_REASSIGNED_VALUE = |
| 103 |
DiagnosticType.warning( |
| 104 |
"JSC_CONSTANT_PROPERTY_REASSIGNED_VALUE", |
| 105 |
"constant property {0} assigned a value more than once"); |
| 106 |
|
| 107 |
static final DiagnosticType CONST_PROPERTY_DELETED = |
| 108 |
DiagnosticType.warning( |
| 109 |
"JSC_CONSTANT_PROPERTY_DELETED", |
| 110 |
"constant property {0} cannot be deleted"); |
| 111 |
|
| 112 |
private final AbstractCompiler compiler; |
| 113 |
private final TypeValidator validator; |
| 114 |
|
| 115 |
|
| 116 |
private int deprecatedDepth = 0; |
| 117 |
private int methodDepth = 0; |
| 118 |
private JSType currentClass = null; |
| 119 |
|
| 120 |
private final Multimap<String, String> initializedConstantProperties; |
| 121 |
|
| 122 |
CheckAccessControls(AbstractCompiler compiler) { |
| 123 |
this.compiler = compiler; |
| 124 |
this.validator = compiler.getTypeValidator(); |
| 125 |
this.initializedConstantProperties = HashMultimap.create(); |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public void process(Node externs, Node root) { |
| 130 |
NodeTraversal.traverse(compiler, root, this); |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public void hotSwapScript(Node scriptRoot, Node originalRoot) { |
| 135 |
NodeTraversal.traverse(compiler, scriptRoot, this); |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public void enterScope(NodeTraversal t) { |
| 140 |
if (!t.inGlobalScope()) { |
| 141 |
Node n = t.getScopeRoot(); |
| 142 |
Node parent = n.getParent(); |
| 143 |
if (isDeprecatedFunction(n)) { |
| 144 |
deprecatedDepth++; |
| 145 |
} |
| 146 |
|
| 147 |
if (methodDepth == 0) { |
| 148 |
currentClass = getClassOfMethod(n, parent); |
| 149 |
} |
| 150 |
methodDepth++; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
public void exitScope(NodeTraversal t) { |
| 156 |
if (!t.inGlobalScope()) { |
| 157 |
Node n = t.getScopeRoot(); |
| 158 |
if (isDeprecatedFunction(n)) { |
| 159 |
deprecatedDepth--; |
| 160 |
} |
| 161 |
|
| 162 |
methodDepth--; |
| 163 |
if (methodDepth == 0) { |
| 164 |
currentClass = null; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
private JSType getClassOfMethod(Node n, Node parent) { |
| 174 |
if (parent.isAssign()) { |
| 175 |
Node lValue = parent.getFirstChild(); |
| 176 |
if (NodeUtil.isGet(lValue)) { |
| 177 |
|
| 178 |
JSType lValueType = lValue.getJSType(); |
| 179 |
if (lValueType != null && lValueType.isNominalConstructor()) { |
| 180 |
|
| 181 |
|
| 182 |
return (lValueType.toMaybeFunctionType()).getInstanceType(); |
| 183 |
} else { |
| 184 |
|
| 185 |
|
| 186 |
return normalizeClassType(lValue.getFirstChild().getJSType()); |
| 187 |
} |
| 188 |
} else { |
| 189 |
|
| 190 |
|
| 191 |
return normalizeClassType(lValue.getJSType()); |
| 192 |
} |
| 193 |
} else if (NodeUtil.isFunctionDeclaration(n) || |
| 194 |
parent.isName()) { |
| 195 |
return normalizeClassType(n.getJSType()); |
| 196 |
} |
| 197 |
|
| 198 |
return null; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
private JSType normalizeClassType(JSType type) { |
| 206 |
if (type == null || type.isUnknownType()) { |
| 207 |
return type; |
| 208 |
} else if (type.isNominalConstructor()) { |
| 209 |
return (type.toMaybeFunctionType()).getInstanceType(); |
| 210 |
} else if (type.isFunctionPrototypeType()) { |
| 211 |
FunctionType owner = ((ObjectType) type).getOwnerFunction(); |
| 212 |
if (owner.isConstructor()) { |
| 213 |
return owner.getInstanceType(); |
| 214 |
} |
| 215 |
} |
| 216 |
return type; |
| 217 |
} |
| 218 |
|
| 219 |
@Override |
| 220 |
public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) { |
| 221 |
return true; |
| 222 |
} |
| 223 |
|
| 224 |
@Override |
| 225 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 226 |
switch (n.getType()) { |
| 227 |
case Token.NAME: |
| 228 |
checkNameDeprecation(t, n, parent); |
| 229 |
checkNameVisibility(t, n, parent); |
| 230 |
break; |
| 231 |
case Token.GETPROP: |
| 232 |
checkPropertyDeprecation(t, n, parent); |
| 233 |
checkPropertyVisibility(t, n, parent); |
| 234 |
checkConstantProperty(t, n); |
| 235 |
break; |
| 236 |
case Token.NEW: |
| 237 |
checkConstructorDeprecation(t, n, parent); |
| 238 |
break; |
| 239 |
case Token.FUNCTION: |
| 240 |
checkFinalClassOverrides(t, n, parent); |
| 241 |
break; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
private void checkConstructorDeprecation(NodeTraversal t, Node n, |
| 249 |
Node parent) { |
| 250 |
JSType type = n.getJSType(); |
| 251 |
|
| 252 |
if (type != null) { |
| 253 |
String deprecationInfo = getTypeDeprecationInfo(type); |
| 254 |
|
| 255 |
if (deprecationInfo != null && |
| 256 |
shouldEmitDeprecationWarning(t, n, parent)) { |
| 257 |
|
| 258 |
if (!deprecationInfo.isEmpty()) { |
| 259 |
compiler.report( |
| 260 |
t.makeError(n, DEPRECATED_CLASS_REASON, |
| 261 |
type.toString(), deprecationInfo)); |
| 262 |
} else { |
| 263 |
compiler.report( |
| 264 |
t.makeError(n, DEPRECATED_CLASS, type.toString())); |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
private void checkNameDeprecation(NodeTraversal t, Node n, Node parent) { |
| 274 |
|
| 275 |
if (parent.isFunction() || parent.isVar() || |
| 276 |
parent.isNew()) { |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
Scope.Var var = t.getScope().getVar(n.getString()); |
| 281 |
JSDocInfo docInfo = var == null ? null : var.getJSDocInfo(); |
| 282 |
|
| 283 |
if (docInfo != null && docInfo.isDeprecated() && |
| 284 |
shouldEmitDeprecationWarning(t, n, parent)) { |
| 285 |
|
| 286 |
if (docInfo.getDeprecationReason() != null) { |
| 287 |
compiler.report( |
| 288 |
t.makeError(n, DEPRECATED_NAME_REASON, n.getString(), |
| 289 |
docInfo.getDeprecationReason())); |
| 290 |
} else { |
| 291 |
compiler.report( |
| 292 |
t.makeError(n, DEPRECATED_NAME, n.getString())); |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
private void checkPropertyDeprecation(NodeTraversal t, Node n, Node parent) { |
| 302 |
|
| 303 |
if (parent.isNew()) { |
| 304 |
return; |
| 305 |
} |
| 306 |
|
| 307 |
ObjectType objectType = |
| 308 |
ObjectType.cast(dereference(n.getFirstChild().getJSType())); |
| 309 |
String propertyName = n.getLastChild().getString(); |
| 310 |
|
| 311 |
if (objectType != null) { |
| 312 |
String deprecationInfo |
| 313 |
= getPropertyDeprecationInfo(objectType, propertyName); |
| 314 |
|
| 315 |
if (deprecationInfo != null && |
| 316 |
shouldEmitDeprecationWarning(t, n, parent)) { |
| 317 |
|
| 318 |
if (!deprecationInfo.isEmpty()) { |
| 319 |
compiler.report( |
| 320 |
t.makeError(n, DEPRECATED_PROP_REASON, propertyName, |
| 321 |
validator.getReadableJSTypeName(n.getFirstChild(), true), |
| 322 |
deprecationInfo)); |
| 323 |
} else { |
| 324 |
compiler.report( |
| 325 |
t.makeError(n, DEPRECATED_PROP, propertyName, |
| 326 |
validator.getReadableJSTypeName(n.getFirstChild(), true))); |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
private void checkNameVisibility(NodeTraversal t, Node name, Node parent) { |
| 338 |
Var var = t.getScope().getVar(name.getString()); |
| 339 |
if (var != null) { |
| 340 |
JSDocInfo docInfo = var.getJSDocInfo(); |
| 341 |
if (docInfo != null) { |
| 342 |
|
| 343 |
Visibility visibility = docInfo.getVisibility(); |
| 344 |
if (visibility == Visibility.PRIVATE) { |
| 345 |
StaticSourceFile varSrc = var.getSourceFile(); |
| 346 |
StaticSourceFile refSrc = name.getStaticSourceFile(); |
| 347 |
if (varSrc != null && |
| 348 |
refSrc != null && |
| 349 |
!varSrc.getName().equals(refSrc.getName())) { |
| 350 |
if (docInfo.isConstructor() && |
| 351 |
isValidPrivateConstructorAccess(parent)) { |
| 352 |
return; |
| 353 |
} |
| 354 |
|
| 355 |
compiler.report( |
| 356 |
t.makeError(name, BAD_PRIVATE_GLOBAL_ACCESS, |
| 357 |
name.getString(), varSrc.getName())); |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
private void checkFinalClassOverrides(NodeTraversal t, Node fn, Node parent) { |
| 368 |
JSType type = fn.getJSType().toMaybeFunctionType(); |
| 369 |
if (type != null && type.isConstructor()) { |
| 370 |
JSType finalParentClass = getFinalParentClass(getClassOfMethod(fn, parent)); |
| 371 |
if (finalParentClass != null) { |
| 372 |
compiler.report( |
| 373 |
t.makeError(fn, EXTEND_FINAL_CLASS, |
| 374 |
type.getDisplayName(), finalParentClass.getDisplayName())); |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
private void checkConstantProperty(NodeTraversal t, |
| 385 |
Node getprop) { |
| 386 |
|
| 387 |
Node parent = getprop.getParent(); |
| 388 |
boolean isDelete = parent.isDelProp(); |
| 389 |
if (!(NodeUtil.isAssignmentOp(parent) && parent.getFirstChild() == getprop) |
| 390 |
&& !parent.isInc() && !parent.isDec() |
| 391 |
&& !isDelete) { |
| 392 |
return; |
| 393 |
} |
| 394 |
|
| 395 |
ObjectType objectType = |
| 396 |
ObjectType.cast(dereference(getprop.getFirstChild().getJSType())); |
| 397 |
String propertyName = getprop.getLastChild().getString(); |
| 398 |
|
| 399 |
boolean isConstant = isPropertyDeclaredConstant(objectType, propertyName); |
| 400 |
|
| 401 |
|
| 402 |
if (isConstant) { |
| 403 |
if (isDelete) { |
| 404 |
compiler.report( |
| 405 |
t.makeError(getprop, CONST_PROPERTY_DELETED, propertyName)); |
| 406 |
return; |
| 407 |
} |
| 408 |
|
| 409 |
ObjectType oType = objectType; |
| 410 |
while (oType != null) { |
| 411 |
if (oType.hasReferenceName()) { |
| 412 |
if (initializedConstantProperties.containsEntry( |
| 413 |
oType.getReferenceName(), propertyName)) { |
| 414 |
compiler.report( |
| 415 |
t.makeError(getprop, CONST_PROPERTY_REASSIGNED_VALUE, |
| 416 |
propertyName)); |
| 417 |
break; |
| 418 |
} |
| 419 |
} |
| 420 |
oType = oType.getImplicitPrototype(); |
| 421 |
} |
| 422 |
|
| 423 |
Preconditions.checkState(objectType.hasReferenceName()); |
| 424 |
initializedConstantProperties.put(objectType.getReferenceName(), |
| 425 |
propertyName); |
| 426 |
|
| 427 |
|
| 428 |
if (objectType.isInstanceType()) { |
| 429 |
ObjectType prototype = objectType.getImplicitPrototype(); |
| 430 |
if (prototype != null) { |
| 431 |
if (prototype.hasProperty(propertyName) |
| 432 |
&& prototype.hasReferenceName()) { |
| 433 |
initializedConstantProperties.put(prototype.getReferenceName(), |
| 434 |
propertyName); |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
private void checkPropertyVisibility(NodeTraversal t, |
| 447 |
Node getprop, Node parent) { |
| 448 |
ObjectType objectType = |
| 449 |
ObjectType.cast(dereference(getprop.getFirstChild().getJSType())); |
| 450 |
String propertyName = getprop.getLastChild().getString(); |
| 451 |
|
| 452 |
if (objectType != null) { |
| 453 |
|
| 454 |
|
| 455 |
boolean isOverride = parent.getJSDocInfo() != null && |
| 456 |
parent.isAssign() && |
| 457 |
parent.getFirstChild() == getprop; |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
if (isOverride) { |
| 462 |
objectType = objectType.getImplicitPrototype(); |
| 463 |
} |
| 464 |
JSDocInfo docInfo = null; |
| 465 |
for (; objectType != null; |
| 466 |
objectType = objectType.getImplicitPrototype()) { |
| 467 |
docInfo = objectType.getOwnPropertyJSDocInfo(propertyName); |
| 468 |
if (docInfo != null && |
| 469 |
docInfo.getVisibility() != Visibility.INHERITED) { |
| 470 |
break; |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
if (objectType == null) { |
| 475 |
|
| 476 |
return; |
| 477 |
} |
| 478 |
|
| 479 |
String referenceSource = getprop.getSourceFileName(); |
| 480 |
String definingSource = docInfo.getSourceName(); |
| 481 |
boolean sameInput = referenceSource != null |
| 482 |
&& referenceSource.equals(definingSource); |
| 483 |
Visibility visibility = docInfo.getVisibility(); |
| 484 |
JSType ownerType = normalizeClassType(objectType); |
| 485 |
if (isOverride) { |
| 486 |
|
| 487 |
|
| 488 |
JSDocInfo overridingInfo = parent.getJSDocInfo(); |
| 489 |
Visibility overridingVisibility = overridingInfo == null ? |
| 490 |
Visibility.INHERITED : overridingInfo.getVisibility(); |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
if (visibility == Visibility.PRIVATE && !sameInput) { |
| 496 |
compiler.report( |
| 497 |
t.makeError(getprop, PRIVATE_OVERRIDE, |
| 498 |
objectType.toString())); |
| 499 |
} else if (overridingVisibility != Visibility.INHERITED && |
| 500 |
overridingVisibility != visibility) { |
| 501 |
compiler.report( |
| 502 |
t.makeError(getprop, VISIBILITY_MISMATCH, |
| 503 |
visibility.name(), objectType.toString(), |
| 504 |
overridingVisibility.name())); |
| 505 |
} |
| 506 |
} else { |
| 507 |
if (sameInput) { |
| 508 |
|
| 509 |
return; |
| 510 |
} else if (visibility == Visibility.PRIVATE && |
| 511 |
(currentClass == null || !ownerType.isEquivalentTo(currentClass))) { |
| 512 |
if (docInfo.isConstructor() && |
| 513 |
isValidPrivateConstructorAccess(parent)) { |
| 514 |
return; |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
compiler.report( |
| 520 |
t.makeError(getprop, |
| 521 |
BAD_PRIVATE_PROPERTY_ACCESS, |
| 522 |
propertyName, |
| 523 |
validator.getReadableJSTypeName( |
| 524 |
getprop.getFirstChild(), true))); |
| 525 |
} else if (visibility == Visibility.PROTECTED) { |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
if (currentClass == null || !currentClass.isSubtype(ownerType)) { |
| 532 |
compiler.report( |
| 533 |
t.makeError(getprop, BAD_PROTECTED_PROPERTY_ACCESS, |
| 534 |
propertyName, |
| 535 |
validator.getReadableJSTypeName( |
| 536 |
getprop.getFirstChild(), true))); |
| 537 |
} |
| 538 |
} |
| 539 |
} |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
private static boolean isValidPrivateConstructorAccess(Node parent) { |
| 558 |
return !parent.isNew(); |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
private boolean shouldEmitDeprecationWarning( |
| 568 |
NodeTraversal t, Node n, Node parent) { |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
if (t.inGlobalScope()) { |
| 575 |
if (!((parent.isCall() && parent.getFirstChild() == n) || |
| 576 |
n.isNew())) { |
| 577 |
return false; |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
if (n.isGetProp() && n == parent.getFirstChild() && |
| 583 |
NodeUtil.isAssignmentOp(parent)) { |
| 584 |
return false; |
| 585 |
} |
| 586 |
|
| 587 |
return !canAccessDeprecatedTypes(t); |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
private boolean canAccessDeprecatedTypes(NodeTraversal t) { |
| 601 |
Node scopeRoot = t.getScopeRoot(); |
| 602 |
Node scopeRootParent = scopeRoot.getParent(); |
| 603 |
return |
| 604 |
|
| 605 |
(deprecatedDepth > 0) || |
| 606 |
|
| 607 |
(getTypeDeprecationInfo(t.getScope().getTypeOfThis()) != null) || |
| 608 |
|
| 609 |
(scopeRootParent != null && scopeRootParent.isAssign() && |
| 610 |
getTypeDeprecationInfo( |
| 611 |
getClassOfMethod(scopeRoot, scopeRootParent)) != null); |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
private static boolean isDeprecatedFunction(Node n) { |
| 618 |
if (n.isFunction()) { |
| 619 |
JSType type = n.getJSType(); |
| 620 |
if (type != null) { |
| 621 |
return getTypeDeprecationInfo(type) != null; |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
return false; |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
private static String getTypeDeprecationInfo(JSType type) { |
| 634 |
if (type == null) { |
| 635 |
return null; |
| 636 |
} |
| 637 |
|
| 638 |
JSDocInfo info = type.getJSDocInfo(); |
| 639 |
if (info != null && info.isDeprecated()) { |
| 640 |
if (info.getDeprecationReason() != null) { |
| 641 |
return info.getDeprecationReason(); |
| 642 |
} |
| 643 |
return ""; |
| 644 |
} |
| 645 |
ObjectType objType = ObjectType.cast(type); |
| 646 |
if (objType != null) { |
| 647 |
ObjectType implicitProto = objType.getImplicitPrototype(); |
| 648 |
if (implicitProto != null) { |
| 649 |
return getTypeDeprecationInfo(implicitProto); |
| 650 |
} |
| 651 |
} |
| 652 |
return null; |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
private static boolean isPropertyDeclaredConstant( |
| 659 |
ObjectType objectType, String prop) { |
| 660 |
for (; |
| 661 |
|
| 662 |
objectType != null && objectType.hasReferenceName(); |
| 663 |
|
| 664 |
objectType = objectType.getImplicitPrototype()) { |
| 665 |
JSDocInfo docInfo = objectType.getOwnPropertyJSDocInfo(prop); |
| 666 |
if (docInfo != null && docInfo.isConstant()) { |
| 667 |
return true; |
| 668 |
} |
| 669 |
} |
| 670 |
return false; |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
private static String getPropertyDeprecationInfo(ObjectType type, |
| 679 |
String prop) { |
| 680 |
JSDocInfo info = type.getOwnPropertyJSDocInfo(prop); |
| 681 |
if (info != null && info.isDeprecated()) { |
| 682 |
if (info.getDeprecationReason() != null) { |
| 683 |
return info.getDeprecationReason(); |
| 684 |
} |
| 685 |
|
| 686 |
return ""; |
| 687 |
} |
| 688 |
ObjectType implicitProto = type.getImplicitPrototype(); |
| 689 |
if (implicitProto != null) { |
| 690 |
return getPropertyDeprecationInfo(implicitProto, prop); |
| 691 |
} |
| 692 |
return null; |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
private static JSType dereference(JSType type) { |
| 699 |
return type == null ? null : type.dereference(); |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
private JSType getFinalParentClass(JSType type) { |
| 706 |
if (type != null) { |
| 707 |
ObjectType iproto = ObjectType.cast(type).getImplicitPrototype(); |
| 708 |
while (iproto != null && iproto.getConstructor() == null) { |
| 709 |
iproto = iproto.getImplicitPrototype(); |
| 710 |
} |
| 711 |
if (iproto != null) { |
| 712 |
Node source = iproto.getConstructor().getSource(); |
| 713 |
JSDocInfo jsDoc = source != null ? NodeUtil.getBestJSDocInfo(source) : null; |
| 714 |
if (jsDoc != null && jsDoc.isConstant()) { |
| 715 |
return iproto; |
| 716 |
} |
| 717 |
} |
| 718 |
} |
| 719 |
return null; |
| 720 |
} |
| 721 |
} |