Skip to content

Commit

Permalink
test(swc-binding): update numericLiteral value to Double
Browse files Browse the repository at this point in the history
  • Loading branch information
yidafu committed Dec 16, 2023
1 parent bcceaf8 commit c4a56f4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 17 deletions.
80 changes: 67 additions & 13 deletions swc-binding/src/test/kotlin/dev/yidafu/swc/SwcNativeTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package dev.yidafu.swc

import dev.yidafu.swc.dsl.*
import dev.yidafu.swc.types.*
import dev.yidafu.swc.types.Module
import org.junit.jupiter.api.assertThrows
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
Expand All @@ -20,7 +21,7 @@ class SwcNativeTest {
add(1,2);
""".trimIndent(),
esParseOptions { },
"temp.js"
"temp.js",
)
println(ast)
assertNotNull(ast) { "ast string can't be null " }
Expand All @@ -30,8 +31,8 @@ class SwcNativeTest {
fun `parse js file to ast str`() {
val ast =
swcNative.parseFileSync(
SwcNativeTest::class.java.classLoader.getResource("test.js").file,
tsParseOptions { }
SwcNativeTest::class.java.classLoader.getResource("test.js")!!.file,
tsParseOptions { },
)
assertNotNull(ast) { "ast string can't be null " }
}
Expand All @@ -41,15 +42,23 @@ class SwcNativeTest {
val ast =
swcNative.transformSync(
"function add(a, b) {return a + b;}; add(1,2)",
true,
false,
options {
jsc =
jscConfig {
parser = tsParserConfig { }
}
}
},
)
assertNotNull(ast) { "transform result can't be null " }
assertEquals(
ast.code.trim(),
"""
function add(a, b) {
return a + b;
}
add(1, 2);
""".trimIndent(),
)
}

@Test
Expand All @@ -63,7 +72,7 @@ class SwcNativeTest {
jscConfig {
parser = esParserConfig { }
}
}
},
)
assertNotNull(ast) { "transform result can't be null " }
}
Expand All @@ -79,7 +88,7 @@ class SwcNativeTest {
jscConfig {
parser = esParserConfig { }
}
}
},
)

assertEquals(
Expand All @@ -89,10 +98,55 @@ class SwcNativeTest {
return a + b;
}
add(1, 2);
""".trimIndent()
""".trimIndent(),
)
}

@Test
fun `parse import statements`() {
val module = swcNative.parseSync(
"""
import { foo, getRoot, bar as baz } from '@jupyter';
import jupyter from '@jupyter';
const b = 345;
console.log(b)
""".trimIndent(),
esParseOptions {
target = "es2020"
comments = false
topLevelAwait = true
nullishCoalescing = true
},
"jupyter-cell.js",
) as Module
val output = swcNative.printSync(
module,
options {
jscConfig {
minify = jsMinifyOptions {
}
}
},
)
println(output.code)
println("moduel => ${module.body?.get(0)}")
}

@Test
fun `parse invalid js code`() {
assertThrows<RuntimeException> {
val module = swcNative.parseSync(
"""
val a = 234; // kotlin code
""".trimIndent(),
esParseOptions { },
"test.js",
) as Module
}
}
// @Test
// fun `transform ast json file to ast str`() {
// val ast = swcNative.transformFileSync(
Expand Down Expand Up @@ -222,7 +276,7 @@ class SwcNativeTest {
"interpreter": null
}
""".trimIndent(),
"{}"
"{}",
)

println(res)
Expand All @@ -233,7 +287,7 @@ class SwcNativeTest {
val output =
swcNative.printSync(
esAddFunction,
options { }
options { },
)

assertEquals(
Expand All @@ -244,7 +298,7 @@ class SwcNativeTest {
}
;
add(1, 2);
""".trimIndent()
""".trimIndent(),
)
}
}
4 changes: 2 additions & 2 deletions swc-binding/src/test/kotlin/dev/yidafu/swc/TypesNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AstNodeTest {
val expr = BinaryExpressionImpl().apply {
operator = BinaryOperator.UnaryPlus
left = NumericLiteralImpl().apply {
value = 2
value = 2.0
raw = "2"
span = Span().apply {
start = 1
Expand All @@ -21,7 +21,7 @@ class AstNodeTest {
}

right = NumericLiteralImpl().apply {
value = 2
value = 2.0
raw = "2"
span = Span().apply {
start = 4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ val esAddFunction: Module =
end = 48
ctxt = 0
}
value = 1
value = 1.0
raw = "1"
}
},
Expand All @@ -189,7 +189,7 @@ val esAddFunction: Module =
end = 50
ctxt = 2
}
value = 2
value = 2.0
raw = "2"
}
}
Expand Down

0 comments on commit c4a56f4

Please sign in to comment.