Skip to content

Commit

Permalink
✨ feat: Add create() v14
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Jun 6, 2024
1 parent 7116c86 commit 87fe788
Show file tree
Hide file tree
Showing 13 changed files with 530 additions and 36 deletions.
14 changes: 6 additions & 8 deletions docs/tutorials/tutorial_07_mimic_babel.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ node.getParent().replaceNode(node, fnExpr);
public Swc4jAstVisitorResponse visitArrowExpr(Swc4jAstArrowExpr node) {
// Transform the params.
List<Swc4jAstParam> params = node.getParams().stream()
.map(param -> new Swc4jAstParam(new ArrayList<>(), param, Swc4jSpan.DUMMY))
.map(Swc4jAstParam::create)
.collect(Collectors.toList());
// Transform the block statement.
Swc4jAstBlockStmt blockStmt;
Expand All @@ -161,22 +161,20 @@ public Swc4jAstVisitorResponse visitArrowExpr(Swc4jAstArrowExpr node) {
// If the body is an expression, put that expression in a return statement
// and add that return statement to the block statement.
List<ISwc4jAstStmt> stmts = new ArrayList<>();
Swc4jAstReturnStmt returnStmt = new Swc4jAstReturnStmt(body.as(ISwc4jAstExpr.class), Swc4jSpan.DUMMY);
stmts.add(returnStmt);
blockStmt = new Swc4jAstBlockStmt(stmts, Swc4jSpan.DUMMY);
stmts.add(Swc4jAstReturnStmt.create(body.as(ISwc4jAstExpr.class)));
blockStmt = Swc4jAstBlockStmt.create(stmts);
}
// Create the function.
Swc4jAstFunction fn = new Swc4jAstFunction(
Swc4jAstFunction fn = Swc4jAstFunction.create(
params,
new ArrayList<>(),
blockStmt,
node.isGenerator(),
node.isAsync(),
node.getTypeParams().orElse(null),
node.getReturnType().orElse(null),
Swc4jSpan.DUMMY);
node.getReturnType().orElse(null));
// Create the function expression.
Swc4jAstFnExpr fnExpr = new Swc4jAstFnExpr(null, fn, Swc4jSpan.DUMMY);
Swc4jAstFnExpr fnExpr = Swc4jAstFnExpr.create(fn);
// Replace the arrow expression with the function expression.
node.getParent().replaceNode(node, fnExpr);
return super.visitArrowExpr(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,79 @@ public static Swc4jAstAutoAccessor create(ISwc4jAstKey key) {
}

public static Swc4jAstAutoAccessor create(ISwc4jAstKey key, ISwc4jAstExpr value) {
return create(key, value, null);
}

public static Swc4jAstAutoAccessor create(
ISwc4jAstKey key,
ISwc4jAstExpr value,
Swc4jAstTsTypeAnn typeAnn) {
return create(key, value, typeAnn, false);
}

public static Swc4jAstAutoAccessor create(
ISwc4jAstKey key,
ISwc4jAstExpr value,
Swc4jAstTsTypeAnn typeAnn,
boolean _static) {
return create(key, value, typeAnn, _static, SimpleList.of());
}

public static Swc4jAstAutoAccessor create(
ISwc4jAstKey key,
ISwc4jAstExpr value,
Swc4jAstTsTypeAnn typeAnn,
boolean _static,
List<Swc4jAstDecorator> decorators) {
return create(key, value, typeAnn, _static, decorators, null);
}

public static Swc4jAstAutoAccessor create(
ISwc4jAstKey key,
ISwc4jAstExpr value,
Swc4jAstTsTypeAnn typeAnn,
boolean _static,
List<Swc4jAstDecorator> decorators,
Swc4jAstAccessibility accessibility) {
return create(key, value, typeAnn, _static, decorators, accessibility, false);
}

public static Swc4jAstAutoAccessor create(
ISwc4jAstKey key,
ISwc4jAstExpr value,
Swc4jAstTsTypeAnn typeAnn,
boolean _static,
List<Swc4jAstDecorator> decorators,
Swc4jAstAccessibility accessibility,
boolean _abstract) {
return create(key, value, typeAnn, _static, decorators, accessibility, _abstract, false);
}

public static Swc4jAstAutoAccessor create(
ISwc4jAstKey key,
ISwc4jAstExpr value,
Swc4jAstTsTypeAnn typeAnn,
boolean _static,
List<Swc4jAstDecorator> decorators,
Swc4jAstAccessibility accessibility,
boolean _abstract,
boolean _override) {
return create(key, value, typeAnn, _static, decorators, accessibility, _abstract, _override, false);
}

public static Swc4jAstAutoAccessor create(
ISwc4jAstKey key,
ISwc4jAstExpr value,
Swc4jAstTsTypeAnn typeAnn,
boolean _static,
List<Swc4jAstDecorator> decorators,
Swc4jAstAccessibility accessibility,
boolean _abstract,
boolean _override,
boolean definite) {
return new Swc4jAstAutoAccessor(
key, value, null, false, SimpleList.of(),
null, false, false, false, Swc4jSpan.DUMMY);
key, value, typeAnn, _static, decorators,
accessibility, _abstract, _override, definite, Swc4jSpan.DUMMY);
}

@Jni2RustMethod
Expand Down
53 changes: 51 additions & 2 deletions src/main/java/com/caoccao/javet/swc4j/ast/clazz/Swc4jAstClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,58 @@ public static Swc4jAstClass create() {
}

public static Swc4jAstClass create(List<ISwc4jAstClassMember> body) {
return create(SimpleList.of(), body);
}

public static Swc4jAstClass create(List<Swc4jAstDecorator> decorators, List<ISwc4jAstClassMember> body) {
return create(decorators, body, null);
}

public static Swc4jAstClass create(
List<Swc4jAstDecorator> decorators,
List<ISwc4jAstClassMember> body,
ISwc4jAstExpr superClass) {
return create(decorators, body, superClass, false);
}

public static Swc4jAstClass create(
List<Swc4jAstDecorator> decorators,
List<ISwc4jAstClassMember> body,
ISwc4jAstExpr superClass,
boolean _abstract) {
return create(decorators, body, superClass, _abstract, null);
}

public static Swc4jAstClass create(
List<Swc4jAstDecorator> decorators,
List<ISwc4jAstClassMember> body,
ISwc4jAstExpr superClass,
boolean _abstract,
Swc4jAstTsTypeParamDecl typeParams) {
return create(decorators, body, superClass, _abstract, typeParams, null);
}

public static Swc4jAstClass create(
List<Swc4jAstDecorator> decorators,
List<ISwc4jAstClassMember> body,
ISwc4jAstExpr superClass,
boolean _abstract,
Swc4jAstTsTypeParamDecl typeParams,
Swc4jAstTsTypeParamInstantiation superTypeParams) {
return create(decorators, body, superClass, _abstract, typeParams, superTypeParams, SimpleList.of());
}

public static Swc4jAstClass create(
List<Swc4jAstDecorator> decorators,
List<ISwc4jAstClassMember> body,
ISwc4jAstExpr superClass,
boolean _abstract,
Swc4jAstTsTypeParamDecl typeParams,
Swc4jAstTsTypeParamInstantiation superTypeParams,
List<Swc4jAstTsExprWithTypeArgs> _implements) {
return new Swc4jAstClass(
SimpleList.of(), body, null, false, null,
null, SimpleList.of(), Swc4jSpan.DUMMY);
decorators, body, superClass, _abstract, typeParams,
superTypeParams, _implements, Swc4jSpan.DUMMY);
}

@Jni2RustMethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,59 @@ public static Swc4jAstClassMethod create(
ISwc4jAstPropName key,
Swc4jAstFunction function,
Swc4jAstMethodKind kind) {
return create(key, function, kind, false);
}

public static Swc4jAstClassMethod create(
ISwc4jAstPropName key,
Swc4jAstFunction function,
Swc4jAstMethodKind kind,
boolean _static) {
return create(key, function, kind, _static, null);
}

public static Swc4jAstClassMethod create(
ISwc4jAstPropName key,
Swc4jAstFunction function,
Swc4jAstMethodKind kind,
boolean _static,
Swc4jAstAccessibility accessibility) {
return create(key, function, kind, _static, accessibility, false);
}

public static Swc4jAstClassMethod create(
ISwc4jAstPropName key,
Swc4jAstFunction function,
Swc4jAstMethodKind kind,
boolean _static,
Swc4jAstAccessibility accessibility,
boolean _abstract) {
return create(key, function, kind, _static, accessibility, _abstract, false);
}

public static Swc4jAstClassMethod create(
ISwc4jAstPropName key,
Swc4jAstFunction function,
Swc4jAstMethodKind kind,
boolean _static,
Swc4jAstAccessibility accessibility,
boolean _abstract,
boolean optional) {
return create(key, function, kind, _static, accessibility, _abstract, optional, false);
}

public static Swc4jAstClassMethod create(
ISwc4jAstPropName key,
Swc4jAstFunction function,
Swc4jAstMethodKind kind,
boolean _static,
Swc4jAstAccessibility accessibility,
boolean _abstract,
boolean optional,
boolean _override) {
return new Swc4jAstClassMethod(
key, function, kind, false, null,
false, false, false, Swc4jSpan.DUMMY);
key, function, kind, _static, accessibility,
_abstract, optional, _override, Swc4jSpan.DUMMY);
}

@Jni2RustMethod
Expand Down
129 changes: 126 additions & 3 deletions src/main/java/com/caoccao/javet/swc4j/ast/clazz/Swc4jAstClassProp.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,133 @@ public static Swc4jAstClassProp create(ISwc4jAstPropName key) {
}

public static Swc4jAstClassProp create(ISwc4jAstPropName key, ISwc4jAstExpr value) {
return create(key, value, null);
}

public static Swc4jAstClassProp create(
ISwc4jAstPropName key,
ISwc4jAstExpr value,
Swc4jAstTsTypeAnn typeAnn) {
return create(key, value, typeAnn, false);
}

public static Swc4jAstClassProp create(
ISwc4jAstPropName key,
ISwc4jAstExpr value,
Swc4jAstTsTypeAnn typeAnn,
boolean _static) {
return create(key, value, typeAnn, _static, SimpleList.of());
}

public static Swc4jAstClassProp create(
ISwc4jAstPropName key,
ISwc4jAstExpr value,
Swc4jAstTsTypeAnn typeAnn,
boolean _static,
List<Swc4jAstDecorator> decorators) {
return create(key, value, typeAnn, _static, decorators, null);
}

public static Swc4jAstClassProp create(
ISwc4jAstPropName key,
ISwc4jAstExpr value,
Swc4jAstTsTypeAnn typeAnn,
boolean _static,
List<Swc4jAstDecorator> decorators,
Swc4jAstAccessibility accessibility) {
return create(key, value, typeAnn, _static, decorators, accessibility, false);
}

public static Swc4jAstClassProp create(
ISwc4jAstPropName key,
ISwc4jAstExpr value,
Swc4jAstTsTypeAnn typeAnn,
boolean _static,
List<Swc4jAstDecorator> decorators,
Swc4jAstAccessibility accessibility,
boolean _abstract) {
return create(key, value, typeAnn, _static, decorators, accessibility, _abstract, false);
}

public static Swc4jAstClassProp create(
ISwc4jAstPropName key,
ISwc4jAstExpr value,
Swc4jAstTsTypeAnn typeAnn,
boolean _static,
List<Swc4jAstDecorator> decorators,
Swc4jAstAccessibility accessibility,
boolean _abstract,
boolean optional) {
return create(key, value, typeAnn, _static, decorators, accessibility, _abstract, optional, false);
}

public static Swc4jAstClassProp create(
ISwc4jAstPropName key,
ISwc4jAstExpr value,
Swc4jAstTsTypeAnn typeAnn,
boolean _static,
List<Swc4jAstDecorator> decorators,
Swc4jAstAccessibility accessibility,
boolean _abstract,
boolean optional,
boolean _override) {
return create(
key, value, typeAnn, _static, decorators,
accessibility, _abstract, optional, _override, false);
}

public static Swc4jAstClassProp create(
ISwc4jAstPropName key,
ISwc4jAstExpr value,
Swc4jAstTsTypeAnn typeAnn,
boolean _static,
List<Swc4jAstDecorator> decorators,
Swc4jAstAccessibility accessibility,
boolean _abstract,
boolean optional,
boolean _override,
boolean readonly) {
return create(
key, value, typeAnn, _static, decorators,
accessibility, _abstract, optional, _override, readonly,
false);
}

public static Swc4jAstClassProp create(
ISwc4jAstPropName key,
ISwc4jAstExpr value,
Swc4jAstTsTypeAnn typeAnn,
boolean _static,
List<Swc4jAstDecorator> decorators,
Swc4jAstAccessibility accessibility,
boolean _abstract,
boolean optional,
boolean _override,
boolean readonly,
boolean declare) {
return create(
key, value, typeAnn, _static, decorators,
accessibility, _abstract, optional, _override, readonly,
declare, false);
}

public static Swc4jAstClassProp create(
ISwc4jAstPropName key,
ISwc4jAstExpr value,
Swc4jAstTsTypeAnn typeAnn,
boolean _static,
List<Swc4jAstDecorator> decorators,
Swc4jAstAccessibility accessibility,
boolean _abstract,
boolean optional,
boolean _override,
boolean readonly,
boolean declare,
boolean definite) {
return new Swc4jAstClassProp(
key, value, null, false, SimpleList.of(),
null, false, false, false, false,
false, false, Swc4jSpan.DUMMY);
key, value, typeAnn, _static, decorators,
accessibility, _abstract, optional, _override, readonly,
declare, definite, Swc4jSpan.DUMMY);
}

@Jni2RustMethod
Expand Down
Loading

0 comments on commit 87fe788

Please sign in to comment.