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

The following examples show how to use com.google.javascript.rhino.jstype.Property. 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: JsDocTest.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
@Test
public void extractsDefineComments_commentInlineWithAnnotation() {
  util.compile(
      path("foo/bar.js"),
      "goog.provide('foo');",
      "",
      "/**",
      " * @define {boolean} Hello, world!",
      " *     Goodbye, world!",
      " */",
      "foo.bar = false;");
  NominalType type = getOnlyElement(typeRegistry.getAllTypes());
  assertThat(type.getName()).isEqualTo("foo");

  Property property = getOnlyElement(getProperties(type));
  assertThat(property.getName()).isEqualTo("bar");

  JsDoc doc = JsDoc.from(property.getJSDocInfo());
  assertThat(doc).isNotNull();
  assertThat(doc.getBlockComment()).isEqualTo("Hello, world!\n" + "     Goodbye, world!");
}
 
Example #2
Source File: JsDocTest.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
@Test
public void extractsDefineComments_blockCommentAboveAnnotation() {
  util.compile(
      path("foo/bar.js"),
      "goog.provide('foo');",
      "",
      "/**",
      " * Hello, world!",
      " * @define {boolean}",
      " */",
      "foo.bar = false;");
  NominalType type = getOnlyElement(typeRegistry.getAllTypes());
  assertThat(type.getName()).isEqualTo("foo");

  Property property = getOnlyElement(getProperties(type));
  assertThat(property.getName()).isEqualTo("bar");

  JsDoc doc = JsDoc.from(property.getJSDocInfo());
  assertThat(doc).isNotNull();
  assertThat(doc.getBlockComment()).isEqualTo("Hello, world!");
}
 
Example #3
Source File: JsDocTest.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
@Test
public void parsesReturnDescription_manyLines() {
  util.compile(
      path("foo/bar.js"),
      "/** @constructor */",
      "var Foo = function() {};",
      "/**",
      " * @return nothing over",
      " *     many",
      " *     lines.",
      " */",
      "Foo.bar = function() { return ''; };");
  NominalType foo = getOnlyElement(typeRegistry.getAllTypes());
  Property bar = getOnlyElement(getProperties(foo));
  assertEquals("bar", bar.getName());
  assertTrue(bar.getType().isFunctionType());
  assertEquals(
      "nothing over\n     many\n     lines.",
      JsDoc.from(bar.getJSDocInfo()).getReturnClause().getDescription());
}
 
Example #4
Source File: JsDocTest.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
@Test
public void parsesReturnDescription_twoLines() {
  util.compile(
      path("foo/bar.js"),
      "/** @constructor */",
      "var Foo = function() {};",
      "/**",
      " * @return nothing over",
      " *     two lines.",
      " */",
      "Foo.bar = function() { return ''; };");
  NominalType foo = getOnlyElement(typeRegistry.getAllTypes());
  Property bar = getOnlyElement(getProperties(foo));
  assertEquals("bar", bar.getName());
  assertTrue(bar.getType().isFunctionType());
  assertEquals(
      "nothing over\n     two lines.",
      JsDoc.from(bar.getJSDocInfo()).getReturnClause().getDescription());
}
 
Example #5
Source File: JsDocTest.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
@Test
public void parsesReturnDescription_oneLineB() {
  util.compile(
      path("foo/bar.js"),
      "/** @constructor */",
      "var Foo = function() {};",
      "/**",
      " * @return nothing.",
      " */",
      "Foo.bar = function() { return ''; };");
  NominalType foo = getOnlyElement(typeRegistry.getAllTypes());
  Property bar = getOnlyElement(getProperties(foo));
  assertEquals("bar", bar.getName());
  assertTrue(bar.getType().isFunctionType());
  assertEquals("nothing.", JsDoc.from(bar.getJSDocInfo()).getReturnClause().getDescription());
}
 
Example #6
Source File: TypeExpressionParserTest.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
@Test
public void parseExpression_typeIsReferenceToNullableExtern() {
  util.compile(
      createSourceFile(
          fs.getPath("source/modules/one.js"),
          "var stream = require('stream');",
          "/** @constructor */",
          "function Writer() {}",
          "/** @type {stream.Stream} */",
          "Writer.prototype.stream = null;",
          "exports.Writer = Writer"));

  NominalType type = typeRegistry.getType("module$exports$module$source$modules$one.Writer");

  Property property = type.getType().toMaybeFunctionType().getInstanceType().getSlot("stream");
  TypeExpression expression =
      parserFactory.create(linkFactoryBuilder.create(type)).parse(property.getType());
  assertThat(expression).isEqualTo(unionType(namedTypeExpression("stream.Stream"), NULL_TYPE));
}
 
Example #7
Source File: TypeExpressionParser.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
private void caseRecordType(final ObjectType type) {
  Iterable<Property> properties =
      type.getOwnPropertyNames()
          .stream()
          .map(type::getOwnSlot)
          .filter(input -> input != null && !input.getType().isNoType())
          .sorted(Comparator.comparing(Property::getName))
          .collect(toList());

  RecordType.Builder recordType = currentExpression().getRecordTypeBuilder();
  for (Property property : properties) {
    RecordType.Entry.Builder entry = recordType.addEntryBuilder();
    entry.setKey(property.getName());
    expressions.addLast(entry.getValueBuilder());
    property.getType().visit(this);
    expressions.removeLast();
  }
}
 
Example #8
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
private Map<String, InstanceProperty> getOwnProperties(ObjectType object) {
  ObjectType definingType = object;
  if (definingType.isFunctionPrototypeType()) {
    definingType = definingType.getOwnerFunction();
  } else if (definingType.isInstanceType()) {
    definingType = definingType.getConstructor();
  }

  Map<String, InstanceProperty> properties = new HashMap<>();
  for (String name : object.getOwnPropertyNames()) {
    if (!"constructor".equals(name)) {
      Property property = object.getOwnSlot(name);
      properties.put(
          property.getName(),
          InstanceProperty.builder()
              .setOwnerType(getFirst(registry.getTypes(definingType), null))
              .setDefinedByType(definingType)
              .setName(property.getName())
              .setType(getType(object, property))
              .setNode(property.getNode())
              .setJsDoc(JsDoc.from(property.getJSDocInfo()))
              .build());
    }
  }
  return properties;
}
 
Example #9
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
private com.github.jsdossier.proto.Property getPropertyData(
    String name,
    JSType type,
    Node node,
    PropertyDocs docs,
    @Nullable DefinedByType definedBy,
    Iterable<InstanceProperty> overrides) {
  com.github.jsdossier.proto.Property.Builder builder =
      com.github.jsdossier.proto.Property.newBuilder()
          .setBase(getBasePropertyDetails(name, type, node, docs, definedBy, overrides));

  TypeExpressionParser parser =
      expressionParserFactory.create(linkFactory.withTypeContext(docs.getContextType()));
  if (docs.getJsDoc().getType() != null) {
    JSTypeExpression typeExpression = docs.getJsDoc().getType();
    type = evaluate(typeExpression);
  }

  if (type != null) {
    TypeExpression expression = parser.parse(type);
    builder.setType(expression);
  }

  return builder.build();
}
 
Example #10
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
private static JSType getType(ObjectType object, Property property) {
  JSType type = object.findPropertyType(property.getName());
  if (type != null && type.isUnknownType()) {
    type = property.getType();
  }
  return type;
}
 
Example #11
Source File: JsDocTest.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
private List<Property> getProperties(NominalType type) {
  List<Property> properties = new ArrayList<>();
  for (String name : type.getType().toObjectType().getOwnPropertyNames()) {
    Property property = type.getType().toObjectType().getOwnSlot(name);
    if (property != null) {
      properties.add(property);
    }
  }
  return properties;
}
 
Example #12
Source File: JsDocTest.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
@Test
public void parsesReturnDescription_oneLineA() {
  util.compile(
      path("foo/bar.js"),
      "/** @constructor */",
      "var Foo = function() {};",
      "/** @return nothing. */",
      "Foo.bar = function() { return ''; };");
  NominalType foo = getOnlyElement(typeRegistry.getAllTypes());
  Property bar = getOnlyElement(getProperties(foo));
  assertEquals("bar", bar.getName());
  assertTrue(bar.getType().isFunctionType());
  assertEquals("nothing.", JsDoc.from(bar.getJSDocInfo()).getReturnClause().getDescription());
}
 
Example #13
Source File: MemberCollector.java    From jsinterop-generator with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean visitEnumMember(Property enumMember) {
  Field enumField = new Field();
  enumField.setType(getJavaTypeRegistry().createTypeReference(enumMember.getType()));
  enumField.setName(enumMember.getName());
  enumField.setEnumConstant(true);

  getCurrentJavaType().addField(enumField);
  return true;
}
 
Example #14
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
/** Returns the raw properties for the given type. */
public List<Property> getProperties(NominalType nominalType) {
  JSType type = nominalType.getType();
  ObjectType object = ObjectType.cast(type);
  if (object == null) {
    return ImmutableList.of();
  }

  List<Property> properties = new ArrayList<>();
  for (String name : object.getOwnPropertyNames()) {
    Property property = null;
    if (type.isFunctionType()) {
      if (!isBuiltInFunctionProperty(type, name)) {
        property = object.getOwnSlot(name);
      }
    } else if (!"prototype".equals(name)) {
      property = object.getOwnSlot(name);
    }

    if (property != null) {
      if (property.getType().isConstructor()
          && isConstructorTypeDefinition(
              property.getType(), JsDoc.from(property.getJSDocInfo()))) {
        continue;
      }

      // If the property is another module and the inspected type is also a module, then count
      // the property as a static property. Otherwise, if the property i registered as a nominal
      // type, it does not count as a static property. It should also be ignored if it is not
      // registered as a nominal type, but its qualified name has been filtered out by the user.
      String qualifiedName = nominalType.getName() + "." + property.getName();
      if (((inspectedType.isModuleExports() && registry.isModule(property.getType()))
              || registry.getTypes(property.getType()).isEmpty())
          && !typeFilter.test(qualifiedName)) {
        properties.add(property);
      }
    }
  }
  return properties;
}
 
Example #15
Source File: RenderDocumentationTaskSupplier.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
private void addInstanceProperties(JsType.Builder spec) {
  TypeInspector.Report report = typeInspector.inspectInstanceType();
  for (com.github.jsdossier.proto.Property prop : report.getProperties()) {
    spec.addField(prop);
    updateInstancePropertyIndex(spec, prop.getBase());
  }
  for (com.github.jsdossier.proto.Function func : report.getFunctions()) {
    spec.addMethod(func);
    updateInstancePropertyIndex(spec, func.getBase());
  }
}
 
Example #16
Source File: TypeCollector.java    From jsinterop-generator with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean visitModule(StaticTypedSlot module) {
  String jsFqn =
      module instanceof Property
          ? ((Property) module).getNode().getOriginalQualifiedName()
          : module.getName();

  Type javaType = createJavaType(jsFqn, NAMESPACE, false);
  getJavaTypeRegistry().registerJavaType(module.getType(), javaType);
  super.pushCurrentJavaType(javaType);
  return true;
}
 
Example #17
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
private com.github.jsdossier.proto.Property getPropertyData(
    String name, JSType type, Node node, PropertyDocs docs) {
  return getPropertyData(name, type, node, docs, null, ImmutableList.of());
}
 
Example #18
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
public abstract ImmutableList.Builder<com.github.jsdossier.proto.Property>
propertiesBuilder();
 
Example #19
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
public abstract ImmutableList.Builder<com.github.jsdossier.proto.Property>
compilerConstantsBuilder();
 
Example #20
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
/**
 * Extracts information on the properties defined directly on the given nominal type. For classes
 * and interfaces, this will return information on the <em>static</em> properties, not instance
 * properties.
 */
public Report inspectType() {
  List<Property> properties = getProperties(inspectedType);
  if (properties.isEmpty()) {
    return Report.empty();
  }

  properties.sort(comparing(Property::getName));
  Report.Builder report = Report.builder();

  for (Property property : properties) {
    String name = property.getName();
    if (!inspectedType.isModuleExports() && !inspectedType.isNamespace()) {
      String typeName = dfs.getDisplayName(inspectedType);
      int index = typeName.lastIndexOf('.');
      if (index != -1) {
        typeName = typeName.substring(index + 1);
      }
      name = typeName + "." + name;
    }

    PropertyDocs docs = findStaticPropertyJsDoc(inspectedType, property);
    JsDoc jsdoc = docs.getJsDoc();

    if (jsdoc.getVisibility() == JSDocInfo.Visibility.PRIVATE
        || (name.endsWith(".superClass_") && property.getType().isFunctionPrototypeType())) {
      continue;
    }

    if (jsdoc.isDefine()) {
      name = dfs.getQualifiedDisplayName(inspectedType) + "." + property.getName();
      report
          .compilerConstantsBuilder()
          .add(getPropertyData(name, property.getType(), property.getNode(), docs));

    } else if (property.getType().isFunctionType()) {
      report
          .functionsBuilder()
          .add(
              getFunctionData(
                  name, property.getType().toMaybeFunctionType(), property.getNode(), docs));

    } else if (!property.getType().isEnumElementType()) {
      report
          .propertiesBuilder()
          .add(getPropertyData(name, property.getType(), property.getNode(), docs));
    }
  }

  return report.build();
}
 
Example #21
Source File: PropertyNameComparator.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(Property a, Property b) {
  return a.getName().compareTo(b.getName());
}
 
Example #22
Source File: RenderDocumentationTaskSupplier.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
private void addEnumValues(JsType.Builder spec) {
  if (!type.getType().isEnumType()) {
    return;
  }
  JSType elementType = ((EnumType) type.getType()).getElementsType();
  JSDocInfo.Visibility visibility = typeRegistry.getVisibility(type);

  Enumeration.Builder enumBuilder =
      spec.getEnumerationBuilder()
          .setType(
              expressionParserFactory
                  .create(linkFactory)
                  .parse(elementType.toMaybeEnumElementType().getPrimitiveType()));
  if (JSDocInfo.Visibility.PUBLIC != visibility) {
    Protos.setVisibility(enumBuilder.getVisibilityBuilder(), visibility);
  }

  // Type may be documented as an enum without an associated object literal for us to analyze:
  //     /** @enum {string} */ namespace.foo;
  List<Property> properties = typeInspector.getProperties(type);
  properties.sort(comparing(Property::getName));
  for (Property property : properties) {
    if (!property.getType().isEnumElementType()) {
      continue;
    }

    Node node = property.getNode();
    JSDocInfo valueInfo = node == null ? null : node.getJSDocInfo();

    Enumeration.Value.Builder valueBuilder =
        enumBuilder.addValueBuilder().setName(property.getName());

    if (valueInfo != null) {
      JsDoc valueJsDoc = JsDoc.from(valueInfo);
      valueBuilder.setDescription(
          parser.parseComment(valueJsDoc.getBlockComment(), linkFactory));

      if (valueJsDoc.isDeprecated()) {
        valueBuilder.setDeprecation(getDeprecation(valueJsDoc));
      }
    }
  }
}
 
Example #23
Source File: TypedScopeCreator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private void finishConstructorDefinition(
    Node n, String variableName, FunctionType fnType,
    Scope scopeToDeclareIn, CompilerInput input, Var newVar) {
  // Declare var.prototype in the scope chain.
  FunctionType superClassCtor = fnType.getSuperClassConstructor();
  Property prototypeSlot = fnType.getSlot("prototype");

  // When we declare the function prototype implicitly, we
  // want to make sure that the function and its prototype
  // are declared at the same node. We also want to make sure
  // that the if a symbol has both a Var and a JSType, they have
  // the same node.
  //
  // This consistency is helpful to users of SymbolTable,
  // because everything gets declared at the same place.
  prototypeSlot.setNode(n);

  String prototypeName = variableName + ".prototype";

  // There are some rare cases where the prototype will already
  // be declared. See TypedScopeCreatorTest#testBogusPrototypeInit.
  // Fortunately, other warnings will complain if this happens.
  Var prototypeVar = scopeToDeclareIn.getVar(prototypeName);
  if (prototypeVar != null && prototypeVar.scope == scopeToDeclareIn) {
    scopeToDeclareIn.undeclare(prototypeVar);
  }

  scopeToDeclareIn.declare(prototypeName,
      n, prototypeSlot.getType(), input,
      /* declared iff there's an explicit supertype */
      superClassCtor == null ||
      superClassCtor.getInstanceType().isEquivalentTo(
          getNativeType(OBJECT_TYPE)));

  // Make sure the variable is initialized to something if
  // it constructs itself.
  if (newVar.getInitialValue() == null &&
      !n.isFromExterns()) {
    compiler.report(
        JSError.make(sourceName, n,
            fnType.isConstructor() ?
            CTOR_INITIALIZER : IFACE_INITIALIZER,
            variableName));
  }
}
 
Example #24
Source File: AbstractClosureVisitor.java    From jsinterop-generator with Apache License 2.0 4 votes vote down vote up
protected boolean visitEnumMember(Property enumMember) {
  return true;
}
 
Example #25
Source File: AbstractClosureVisitor.java    From jsinterop-generator with Apache License 2.0 4 votes vote down vote up
private void acceptEnumMember(Property enumMember) {
  visitEnumMember(enumMember);
}
 
Example #26
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 votes vote down vote up
public abstract ImmutableList<com.github.jsdossier.proto.Property> getProperties(); 
Example #27
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 votes vote down vote up
public abstract ImmutableList<com.github.jsdossier.proto.Property> getCompilerConstants();