Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/VariableInitializer.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 /**
12 * A variable declaration or initializer, part of a {@link VariableDeclaration}
13 * expression. The variable "target" can be a simple name or a destructuring
14 * form. The initializer, if present, can be any expression.<p>
15 *
16 * Node type is one of {@link Token#VAR}, {@link Token#CONST}, or
17 * {@link Token#LET}.<p>
18 */
19 public class VariableInitializer extends AstNode {
20
21 private AstNode target;
22 private AstNode initializer;
23
24 {
25 type = Token.VAR;
26 }
27
28 /**
29 * Sets the node type.
30 * @throws IllegalArgumentException if {@code nodeType} is not one of
31 * {@link Token#VAR}, {@link Token#CONST}, or {@link Token#LET}
32 */
33 public void setNodeType(int nodeType) {
34 if (nodeType != Token.VAR
35 && nodeType != Token.CONST
36 && nodeType != Token.LET)
37 throw new IllegalArgumentException("invalid node type");
38 setType(nodeType);
39 }
40
41 public VariableInitializer() {
42 }
43
44 public VariableInitializer(int pos) {
45 super(pos);
46 }
47
48 public VariableInitializer(int pos, int len) {
49 super(pos, len);
50 }
51
52
53 /**
54 * Returns true if this is a destructuring assignment. If so, the
55 * initializer must be non-{@code null}.
56 * @return {@code true} if the {@code target} field is a destructuring form
57 * (an {@link ArrayLiteral} or {@link ObjectLiteral} node)
58 */
59 public boolean isDestructuring() {
60 return !(target instanceof Name);
61 }
62
63 /**
64 * Returns the variable name or destructuring form
65 */
66 public AstNode getTarget() {
67 return target;
68 }
69
70 /**
71 * Sets the variable name or destructuring form, and sets
72 * its parent to this node.
73 * @throws IllegalArgumentException if target is {@code null}
74 */
75 public void setTarget(AstNode target) {
76 // Don't throw exception if target is an "invalid" node type.
77 // See mozilla/js/tests/js1_7/block/regress-350279.js
78 if (target == null)
79 throw new IllegalArgumentException("invalid target arg");
80 this.target = target;
81 target.setParent(this);
82 }
83
84 /**
85 * Returns the initial value, or {@code null} if not provided
86 */
87 public AstNode getInitializer() {
88 return initializer;
89 }
90
91 /**
92 * Sets the initial value expression, and sets its parent to this node.
93 * @param initializer the initial value. May be {@code null}.
94 */
95 public void setInitializer(AstNode initializer) {
96 this.initializer = initializer;
97 if (initializer != null)
98 initializer.setParent(this);
99 }
100
101 @Override
102 public String toSource(int depth) {
103 StringBuilder sb = new StringBuilder();
104 sb.append(makeIndent(depth));
105 sb.append(target.toSource(0));
106 if (initializer != null) {
107 sb.append (" = ");
108 sb.append(initializer.toSource(0));
109 }
110 return sb.toString();
111 }
112
113 /**
114 * Visits this node, then the target expression, then the initializer
115 * expression if present.
116 */
117 @Override
118 public void visit(NodeVisitor v) {
119 if (v.visit(this)) {
120 target.visit(v);
121 if (initializer != null) {
122 initializer.visit(v);
123 }
124 }
125 }
126 }