Java Code Examples for com.google.javascript.jscomp.TypedVar#getName()

The following examples show how to use com.google.javascript.jscomp.TypedVar#getName() . 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: DeclarationGenerator.java    From clutz with MIT License 5 votes vote down vote up
/**
 * Special emit for emitting namespaces for typedefs as one-offs. Typedefs (type aliases in TS)
 * are special because their information does not come solely from a TypedVar object, but rather
 * from a pair of name (string) and type (JSType).
 *
 * <p>Note that this only handles default exports of typedefs. Typedefs as properties on an
 * already exported object (like class) are handled separately.
 */
private void declareTypedefNamespace(TypedVar typedef, JSType typedefType, Set<String> provides) {
  String typedefName = typedef.getName();
  String namespace = getNamespace(typedefName);

  emitGeneratedFromFileComment(typedef.getSourceFile());
  // Ideally we should be using declareNamespace here, but it cannot handle gluing the TypedVar
  // symbol with the additional JSType typedefType.
  emitNamespaceBegin(namespace);

  new TreeWalker(compiler.getTypeRegistry(), provides, false, false)
      .visitTypeAlias(typedefType, typedefName, false);

  emitNamespaceEnd();
}
 
Example 2
Source File: DeclarationGenerator.java    From clutz with MIT License 5 votes vote down vote up
private boolean isAliasedClassOrInterface(TypedVar symbol, JSType type) {
  // Confusingly typedefs are constructors. However, they cannot be aliased AFAICT.
  if (type.isNoType()) return false;
  if (!type.isConstructor() && !type.isInterface()) return false;
  String symbolName = symbol.getName();
  String typeName = type.getDisplayName();
  // Turns out that for aliases the symbol and type name differ.
  return !symbolName.equals(typeName) || KNOWN_CLASS_ALIASES.containsKey(symbolName);
}
 
Example 3
Source File: DeclarationGenerator.java    From clutz with MIT License 4 votes vote down vote up
/**
 * Closure does not require all types to be explicitly provided, if they are only used in type
 * positions. However, our emit phases only emits goog.provided symbols and namespaces, so this
 * extra pass is required, in order to have valid output.
 */
private void processUnprovidedTypes(Set<String> provides, Set<String> transitiveProvides) {
  /**
   * A new set of types can be discovered while visiting unprovided types. To prevent an infinite
   * loop in a pathological case, limit to a number of passes.
   *
   * <p>TODO(rado): investigate https://github.com/angular/clutz/pull/246 and removing this pass
   * altogether.
   */
  int maxTypeUsedDepth = 5;
  Set<String> typesEmitted = new LinkedHashSet<>();
  while (maxTypeUsedDepth > 0) {
    int typesUsedCount = typesUsed.size();
    // AFAICT, there is no api for going from type to symbol, so iterate all symbols first.
    for (TypedVar symbol : compiler.getTopScope().getAllSymbols()) {
      String name = symbol.getName();
      String namespace = getNamespace(name);
      // skip unused symbols, symbols already emitted or symbols whose namespace is emitted
      // (unless the symbols have their own provide).
      if (!typesUsed.contains(name)
          || typesEmitted.contains(name)
          || (!transitiveProvides.contains(name) && typesEmitted.contains(namespace))) {
        continue;
      }

      // skip provided symbols (as default or in an namespace).
      if (provides.contains(name)
          || (!transitiveProvides.contains(name) && provides.contains(namespace))) {
        continue;
      }
      // skip emit for provided inner symbols too as they are covered by the walkInnerSymbols
      // pass.
      if (isInnerSymbol(provides, name)) {
        continue;
      }

      // Skip extern symbols (they have a separate pass) and skip built-ins.
      // Built-ins can be indentified by having null as input file.
      CompilerInput symbolInput = this.compiler.getInput(new InputId(symbol.getInputName()));
      if (symbolInput == null || symbolInput.isExtern()) continue;

      // Type inference sometimes creates symbols for undeclared qualified names when narrowing
      // their type in a flow scope. These should not be emitted and can be detected by noticing
      // the declaration 'node' is not used as an lvalue.
      Node nameNode = symbol.getNode();
      if (!NodeUtil.isLValue(nameNode) && !nameNode.getParent().isExprResult()) {
        continue;
      }

      // A symbol with a name, but a null type is likely a typedef. DeclareNamespace cannot handle
      // this scenario, but declareTypedefNamespace
      if (symbol.getType() == null) {
        JSType typedef = compiler.getTypeRegistry().getGlobalType(name);
        if (typedef != null) {
          declareTypedefNamespace(symbol, typedef, Collections.emptySet());
          typesEmitted.add(name);
        }
        continue;
      }

      declareNamespace(
          namespace,
          symbol,
          name,
          /* isDefault */ true,
          Collections.<String>emptySet(),
          /* isExtern */ false);
      typesEmitted.add(name);
    }
    // if no new types seen, safely break out.
    if (typesUsed.size() == typesUsedCount) break;
    maxTypeUsedDepth--;
  }
}