com.google.javascript.rhino.jstype.JSTypeRegistry Java Examples

The following examples show how to use com.google.javascript.rhino.jstype.JSTypeRegistry. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Closure_11_TypeCheck_s.java    From coming with MIT License 6 votes vote down vote up
public TypeCheck(AbstractCompiler compiler,
    ReverseAbstractInterpreter reverseInterpreter,
    JSTypeRegistry typeRegistry,
    Scope topScope,
    ScopeCreator scopeCreator,
    CheckLevel reportMissingOverride,
    CheckLevel reportUnknownTypes) {
  this.compiler = compiler;
  this.validator = compiler.getTypeValidator();
  this.reverseInterpreter = reverseInterpreter;
  this.typeRegistry = typeRegistry;
  this.topScope = topScope;
  this.scopeCreator = scopeCreator;
  this.reportMissingOverride = reportMissingOverride;
  this.reportUnknownTypes = reportUnknownTypes;
  this.inferJSDocInfo = new InferJSDocInfo(compiler);
}
 
Example #2
Source File: Closure_69_TypeCheck_s.java    From coming with MIT License 6 votes vote down vote up
public TypeCheck(AbstractCompiler compiler,
    ReverseAbstractInterpreter reverseInterpreter,
    JSTypeRegistry typeRegistry,
    Scope topScope,
    ScopeCreator scopeCreator,
    CheckLevel reportMissingOverride,
    CheckLevel reportUnknownTypes) {
  this.compiler = compiler;
  this.validator = compiler.getTypeValidator();
  this.reverseInterpreter = reverseInterpreter;
  this.typeRegistry = typeRegistry;
  this.topScope = topScope;
  this.scopeCreator = scopeCreator;
  this.reportMissingOverride = reportMissingOverride;
  this.reportUnknownTypes = reportUnknownTypes;
  this.inferJSDocInfo = new InferJSDocInfo(compiler);
}
 
Example #3
Source File: TypeValidatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testFunctionMismatch() throws Exception {
  testSame(
      "/** \n" +
      " * @param {function(string): number} x \n" +
      " * @return {function(boolean): string} \n" +
      " */ function f(x) { return x; }",
      TYPE_MISMATCH_WARNING);

  JSTypeRegistry registry = compiler.getTypeRegistry();
  JSType string = registry.getNativeType(STRING_TYPE);
  JSType bool = registry.getNativeType(BOOLEAN_TYPE);
  JSType number = registry.getNativeType(NUMBER_TYPE);
  JSType firstFunction = registry.createFunctionType(number, string);
  JSType secondFunction = registry.createFunctionType(string, bool);

  assertMismatches(
      Lists.newArrayList(
          new TypeMismatch(firstFunction, secondFunction, null),
          fromNatives(STRING_TYPE, BOOLEAN_TYPE),
          fromNatives(NUMBER_TYPE, STRING_TYPE)));
}
 
Example #4
Source File: TypeRegistry.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
/**
 * Iterates over all registered node and closure modules, collecting the names of internal
 * variables that are aliases for other types. This is used for fast lookup in {@link
 * #resolveAlias(NominalType, String)}.
 *
 * @param jsRegistry The JS registry to use when resolving aliases.
 */
public void collectModuleContentAliases(JSTypeRegistry jsRegistry) {
  for (Module module : getAllModules()) {
    if (module.getId().getType() == Module.Type.ES6) {
      continue;
    }

    AliasRegion aliasRegion = module.getAliases();
    for (String alias : aliasRegion.getAliases()) {
      String name = aliasRegion.resolveAlias(alias);
      if (Types.isModuleContentsVar(alias)) {
        if (isType(name)) {
          resolvedModuleContentAliases.put(alias, getType(name));
        } else {
          JSType type = jsRegistry.getGlobalType(name);
          if (type != null) {
            Iterator<NominalType> types = getTypes(type).iterator();
            if (types.hasNext()) {
              resolvedModuleContentAliases.put(alias, types.next());
            }
          }
        }
      }
    }
  }
}
 
Example #5
Source File: DevirtualizePrototypeMethods.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a new JSType based on the original function type by
 * adding the original this pointer type to the beginning of the
 * argument type list and replacing the this pointer type with
 * NO_TYPE.
 */
private void fixFunctionType(Node functionNode) {
  FunctionType type = JSType.toMaybeFunctionType(functionNode.getJSType());
  if (type != null) {
    JSTypeRegistry typeRegistry = compiler.getTypeRegistry();

    List<JSType> parameterTypes = Lists.newArrayList();
    parameterTypes.add(type.getTypeOfThis());

    for (Node param : type.getParameters()) {
      parameterTypes.add(param.getJSType());
    }

    ObjectType thisType =
        typeRegistry.getNativeObjectType(JSTypeNative.UNKNOWN_TYPE);
    JSType returnType = type.getReturnType();

    JSType newType = typeRegistry.createFunctionType(
        thisType, returnType, parameterTypes);
    functionNode.setJSType(newType);
  }
}
 
Example #6
Source File: TypeValidatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testFunctionMismatch2() throws Exception {
  testSame(
      "/** \n" +
      " * @param {function(string): number} x \n" +
      " * @return {function(boolean): number} \n" +
      " */ function f(x) { return x; }",
      TYPE_MISMATCH_WARNING);

  JSTypeRegistry registry = compiler.getTypeRegistry();
  JSType string = registry.getNativeType(STRING_TYPE);
  JSType bool = registry.getNativeType(BOOLEAN_TYPE);
  JSType number = registry.getNativeType(NUMBER_TYPE);
  JSType firstFunction = registry.createFunctionType(number, string);
  JSType secondFunction = registry.createFunctionType(number, bool);

  assertMismatches(
      Lists.newArrayList(
          new TypeMismatch(firstFunction, secondFunction, null),
          fromNatives(STRING_TYPE, BOOLEAN_TYPE)));
}
 
Example #7
Source File: Nopol2017_0029_s.java    From coming with MIT License 6 votes vote down vote up
public TypeCheck(AbstractCompiler compiler,
    ReverseAbstractInterpreter reverseInterpreter,
    JSTypeRegistry typeRegistry,
    Scope topScope,
    MemoizedScopeCreator scopeCreator,
    CheckLevel reportMissingOverride,
    CheckLevel reportUnknownTypes) {
  this.compiler = compiler;
  this.validator = compiler.getTypeValidator();
  this.reverseInterpreter = reverseInterpreter;
  this.typeRegistry = typeRegistry;
  this.topScope = topScope;
  this.scopeCreator = scopeCreator;
  this.reportMissingOverride = reportMissingOverride;
  this.reportUnknownTypes = reportUnknownTypes;
  this.inferJSDocInfo = new InferJSDocInfo(compiler);
}
 
Example #8
Source File: Closure_96_TypeCheck_t.java    From coming with MIT License 6 votes vote down vote up
public TypeCheck(AbstractCompiler compiler,
    ReverseAbstractInterpreter reverseInterpreter,
    JSTypeRegistry typeRegistry,
    Scope topScope,
    ScopeCreator scopeCreator,
    CheckLevel reportMissingOverride,
    CheckLevel reportUnknownTypes) {
  this.compiler = compiler;
  this.validator = compiler.getTypeValidator();
  this.reverseInterpreter = reverseInterpreter;
  this.typeRegistry = typeRegistry;
  this.topScope = topScope;
  this.scopeCreator = scopeCreator;
  this.reportMissingOverride = reportMissingOverride;
  this.reportUnknownTypes = reportUnknownTypes;
  this.inferJSDocInfo = new InferJSDocInfo(compiler);
}
 
Example #9
Source File: Nopol2017_0029_t.java    From coming with MIT License 6 votes vote down vote up
public TypeCheck(AbstractCompiler compiler,
    ReverseAbstractInterpreter reverseInterpreter,
    JSTypeRegistry typeRegistry,
    Scope topScope,
    MemoizedScopeCreator scopeCreator,
    CheckLevel reportMissingOverride,
    CheckLevel reportUnknownTypes) {
  this.compiler = compiler;
  this.validator = compiler.getTypeValidator();
  this.reverseInterpreter = reverseInterpreter;
  this.typeRegistry = typeRegistry;
  this.topScope = topScope;
  this.scopeCreator = scopeCreator;
  this.reportMissingOverride = reportMissingOverride;
  this.reportUnknownTypes = reportUnknownTypes;
  this.inferJSDocInfo = new InferJSDocInfo(compiler);
}
 
Example #10
Source File: Closure_96_TypeCheck_s.java    From coming with MIT License 6 votes vote down vote up
public TypeCheck(AbstractCompiler compiler,
    ReverseAbstractInterpreter reverseInterpreter,
    JSTypeRegistry typeRegistry,
    Scope topScope,
    ScopeCreator scopeCreator,
    CheckLevel reportMissingOverride,
    CheckLevel reportUnknownTypes) {
  this.compiler = compiler;
  this.validator = compiler.getTypeValidator();
  this.reverseInterpreter = reverseInterpreter;
  this.typeRegistry = typeRegistry;
  this.topScope = topScope;
  this.scopeCreator = scopeCreator;
  this.reportMissingOverride = reportMissingOverride;
  this.reportUnknownTypes = reportUnknownTypes;
  this.inferJSDocInfo = new InferJSDocInfo(compiler);
}
 
Example #11
Source File: ClosureCodingConvention.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the type for a type assertion, or null if the function asserts
 * that the node must not be null or undefined.
 */
@Override
public JSType getAssertedType(Node call, JSTypeRegistry registry) {
  if (call.getChildCount() > 2) {
    Node constructor = call.getFirstChild().getNext().getNext();
    if (constructor != null) {
      JSType ownerType = constructor.getJSType();
      if (ownerType != null
          && ownerType.isFunctionType()
          && ownerType.isConstructor()) {
        FunctionType functionType = ((FunctionType) ownerType);
        return functionType.getInstanceType();
      }
    }
  }
  return super.getAssertedType(call, registry);
}
 
Example #12
Source File: LinkFactory.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new link factory.
 *
 * @param dfs used to generate paths to documentation in the output file system.
 * @param typeRegistry used to lookup nominal types.
 * @param jsTypeRegistry used to lookup JavaScript types.
 * @param typeContext defines the context in which to resolve type names.
 * @param urlTemplate if provided, defines a template for links to source files.
 * @param pathContext the object, if any, to generate paths relative to in the output file system.
 *     If {@code null}, paths will be relative to the output root.
 */
LinkFactory(
    @Provided DossierFileSystem dfs,
    @Provided TypeRegistry typeRegistry,
    @Provided JSTypeRegistry jsTypeRegistry,
    @Provided NodeLibrary nodeLibrary,
    @Provided ModuleNamingConvention namingConvention,
    @Provided TypeContext typeContext,
    @Provided @SourceUrlTemplate Optional<String> urlTemplate,
    @Provided @TypeFilter Predicate<String> typeNameFilter,
    @Nullable NominalType pathContext) {
  this.dfs = dfs;
  this.typeRegistry = typeRegistry;
  this.jsTypeRegistry = jsTypeRegistry;
  this.nodeLibrary = nodeLibrary;
  this.namingConvention = namingConvention;
  this.pathContext = Optional.ofNullable(pathContext);
  this.typeContext = typeContext;
  this.urlTemplate = urlTemplate;
  this.typeNameFilter = typeNameFilter;
}
 
Example #13
Source File: Closure_2_TypeCheck_t.java    From coming with MIT License 6 votes vote down vote up
public TypeCheck(AbstractCompiler compiler,
    ReverseAbstractInterpreter reverseInterpreter,
    JSTypeRegistry typeRegistry,
    Scope topScope,
    MemoizedScopeCreator scopeCreator,
    CheckLevel reportMissingOverride,
    CheckLevel reportUnknownTypes) {
  this.compiler = compiler;
  this.validator = compiler.getTypeValidator();
  this.reverseInterpreter = reverseInterpreter;
  this.typeRegistry = typeRegistry;
  this.topScope = topScope;
  this.scopeCreator = scopeCreator;
  this.reportMissingOverride = reportMissingOverride;
  this.reportUnknownTypes = reportUnknownTypes;
  this.inferJSDocInfo = new InferJSDocInfo(compiler);
}
 
Example #14
Source File: Nopol2017_0051_s.java    From coming with MIT License 6 votes vote down vote up
public TypeCheck(AbstractCompiler compiler,
    ReverseAbstractInterpreter reverseInterpreter,
    JSTypeRegistry typeRegistry,
    Scope topScope,
    ScopeCreator scopeCreator,
    CheckLevel reportMissingOverride,
    CheckLevel reportUnknownTypes) {
  this.compiler = compiler;
  this.validator = compiler.getTypeValidator();
  this.reverseInterpreter = reverseInterpreter;
  this.typeRegistry = typeRegistry;
  this.topScope = topScope;
  this.scopeCreator = scopeCreator;
  this.reportMissingOverride = reportMissingOverride;
  this.reportUnknownTypes = reportUnknownTypes;
  this.inferJSDocInfo = new InferJSDocInfo(compiler);
}
 
Example #15
Source File: ClosureCodingConventionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testApplySubclassRelationship() {
  JSTypeRegistry registry = new JSTypeRegistry(null);

  Node nodeA = new Node(Token.FUNCTION);
  FunctionType ctorA = registry.createConstructorType("A", nodeA,
      new Node(Token.PARAM_LIST), null, null);

  Node nodeB = new Node(Token.FUNCTION);
  FunctionType ctorB = registry.createConstructorType("B", nodeB,
      new Node(Token.PARAM_LIST), null, null);

  conv.applySubclassRelationship(ctorA, ctorB, SubclassType.INHERITS);

  assertTrue(ctorB.getPrototype().hasOwnProperty("constructor"));
  assertEquals(nodeB, ctorB.getPrototype().getPropertyNode("constructor"));

  assertTrue(ctorB.hasOwnProperty("superClass_"));
  assertEquals(nodeB, ctorB.getPropertyNode("superClass_"));
}
 
Example #16
Source File: InlineProperties.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void buildInvalidatingTypeSet() {
  JSTypeRegistry registry = compiler.getTypeRegistry();
  invalidatingTypes = Sets.newHashSet(
      registry.getNativeType(JSTypeNative.ALL_TYPE),
      registry.getNativeType(JSTypeNative.NO_OBJECT_TYPE),
      registry.getNativeType(JSTypeNative.NO_TYPE),
      registry.getNativeType(JSTypeNative.NULL_TYPE),
      registry.getNativeType(JSTypeNative.VOID_TYPE),
      registry.getNativeType(JSTypeNative.FUNCTION_FUNCTION_TYPE),
      registry.getNativeType(JSTypeNative.FUNCTION_INSTANCE_TYPE),
      registry.getNativeType(JSTypeNative.FUNCTION_PROTOTYPE),
      registry.getNativeType(JSTypeNative.GLOBAL_THIS),
      registry.getNativeType(JSTypeNative.OBJECT_TYPE),
      registry.getNativeType(JSTypeNative.OBJECT_PROTOTYPE),
      registry.getNativeType(JSTypeNative.OBJECT_FUNCTION_TYPE),
      registry.getNativeType(JSTypeNative.TOP_LEVEL_PROTOTYPE),
      registry.getNativeType(JSTypeNative.UNKNOWN_TYPE));

  for (TypeMismatch mis : compiler.getTypeValidator().getMismatches()) {
    addInvalidatingType(mis.typeA);
    addInvalidatingType(mis.typeB);
  }
}
 
Example #17
Source File: NodeTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testCheckTreeTypeAwareEqualsSameNull() {
  TestErrorReporter testErrorReporter = new TestErrorReporter(null, null);
  JSTypeRegistry registry = new JSTypeRegistry(testErrorReporter);
  Node node1 = Node.newString(Token.NAME, "f");
  Node node2 = Node.newString(Token.NAME, "f");
  assertTrue(node1.isEquivalentToTyped(node2));
}
 
Example #18
Source File: Closure_2_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
public TypeCheck(AbstractCompiler compiler,
    ReverseAbstractInterpreter reverseInterpreter,
    JSTypeRegistry typeRegistry,
    CheckLevel reportMissingOverride,
    CheckLevel reportUnknownTypes) {
  this(compiler, reverseInterpreter, typeRegistry, null, null,
      reportMissingOverride, reportUnknownTypes);
}
 
Example #19
Source File: BaseJSTypeTestCase.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception {
  super.setUp();
  errorReporter = new TestErrorReporter(null, null);
  registry = new JSTypeRegistry(errorReporter);
  initTypes();
}
 
Example #20
Source File: DisambiguatePropertiesTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void runFindHighestTypeInChain() {
  // Check that this doesn't go into an infinite loop.
  DisambiguateProperties.forJSTypeSystem(new Compiler(),
      Maps.<String, CheckLevel>newHashMap())
      .getTypeWithProperty("no",
          new JSTypeRegistry(new TestErrorReporter(null, null))
          .getNativeType(JSTypeNative.OBJECT_PROTOTYPE));
}
 
Example #21
Source File: Closure_2_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
public TypeCheck(AbstractCompiler compiler,
    ReverseAbstractInterpreter reverseInterpreter,
    JSTypeRegistry typeRegistry,
    CheckLevel reportMissingOverride,
    CheckLevel reportUnknownTypes) {
  this(compiler, reverseInterpreter, typeRegistry, null, null,
      reportMissingOverride, reportUnknownTypes);
}
 
Example #22
Source File: BaseJSTypeTestCase.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private static void addMethod(
    JSTypeRegistry registry, ObjectType receivingType, String methodName,
    JSType returnType) {
  receivingType.defineDeclaredProperty(methodName,
      new FunctionBuilder(registry).withReturnType(returnType).build(),
      null);
}
 
Example #23
Source File: TypeRegistry.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
private JSType getInstanceType(final JSTypeRegistry jsRegistry, FunctionType ctor) {
  ObjectType instance = ctor.getInstanceType();
  if (ctor.getJSDocInfo() != null && !ctor.getJSDocInfo().getTemplateTypeNames().isEmpty()) {
    ImmutableList<JSType> templateTypes =
        ctor.getJSDocInfo()
            .getTemplateTypeNames()
            .stream()
            .map(jsRegistry::createTemplateType)
            .collect(toImmutableList());
    instance = jsRegistry.createTemplatizedType(instance, templateTypes);
  }
  return instance;
}
 
Example #24
Source File: TypeRegistry.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
/**
 * Recomputes the type hierarchy relationships for all nominal types in this registry using the
 * given global scope and JS registry.
 */
public void computeTypeRelationships(StaticTypedScope globalScope, JSTypeRegistry jsRegistry) {
  checkArgument(globalScope.getParentScope() == null, "not a global scope");

  knownImplementations.clear();
  subInterfaces.clear();
  directSubtypes.clear();
  implementedInterfaces.clear();

  Set<FunctionType> processed = new HashSet<>();
  for (NominalType nominalType : typesByName.values()) {
    JSType jsType = nominalType.getType();
    if (!jsType.isConstructor() && !jsType.isInterface()) {
      continue;
    }

    FunctionType ctor = jsType.toMaybeFunctionType();
    if (ctor == null || !processed.add(ctor)) {
      continue;
    }

    if (ctor.isInterface()) {
      scanExtendedInterfaces(new HashSet<>(), ctor);

    } else {
      scanImplementedInterfaces(ctor);
      computeTypeHiearchy(ctor, globalScope, jsRegistry);
    }
  }
}
 
Example #25
Source File: Closure_7_ChainableReverseAbstractInterpreter_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Constructs an interpreter, which is the only link in a chain. Interpreters
 * can be appended using {@link #append}.
 */
public ChainableReverseAbstractInterpreter(CodingConvention convention,
    JSTypeRegistry typeRegistry) {
  Preconditions.checkNotNull(convention);
  this.convention = convention;
  this.typeRegistry = typeRegistry;
  firstLink = this;
  nextLink = null;
}
 
Example #26
Source File: TypeContext.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
@Inject
TypeContext(
    TypeRegistry typeRegistry,
    JSTypeRegistry jsTypeRegistry,
    ModuleNamingConvention moduleNamingConvention,
    DossierFileSystem dfs) {
  this(typeRegistry, jsTypeRegistry, dfs, moduleNamingConvention, Optional.empty());
}
 
Example #27
Source File: Closure_66_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
public TypeCheck(AbstractCompiler compiler,
    ReverseAbstractInterpreter reverseInterpreter,
    JSTypeRegistry typeRegistry,
    CheckLevel reportMissingOverride,
    CheckLevel reportUnknownTypes) {
  this(compiler, reverseInterpreter, typeRegistry, null, null,
      reportMissingOverride, reportUnknownTypes);
}
 
Example #28
Source File: Closure_66_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
public TypeCheck(AbstractCompiler compiler,
    ReverseAbstractInterpreter reverseInterpreter,
    JSTypeRegistry typeRegistry,
    CheckLevel reportMissingOverride,
    CheckLevel reportUnknownTypes) {
  this(compiler, reverseInterpreter, typeRegistry, null, null,
      reportMissingOverride, reportUnknownTypes);
}
 
Example #29
Source File: ChainableReverseAbstractInterpreter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs an interpreter, which is the only link in a chain. Interpreters
 * can be appended using {@link #append}.
 */
public ChainableReverseAbstractInterpreter(CodingConvention convention,
    JSTypeRegistry typeRegistry) {
  Preconditions.checkNotNull(convention);
  this.convention = convention;
  this.typeRegistry = typeRegistry;
  firstLink = this;
  nextLink = null;
}
 
Example #30
Source File: TypeContext.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
private TypeContext(
    TypeRegistry typeRegistry,
    JSTypeRegistry jsTypeRegistry,
    DossierFileSystem dfs,
    ModuleNamingConvention moduleNamingConvention,
    Optional<NominalType> context) {
  this.typeRegistry = typeRegistry;
  this.jsTypeRegistry = jsTypeRegistry;
  this.dfs = dfs;
  this.moduleNamingConvention = moduleNamingConvention;
  this.context = context;
}