Esempio n. 1
0
  public void testLinenoFunctionCall() {
    AstNode root = parse("\nfoo.\n" + "bar.\n" + "baz(1);");

    ExpressionStatement stmt = (ExpressionStatement) root.getFirstChild();
    FunctionCall fc = (FunctionCall) stmt.getExpression();
    // Line number should get closest to the actual paren.
    assertEquals(3, fc.getLineno());
  }
Esempio n. 2
0
 public void testJSDocAttachment2() {
   AstRoot root = parse("/** @type number */a.b;");
   assertNotNull(root.getComments());
   assertEquals(1, root.getComments().size());
   assertEquals("/** @type number */", root.getComments().first().getValue());
   ExpressionStatement st = (ExpressionStatement) root.getFirstChild();
   assertNotNull(st.getExpression().getJsDoc());
 }
Esempio n. 3
0
  public void testJSDocAttachment15() {
    AstRoot root = parse("/** @private */ x(); function f() {}");
    assertNotNull(root.getComments());
    assertEquals(1, root.getComments().size());

    ExpressionStatement st = (ExpressionStatement) root.getFirstChild();
    assertNotNull(st.getExpression().getJsDoc());
  }
Esempio n. 4
0
 public void testJSDocAttachment4() {
   AstRoot root = parse("(function() {/** should not be attached */})()");
   assertNotNull(root.getComments());
   assertEquals(1, root.getComments().size());
   ExpressionStatement st = (ExpressionStatement) root.getFirstChild();
   FunctionCall fc = (FunctionCall) st.getExpression();
   ParenthesizedExpression pe = (ParenthesizedExpression) fc.getTarget();
   assertNull(pe.getJsDoc());
 }
Esempio n. 5
0
  public void testLinenoGetElem() {
    AstRoot root = parse("\nfoo[123]");
    ExpressionStatement st = (ExpressionStatement) root.getFirstChild();
    AstNode n = st.getExpression();

    assertTrue(n instanceof ElementGet);
    assertEquals(Token.GETELEM, n.getType());
    assertEquals(1, n.getLineno());
  }
Esempio n. 6
0
  public void testLinenoCall() {
    AstRoot root = parse("\nfoo(123);");
    ExpressionStatement st = (ExpressionStatement) root.getFirstChild();
    AstNode n = st.getExpression();

    assertTrue(n instanceof FunctionCall);
    assertEquals(Token.CALL, n.getType());
    assertEquals(1, n.getLineno());
  }
Esempio n. 7
0
  public void testLinenoAssign() {
    AstRoot root = parse("\n\na = b");
    ExpressionStatement st = (ExpressionStatement) root.getFirstChild();
    AstNode n = st.getExpression();

    assertTrue(n instanceof Assignment);
    assertEquals(Token.ASSIGN, n.getType());
    assertEquals(2, n.getLineno());
  }
Esempio n. 8
0
 public void testJSDocAttachment9() {
   AstRoot root = parse("({/** attach me */ foo: 2});");
   assertNotNull(root.getComments());
   assertEquals(1, root.getComments().size());
   ExpressionStatement st = (ExpressionStatement) root.getFirstChild();
   ParenthesizedExpression pt = (ParenthesizedExpression) st.getExpression();
   ObjectLiteral lit = (ObjectLiteral) pt.getExpression();
   Name objLitKey = (Name) lit.getElements().get(0).getLeft();
   assertNotNull(objLitKey.getJsDoc());
 }
Esempio n. 9
0
 public void testJSDocAttachment10() {
   AstRoot root = parse("({foo: /** attach me */ (bar)});");
   assertNotNull(root.getComments());
   assertEquals(1, root.getComments().size());
   ExpressionStatement st = (ExpressionStatement) root.getFirstChild();
   ParenthesizedExpression pt = (ParenthesizedExpression) st.getExpression();
   ObjectLiteral lit = (ObjectLiteral) pt.getExpression();
   ParenthesizedExpression parens = (ParenthesizedExpression) lit.getElements().get(0).getRight();
   assertNotNull(parens.getJsDoc());
 }
Esempio n. 10
0
 public void testJSDocAttachment13() {
   AstRoot root = parse("({/** attach me */ get 'foo'() {}});");
   assertNotNull(root.getComments());
   assertEquals(1, root.getComments().size());
   ExpressionStatement st = (ExpressionStatement) root.getFirstChild();
   ParenthesizedExpression pt = (ParenthesizedExpression) st.getExpression();
   ObjectLiteral lit = (ObjectLiteral) pt.getExpression();
   StringLiteral stringLit = (StringLiteral) lit.getElements().get(0).getLeft();
   assertNotNull(stringLit.getJsDoc());
 }
Esempio n. 11
0
  public void testLinenoPlusEq() {
    AstRoot root = parse("\na\n" + "+=\n" + "b\n");
    ExpressionStatement expr = (ExpressionStatement) root.getFirstChild();
    Assignment assign = (Assignment) expr.getExpression();
    AstNode lhs = assign.getLeft();
    AstNode rhs = assign.getRight();

    assertEquals(1, lhs.getLineno());
    assertEquals(1, assign.getLineno());
    assertEquals(3, rhs.getLineno());
  }
Esempio n. 12
0
  public void testLinenoEq() {
    AstRoot root = parse("\na\n" + "==\n" + "b\n");
    ExpressionStatement expr = (ExpressionStatement) root.getFirstChild();
    InfixExpression compare = (InfixExpression) expr.getExpression();
    AstNode lhs = compare.getLeft();
    AstNode rhs = compare.getRight();

    assertEquals(1, lhs.getLineno());
    assertEquals(1, compare.getLineno());
    assertEquals(3, rhs.getLineno());
  }
Esempio n. 13
0
  public void testLinenoName() {
    AstNode root = parse("\na;\n" + "b.\n" + "c;\n");

    ExpressionStatement exprStmt = (ExpressionStatement) root.getFirstChild();
    AstNode aRef = exprStmt.getExpression();
    ExpressionStatement bExprStmt = (ExpressionStatement) exprStmt.getNext();
    AstNode bRef = bExprStmt.getExpression();

    assertEquals(1, aRef.getLineno());
    assertEquals(2, bRef.getLineno());
  }
Esempio n. 14
0
 public void testJSDocAttachment6() {
   AstRoot root = parse("({1: /** don't attach me */ 2, 3: 4});");
   assertNotNull(root.getComments());
   assertEquals(1, root.getComments().size());
   ExpressionStatement st = (ExpressionStatement) root.getFirstChild();
   ParenthesizedExpression pt = (ParenthesizedExpression) st.getExpression();
   ObjectLiteral lit = (ObjectLiteral) pt.getExpression();
   for (ObjectProperty el : lit.getElements()) {
     assertNull(el.getLeft().getJsDoc());
     assertNull(el.getRight().getJsDoc());
   }
 }
Esempio n. 15
0
  public void testLinenoConditional() {
    AstRoot root = parse("\na\n" + "    ?\n" + "    b\n" + "    :\n" + "    c\n" + "    ;\n");

    ExpressionStatement ex = (ExpressionStatement) root.getFirstChild();
    ConditionalExpression hook = (ConditionalExpression) ex.getExpression();
    AstNode condExpr = hook.getTestExpression();
    AstNode thenExpr = hook.getTrueExpression();
    AstNode elseExpr = hook.getFalseExpression();

    assertEquals(2, hook.getLineno());
    assertEquals(1, condExpr.getLineno());
    assertEquals(3, thenExpr.getLineno());
    assertEquals(5, elseExpr.getLineno());
  }
Esempio n. 16
0
  public void testLinenoPrefix() {
    AstRoot root = parse("\na++;\n" + "   --\n" + "   b;\n");

    ExpressionStatement first = (ExpressionStatement) root.getFirstChild();
    ExpressionStatement secondStmt = (ExpressionStatement) first.getNext();
    UnaryExpression firstOp = (UnaryExpression) first.getExpression();
    UnaryExpression secondOp = (UnaryExpression) secondStmt.getExpression();
    AstNode firstVarRef = firstOp.getOperand();
    AstNode secondVarRef = secondOp.getOperand();

    assertEquals(1, firstOp.getLineno());
    assertEquals(2, secondOp.getLineno());
    assertEquals(1, firstVarRef.getLineno());
    assertEquals(3, secondVarRef.getLineno());
  }
Esempio n. 17
0
  public void testLinenoDeclaration() {
    AstNode root = parse("\na.\n" + "b=\n" + "function() {};\n");

    ExpressionStatement exprStmt = (ExpressionStatement) root.getFirstChild();
    Assignment fnAssignment = (Assignment) exprStmt.getExpression();
    PropertyGet aDotbName = (PropertyGet) fnAssignment.getLeft();
    AstNode aName = aDotbName.getLeft();
    AstNode bName = aDotbName.getRight();
    FunctionNode fnNode = (FunctionNode) fnAssignment.getRight();

    assertEquals(1, fnAssignment.getLineno());
    assertEquals(1, aDotbName.getLineno());
    assertEquals(1, aName.getLineno());
    assertEquals(2, bName.getLineno());
    assertEquals(3, fnNode.getLineno());
  }
Esempio n. 18
0
  public void testLinenoComma() {
    AstRoot root = parse("\na,\n" + "    b,\n" + "    c;\n");

    ExpressionStatement stmt = (ExpressionStatement) root.getFirstChild();
    InfixExpression comma1 = (InfixExpression) stmt.getExpression();
    InfixExpression comma2 = (InfixExpression) comma1.getLeft();
    AstNode cRef = comma1.getRight();
    AstNode aRef = comma2.getLeft();
    AstNode bRef = comma2.getRight();

    assertEquals(1, comma1.getLineno());
    assertEquals(1, comma2.getLineno());
    assertEquals(1, aRef.getLineno());
    assertEquals(2, bRef.getLineno());
    assertEquals(3, cRef.getLineno());
  }
Esempio n. 19
0
  public void testLinenoGetProp() {
    AstRoot root = parse("\nfoo.bar");
    ExpressionStatement st = (ExpressionStatement) root.getFirstChild();
    AstNode n = st.getExpression();

    assertTrue(n instanceof PropertyGet);
    assertEquals(Token.GETPROP, n.getType());
    assertEquals(1, n.getLineno());

    PropertyGet getprop = (PropertyGet) n;
    AstNode m = getprop.getRight();

    assertTrue(m instanceof Name);
    assertEquals(Token.NAME, m.getType()); // used to be Token.STRING!
    assertEquals(1, m.getLineno());
  }
Esempio n. 20
0
  public void testLinenoReturn() {
    AstRoot root =
        parse(
            "\nfunction\n"
                + "    foo(\n"
                + "    a,\n"
                + "    b,\n"
                + "    c) {\n"
                + "    return\n"
                + "    4;\n"
                + "}\n");
    FunctionNode function = (FunctionNode) root.getFirstChild();
    Name functionName = function.getFunctionName();

    AstNode body = function.getBody();
    ReturnStatement returnStmt = (ReturnStatement) body.getFirstChild();
    ExpressionStatement exprStmt = (ExpressionStatement) returnStmt.getNext();
    AstNode returnVal = exprStmt.getExpression();

    assertEquals(6, returnStmt.getLineno());
    assertEquals(7, exprStmt.getLineno());
    assertEquals(7, returnVal.getLineno());
  }
Esempio n. 21
0
  public void testLinenoSwitch() {
    AstRoot root =
        parse(
            "\nswitch (a) {\n"
                + "   case\n"
                + "     1:\n"
                + "     b++;\n"
                + "   case 2:\n"
                + "   default:\n"
                + "     b--;\n"
                + "  }\n");

    SwitchStatement switchStmt = (SwitchStatement) root.getFirstChild();
    AstNode switchVar = switchStmt.getExpression();
    List<SwitchCase> cases = switchStmt.getCases();
    SwitchCase firstCase = cases.get(0);
    AstNode caseArg = firstCase.getExpression();
    List<AstNode> caseBody = firstCase.getStatements();
    ExpressionStatement exprStmt = (ExpressionStatement) caseBody.get(0);
    UnaryExpression incrExpr = (UnaryExpression) exprStmt.getExpression();
    AstNode incrVar = incrExpr.getOperand();

    SwitchCase secondCase = cases.get(1);
    AstNode defaultCase = cases.get(2);
    AstNode returnStmt = (AstNode) switchStmt.getNext();

    assertEquals(1, switchStmt.getLineno());
    assertEquals(1, switchVar.getLineno());
    assertEquals(2, firstCase.getLineno());
    assertEquals(3, caseArg.getLineno());
    assertEquals(4, exprStmt.getLineno());
    assertEquals(4, incrExpr.getLineno());
    assertEquals(4, incrVar.getLineno());
    assertEquals(5, secondCase.getLineno());
    assertEquals(6, defaultCase.getLineno());
  }
 @Override
 public AstNode expressionStatement(AstNode expr) {
   ExpressionStatement e = new ExpressionStatement();
   e.setExpression(expr);
   return e;
 }
Esempio n. 23
0
 public void testParseUnicodeFormatStringLiteral() {
   AstRoot root = parse("'A\u200DB'");
   ExpressionStatement st = (ExpressionStatement) root.getFirstChild();
   StringLiteral stringLit = (StringLiteral) st.getExpression();
   assertEquals("A\u200DB", stringLit.getValue());
 }