-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
240 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
.../java/com/caoccao/javet/swc4j/plugins/es2015/Swc4jPluginVisitorEs2015TransformSpread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright (c) 2024. caoccao.com Sam Cao | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.caoccao.javet.swc4j.plugins.es2015; | ||
|
||
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstCallExpr; | ||
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstExprOrSpread; | ||
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstIdent; | ||
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstMemberExpr; | ||
import com.caoccao.javet.swc4j.ast.expr.lit.Swc4jAstArrayLit; | ||
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAst; | ||
import com.caoccao.javet.swc4j.ast.visitors.Swc4jAstVisitor; | ||
import com.caoccao.javet.swc4j.ast.visitors.Swc4jAstVisitorResponse; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* The type Swc4j plugin visitor ES2015 transform spread. | ||
* | ||
* @since 0.8.0 | ||
*/ | ||
public class Swc4jPluginVisitorEs2015TransformSpread extends Swc4jAstVisitor { | ||
@Override | ||
public Swc4jAstVisitorResponse visitExprOrSpread(Swc4jAstExprOrSpread node) { | ||
if (node.getSpread().isPresent()) { | ||
switch (node.getParent().getType()) { | ||
case ArrayLit: | ||
Swc4jAstArrayLit arrayLit = node.getParent().as(Swc4jAstArrayLit.class); | ||
if (arrayLit.getElems().size() == 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); | ||
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); | ||
callExpr = Swc4jAstCallExpr.create(memberExpr); | ||
} else { | ||
// TODO | ||
} | ||
} else { | ||
// TODO | ||
} | ||
} | ||
arrayLit.getParent().replaceNode(arrayLit, newNode); | ||
} | ||
} | ||
break; | ||
case CallExpr: | ||
break; | ||
case NewExpr: | ||
break; | ||
case OptCall: | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
return super.visitExprOrSpread(node); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/test/java/com/caoccao/javet/swc4j/plugins/BaseTestSuiteSwc4jPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright (c) 2024. caoccao.com Sam Cao | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.caoccao.javet.swc4j.plugins; | ||
|
||
import com.caoccao.javet.interop.V8Host; | ||
import com.caoccao.javet.interop.V8Runtime; | ||
import com.caoccao.javet.swc4j.BaseTestSuite; | ||
import com.caoccao.javet.swc4j.enums.Swc4jSourceMapOption; | ||
import com.caoccao.javet.swc4j.utils.SimpleList; | ||
import org.junit.jupiter.api.BeforeEach; | ||
|
||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
public class BaseTestSuiteSwc4jPlugin extends BaseTestSuite { | ||
protected void assertEvalAsString(String code, String expectedCode) { | ||
assertEquals( | ||
evalAsString(code), | ||
evalAsString(expectedCode), | ||
"Failed to evaluate.\nFrom:\n" + code + "\nTo:\n" + expectedCode); | ||
} | ||
|
||
protected void assertTransform(String code, String expectedCode) { | ||
try { | ||
assertEquals(expectedCode, swc4j.transform(code, jsScriptTransformOptions).getCode()); | ||
} catch (Throwable t) { | ||
fail("Failed to transform.\nFrom:\n" + code + "\nTo:\n" + expectedCode, t); | ||
} | ||
} | ||
|
||
protected void assertTransformAndEvalAsString(Map<String, String> testCaseMap) { | ||
testCaseMap.forEach((code, expectedCode) -> { | ||
assertTransform(code, expectedCode); | ||
assertEvalAsString(code, expectedCode); | ||
}); | ||
} | ||
|
||
@BeforeEach | ||
@Override | ||
protected void beforeEach() { | ||
super.beforeEach(); | ||
SimpleList.of(jsModuleTransformOptions, | ||
jsProgramTransformOptions, | ||
jsScriptTransformOptions, | ||
jsxModuleTransformOptions, | ||
jsxProgramTransformOptions, | ||
jsxScriptTransformOptions, | ||
tsModuleTransformOptions, | ||
tsProgramTransformOptions, | ||
tsScriptTransformOptions) | ||
.forEach(options -> options.setSourceMap(Swc4jSourceMapOption.None)); | ||
} | ||
|
||
protected String evalAsString(String code) { | ||
try (V8Runtime v8Runtime = V8Host.getV8Instance().createV8Runtime()) { | ||
return v8Runtime.getExecutor(code).executeString(); | ||
} catch (Throwable t) { | ||
fail("Failed to execute " + code, t); | ||
} | ||
return null; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...test/java/com/caoccao/javet/swc4j/plugins/es2015/TestSwc4jPluginVisitorJsFuckDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright (c) 2024. caoccao.com Sam Cao | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.caoccao.javet.swc4j.plugins.es2015; | ||
|
||
import com.caoccao.javet.swc4j.plugins.BaseTestSuiteSwc4jPlugin; | ||
import com.caoccao.javet.swc4j.plugins.Swc4jPluginHost; | ||
import com.caoccao.javet.swc4j.plugins.Swc4jPluginVisitors; | ||
import com.caoccao.javet.swc4j.utils.SimpleMap; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class TestSwc4jPluginVisitorJsFuckDecoder extends BaseTestSuiteSwc4jPlugin { | ||
|
||
@BeforeEach | ||
@Override | ||
protected void beforeEach() { | ||
super.beforeEach(); | ||
Swc4jPluginVisitors pluginVisitors = new Swc4jPluginVisitors() | ||
.add(new Swc4jPluginVisitorEs2015TransformSpread()); | ||
Swc4jPluginHost pluginHost = new Swc4jPluginHost() | ||
.add(pluginVisitors); | ||
jsScriptTransformOptions.setPluginHost(pluginHost); | ||
} | ||
|
||
@Test | ||
public void testArrayLitFirst() { | ||
assertTransformAndEvalAsString(SimpleMap.of( | ||
"const a = [1,2]; const b = [3,4]; JSON.stringify([...a, ...b]);", | ||
"const a=[1,2];const b=[3,4];JSON.stringify(a.concat(b));", | ||
"const a = [1,2]; const b = [3,4]; JSON.stringify([...a, ...b, ...b, ...a]);", | ||
"const a=[1,2];const b=[3,4];JSON.stringify(a.concat(b).concat(b).concat(a));")); | ||
} | ||
|
||
@Test | ||
public void testArrayLitOne() { | ||
assertTransformAndEvalAsString(SimpleMap.of( | ||
"const a = [1,2]; JSON.stringify([...a]);", | ||
"const a=[1,2];JSON.stringify(a);")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters