Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/VariableDeclaration.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript.ast;
8
9 import org.mozilla.javascript.Token;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 /**
15 * A list of one or more var, const or let declarations.
16 * Node type is {@link Token#VAR}, {@link Token#CONST} or
17 * {@link Token#LET}.<p>
18 *
19 * If the node is for {@code var} or {@code const}, the node position
20 * is the beginning of the {@code var} or {@code const} keyword.
21 * For {@code let} declarations, the node position coincides with the
22 * first {@link VariableInitializer} child.<p>
23 *
24 * A standalone variable declaration in a statement context returns {@code true}
25 * from its {@link #isStatement()} method.
26 */
27 public class VariableDeclaration extends AstNode {
28
29 private List<VariableInitializer> variables = new ArrayList<VariableInitializer>();
30 private boolean isStatement;
31
32 {
33 type = Token.VAR;
34 }
35
36 public VariableDeclaration() {
37 }
38
39 public VariableDeclaration(int pos) {
40 super(pos);
41 }
42
43 public VariableDeclaration(int pos, int len) {
44 super(pos, len);
45 }
46
47 /**
48 * Returns variable list. Never {@code null}.
49 */
50 public List<VariableInitializer> getVariables() {
51 return variables;
52 }
53
54 /**
55 * Sets variable list
56 * @throws IllegalArgumentException if variables list is {@code null}
57 */
58 public void setVariables(List<VariableInitializer> variables) {
59 assertNotNull(variables);
60 this.variables.clear();
61 for (VariableInitializer vi : variables) {
62 addVariable(vi);
63 }
64 }
65
66 /**
67 * Adds a variable initializer node to the child list.
68 * Sets initializer node's parent to this node.
69 * @throws IllegalArgumentException if v is {@code null}
70 */
71 public void addVariable(VariableInitializer v) {
72 assertNotNull(v);
73 variables.add(v);
74 v.setParent(this);
75 }
76
77 /**
78 * Sets the node type and returns this node.
79 * @throws IllegalArgumentException if {@code declType} is invalid
80 */
81 @Override
82 public org.mozilla.javascript.Node setType(int type) {
83 if (type != Token.VAR
84 && type != Token.CONST
85 && type != Token.LET)
86 throw new IllegalArgumentException("invalid decl type: " + type);
87 return super.setType(type);
88 }
89
90 /**
91 * Returns true if this is a {@code var} (not
92 * {@code const} or {@code let}) declaration.
93 * @return true if {@code declType} is {@link Token#VAR}
94 */
95 public boolean isVar() {
96 return type == Token.VAR;
97 }
98
99 /**
100 * Returns true if this is a {@link Token#CONST} declaration.
101 */
102 public boolean isConst() {
103 return type == Token.CONST;
104 }
105
106 /**
107 * Returns true if this is a {@link Token#LET} declaration.
108 */
109 public boolean isLet() {
110 return type == Token.LET;
111 }
112
113 /**
114 * Returns true if this node represents a statement.
115 */
116 public boolean isStatement() {
117 return isStatement;
118 }
119
120 /**
121 * Set or unset the statement flag.
122 */
123 public void setIsStatement(boolean isStatement) {
124 this.isStatement = isStatement;
125 }
126
127 private String declTypeName() {
128 return Token.typeToName(type).toLowerCase();
129 }
130
131 @Override
132 public String toSource(int depth) {
133 StringBuilder sb = new StringBuilder();
134 sb.append(makeIndent(depth));
135 sb.append(declTypeName());
136 sb.append(" ");
137 printList(variables, sb);
138 if (isStatement()) {
139 sb.append(";\n");
140 }
141 return sb.toString();
142 }
143
144 /**
145 * Visits this node, then each {@link VariableInitializer} child.
146 */
147 @Override
148 public void visit(NodeVisitor v) {
149 if (v.visit(this)) {
150 for (AstNode var : variables) {
151 var.visit(v);
152 }
153 }
154 }
155 }