Example #1
0
  public void testObjectLitLocation() {
    AstNode root =
        parse(
            "\nvar foo =\n"
                + "{ \n"
                + "'A' : 'A', \n"
                + "'B' : 'B', \n"
                + "'C' : \n"
                + "      'C' \n"
                + "};\n");

    VariableDeclaration firstVarDecl = (VariableDeclaration) root.getFirstChild();
    List<VariableInitializer> vars1 = firstVarDecl.getVariables();
    VariableInitializer firstInitializer = vars1.get(0);
    Name firstVarName = (Name) firstInitializer.getTarget();

    ObjectLiteral objectLiteral = (ObjectLiteral) firstInitializer.getInitializer();
    List<ObjectProperty> props = objectLiteral.getElements();
    ObjectProperty firstObjectLit = props.get(0);
    ObjectProperty secondObjectLit = props.get(1);
    ObjectProperty thirdObjectLit = props.get(2);

    AstNode firstKey = firstObjectLit.getLeft();
    AstNode firstValue = firstObjectLit.getRight();
    AstNode secondKey = secondObjectLit.getLeft();
    AstNode secondValue = secondObjectLit.getRight();
    AstNode thirdKey = thirdObjectLit.getLeft();
    AstNode thirdValue = thirdObjectLit.getRight();

    assertEquals(1, firstVarName.getLineno());
    assertEquals(2, objectLiteral.getLineno());
    assertEquals(3, firstObjectLit.getLineno());
    assertEquals(3, firstKey.getLineno());
    assertEquals(3, firstValue.getLineno());

    assertEquals(4, secondKey.getLineno());
    assertEquals(4, secondValue.getLineno());

    assertEquals(5, thirdKey.getLineno());
    assertEquals(6, thirdValue.getLineno());
  }