diff --git a/src/com/google/javascript/jscomp/GlobalNamespace.java b/src/com/google/javascript/jscomp/GlobalNamespace.java index 3587a918691..3fcdfcf0398 100644 --- a/src/com/google/javascript/jscomp/GlobalNamespace.java +++ b/src/com/google/javascript/jscomp/GlobalNamespace.java @@ -42,14 +42,13 @@ import javax.annotation.Nullable; /** - * Builds a global namespace of all the objects and their properties in - * the global scope. Also builds an index of all the references to those names. + * Builds a global namespace of all the objects and their properties in the global scope. Also + * builds an index of all the references to those names. * * @author nicksantos@google.com (Nick Santos) */ class GlobalNamespace - implements StaticTypedScope, - StaticSymbolTable { + implements StaticTypedScope, StaticSymbolTable { private final AbstractCompiler compiler; private final Node root; @@ -108,7 +107,7 @@ public Node getRootNode() { } @Override - public StaticTypedScope getParentScope() { + public StaticTypedScope getParentScope() { return null; } @@ -135,7 +134,7 @@ public Iterable getReferences(Name slot) { } @Override - public StaticTypedScope getScope(Name slot) { + public StaticTypedScope getScope(Name slot) { return this; } @@ -1008,12 +1007,11 @@ Name getOrCreateName(String name, boolean shouldCreateProp) { // ------------------------------------------------------------------------- /** - * A name defined in global scope (e.g. "a" or "a.b.c.d"). These form a tree. - * As the parse tree traversal proceeds, we'll discover that some names - * correspond to JavaScript objects whose properties we should consider - * collapsing. + * A name defined in global scope (e.g. "a" or "a.b.c.d"). These form a tree. As the parse tree + * traversal proceeds, we'll discover that some names correspond to JavaScript objects whose + * properties we should consider collapsing. */ - static class Name implements StaticTypedSlot { + static class Name implements StaticTypedSlot { enum Type { CLASS, OBJECTLIT, @@ -1111,7 +1109,7 @@ public JSType getType() { } @Override - public StaticTypedScope getScope() { + public StaticTypedScope getScope() { throw new UnsupportedOperationException(); } @@ -1455,10 +1453,10 @@ boolean isModuleExport() { // ------------------------------------------------------------------------- /** - * A global name reference. Contains references to the relevant parse tree - * node and its ancestors that may be affected. + * A global name reference. Contains references to the relevant parse tree node and its ancestors + * that may be affected. */ - static class Ref implements StaticTypedRef { + static class Ref implements StaticTypedRef { // Note: we are more aggressive about collapsing @enum and @constructor // declarations than implied here, see Name#canCollapse @@ -1563,7 +1561,7 @@ public StaticSourceFile getSourceFile() { } @Override - public StaticTypedSlot getSymbol() { + public StaticTypedSlot getSymbol() { return name; } diff --git a/src/com/google/javascript/jscomp/LinkedFlowScope.java b/src/com/google/javascript/jscomp/LinkedFlowScope.java index 7d5b9551998..3bf9070439e 100644 --- a/src/com/google/javascript/jscomp/LinkedFlowScope.java +++ b/src/com/google/javascript/jscomp/LinkedFlowScope.java @@ -164,19 +164,17 @@ public Node getRootNode() { } @Override - public StaticTypedScope getParentScope() { + public StaticTypedScope getParentScope() { throw new UnsupportedOperationException(); } - /** - * Get the slot for the given symbol. - */ + /** Get the slot for the given symbol. */ @Override - public StaticTypedSlot getSlot(String name) { + public StaticTypedSlot getSlot(String name) { return getSlot(getVarFromSyntacticScope(name)); } - private StaticTypedSlot getSlot(ScopedName var) { + private StaticTypedSlot getSlot(ScopedName var) { if (cache.dirtySymbols.contains(var)) { for (LinkedFlowSlot slot = lastSlot; slot != null; slot = slot.parent) { if (slot.var.equals(var)) { @@ -210,7 +208,7 @@ private ScopedName getVarFromSyntacticScope(String name) { } @Override - public StaticTypedSlot getOwnSlot(String name) { + public StaticTypedSlot getOwnSlot(String name) { throw new UnsupportedOperationException(); } @@ -220,7 +218,7 @@ public FlowScope createChildFlowScope() { } @Override - public FlowScope createChildFlowScope(StaticTypedScope scope) { + public FlowScope createChildFlowScope(StaticTypedScope scope) { frozen = true; TypedScope typedScope = (TypedScope) scope; @@ -381,10 +379,9 @@ public boolean equals(Object other) { } /** - * Determines whether two slots are meaningfully different for the - * purposes of data flow analysis. + * Determines whether two slots are meaningfully different for the purposes of data flow analysis. */ - private static boolean diffSlots(StaticTypedSlot slotA, StaticTypedSlot slotB) { + private static boolean diffSlots(StaticTypedSlot slotA, StaticTypedSlot slotB) { boolean aIsNull = slotA == null || slotA.getType() == null; boolean bIsNull = slotB == null || slotB.getType() == null; if (aIsNull || bIsNull) { @@ -432,7 +429,7 @@ public int hashCode() { } /** A static slot with a linked list built in. */ - private static class LinkedFlowSlot implements StaticTypedSlot { + private static class LinkedFlowSlot implements StaticTypedSlot { final ScopedName var; final JSType type; final LinkedFlowSlot parent; @@ -459,7 +456,7 @@ public boolean isTypeInferred() { } @Override - public StaticTypedRef getDeclaration() { + public StaticTypedRef getDeclaration() { return null; } @@ -469,7 +466,7 @@ public JSDocInfo getJSDocInfo() { } @Override - public StaticTypedScope getScope() { + public StaticTypedScope getScope() { throw new UnsupportedOperationException(); } } diff --git a/src/com/google/javascript/jscomp/PreprocessorSymbolTable.java b/src/com/google/javascript/jscomp/PreprocessorSymbolTable.java index fe4d5f440c3..458804fac00 100644 --- a/src/com/google/javascript/jscomp/PreprocessorSymbolTable.java +++ b/src/com/google/javascript/jscomp/PreprocessorSymbolTable.java @@ -32,15 +32,13 @@ import javax.annotation.Nullable; /** - * A symbol table for references that are removed by preprocessor passes - * (like {@code ProcessClosurePrimitives}). + * A symbol table for references that are removed by preprocessor passes (like {@code + * ProcessClosurePrimitives}). * * @author nicksantos@google.com (Nick Santos) */ final class PreprocessorSymbolTable - implements StaticTypedScope, - StaticSymbolTable { + implements StaticTypedScope, StaticSymbolTable { /** * All preprocessor symbols are globals. @@ -67,7 +65,7 @@ public JSType getTypeOfThis() { } @Override - public StaticTypedScope getParentScope() { + public StaticTypedScope getParentScope() { return null; } @@ -92,7 +90,7 @@ public Iterable getAllSymbols() { } @Override - public StaticTypedScope getScope(SimpleSlot slot) { + public StaticTypedScope getScope(SimpleSlot slot) { return this; } diff --git a/src/com/google/javascript/jscomp/SymbolTable.java b/src/com/google/javascript/jscomp/SymbolTable.java index 731eb2e7bef..7e52fb9876a 100644 --- a/src/com/google/javascript/jscomp/SymbolTable.java +++ b/src/com/google/javascript/jscomp/SymbolTable.java @@ -1880,21 +1880,21 @@ private int getLexicalScopeDepth(SymbolScope scope) { private JSType getType(StaticSlot sym) { if (sym instanceof StaticTypedSlot) { - return ((StaticTypedSlot) sym).getType(); + return ((StaticTypedSlot) sym).getType(); } return null; } private JSType getTypeOfThis(StaticScope s) { if (s instanceof StaticTypedScope) { - return ((StaticTypedScope) s).getTypeOfThis(); + return ((StaticTypedScope) s).getTypeOfThis(); } return null; } private boolean isTypeInferred(StaticSlot sym) { if (sym instanceof StaticTypedSlot) { - return ((StaticTypedSlot) sym).isTypeInferred(); + return ((StaticTypedSlot) sym).isTypeInferred(); } return true; } diff --git a/src/com/google/javascript/jscomp/TypeInference.java b/src/com/google/javascript/jscomp/TypeInference.java index d159df82925..4ec54196023 100644 --- a/src/com/google/javascript/jscomp/TypeInference.java +++ b/src/com/google/javascript/jscomp/TypeInference.java @@ -866,7 +866,7 @@ private FlowScope traverseName(Node n, FlowScope scope) { updateScopeForTypeChange(scope, n, type, resultType); type = resultType; } else { - StaticTypedSlot var = scope.getSlot(varName); + StaticTypedSlot var = scope.getSlot(varName); if (var != null) { // There are two situations where we don't want to use type information // from the scope, even if we have it. @@ -1657,7 +1657,7 @@ private JSType getPropertyType(JSType objType, String propName, // Scopes sometimes contain inferred type info about qualified names. String qualifiedName = n.getQualifiedName(); - StaticTypedSlot var = qualifiedName != null ? scope.getSlot(qualifiedName) : null; + StaticTypedSlot var = qualifiedName != null ? scope.getSlot(qualifiedName) : null; if (var != null) { JSType varType = var.getType(); if (varType != null) { diff --git a/src/com/google/javascript/jscomp/TypeTransformation.java b/src/com/google/javascript/jscomp/TypeTransformation.java index 3f14ac51f2c..e03dd4f2a5a 100644 --- a/src/com/google/javascript/jscomp/TypeTransformation.java +++ b/src/com/google/javascript/jscomp/TypeTransformation.java @@ -94,7 +94,7 @@ class TypeTransformation { private final AbstractCompiler compiler; private final JSTypeRegistry registry; - private final StaticTypedScope typeEnv; + private final StaticTypedScope typeEnv; /** * A helper class for holding the information about the type variables @@ -111,7 +111,7 @@ private static class NameResolver { } @SuppressWarnings("unchecked") - TypeTransformation(AbstractCompiler compiler, StaticTypedScope typeEnv) { + TypeTransformation(AbstractCompiler compiler, StaticTypedScope typeEnv) { this.compiler = compiler; this.registry = compiler.getTypeRegistry(); this.typeEnv = typeEnv; @@ -138,7 +138,7 @@ private JSType getType(String typeName) { if (type != null) { return type; } - StaticTypedSlot slot = typeEnv.getSlot(typeName); + StaticTypedSlot slot = typeEnv.getSlot(typeName); type = slot != null ? slot.getType() : null; if (type != null) { if (type.isConstructor() || type.isInterface()) { @@ -761,7 +761,7 @@ private JSType evalMaprecord(Node ttlAst, NameResolver nameResolver) { private JSType evalTypeOfVar(Node ttlAst) { String name = getCallArgument(ttlAst, 0).getString(); - StaticTypedSlot slot = typeEnv.getSlot(name); + StaticTypedSlot slot = typeEnv.getSlot(name); JSType type = slot != null ? slot.getType() : null; if (type == null) { reportWarning(ttlAst, VAR_UNDEFINED, name); diff --git a/src/com/google/javascript/jscomp/TypeValidator.java b/src/com/google/javascript/jscomp/TypeValidator.java index 558a1a9c567..f3a867c54aa 100644 --- a/src/com/google/javascript/jscomp/TypeValidator.java +++ b/src/com/google/javascript/jscomp/TypeValidator.java @@ -787,7 +787,7 @@ void expectAllInterfaceProperties(NodeTraversal t, Node n, */ private void expectInterfaceProperty(NodeTraversal t, Node n, ObjectType instance, ObjectType implementedInterface, String prop) { - StaticTypedSlot propSlot = instance.getSlot(prop); + StaticTypedSlot propSlot = instance.getSlot(prop); if (propSlot == null) { // Not implemented String sourceName = n.getSourceFileName(); diff --git a/src/com/google/javascript/jscomp/TypedScope.java b/src/com/google/javascript/jscomp/TypedScope.java index 2ba25c05e3c..fe04c76b7fa 100644 --- a/src/com/google/javascript/jscomp/TypedScope.java +++ b/src/com/google/javascript/jscomp/TypedScope.java @@ -27,24 +27,21 @@ import com.google.javascript.rhino.jstype.StaticTypedSlot; /** - * TypedScope contains information about variables and their types. - * Scopes can be nested, a scope points back to its parent scope. - *

- * TypedScope is also used as a lattice element for flow-sensitive type inference. - * As a lattice element, a scope is viewed as a map from names to types. A name - * not in the map is considered to have the bottom type. The join of two maps m1 - * and m2 is the map of the union of names with {@link JSType#getLeastSupertype} - * to meet the m1 type and m2 type. + * TypedScope contains information about variables and their types. Scopes can be nested, a scope + * points back to its parent scope. + * + *

TypedScope is also used as a lattice element for flow-sensitive type inference. As a lattice + * element, a scope is viewed as a map from names to types. A name not in the map is considered to + * have the bottom type. The join of two maps m1 and m2 is the map of the union of names with {@link + * JSType#getLeastSupertype} to meet the m1 type and m2 type. * * @see NodeTraversal * @see DataFlowAnalysis - * - * Several methods in this class, such as {@code isBlockScope} throw an exception when called. - * The reason for this is that we want to shadow methods from the parent class, to avoid calling - * them accidentally. + *

Several methods in this class, such as {@code isBlockScope} throw an exception when + * called. The reason for this is that we want to shadow methods from the parent class, to avoid + * calling them accidentally. */ -public class TypedScope extends AbstractScope - implements StaticTypedScope { +public class TypedScope extends AbstractScope implements StaticTypedScope { private final TypedScope parent; private final int depth; @@ -202,12 +199,12 @@ void setTypeResolver(TypeResolver resolver) { } public JSType getNamespaceOrTypedefType(String typeName) { - StaticTypedSlot slot = getSlot(typeName); + StaticTypedSlot slot = getSlot(typeName); return slot == null ? null : slot.getType(); } public JSDocInfo getJsdocOfTypeDeclaration(String typeName) { - StaticTypedSlot slot = getSlot(typeName); + StaticTypedSlot slot = getSlot(typeName); return slot == null ? null : slot.getJSDocInfo(); } } diff --git a/src/com/google/javascript/jscomp/TypedVar.java b/src/com/google/javascript/jscomp/TypedVar.java index 089b22f8beb..bfe6a1adbbc 100644 --- a/src/com/google/javascript/jscomp/TypedVar.java +++ b/src/com/google/javascript/jscomp/TypedVar.java @@ -25,16 +25,14 @@ /** * {@link AbstractVar} subclass for use with {@link TypedScope}. * - *

Note that this class inherits its {@link #equals} and {@link #hashCode} - * implementations from {@link ScopedName}, which does not include any type - * information. This is necessary because {@code Var}-keyed maps are used - * across multiple top scopes, but it comes with the caveat that if {@code - * TypedVar} instances are stored in a set, the type information is at risk - * of disappearing if an untyped (or differently typed) var is added for the - * same symbol. + *

Note that this class inherits its {@link #equals} and {@link #hashCode} implementations from + * {@link ScopedName}, which does not include any type information. This is necessary because {@code + * Var}-keyed maps are used across multiple top scopes, but it comes with the caveat that if {@code + * TypedVar} instances are stored in a set, the type information is at risk of disappearing if an + * untyped (or differently typed) var is added for the same symbol. */ public class TypedVar extends AbstractVar - implements StaticTypedSlot, StaticTypedRef { + implements StaticTypedSlot, StaticTypedRef { private JSType type; // The next two fields and the associated methods are only used by diff --git a/src/com/google/javascript/jscomp/type/ChainableReverseAbstractInterpreter.java b/src/com/google/javascript/jscomp/type/ChainableReverseAbstractInterpreter.java index 47c1552d97d..aceafc07919 100644 --- a/src/com/google/javascript/jscomp/type/ChainableReverseAbstractInterpreter.java +++ b/src/com/google/javascript/jscomp/type/ChainableReverseAbstractInterpreter.java @@ -118,7 +118,7 @@ protected FlowScope nextPreciserScopeKnowingConditionOutcome(Node condition, protected JSType getTypeIfRefinable(Node node, FlowScope scope) { switch (node.getToken()) { case NAME: - StaticTypedSlot nameVar = scope.getSlot(node.getString()); + StaticTypedSlot nameVar = scope.getSlot(node.getString()); if (nameVar != null) { JSType nameVarType = nameVar.getType(); if (nameVarType == null) { @@ -133,7 +133,7 @@ protected JSType getTypeIfRefinable(Node node, FlowScope scope) { if (qualifiedName == null) { return null; } - StaticTypedSlot propVar = scope.getSlot(qualifiedName); + StaticTypedSlot propVar = scope.getSlot(qualifiedName); JSType propVarType = null; if (propVar != null) { propVarType = propVar.getType(); diff --git a/src/com/google/javascript/jscomp/type/FlowScope.java b/src/com/google/javascript/jscomp/type/FlowScope.java index 36722a3de79..9ee81df6965 100644 --- a/src/com/google/javascript/jscomp/type/FlowScope.java +++ b/src/com/google/javascript/jscomp/type/FlowScope.java @@ -24,12 +24,12 @@ /** * A symbol table for inferring types during data flow analysis. * - * Each flow scope represents the types of all variables in the scope at - * a particular point in the flow analysis. + *

Each flow scope represents the types of all variables in the scope at a particular point in + * the flow analysis. * * @author nicksantos@google.com (Nick Santos) */ -public interface FlowScope extends StaticTypedScope, LatticeElement { +public interface FlowScope extends StaticTypedScope, LatticeElement { /** * Creates a child of this flow scope, to represent an instruction @@ -41,7 +41,7 @@ public interface FlowScope extends StaticTypedScope, LatticeElement { * Creates a child flow scope with the given syntactic scope, which may be required to be a * specific subclass, such as TypedScope. */ - FlowScope createChildFlowScope(StaticTypedScope scope); + FlowScope createChildFlowScope(StaticTypedScope scope); /** * Defines the type of a symbol at this point in the flow. @@ -70,5 +70,5 @@ void inferQualifiedSlot(Node node, String symbol, JSType bottomType, FlowScope optimize(); /** Returns the underlying TypedScope. */ - StaticTypedScope getDeclarationScope(); + StaticTypedScope getDeclarationScope(); } diff --git a/src/com/google/javascript/jscomp/type/SemanticReverseAbstractInterpreter.java b/src/com/google/javascript/jscomp/type/SemanticReverseAbstractInterpreter.java index 056547aef2a..ed904d5c138 100644 --- a/src/com/google/javascript/jscomp/type/SemanticReverseAbstractInterpreter.java +++ b/src/com/google/javascript/jscomp/type/SemanticReverseAbstractInterpreter.java @@ -355,7 +355,7 @@ private FlowScope caseAndOrMaybeShortCircuiting(Node left, Node right, Set refinements = new HashSet<>(); blindScope = new RefinementTrackingFlowScope(blindScope, refinements); FlowScope leftScope = firstPreciserScopeKnowingConditionOutcome(left, blindScope, !outcome); - StaticTypedSlot leftVar = + StaticTypedSlot leftVar = refinements.size() == 1 ? leftScope.getSlot(refinements.iterator().next()) : null; if (leftVar == null) { // If we did create a more precise scope, blindScope has a child and @@ -369,7 +369,7 @@ private FlowScope caseAndOrMaybeShortCircuiting(Node left, Node right, new RefinementTrackingFlowScope( firstPreciserScopeKnowingConditionOutcome(left, blindScope, outcome), refinements); rightScope = firstPreciserScopeKnowingConditionOutcome(right, rightScope, !outcome); - StaticTypedSlot rightVar = + StaticTypedSlot rightVar = refinements.size() == 1 ? rightScope.getSlot(refinements.iterator().next()) : null; if (rightVar == null || !leftVar.getName().equals(rightVar.getName())) { return unwrap(blindScope == rightScope ? blindScope : blindScope.createChildFlowScope()); @@ -631,7 +631,7 @@ public FlowScope createChildFlowScope() { } @Override - public FlowScope createChildFlowScope(StaticTypedScope scope) { + public FlowScope createChildFlowScope(StaticTypedScope scope) { return new RefinementTrackingFlowScope(delegate.createChildFlowScope(scope), refinements); } @@ -655,7 +655,7 @@ public FlowScope optimize() { } @Override - public StaticTypedScope getDeclarationScope() { + public StaticTypedScope getDeclarationScope() { return delegate.getDeclarationScope(); } @@ -665,17 +665,17 @@ public Node getRootNode() { } @Override - public StaticTypedScope getParentScope() { + public StaticTypedScope getParentScope() { throw new UnsupportedOperationException(); } @Override - public StaticTypedSlot getSlot(String name) { + public StaticTypedSlot getSlot(String name) { return delegate.getSlot(name); } @Override - public StaticTypedSlot getOwnSlot(String name) { + public StaticTypedSlot getOwnSlot(String name) { return delegate.getOwnSlot(name); } diff --git a/src/com/google/javascript/rhino/JSTypeExpression.java b/src/com/google/javascript/rhino/JSTypeExpression.java index afa3da03452..f099ec9ba08 100644 --- a/src/com/google/javascript/rhino/JSTypeExpression.java +++ b/src/com/google/javascript/rhino/JSTypeExpression.java @@ -93,10 +93,8 @@ public boolean isVarArgs() { return root.getToken() == Token.ELLIPSIS; } - /** - * Evaluates the type expression into a {@code JSType} object. - */ - public JSType evaluate(StaticTypedScope scope, JSTypeRegistry registry) { + /** Evaluates the type expression into a {@code JSType} object. */ + public JSType evaluate(StaticTypedScope scope, JSTypeRegistry registry) { JSType type = registry.createTypeFromCommentNode(root, sourceName, scope); root.setJSType(type); return type; diff --git a/src/com/google/javascript/rhino/jstype/JSTypeRegistry.java b/src/com/google/javascript/rhino/jstype/JSTypeRegistry.java index 7bb68d7dbb6..1485f167ac5 100644 --- a/src/com/google/javascript/rhino/jstype/JSTypeRegistry.java +++ b/src/com/google/javascript/rhino/jstype/JSTypeRegistry.java @@ -205,7 +205,7 @@ public class JSTypeRegistry implements Serializable { LinkedHashMultimap.create(); // All the unresolved named types. - private final Multimap, NamedType> unresolvedNamedTypes = + private final Multimap unresolvedNamedTypes = ArrayListMultimap.create(); // The template type name. @@ -1478,11 +1478,7 @@ public JSType getType(StaticScope scope, String jsTypeName) { * corresponding JSType object. */ public JSType getType( - StaticTypedScope scope, - String jsTypeName, - String sourceName, - int lineno, - int charno) { + StaticTypedScope scope, String jsTypeName, String sourceName, int lineno, int charno) { return getType(scope, jsTypeName, sourceName, lineno, charno, true); } @@ -1492,7 +1488,7 @@ public JSType getType( * type args). */ private JSType getType( - StaticTypedScope scope, + StaticTypedScope scope, String jsTypeName, String sourceName, int lineno, @@ -1565,10 +1561,8 @@ public void clearNamedTypes() { unresolvedNamedTypes.clear(); } - /** - * Resolve all the unresolved types in the given scope. - */ - public void resolveTypesInScope(StaticTypedScope scope) { + /** Resolve all the unresolved types in the given scope. */ + public void resolveTypesInScope(StaticTypedScope scope) { for (NamedType type : unresolvedNamedTypes.get(scope)) { type.resolve(reporter); } @@ -2050,8 +2044,8 @@ public TemplatizedType createTemplatizedType(ObjectType baseType, JSType... temp /** Creates a named type. */ @VisibleForTesting - public NamedType createNamedType(StaticTypedScope scope, String reference, - String sourceName, int lineno, int charno) { + public NamedType createNamedType( + StaticTypedScope scope, String reference, String sourceName, int lineno, int charno) { if (reference.endsWith(".")) { return new NamespaceType(this, reference, sourceName, lineno, charno); } else { @@ -2067,7 +2061,7 @@ public void identifyNonNullableName(String name) { nonNullableTypeNames.add(name); } - public JSType evaluateTypeExpression(JSTypeExpression expr, StaticTypedScope scope) { + public JSType evaluateTypeExpression(JSTypeExpression expr, StaticTypedScope scope) { return createTypeFromCommentNode(expr.getRoot(), expr.getSourceName(), scope); } @@ -2077,18 +2071,18 @@ public JSType createTypeFromCommentNode(Node n) { /** * Creates a JSType from the nodes representing a type. + * * @param n The node with type info. * @param sourceName The source file name. * @param scope A scope for doing type name lookups. */ @SuppressWarnings("unchecked") - public JSType createTypeFromCommentNode( - Node n, String sourceName, StaticTypedScope scope) { + public JSType createTypeFromCommentNode(Node n, String sourceName, StaticTypedScope scope) { return createFromTypeNodesInternal(n, sourceName, scope, true); } - private JSType createFromTypeNodesInternal(Node n, String sourceName, - StaticTypedScope scope, boolean recordUnresolvedTypes) { + private JSType createFromTypeNodesInternal( + Node n, String sourceName, StaticTypedScope scope, boolean recordUnresolvedTypes) { switch (n.getToken()) { case LC: // Record type. return createRecordTypeFromNodes( @@ -2296,12 +2290,12 @@ private JSType createFromTypeNodesInternal(Node n, String sourceName, /** * Creates a RecordType from the nodes representing said record type. + * * @param n The node with type info. * @param sourceName The source file name. * @param scope A scope for doing type name lookups. */ - private JSType createRecordTypeFromNodes(Node n, String sourceName, - StaticTypedScope scope) { + private JSType createRecordTypeFromNodes(Node n, String sourceName, StaticTypedScope scope) { RecordTypeBuilder builder = new RecordTypeBuilder(this); diff --git a/src/com/google/javascript/rhino/jstype/NamedType.java b/src/com/google/javascript/rhino/jstype/NamedType.java index ee60386456c..e122092a2b1 100644 --- a/src/com/google/javascript/rhino/jstype/NamedType.java +++ b/src/com/google/javascript/rhino/jstype/NamedType.java @@ -106,19 +106,21 @@ public class NamedType extends ProxyObjectType { */ @Nullable private final ImmutableList templateTypes; + @Nullable private StaticTypedScope resolutionScope; - @Nullable private StaticTypedScope resolutionScope; - - /** - * Create a named type based on the reference. - */ - NamedType(StaticTypedScope scope, JSTypeRegistry registry, String reference, - String sourceName, int lineno, int charno) { + /** Create a named type based on the reference. */ + NamedType( + StaticTypedScope scope, + JSTypeRegistry registry, + String reference, + String sourceName, + int lineno, + int charno) { this(scope, registry, reference, sourceName, lineno, charno, null); } NamedType( - StaticTypedScope scope, + StaticTypedScope scope, JSTypeRegistry registry, String reference, String sourceName, @@ -295,7 +297,7 @@ private JSType lookupViaProperties(ErrorReporter reporter) { if (componentNames[0].length() == 0) { return null; } - StaticTypedSlot slot = resolutionScope.getSlot(componentNames[0]); + StaticTypedSlot slot = resolutionScope.getSlot(componentNames[0]); if (slot == null) { return null; } @@ -377,7 +379,7 @@ private void handleUnresolvedType( setResolvedTypeInternal(getReferencedType()); } - private JSType getTypedefType(ErrorReporter reporter, StaticTypedSlot slot) { + private JSType getTypedefType(ErrorReporter reporter, StaticTypedSlot slot) { JSType type = slot.getType(); if (type != null) { return type; diff --git a/src/com/google/javascript/rhino/jstype/ObjectType.java b/src/com/google/javascript/rhino/jstype/ObjectType.java index d773eb8739a..936c9571072 100644 --- a/src/com/google/javascript/rhino/jstype/ObjectType.java +++ b/src/com/google/javascript/rhino/jstype/ObjectType.java @@ -482,7 +482,7 @@ public JSType findPropertyType(String propertyName) { * returns {@code null}. */ public JSType getPropertyType(String propertyName) { - StaticTypedSlot slot = getSlot(propertyName); + StaticTypedSlot slot = getSlot(propertyName); if (slot == null) { if (isNoResolvedType() || isCheckedUnknownType()) { return getNativeType(JSTypeNative.CHECKED_UNKNOWN_TYPE); @@ -534,7 +534,7 @@ public Set getOwnPropertyNames() { * Checks whether the property's type is inferred. */ public boolean isPropertyTypeInferred(String propertyName) { - StaticTypedSlot slot = getSlot(propertyName); + StaticTypedSlot slot = getSlot(propertyName); return slot == null ? false : slot.isTypeInferred(); } @@ -542,7 +542,7 @@ public boolean isPropertyTypeInferred(String propertyName) { * Checks whether the property's type is declared. */ public boolean isPropertyTypeDeclared(String propertyName) { - StaticTypedSlot slot = getSlot(propertyName); + StaticTypedSlot slot = getSlot(propertyName); return slot == null ? false : !slot.isTypeInferred(); } diff --git a/src/com/google/javascript/rhino/jstype/Property.java b/src/com/google/javascript/rhino/jstype/Property.java index 89de7462846..95361efa831 100644 --- a/src/com/google/javascript/rhino/jstype/Property.java +++ b/src/com/google/javascript/rhino/jstype/Property.java @@ -47,10 +47,10 @@ /** * A property slot of an object. + * * @author nicksantos@google.com (Nick Santos) */ -public final class Property - implements Serializable, StaticTypedSlot, StaticTypedRef { +public final class Property implements Serializable, StaticTypedSlot, StaticTypedRef { private static final long serialVersionUID = 1L; /** @@ -149,7 +149,7 @@ public String toString() { } @Override - public StaticTypedScope getScope() { + public StaticTypedScope getScope() { throw new UnsupportedOperationException(); } diff --git a/src/com/google/javascript/rhino/jstype/SimpleReference.java b/src/com/google/javascript/rhino/jstype/SimpleReference.java index 3940e492cc5..dd236e4d01c 100644 --- a/src/com/google/javascript/rhino/jstype/SimpleReference.java +++ b/src/com/google/javascript/rhino/jstype/SimpleReference.java @@ -44,10 +44,10 @@ /** * A simple immutable reference. + * * @author nicksantos@google.com (Nick Santos) */ -public class SimpleReference> - implements StaticTypedRef { +public class SimpleReference implements StaticTypedRef { private final T symbol; private final Node node; diff --git a/src/com/google/javascript/rhino/jstype/SimpleSlot.java b/src/com/google/javascript/rhino/jstype/SimpleSlot.java index ef4ff17061a..1cc38c2fc8a 100644 --- a/src/com/google/javascript/rhino/jstype/SimpleSlot.java +++ b/src/com/google/javascript/rhino/jstype/SimpleSlot.java @@ -43,11 +43,11 @@ import java.io.Serializable; /** - * The minimum implementation of {@code StaticTypedSlot}. + * The minimum implementation of {@code StaticTypedSlot}. * * @author nicksantos@google.com (Nick Santos) */ -public class SimpleSlot implements StaticTypedSlot, Serializable { +public class SimpleSlot implements StaticTypedSlot, Serializable { private static final long serialVersionUID = 1L; final String name; @@ -76,7 +76,7 @@ public boolean isTypeInferred() { } @Override - public StaticTypedRef getDeclaration() { + public StaticTypedRef getDeclaration() { return null; } @@ -86,7 +86,7 @@ public JSDocInfo getJSDocInfo() { } @Override - public StaticTypedScope getScope() { + public StaticTypedScope getScope() { throw new UnsupportedOperationException(); } } diff --git a/src/com/google/javascript/rhino/jstype/StaticTypedRef.java b/src/com/google/javascript/rhino/jstype/StaticTypedRef.java index f9f24693e1c..07ec58b3223 100644 --- a/src/com/google/javascript/rhino/jstype/StaticTypedRef.java +++ b/src/com/google/javascript/rhino/jstype/StaticTypedRef.java @@ -41,15 +41,13 @@ import com.google.javascript.rhino.StaticRef; /** - * The {@code StaticTypedRef} tells us all the ways that a {@code StaticTypedSlot} - * is used in a program. + * The {@code StaticTypedRef} tells us all the ways that a {@code StaticTypedSlot} is used in a + * program. * * @author nicksantos@google.com (Nick Santos) */ -public interface StaticTypedRef extends StaticRef { - /** - * The variable that this reference points to. - */ +public interface StaticTypedRef extends StaticRef { + /** The variable that this reference points to. */ @Override - StaticTypedSlot getSymbol(); + StaticTypedSlot getSymbol(); } diff --git a/src/com/google/javascript/rhino/jstype/StaticTypedScope.java b/src/com/google/javascript/rhino/jstype/StaticTypedScope.java index 42325e7bcba..fd7153d83b0 100644 --- a/src/com/google/javascript/rhino/jstype/StaticTypedScope.java +++ b/src/com/google/javascript/rhino/jstype/StaticTypedScope.java @@ -42,33 +42,30 @@ import com.google.javascript.rhino.StaticScope; /** - * The {@code StaticTypedScope} interface must be implemented by any object that - * defines variables for the purposes of static analysis. It is distinguished - * from the {@code Scriptable} class that Rhino normally uses to represent a - * run-time scope. + * The {@code StaticTypedScope} interface must be implemented by any object that defines variables + * for the purposes of static analysis. It is distinguished from the {@code Scriptable} class that + * Rhino normally uses to represent a run-time scope. * - * @param The type of information stored about the slot */ -public interface StaticTypedScope extends StaticScope { +public interface StaticTypedScope extends StaticScope { /** Returns the scope enclosing this one or null if none. */ @Override - StaticTypedScope getParentScope(); + StaticTypedScope getParentScope(); /** - * Returns any defined slot within this scope for this name. This call - * continues searching through parent scopes if a slot with this name is not - * found in the current scope. + * Returns any defined slot within this scope for this name. This call continues searching through + * parent scopes if a slot with this name is not found in the current scope. + * * @param name The name of the variable slot to look up. - * @return The defined slot for the variable, or {@code null} if no - * definition exists. + * @return The defined slot for the variable, or {@code null} if no definition exists. */ @Override - StaticTypedSlot getSlot(String name); + StaticTypedSlot getSlot(String name); /** Like {@code getSlot} but does not recurse into parent scopes. */ @Override - StaticTypedSlot getOwnSlot(String name); + StaticTypedSlot getOwnSlot(String name); /** Returns the expected type of {@code this} in the current scope. */ - T getTypeOfThis(); + JSType getTypeOfThis(); } diff --git a/src/com/google/javascript/rhino/jstype/StaticTypedSlot.java b/src/com/google/javascript/rhino/jstype/StaticTypedSlot.java index 0850af76428..649203789c0 100644 --- a/src/com/google/javascript/rhino/jstype/StaticTypedSlot.java +++ b/src/com/google/javascript/rhino/jstype/StaticTypedSlot.java @@ -42,17 +42,17 @@ import com.google.javascript.rhino.StaticSlot; /** - * The {@code StaticTypedSlot} interface must be implemented by variables that can - * appear as members of a {@code StaticTypedScope}. + * The {@code StaticTypedSlot} interface must be implemented by variables that can appear as members + * of a {@code StaticTypedScope}. * - * @param The type of information stored about the slot */ -public interface StaticTypedSlot extends StaticSlot { +public interface StaticTypedSlot extends StaticSlot { /** * Returns the type information, if any, for this slot. + * * @return The type or {@code null} if no type is declared for it. */ - T getType(); + JSType getType(); /** * Returns whether the type has been inferred (as opposed to declared). @@ -61,8 +61,8 @@ public interface StaticTypedSlot extends StaticSlot { /** Gets the declaration of this symbol. May not exist. */ @Override - StaticTypedRef getDeclaration(); + StaticTypedRef getDeclaration(); @Override - StaticTypedScope getScope(); + StaticTypedScope getScope(); } diff --git a/src/com/google/javascript/rhino/testing/AbstractStaticScope.java b/src/com/google/javascript/rhino/testing/AbstractStaticScope.java index 48f1bf27c93..f1cc49a6fb8 100644 --- a/src/com/google/javascript/rhino/testing/AbstractStaticScope.java +++ b/src/com/google/javascript/rhino/testing/AbstractStaticScope.java @@ -45,9 +45,10 @@ /** * A scope that just returns null for everything. + * * @author nicksantos@google.com (Nick Santos) */ -public abstract class AbstractStaticScope implements StaticTypedScope { +public abstract class AbstractStaticScope implements StaticTypedScope { @Override public Node getRootNode() { @@ -55,20 +56,20 @@ public Node getRootNode() { } @Override - public StaticTypedScope getParentScope() { + public StaticTypedScope getParentScope() { return null; } @Override - public abstract StaticTypedSlot getSlot(String name); + public abstract StaticTypedSlot getSlot(String name); @Override - public StaticTypedSlot getOwnSlot(String name) { + public StaticTypedSlot getOwnSlot(String name) { return getSlot(name); } @Override - public T getTypeOfThis() { + public JSType getTypeOfThis() { return null; } } diff --git a/src/com/google/javascript/rhino/testing/MapBasedScope.java b/src/com/google/javascript/rhino/testing/MapBasedScope.java index 88114aeede9..cb1f59f32ef 100644 --- a/src/com/google/javascript/rhino/testing/MapBasedScope.java +++ b/src/com/google/javascript/rhino/testing/MapBasedScope.java @@ -42,22 +42,20 @@ import com.google.javascript.rhino.jstype.JSType; import com.google.javascript.rhino.jstype.SimpleSlot; import com.google.javascript.rhino.jstype.StaticTypedSlot; - import java.util.HashMap; import java.util.Map; /** * A scope based on a simple hashmap. + * * @author nicksantos@google.com (Nick Santos) */ -public class MapBasedScope extends AbstractStaticScope { - private final Map> slots = new HashMap<>(); +public class MapBasedScope extends AbstractStaticScope { + private final Map slots = new HashMap<>(); public MapBasedScope(Map namesToTypes) { for (Map.Entry entry : namesToTypes.entrySet()) { - slots.put( - entry.getKey(), - new SimpleSlot(entry.getKey(), entry.getValue(), false)); + slots.put(entry.getKey(), new SimpleSlot(entry.getKey(), entry.getValue(), false)); } } @@ -66,7 +64,7 @@ public static MapBasedScope emptyScope() { } @Override - public StaticTypedSlot getSlot(String name) { + public StaticTypedSlot getSlot(String name) { return slots.get(name); } } diff --git a/test/com/google/javascript/jscomp/SimpleReplaceScriptTest.java b/test/com/google/javascript/jscomp/SimpleReplaceScriptTest.java index d40dda130fb..9f5b8ac98b8 100644 --- a/test/com/google/javascript/jscomp/SimpleReplaceScriptTest.java +++ b/test/com/google/javascript/jscomp/SimpleReplaceScriptTest.java @@ -744,7 +744,7 @@ public void testPrototypeSlotChangedOnCompile() { getOptions(), ImmutableList.of(CLOSURE_BASE, src), 0, 0, false); JSType type = compiler.getTypeRegistry().getGlobalType("ns.Foo"); FunctionType fnType = type.toObjectType().getConstructor(); - StaticTypedSlot originalSlot = fnType.getSlot("prototype"); + StaticTypedSlot originalSlot = fnType.getSlot("prototype"); doReplaceScript(compiler, src, 1); @@ -752,7 +752,7 @@ public void testPrototypeSlotChangedOnCompile() { type = compiler.getTypeRegistry().getGlobalType("ns.Foo"); fnType = type.toObjectType().getConstructor(); - StaticTypedSlot newSlot = fnType.getSlot("prototype"); + StaticTypedSlot newSlot = fnType.getSlot("prototype"); assertNotSame(originalSlot, newSlot); } diff --git a/test/com/google/javascript/jscomp/TypeInferenceTest.java b/test/com/google/javascript/jscomp/TypeInferenceTest.java index 932c4c0375e..972c9df77df 100644 --- a/test/com/google/javascript/jscomp/TypeInferenceTest.java +++ b/test/com/google/javascript/jscomp/TypeInferenceTest.java @@ -224,7 +224,7 @@ private TypeSubject assertTypeOfExpression(String label) { private JSType getType(String name) { assertNotNull("The return scope should not be null.", returnScope); - StaticTypedSlot var = returnScope.getSlot(name); + StaticTypedSlot var = returnScope.getSlot(name); assertNotNull("The variable " + name + " is missing from the scope.", var); return var.getType(); } diff --git a/test/com/google/javascript/rhino/jstype/JSTypeTest.java b/test/com/google/javascript/rhino/jstype/JSTypeTest.java index 2989087d967..0d948890098 100644 --- a/test/com/google/javascript/rhino/jstype/JSTypeTest.java +++ b/test/com/google/javascript/rhino/jstype/JSTypeTest.java @@ -83,8 +83,7 @@ public class JSTypeTest extends BaseJSTypeTestCase { private EnumElementType elementsType; private NamedType forwardDeclaredNamedType; - private static final StaticTypedScope EMPTY_SCOPE = - MapBasedScope.emptyScope(); + private static final StaticTypedScope EMPTY_SCOPE = MapBasedScope.emptyScope(); /** * A non exhaustive list of representative types used to test simple