Skip to content

Commit

Permalink
✨ feat: Add create() v5
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed May 29, 2024
1 parent bea4c99 commit c2d6ae7
Show file tree
Hide file tree
Showing 27 changed files with 193 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public Swc4jAstFnExpr(
setIdent(ident);
}

public static Swc4jAstFnExpr create(Swc4jAstFunction function) {
return create(null, function);
}

public static Swc4jAstFnExpr create(Swc4jAstIdent ident, Swc4jAstFunction function) {
return new Swc4jAstFnExpr(ident, function, Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
List<ISwc4jAst> childNodes = SimpleList.of(function);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ public Swc4jAstIdent(
}

public static Swc4jAstIdent create(String sym) {
return new Swc4jAstIdent(sym, false, Swc4jSpan.DUMMY);
return create(sym, false);
}

public static Swc4jAstIdent create(String sym, boolean optional) {
return new Swc4jAstIdent(sym, optional, Swc4jSpan.DUMMY);
}

public static Swc4jAstIdent createApply() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ public Swc4jAstJsxElement(
this.children.forEach(node -> node.setParent(this));
}

public static Swc4jAstJsxElement create(Swc4jAstJsxOpeningElement opening) {
return create(opening, SimpleList.of());
}

public static Swc4jAstJsxElement create(
Swc4jAstJsxOpeningElement opening,
List<ISwc4jAstJsxElementChild> children) {
return create(opening, children, null);
}

public static Swc4jAstJsxElement create(
Swc4jAstJsxOpeningElement opening,
List<ISwc4jAstJsxElementChild> children,
Swc4jAstJsxClosingElement closing) {
return new Swc4jAstJsxElement(opening, children, closing, Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
List<ISwc4jAst> childNodes = SimpleList.copyOf(children);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public Swc4jAstJsxEmptyExpr(
super(span);
}

public static Swc4jAstJsxEmptyExpr create() {
return new Swc4jAstJsxEmptyExpr(Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
return EMPTY_CHILD_NODES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public Swc4jAstJsxExprContainer(
setExpr(expr);
}

public static Swc4jAstJsxExprContainer create(ISwc4jAstJsxExpr expr) {
return new Swc4jAstJsxExprContainer(expr, Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
return SimpleList.of(expr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ public Swc4jAstJsxFragment(
this.children.forEach(node -> node.setParent(this));
}

public static Swc4jAstJsxFragment create(Swc4jAstJsxOpeningFragment opening) {
return create(opening, SimpleList.of());
}

public static Swc4jAstJsxFragment create(
Swc4jAstJsxOpeningFragment opening,
List<ISwc4jAstJsxElementChild> children) {
return create(opening, children, null);
}

public static Swc4jAstJsxFragment create(
Swc4jAstJsxOpeningFragment opening,
List<ISwc4jAstJsxElementChild> children,
Swc4jAstJsxClosingFragment closing) {
return new Swc4jAstJsxFragment(opening, children, closing, Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
List<ISwc4jAst> childNodes = SimpleList.copyOf(children);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public Swc4jAstJsxMemberExpr(
setProp(prop);
}

public static Swc4jAstJsxMemberExpr create(ISwc4jAstJsxObject obj, Swc4jAstIdent prop) {
return new Swc4jAstJsxMemberExpr(obj, prop, Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
return SimpleList.of(obj, prop);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public Swc4jAstJsxNamespacedName(
setNs(ns);
}

public static Swc4jAstJsxNamespacedName create(Swc4jAstIdent ns, Swc4jAstIdent name) {
return new Swc4jAstJsxNamespacedName(ns, name, Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
return SimpleList.of(ns, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public Swc4jAstJsxSpreadChild(
setExpr(expr);
}

public static Swc4jAstJsxSpreadChild create(ISwc4jAstExpr expr) {
return new Swc4jAstJsxSpreadChild(expr, Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
return SimpleList.of(expr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public Swc4jAstMetaPropExpr(
setKind(kind);
}

public static Swc4jAstMetaPropExpr create(Swc4jAstMetaPropKind kind) {
return new Swc4jAstMetaPropExpr(kind, Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
return EMPTY_CHILD_NODES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ public Swc4jAstNewExpr(
setTypeArgs(typeArgs);
}

public static Swc4jAstNewExpr create(ISwc4jAstExpr callee) {
return create(callee, null);
}

public static Swc4jAstNewExpr create(
ISwc4jAstExpr callee,
List<Swc4jAstExprOrSpread> args) {
return create(callee, args, null);
}

public static Swc4jAstNewExpr create(
ISwc4jAstExpr callee,
List<Swc4jAstExprOrSpread> args,
Swc4jAstTsTypeParamInstantiation typeArgs) {
return new Swc4jAstNewExpr(callee, args, typeArgs, Swc4jSpan.DUMMY);
}

@Jni2RustMethod
public Optional<List<Swc4jAstExprOrSpread>> getArgs() {
return args;
Expand Down Expand Up @@ -110,7 +127,7 @@ public boolean replaceNode(ISwc4jAst oldNode, ISwc4jAst newNode) {
}

public Swc4jAstNewExpr setArgs(List<Swc4jAstExprOrSpread> args) {
this.args = Optional.ofNullable(args).map(SimpleList::immutableCopyOf);
this.args = Optional.ofNullable(args);
this.args.ifPresent(nodes -> nodes.forEach(node -> node.setParent(this)));
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ public Swc4jAstOptChainExpr(
setOptional(optional);
}

public static Swc4jAstOptChainExpr create(ISwc4jAstOptChainBase base) {
return create(false, base);
}

public static Swc4jAstOptChainExpr create(boolean optional, ISwc4jAstOptChainBase base) {
return new Swc4jAstOptChainExpr(optional, base, Swc4jSpan.DUMMY);
}

@Jni2RustMethod
public ISwc4jAstOptChainBase getBase() {
return base;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public Swc4jAstParenExpr(
setExpr(expr);
}

public static Swc4jAstParenExpr create(ISwc4jAstExpr expr) {
return new Swc4jAstParenExpr(expr, Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
return SimpleList.of(expr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public Swc4jAstSeqExpr(
this.exprs.forEach(node -> node.setParent(this));
}

public static Swc4jAstSeqExpr create(List<ISwc4jAstExpr> exprs) {
return new Swc4jAstSeqExpr(exprs, Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
return SimpleList.copyOf(exprs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ public Swc4jAstSpreadElement(
setExpr(expr);
}

public static Swc4jAstSpreadElement create(ISwc4jAstExpr expr) {
return create(Swc4jSpan.DUMMY, expr);
}

public static Swc4jAstSpreadElement create(Swc4jSpan dot3Token, ISwc4jAstExpr expr) {
return new Swc4jAstSpreadElement(dot3Token, expr, Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
return SimpleList.of(expr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public Swc4jAstSuperPropExpr(
setProp(prop);
}

public static Swc4jAstSuperPropExpr create(Swc4jAstSuper obj, ISwc4jAstSuperProp prop) {
return new Swc4jAstSuperPropExpr(obj, prop, Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
return SimpleList.of(obj, prop);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstType;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAst;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstExpr;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstPropName;
import com.caoccao.javet.swc4j.ast.ts.Swc4jAstTsTypeAnn;
import com.caoccao.javet.swc4j.ast.ts.Swc4jAstTsTypeParamInstantiation;
import com.caoccao.javet.swc4j.ast.visitors.ISwc4jAstVisitor;
import com.caoccao.javet.swc4j.ast.visitors.Swc4jAstVisitorResponse;
Expand Down Expand Up @@ -56,6 +54,17 @@ public Swc4jAstTaggedTpl(
setTypeParams(typeParams);
}

public static Swc4jAstTaggedTpl create(ISwc4jAstExpr tag, Swc4jAstTpl tpl) {
return create(tag, null, tpl);
}

public static Swc4jAstTaggedTpl create(
ISwc4jAstExpr tag,
Swc4jAstTsTypeParamInstantiation typeParams,
Swc4jAstTpl tpl) {
return new Swc4jAstTaggedTpl(tag, typeParams, tpl, Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
List<ISwc4jAst> childNodes = SimpleList.of(tag, tpl);
Expand All @@ -78,6 +87,11 @@ public Swc4jAstType getType() {
return Swc4jAstType.TaggedTpl;
}

@Jni2RustMethod
public Optional<Swc4jAstTsTypeParamInstantiation> getTypeParams() {
return typeParams;
}

@Override
public boolean replaceNode(ISwc4jAst oldNode, ISwc4jAst newNode) {
if (tag == oldNode && newNode instanceof ISwc4jAstExpr) {
Expand All @@ -95,11 +109,6 @@ public boolean replaceNode(ISwc4jAst oldNode, ISwc4jAst newNode) {
return false;
}

@Jni2RustMethod
public Optional<Swc4jAstTsTypeParamInstantiation> getTypeParams() {
return typeParams;
}

public Swc4jAstTaggedTpl setTag(ISwc4jAstExpr tag) {
this.tag = AssertionUtils.notNull(tag, "Tag");
this.tag.setParent(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public Swc4jAstThisExpr(
super(span);
}

public static Swc4jAstThisExpr create() {
return new Swc4jAstThisExpr(Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
return EMPTY_CHILD_NODES;
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstTpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ public Swc4jAstTpl(
this.quasis.forEach(node -> node.setParent(this));
}

public static Swc4jAstTpl create() {
return create(SimpleList.of());
}

public static Swc4jAstTpl create(List<ISwc4jAstExpr> exprs) {
return create(exprs, SimpleList.of());
}

public static Swc4jAstTpl create(List<ISwc4jAstExpr> exprs, List<Swc4jAstTplElement> quasis) {
return new Swc4jAstTpl(exprs, quasis, Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
List<ISwc4jAst> childNodes = SimpleList.copyOf(exprs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public Swc4jAstTsAsExpr(
setTypeAnn(typeAnn);
}

public static Swc4jAstTsAsExpr create(ISwc4jAstExpr expr, ISwc4jAstTsType typeAnn) {
return new Swc4jAstTsAsExpr(expr, typeAnn, Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
return SimpleList.of(expr, typeAnn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public Swc4jAstTsConstAssertion(
setExpr(expr);
}

public static Swc4jAstTsConstAssertion create(ISwc4jAstExpr expr) {
return new Swc4jAstTsConstAssertion(expr, Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
return SimpleList.of(expr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public Swc4jAstTsInstantiation(
setTypeArgs(typeArgs);
}

public static Swc4jAstTsInstantiation create(ISwc4jAstExpr expr, Swc4jAstTsTypeParamInstantiation typeArgs) {
return new Swc4jAstTsInstantiation(expr, typeArgs, Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
return SimpleList.of(expr, typeArgs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public Swc4jAstTsNonNullExpr(
setExpr(expr);
}

public static Swc4jAstTsNonNullExpr create(ISwc4jAstExpr expr) {
return new Swc4jAstTsNonNullExpr(expr, Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
return SimpleList.of(expr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public Swc4jAstTsSatisfiesExpr(
setTypeAnn(typeAnn);
}

public static Swc4jAstTsSatisfiesExpr create(ISwc4jAstExpr expr, ISwc4jAstTsType typeAnn) {
return new Swc4jAstTsSatisfiesExpr(expr, typeAnn, Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
return SimpleList.of(expr, typeAnn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public Swc4jAstTsTypeAssertion(
setTypeAnn(typeAnn);
}

public static Swc4jAstTsTypeAssertion create(ISwc4jAstExpr expr, ISwc4jAstTsType typeAnn) {
return new Swc4jAstTsTypeAssertion(expr, typeAnn, Swc4jSpan.DUMMY);
}

@Override
public List<ISwc4jAst> getChildNodes() {
return SimpleList.of(expr, typeAnn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ public Swc4jAstUpdateExpr(
setPrefix(prefix);
}

public static Swc4jAstUpdateExpr create(Swc4jAstUpdateOp op, ISwc4jAstExpr arg) {
return create(op, false, arg);
}

public static Swc4jAstUpdateExpr create(Swc4jAstUpdateOp op, boolean prefix, ISwc4jAstExpr arg) {
return new Swc4jAstUpdateExpr(op, prefix, arg, Swc4jSpan.DUMMY);
}

@Jni2RustMethod
public ISwc4jAstExpr getArg() {
return arg;
Expand Down
Loading

0 comments on commit c2d6ae7

Please sign in to comment.