Skip to content

Commit

Permalink
✨ feat: ES2015 transform spread v2
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed May 22, 2024
1 parent 618f5ca commit 66b1a12
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,26 @@ public Swc4jAstVisitorResponse visitExprOrSpread(Swc4jAstExprOrSpread node) {
switch (node.getParent().getType()) {
case ArrayLit:
Swc4jAstArrayLit arrayLit = node.getParent().as(Swc4jAstArrayLit.class);
if (arrayLit.getElems().size() == 1) {
final int length = arrayLit.getElems().size();
if (length == 1) {
arrayLit.getParent().replaceNode(arrayLit, node.getExpr());
} else {
Swc4jAstIdent identConcat = Swc4jAstIdent.create(Swc4jAstArrayLit.CONCAT);
int index = arrayLit.indexOf(node);
if (index == 0) {
Swc4jAstMemberExpr memberExpr = Swc4jAstMemberExpr.create(node.getExpr(), identConcat);
Swc4jAstMemberExpr memberExpr = Swc4jAstMemberExpr.create(
node.getExpr(),
Swc4jAstIdent.create(Swc4jAstArrayLit.CONCAT));
Swc4jAstCallExpr callExpr = Swc4jAstCallExpr.create(memberExpr);
ISwc4jAst newNode = callExpr;
final int length = arrayLit.getElems().size();
for (int i = 1; i < length; i++) {
Optional<Swc4jAstExprOrSpread> elem = arrayLit.getElems().get(i);
if (elem.isPresent()) {
if (elem.get().getSpread().isPresent()) {
callExpr.getArgs().add(Swc4jAstExprOrSpread.create(elem.get().getExpr()));
newNode = callExpr;
memberExpr = Swc4jAstMemberExpr.create(callExpr, identConcat);
memberExpr = Swc4jAstMemberExpr.create(
callExpr,
Swc4jAstIdent.create(Swc4jAstArrayLit.CONCAT));
callExpr = Swc4jAstCallExpr.create(memberExpr);
} else {
// TODO
Expand All @@ -65,6 +68,10 @@ public Swc4jAstVisitorResponse visitExprOrSpread(Swc4jAstExprOrSpread node) {
}
}
arrayLit.getParent().replaceNode(arrayLit, newNode);
} else if (index == length - 1) {
// TODO
} else {
// TODO
}
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class TestSwc4jPluginVisitorJsFuckDecoder extends BaseTestSuiteSwc4jPlugin {
public class TestSwc4jPluginVisitorEs2015TransformSpread extends BaseTestSuiteSwc4jPlugin {

@BeforeEach
@Override
Expand Down

0 comments on commit 66b1a12

Please sign in to comment.