Java Code Examples for com.google.javascript.rhino.jstype.JSTypeRegistry#getNativeType()

The following examples show how to use com.google.javascript.rhino.jstype.JSTypeRegistry#getNativeType() . 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: 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 2
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 3
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
TypeInspector(
    @Provided DossierFileSystem dfs,
    @Provided CommentParser parser,
    @Provided TypeRegistry registry,
    @Provided StaticTypedScope globalScope,
    @Provided JSTypeRegistry jsRegistry,
    @Provided @TypeFilter Predicate<String> typeFilter,
    @Provided TypeExpressionParserFactory expressionParserFactory,
    @Provided LinkFactoryBuilder linkFactoryBuilder,
    NominalType inspectedType) {
  this.dfs = dfs;
  this.parser = parser;
  this.registry = registry;
  this.globalScope = globalScope;
  this.jsRegistry = jsRegistry;
  this.expressionParserFactory = expressionParserFactory;
  this.typeFilter = typeFilter;
  this.linkFactory = linkFactoryBuilder.create(inspectedType);
  this.inspectedType = inspectedType;

  JSType type = inspectedType.getType();
  if (type.isConstructor() || type.isInterface()) {
    type = type.toMaybeFunctionType().getInstanceType();
  } else {
    type = jsRegistry.getNativeType(JSTypeNative.UNKNOWN_TYPE);
  }
  typeMapReplacer =
      TemplateTypeReplacer.forPartialReplacement(jsRegistry, type.getTemplateTypeMap());
}
 
Example 4
Source File: TypeValidatorTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private TypeMismatch fromNatives(JSTypeNative a, JSTypeNative b) {
  JSTypeRegistry registry = compiler.getTypeRegistry();
  return new TypeMismatch(
      registry.getNativeType(a), registry.getNativeType(b), null);
}
 
Example 5
Source File: ConcreteTypeTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void setUp() {
  typeRegistry = new JSTypeRegistry(new TestErrorReporter(null, null));
  unknownType = typeRegistry.getNativeType(JSTypeNative.UNKNOWN_TYPE);
  factory = new FakeFactory();
}
 
Example 6
Source File: CodingConvention.java    From astor with GNU General Public License v2.0 2 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.
 */
public JSType getAssertedType(Node call, JSTypeRegistry registry) {
  return assertedType != null ? registry.getNativeType(assertedType) : null;
}