Exemplo n.º 1
0
  public void testLinenoCharnoObjectLiteral() throws Exception {
    Node n = parse("\n\n var a = {a:0\n,b :1};").getFirstChild().getFirstChild().getFirstChild();

    assertEquals(Token.OBJECTLIT, n.getType());
    assertEquals(3, n.getLineno());
    assertEquals(9, n.getCharno());

    Node key = n.getFirstChild();

    assertEquals(Token.STRING_KEY, key.getType());
    assertEquals(3, key.getLineno());
    assertEquals(10, key.getCharno());

    Node value = key.getFirstChild();

    assertEquals(Token.NUMBER, value.getType());
    assertEquals(3, value.getLineno());
    assertEquals(12, value.getCharno());

    key = key.getNext();

    assertEquals(Token.STRING_KEY, key.getType());
    assertEquals(4, key.getLineno());
    assertEquals(1, key.getCharno());

    value = key.getFirstChild();

    assertEquals(Token.NUMBER, value.getType());
    assertEquals(4, value.getLineno());
    assertEquals(4, value.getCharno());
  }
Exemplo n.º 2
0
  /**
   * Locates the array literal specified by the <code>array</code> node, in the source using the
   * source location information provided by the node, and inserts <code>str</code> as a string
   * literal (quoted) in the source at the end of the array.
   *
   * @param array Node with source position information
   * @param str the string to insert into the source at the end of the specified array
   */
  public void appendToArrayLit(Node array, String str) {
    final String sourceMethod = "appendToArrayLit"; // $NON-NLS-1$
    if (isTraceLogging) {
      log.entering(JSSource.class.getName(), sourceMethod, new Object[] {array, str});
    }
    // Get the node for the last element in the array
    Node lastChild = array.getLastChild();

    // If token is not a string, then punt
    if (lastChild.getType() != Token.STRING && lastChild.getType() != Token.NAME) {
      if (log.isLoggable(Level.WARNING)) {
        log.warning(
            "Last element of array at "
                + mid
                + "("
                + array.getLineno()
                + ","
                + array.getCharno()
                + ") is type "
                + array.getType()); // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
      }
      if (isTraceLogging) {
        log.exiting(JSSource.class.getName(), sourceMethod);
      }
      return;
    }
    int len =
        lastChild.getString().length()
            + (lastChild.getType() == Token.STRING ? 2 : 0); // add 2 for string quotes

    // Search the source for the closing array bracket ']', skipping over
    // whitespace and comments.  In order to be valid javascript, the next
    // token must be the closing bracket.
    int lineno = lastChild.getLineno();
    int charno = lastChild.getCharno() + len;
    PositionLocator pos = new PositionLocator(lineno, charno);
    if (pos.findNextJSToken() == ']') {
      insert(",\"" + str + "\"", pos.getLineno(), pos.getCharno()); // $NON-NLS-1$ //$NON-NLS-2$
    } else {
      if (log.isLoggable(Level.WARNING)) {
        log.warning(
            "Closing array bracket not found in "
                + mid
                + "("
                + lineno
                + ","
                + charno
                + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
      }
    }
    if (isTraceLogging) {
      log.exiting(JSSource.class.getName(), sourceMethod);
    }
  }
Exemplo n.º 3
0
  public void testLinenoCharnoGetProp2() throws Exception {
    Node getprop = parse("\n foo.\nbar").getFirstChild().getFirstChild();

    assertEquals(Token.GETPROP, getprop.getType());
    assertEquals(2, getprop.getLineno());
    assertEquals(1, getprop.getCharno());

    Node name = getprop.getFirstChild().getNext();
    assertEquals(Token.STRING, name.getType());
    assertEquals(3, name.getLineno());
    assertEquals(0, name.getCharno());
  }
Exemplo n.º 4
0
  public void testLinenoCharnoCall() throws Exception {
    Node call = parse("\n foo(123);").getFirstChild().getFirstChild();

    assertEquals(Token.CALL, call.getType());
    assertEquals(2, call.getLineno());
    assertEquals(1, call.getCharno());
  }
Exemplo n.º 5
0
  public void testLinenoCharnoAssign2() throws Exception {
    Node assign = parse("\n a.g.h.k    =  45").getFirstChild().getFirstChild();

    assertEquals(Token.ASSIGN, assign.getType());
    assertEquals(2, assign.getLineno());
    assertEquals(1, assign.getCharno());
  }
Exemplo n.º 6
0
  public void testLinenoCharnoHook() throws Exception {
    Node n = parse("\n a ? 9 : 0").getFirstChild().getFirstChild();

    assertEquals(Token.HOOK, n.getType());
    assertEquals(2, n.getLineno());
    assertEquals(1, n.getCharno());
  }
Exemplo n.º 7
0
  public void testLinenoCharnoForComparison() throws Exception {
    Node lt = parse("for (; i < j;){}").getFirstChild().getFirstChild().getNext();

    assertEquals(Token.LT, lt.getType());
    assertEquals(1, lt.getLineno());
    assertEquals(7, lt.getCharno());
  }
Exemplo n.º 8
0
  public void testLinenoCharnoGetelem3() throws Exception {
    Node call = parse("\n   \n (8 + kl)[123]").getFirstChild().getFirstChild();

    assertEquals(Token.GETELEM, call.getType());
    assertEquals(3, call.getLineno());
    assertEquals(1, call.getCharno());
  }
Exemplo n.º 9
0
  /**
   * Infers the type of {@code this}.
   *
   * @param info The JSDocInfo for this function.
   * @param owner The node for the object whose prototype "owns" this function. For example, {@code
   *     A} in the expression {@code A.prototype.foo}. May be null to indicate that this is not a
   *     prototype property.
   */
  FunctionTypeBuilder inferThisType(JSDocInfo info, @Nullable Node owner) {
    ObjectType maybeThisType = null;
    if (info != null && info.hasThisType()) {
      maybeThisType = ObjectType.cast(info.getThisType().evaluate(scope, typeRegistry));
    }
    if (maybeThisType != null) {
      thisType = maybeThisType;
      thisType.setValidator(new ThisTypeValidator());
    } else if (owner != null && (info == null || !info.hasType())) {
      // If the function is of the form:
      // x.prototype.y = function() {}
      // then we can assume "x" is the @this type. On the other hand,
      // if it's of the form:
      // /** @type {Function} */ x.prototype.y;
      // then we should not give it a @this type.
      String ownerTypeName = owner.getQualifiedName();
      ObjectType ownerType =
          ObjectType.cast(
              typeRegistry.getForgivingType(
                  scope, ownerTypeName, sourceName, owner.getLineno(), owner.getCharno()));
      if (ownerType != null) {
        thisType = ownerType;
      }
    }

    return this;
  }
Exemplo n.º 10
0
  public void testLinenoCharnoArrayLiteral() throws Exception {
    Node n = parse("\n  [8, 9]").getFirstChild().getFirstChild();

    assertEquals(Token.ARRAYLIT, n.getType());
    assertEquals(2, n.getLineno());
    assertEquals(2, n.getCharno());

    n = n.getFirstChild();

    assertEquals(Token.NUMBER, n.getType());
    assertEquals(2, n.getLineno());
    assertEquals(3, n.getCharno());

    n = n.getNext();

    assertEquals(Token.NUMBER, n.getType());
    assertEquals(2, n.getLineno());
    assertEquals(6, n.getCharno());
  }
Exemplo n.º 11
0
 /**
  * Gets the current char number, or zero if it cannot be determined. The line number is retrieved
  * lazily as a running time optimization.
  */
 public int getCharno() {
   Node cur = curNode;
   while (cur != null) {
     int line = cur.getCharno();
     if (line >= 0) {
       return line;
     }
     cur = cur.getParent();
   }
   return 0;
 }
Exemplo n.º 12
0
  private String formatNodePosition(Node n) {
    String sourceFileName = getBestSourceFileName(n);
    if (sourceFileName == null) {
      return MISSING_SOURCE + "\n";
    }

    int lineNumber = n.getLineno();
    int columnNumber = n.getCharno();
    String src = compiler.getSourceLine(sourceFileName, lineNumber);
    if (src == null) {
      src = MISSING_SOURCE;
    }
    return sourceFileName + ":" + lineNumber + ":" + columnNumber + "\n" + src + "\n";
  }
Exemplo n.º 13
0
  private void testLinenoCharnoBinop(String binop) {
    Node op = parse("var a = 89 " + binop + " 76").getFirstChild().getFirstChild().getFirstChild();

    assertEquals(1, op.getLineno());
    assertEquals(8, op.getCharno());
  }