Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2008 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
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 * A compiler pass that checks that the programmer has obeyed all the access
35 * control restrictions indicated by JSDoc annotations, like
36 * {@code @private} and {@code @deprecated}.
37 *
38 * Because access control restrictions are attached to type information,
39 * it's important that TypedScopeCreator, TypeInference, and InferJSDocInfo
40 * all run before this pass. TypedScopeCreator creates and resolves types,
41 * TypeInference propagates those types across the AST, and InferJSDocInfo
42 * propagates JSDoc across the types.
43 *
44 * @author [email protected] (Nick Santos)
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 // State about the current traversal.
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 * Gets the type of the class that "owns" a method, or null if
171 * we know that its un-owned.
172 */
173 private JSType getClassOfMethod(Node n, Node parent) {
174 if (parent.isAssign()) {
175 Node lValue = parent.getFirstChild();
176 if (NodeUtil.isGet(lValue)) {
177 // We have an assignment of the form "a.b = ...".
178 JSType lValueType = lValue.getJSType();
179 if (lValueType != null && lValueType.isNominalConstructor()) {
180 // If a.b is a constructor, then everything in this function
181 // belongs to the "a.b" type.
182 return (lValueType.toMaybeFunctionType()).getInstanceType();
183 } else {
184 // If a.b is not a constructor, then treat this as a method
185 // of whatever type is on "a".
186 return normalizeClassType(lValue.getFirstChild().getJSType());
187 }
188 } else {
189 // We have an assignment of the form "a = ...", so pull the
190 // type off the "a".
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 * Normalize the type of a constructor, its instance, and its prototype
203 * all down to the same type (the instance type).
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 * Checks the given NEW node to ensure that access restrictions are obeyed.
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 * Checks the given NAME node to ensure that access restrictions are obeyed.
272 */
273 private void checkNameDeprecation(NodeTraversal t, Node n, Node parent) {
274 // Don't bother checking definitions or constructors.
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 * Checks the given GETPROP node to ensure that access restrictions are
299 * obeyed.
300 */
301 private void checkPropertyDeprecation(NodeTraversal t, Node n, Node parent) {
302 // Don't bother checking constructors.
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 * Determines whether the given name is visible in the current context.
334 * @param t The current traversal.
335 * @param name The name node.
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 // If a name is private, make sure that we're in the same file.
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 * Checks if a constructor is trying to override a final class.
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 * Determines whether the given property with @const tag got reassigned
381 * @param t The current traversal.
382 * @param getprop The getprop node.
383 */
384 private void checkConstantProperty(NodeTraversal t,
385 Node getprop) {
386 // Check whether the property is modified
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 // Check whether constant properties are reassigned
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 // Add the prototype when we're looking at an instance object
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 * Determines whether the given property is visible in the current context.
443 * @param t The current traversal.
444 * @param getprop The getprop node.
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 // Is this a normal property access, or are we trying to override
454 // an existing property?
455 boolean isOverride = parent.getJSDocInfo() != null &&
456 parent.isAssign() &&
457 parent.getFirstChild() == getprop;
458
459 // Find the lowest property defined on a class with visibility
460 // information.
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 // We couldn't find a visibility modifier; assume it's public.
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 // Check an ASSIGN statement that's trying to override a property
487 // on a superclass.
488 JSDocInfo overridingInfo = parent.getJSDocInfo();
489 Visibility overridingVisibility = overridingInfo == null ?
490 Visibility.INHERITED : overridingInfo.getVisibility();
491
492 // Check that (a) the property *can* be overridden, and
493 // (b) that the visibility of the override is the same as the
494 // visibility of the original property.
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 // private access is always allowed in the same file.
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 // private access is not allowed outside the file from a different
518 // enclosing class.
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 // There are 3 types of legal accesses of a protected property:
527 // 1) Accesses in the same file
528 // 2) Overriding the property in a subclass
529 // 3) Accessing the property from inside a subclass
530 // The first two have already been checked for.
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 * Whether the given access of a private constructor is legal.
545 *
546 * For example,
547 * new PrivateCtor_(); // not legal
548 * PrivateCtor_.newInstance(); // legal
549 * x instanceof PrivateCtor_ // legal
550 *
551 * This is a weird special case, because our visibility system is inherited
552 * from Java, and JavaScript has no distinction between classes and
553 * constructors like Java does.
554 *
555 * We may want to revisit this if we decide to make the restrictions tighter.
556 */
557 private static boolean isValidPrivateConstructorAccess(Node parent) {
558 return !parent.isNew();
559 }
560
561 /**
562 * Determines whether a deprecation warning should be emitted.
563 * @param t The current traversal.
564 * @param n The node which we are checking.
565 * @param parent The parent of the node which we are checking.
566 */
567 private boolean shouldEmitDeprecationWarning(
568 NodeTraversal t, Node n, Node parent) {
569 // In the global scope, there are only two kinds of accesses that should
570 // be flagged for warnings:
571 // 1) Calls of deprecated functions and methods.
572 // 2) Instantiations of deprecated classes.
573 // For now, we just let everything else by.
574 if (t.inGlobalScope()) {
575 if (!((parent.isCall() && parent.getFirstChild() == n) ||
576 n.isNew())) {
577 return false;
578 }
579 }
580
581 // We can always assign to a deprecated property, to keep it up to date.
582 if (n.isGetProp() && n == parent.getFirstChild() &&
583 NodeUtil.isAssignmentOp(parent)) {
584 return false;
585 }
586
587 return !canAccessDeprecatedTypes(t);
588 }
589
590 /**
591 * Returns whether it's currently OK to access deprecated names and
592 * properties.
593 *
594 * There are 3 exceptions when we're allowed to use a deprecated
595 * type or property:
596 * 1) When we're in a deprecated function.
597 * 2) When we're in a deprecated class.
598 * 3) When we're in a static method of a deprecated class.
599 */
600 private boolean canAccessDeprecatedTypes(NodeTraversal t) {
601 Node scopeRoot = t.getScopeRoot();
602 Node scopeRootParent = scopeRoot.getParent();
603 return
604 // Case #1
605 (deprecatedDepth > 0) ||
606 // Case #2
607 (getTypeDeprecationInfo(t.getScope().getTypeOfThis()) != null) ||
608 // Case #3
609 (scopeRootParent != null && scopeRootParent.isAssign() &&
610 getTypeDeprecationInfo(
611 getClassOfMethod(scopeRoot, scopeRootParent)) != null);
612 }
613
614 /**
615 * Returns whether this is a function node annotated as deprecated.
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 * Returns the deprecation reason for the type if it is marked
630 * as being deprecated. Returns empty string if the type is deprecated
631 * but no reason was given. Returns null if the type is not deprecated.
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 * Returns if a property is declared constant.
657 */
658 private static boolean isPropertyDeclaredConstant(
659 ObjectType objectType, String prop) {
660 for (;
661 // Only objects with reference names can have constant properties.
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 * Returns the deprecation reason for the property if it is marked
675 * as being deprecated. Returns empty string if the property is deprecated
676 * but no reason was given. Returns null if the property is not deprecated.
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 * Dereference a type, autoboxing it and filtering out null.
697 */
698 private static JSType dereference(JSType type) {
699 return type == null ? null : type.dereference();
700 }
701
702 /**
703 * Returns the super class of the given type that has a constructor.
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 }