public void testLinenoInfix() { AstRoot root = parse( "\nvar d = a\n" + " + \n" + " b;\n" + "var\n" + " e =\n" + " a +\n" + " c;\n" + "var f = b\n" + " / c;\n"); VariableDeclaration stmt1 = (VariableDeclaration) root.getFirstChild(); List<VariableInitializer> vars1 = stmt1.getVariables(); VariableInitializer var1 = vars1.get(0); Name firstVarName = (Name) var1.getTarget(); InfixExpression var1Add = (InfixExpression) var1.getInitializer(); VariableDeclaration stmt2 = (VariableDeclaration) stmt1.getNext(); List<VariableInitializer> vars2 = stmt2.getVariables(); VariableInitializer var2 = vars2.get(0); Name secondVarName = (Name) var2.getTarget(); InfixExpression var2Add = (InfixExpression) var2.getInitializer(); VariableDeclaration stmt3 = (VariableDeclaration) stmt2.getNext(); List<VariableInitializer> vars3 = stmt3.getVariables(); VariableInitializer var3 = vars3.get(0); Name thirdVarName = (Name) var3.getTarget(); InfixExpression thirdVarDiv = (InfixExpression) var3.getInitializer(); ReturnStatement returnStmt = (ReturnStatement) stmt3.getNext(); assertEquals(1, var1.getLineno()); assertEquals(1, firstVarName.getLineno()); assertEquals(1, var1Add.getLineno()); assertEquals(1, var1Add.getLeft().getLineno()); assertEquals(3, var1Add.getRight().getLineno()); // var directive with name on next line wrong -- // should be 6. assertEquals(5, var2.getLineno()); assertEquals(5, secondVarName.getLineno()); assertEquals(6, var2Add.getLineno()); assertEquals(6, var2Add.getLeft().getLineno()); assertEquals(7, var2Add.getRight().getLineno()); assertEquals(8, var3.getLineno()); assertEquals(8, thirdVarName.getLineno()); assertEquals(8, thirdVarDiv.getLineno()); assertEquals(8, thirdVarDiv.getLeft().getLineno()); assertEquals(9, thirdVarDiv.getRight().getLineno()); }
public void testLinenoLiteral() { AstRoot root = parse( "\nvar d =\n" + " \"foo\";\n" + "var e =\n" + " 1;\n" + "var f = \n" + " 1.2;\n" + "var g = \n" + " 2e5;\n" + "var h = \n" + " 'bar';\n"); VariableDeclaration stmt1 = (VariableDeclaration) root.getFirstChild(); List<VariableInitializer> vars1 = stmt1.getVariables(); VariableInitializer firstVar = vars1.get(0); Name firstVarName = (Name) firstVar.getTarget(); AstNode firstVarLiteral = firstVar.getInitializer(); VariableDeclaration stmt2 = (VariableDeclaration) stmt1.getNext(); List<VariableInitializer> vars2 = stmt2.getVariables(); VariableInitializer secondVar = vars2.get(0); Name secondVarName = (Name) secondVar.getTarget(); AstNode secondVarLiteral = secondVar.getInitializer(); VariableDeclaration stmt3 = (VariableDeclaration) stmt2.getNext(); List<VariableInitializer> vars3 = stmt3.getVariables(); VariableInitializer thirdVar = vars3.get(0); Name thirdVarName = (Name) thirdVar.getTarget(); AstNode thirdVarLiteral = thirdVar.getInitializer(); VariableDeclaration stmt4 = (VariableDeclaration) stmt3.getNext(); List<VariableInitializer> vars4 = stmt4.getVariables(); VariableInitializer fourthVar = vars4.get(0); Name fourthVarName = (Name) fourthVar.getTarget(); AstNode fourthVarLiteral = fourthVar.getInitializer(); VariableDeclaration stmt5 = (VariableDeclaration) stmt4.getNext(); List<VariableInitializer> vars5 = stmt5.getVariables(); VariableInitializer fifthVar = vars5.get(0); Name fifthVarName = (Name) fifthVar.getTarget(); AstNode fifthVarLiteral = fifthVar.getInitializer(); assertEquals(2, firstVarLiteral.getLineno()); assertEquals(4, secondVarLiteral.getLineno()); assertEquals(6, thirdVarLiteral.getLineno()); assertEquals(8, fourthVarLiteral.getLineno()); assertEquals(10, fifthVarLiteral.getLineno()); }
public void testParsingWithoutJSDoc() { AstRoot root = parse("var a = /** @type number */(x);", false); assertNotNull(root.getComments()); assertEquals(1, root.getComments().size()); assertEquals("/** @type number */", root.getComments().first().getValue()); VariableDeclaration vd = (VariableDeclaration) root.getFirstChild(); VariableInitializer vi = vd.getVariables().get(0); assertTrue(vi.getInitializer() instanceof ParenthesizedExpression); }
public void testJSDocAttachment14() { AstRoot root = parse("var a = (/** @type {!Foo} */ {});"); assertNotNull(root.getComments()); assertEquals(1, root.getComments().size()); assertEquals("/** @type {!Foo} */", root.getComments().first().getValue()); VariableDeclaration vd = (VariableDeclaration) root.getFirstChild(); VariableInitializer vi = vd.getVariables().get(0); assertNotNull(((ParenthesizedExpression) vi.getInitializer()).getExpression().getJsDoc()); }
public void testJSDocAttachment3() { AstRoot root = parse("var a = /** @type number */(x);"); assertNotNull(root.getComments()); assertEquals(1, root.getComments().size()); assertEquals("/** @type number */", root.getComments().first().getValue()); VariableDeclaration vd = (VariableDeclaration) root.getFirstChild(); VariableInitializer vi = vd.getVariables().get(0); assertNotNull(vi.getInitializer().getJsDoc()); }
@Override public String toSource(int depth) { String pad = makeIndent(depth); StringBuilder sb = new StringBuilder(); sb.append(pad); sb.append("let ("); printList(variables.getVariables(), sb); sb.append(") "); if (body != null) { sb.append(body.toSource(depth)); } return sb.toString(); }
public void testLinenoVarDecl() { AstRoot root = parse("\nvar\n" + " a =\n" + " 3\n"); VariableDeclaration decl = (VariableDeclaration) root.getFirstChild(); List<VariableInitializer> vars = decl.getVariables(); VariableInitializer init = vars.get(0); AstNode declName = init.getTarget(); AstNode expr = init.getInitializer(); assertEquals(1, decl.getLineno()); assertEquals(2, init.getLineno()); assertEquals(2, declName.getLineno()); assertEquals(3, expr.getLineno()); }
public void testRegexpLocation() { AstNode root = parse("\nvar path =\n" + " replace(\n" + "/a/g," + "'/');\n"); VariableDeclaration firstVarDecl = (VariableDeclaration) root.getFirstChild(); List<VariableInitializer> vars1 = firstVarDecl.getVariables(); VariableInitializer firstInitializer = vars1.get(0); Name firstVarName = (Name) firstInitializer.getTarget(); FunctionCall callNode = (FunctionCall) firstInitializer.getInitializer(); AstNode fnName = callNode.getTarget(); List<AstNode> args = callNode.getArguments(); RegExpLiteral regexObject = (RegExpLiteral) args.get(0); AstNode aString = args.get(1); assertEquals(1, firstVarDecl.getLineno()); assertEquals(1, firstVarName.getLineno()); assertEquals(2, callNode.getLineno()); assertEquals(2, fnName.getLineno()); assertEquals(3, regexObject.getLineno()); assertEquals(3, aString.getLineno()); }
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()); }