package com.oracle.js.parser.ir;
import com.oracle.js.parser.ir.visitor.NodeVisitor;
import com.oracle.js.parser.ir.visitor.TranslatorNodeVisitor;
public class ExportNode extends Node {
private final NamedExportsNode namedExports;
private final FromNode from;
private final IdentNode exportIdent;
private final VarNode var;
private final Expression expression;
private final boolean isDefault;
public ExportNode(final long token, final int start, final int finish, final IdentNode ident, final FromNode from) {
this(token, start, finish, null, from, ident, null, null, false);
}
public ExportNode(final long token, final int start, final int finish, final NamedExportsNode exportClause, final FromNode from) {
this(token, start, finish, exportClause, from, null, null, null, false);
}
public ExportNode(final long token, final int start, final int finish, final IdentNode ident, final Expression expression, final boolean isDefault) {
this(token, start, finish, null, null, ident, null, expression, isDefault);
}
public ExportNode(final long token, final int start, final int finish, final IdentNode ident, final VarNode var) {
this(token, start, finish, null, null, ident, var, null, false);
}
private ExportNode(final long token, final int start, final int finish, final NamedExportsNode namedExports,
final FromNode from, final IdentNode exportIdent, final VarNode var, final Expression expression, final boolean isDefault) {
super(token, start, finish);
this.namedExports = namedExports;
this.from = from;
this.exportIdent = exportIdent;
this.var = var;
this.expression = expression;
this.isDefault = isDefault;
assert (namedExports == null) || (exportIdent == null);
assert !isDefault || (namedExports == null && from == null);
assert (var == null && expression == null) || isDefault || (exportIdent != null && exportIdent == getIdent(var, expression));
}
private ExportNode(final ExportNode node, final NamedExportsNode namedExports,
final FromNode from, final IdentNode exportIdent, final VarNode var, final Expression expression) {
super(node);
this.isDefault = node.isDefault;
this.namedExports = namedExports;
this.from = from;
this.exportIdent = exportIdent;
this.var = var;
this.expression = expression;
}
public NamedExportsNode getNamedExports() {
return namedExports;
}
public FromNode getFrom() {
return from;
}
public IdentNode getExportIdentifier() {
return exportIdent;
}
public VarNode getVar() {
return var;
}
public Expression getExpression() {
return expression;
}
public boolean isDefault() {
return isDefault;
}
public ExportNode setExportClause(NamedExportsNode exportClause) {
assert exportIdent == null;
if (this.namedExports == exportClause) {
return this;
}
return new ExportNode(this, exportClause, from, exportIdent, var, expression);
}
public ExportNode setFrom(FromNode from) {
assert exportIdent == null;
if (this.from == from) {
return this;
}
return new ExportNode(this, namedExports, from, exportIdent, var, expression);
}
@Override
public Node accept(NodeVisitor<? extends LexicalContext> visitor) {
if (visitor.enterExportNode(this)) {
NamedExportsNode newExportClause = namedExports == null ? null : (NamedExportsNode) namedExports.accept(visitor);
FromNode newFrom = from == null ? null : (FromNode) from.accept(visitor);
VarNode newVar = var == null ? null : (VarNode) var.accept(visitor);
Expression newExpression = expression == null ? null : (Expression) expression.accept(visitor);
IdentNode newIdent = (exportIdent == null || isDefault()) ? exportIdent : getIdent(newVar, newExpression);
ExportNode newNode = (this.namedExports == newExportClause && this.from == newFrom && this.exportIdent == newIdent && this.var == newVar && this.expression == newExpression)
? this
: new ExportNode(this, namedExports, from, exportIdent, var, expression);
return visitor.leaveExportNode(newNode);
}
return this;
}
private static IdentNode getIdent(VarNode newVar, Expression newExpression) {
if (newVar != null) {
return newVar.getName();
} else if (newExpression instanceof FunctionNode) {
return ((FunctionNode) newExpression).getIdent();
} else if (newExpression instanceof ClassNode) {
return ((ClassNode) newExpression).getIdent();
}
return null;
}
@Override
public <R> R accept(TranslatorNodeVisitor<? extends LexicalContext, R> visitor) {
return visitor.enterExportNode(this);
}
@Override
public void toString(StringBuilder sb, boolean printType) {
sb.append("export ");
if (isDefault) {
sb.append("default ");
}
if (expression != null) {
expression.toString(sb, printType);
if (expression.isAssignment()) {
sb.append(';');
}
} else if (var != null) {
var.toString(sb, printType);
sb.append(';');
} else {
if (namedExports == null) {
sb.append("* ");
if (exportIdent != null) {
sb.append("as ").append(exportIdent).append(' ');
}
} else {
namedExports.toString(sb, printType);
}
if (from != null) {
from.toString(sb, printType);
}
sb.append(';');
}
}
}