diff --git a/Sources/SwiftKotlinApp/Info.plist b/Sources/SwiftKotlinApp/Info.plist
index 2bb2f8e..2c9fd51 100644
--- a/Sources/SwiftKotlinApp/Info.plist
+++ b/Sources/SwiftKotlinApp/Info.plist
@@ -17,7 +17,7 @@
CFBundlePackageType
APPL
CFBundleShortVersionString
- 1.0
+ 0.1.3
CFBundleVersion
1
LSMinimumSystemVersion
diff --git a/Sources/SwiftKotlinCommandLine/main.swift b/Sources/SwiftKotlinCommandLine/main.swift
index cefe76d..53b3639 100644
--- a/Sources/SwiftKotlinCommandLine/main.swift
+++ b/Sources/SwiftKotlinCommandLine/main.swift
@@ -15,7 +15,7 @@ let kotlinTokenizer = KotlinTokenizer(
FoundationMethodsTransformPlugin()
]
)
-let version = "0.1.0"
+let version = "0.1.3"
let arguments = [
"output",
"help",
diff --git a/Sources/SwiftKotlinFramework/KotlinTokenizer.swift b/Sources/SwiftKotlinFramework/KotlinTokenizer.swift
index 2e535b6..326773f 100644
--- a/Sources/SwiftKotlinFramework/KotlinTokenizer.swift
+++ b/Sources/SwiftKotlinFramework/KotlinTokenizer.swift
@@ -23,9 +23,27 @@ public class KotlinTokenizer: SwiftTokenizer {
}
open override func tokenize(_ declaration: FunctionDeclaration) -> [Token] {
- return super.tokenize(declaration)
- .replacing({ $0.value == "func"},
- with: [declaration.newToken(.keyword, "fun")])
+ let attrsTokens = tokenize(declaration.attributes, node: declaration)
+ let modifierTokens = declaration.modifiers.map { tokenize($0, node: declaration) }
+ .joined(token: declaration.newToken(.space, " "))
+ let genericParameterClauseTokens = declaration.genericParameterClause.map { tokenize($0, node: declaration) } ?? []
+
+ let headTokens = [
+ attrsTokens,
+ modifierTokens,
+ [declaration.newToken(.keyword, "fun")],
+ genericParameterClauseTokens
+ ].joined(token: declaration.newToken(.space, " "))
+
+ let signatureTokens = tokenize(declaration.signature, node: declaration)
+ let bodyTokens = declaration.body.map(tokenize) ?? []
+
+ return [
+ headTokens,
+ [declaration.newToken(.identifier, declaration.name)] + signatureTokens,
+ bodyTokens
+ ].joined(token: declaration.newToken(.space, " "))
+ .prefix(with: declaration.newToken(.linebreak, "\n"))
}
open override func tokenize(_ parameter: FunctionSignature.Parameter, node: ASTNode) -> [Token] {
@@ -323,14 +341,13 @@ public class KotlinTokenizer: SwiftTokenizer {
guard unionCases.count == declaration.members.count &&
declaration.genericParameterClause == nil &&
- declaration.genericWhereClause == nil &&
- declaration.typeInheritanceClause == nil else {
+ declaration.genericWhereClause == nil else {
return self.unsupportedTokens(message: "Complex enums not supported yet", element: declaration, node: declaration).suffix(with: lineBreak) +
super.tokenize(declaration)
}
// Simple enums (no tuples)
- if !simpleCases.contains(where: { $0.tuple != nil }) {
+ if !simpleCases.contains(where: { $0.tuple != nil }) && declaration.typeInheritanceClause == nil {
let attrsTokens = tokenize(declaration.attributes, node: declaration)
let modifierTokens = declaration.accessLevelModifier.map { tokenize($0, node: declaration) } ?? []
let headTokens = [
@@ -353,16 +370,18 @@ public class KotlinTokenizer: SwiftTokenizer {
indent(membersTokens) +
[lineBreak, declaration.newToken(.endOfScope, "}")]
}
- // Tuples required sealed classes
+ // Tuples or inhertance required sealed classes
else {
let attrsTokens = tokenize(declaration.attributes, node: declaration)
let modifierTokens = declaration.accessLevelModifier.map { tokenize($0, node: declaration) } ?? []
+ let inheritanceTokens = declaration.typeInheritanceClause.map { tokenize($0, node: declaration) } ?? []
let headTokens = [
attrsTokens,
modifierTokens,
[declaration.newToken(.keyword, "sealed")],
[declaration.newToken(.keyword, "class")],
[declaration.newToken(.identifier, declaration.name)],
+ inheritanceTokens
].joined(token: space)
let membersTokens = simpleCases.map { c in
@@ -394,6 +413,21 @@ public class KotlinTokenizer: SwiftTokenizer {
}
+ open override func tokenize(_ codeBlock: CodeBlock) -> [Token] {
+ guard codeBlock.statements.count == 1,
+ let returnStatement = codeBlock.statements.first as? ReturnStatement,
+ let parent = codeBlock.lexicalParent as? Declaration else {
+ return super.tokenize(codeBlock)
+ }
+ let sameLine = parent is VariableDeclaration
+ let separator = sameLine ? codeBlock.newToken(.space, " ") : codeBlock.newToken(.linebreak, "\n")
+ let tokens = Array(tokenize(returnStatement).dropFirst(2))
+ return [
+ [codeBlock.newToken(.symbol, "=")],
+ sameLine ? tokens : indent(tokens)
+ ].joined(token: separator)
+ }
+
// MARK: - Statements
open override func tokenize(_ statement: GuardStatement) -> [Token] {
@@ -574,6 +608,18 @@ public class KotlinTokenizer: SwiftTokenizer {
with: [expression.newToken(.symbol, binaryOperator)])
}
+ open override func tokenize(_ expression: FunctionCallExpression) -> [Token] {
+ var tokens = super.tokenize(expression)
+ if (expression.postfixExpression is OptionalChainingExpression || expression.postfixExpression is ForcedValueExpression),
+ let startIndex = tokens.indexOf(kind: .startOfScope, after: 0) {
+ tokens.insert(contentsOf: [
+ expression.newToken(.symbol, "."),
+ expression.newToken(.keyword, "invoke")
+ ], at: startIndex)
+ }
+ return tokens
+ }
+
open override func tokenize(_ expression: FunctionCallExpression.Argument, node: ASTNode) -> [Token] {
return super.tokenize(expression, node: node)
.replacing({ $0.value == ": " && $0.kind == .delimiter },
@@ -692,7 +738,14 @@ public class KotlinTokenizer: SwiftTokenizer {
}
}
-
+ open override func tokenize(_ expression: OptionalChainingExpression) -> [Token] {
+ var tokens = tokenize(expression.postfixExpression)
+ if tokens.last?.value != "this" {
+ tokens.append(expression.newToken(.symbol, "?"))
+ }
+ return tokens
+ }
+
// MARK: - Types
open override func tokenize(_ type: ArrayType, node: ASTNode) -> [Token] {
return
diff --git a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/annotations.kt b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/annotations.kt
index 6ec5b3c..25096fe 100644
--- a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/annotations.kt
+++ b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/annotations.kt
@@ -12,6 +12,5 @@ fun collectCustomerProviders(customerProvider: () -> String) {
customerProviders.append(customerProvider)
}
-fun foo(code: (() -> String)) : String {
- return "foo ${bar(code)}"
-}
+fun foo(code: (() -> String)) : String =
+ "foo ${bar(code)}"
diff --git a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/enums.kt b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/enums.kt
index 65957fc..3446fa9 100644
--- a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/enums.kt
+++ b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/enums.kt
@@ -34,3 +34,8 @@ when (exception) {
}
else -> trackError(name = "generic", message = R.string.localizable.generic_error())
}
+public sealed class SDKException : Error {
+ object notFound : SDKException()
+ object unauthorized : SDKException()
+ data class network(val v1: HttpResponse, val v2: Error?) : SDKException()
+}
diff --git a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/enums.swift b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/enums.swift
index 57792e1..3fcdb57 100644
--- a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/enums.swift
+++ b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/enums.swift
@@ -35,3 +35,9 @@ case .qrCode(_):
default:
trackError(name: "generic", message: R.string.localizable.generic_error())
}
+
+public enum SDKException: Error {
+ case notFound
+ case unauthorized
+ case network(HttpResponse, Error?)
+}
diff --git a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/extensions.kt b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/extensions.kt
index af8175c..e21c5ac 100644
--- a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/extensions.kt
+++ b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/extensions.kt
@@ -1,24 +1,15 @@
val Double.km: Double
- get() {
- return this * 1000.0
- }
+ get() = this * 1000.0
val Double.m: Double
- get() {
- return this
- }
+ get() = this
-open fun Double.toKm() : Double {
- return this * 1000.0
-}
+open fun Double.toKm() : Double =
+ this * 1000.0
-fun Double.toMeter() : Double {
- return this
-}
+fun Double.toMeter() : Double =
+ this
-public fun Double.Companion.toKm() : Double {
- return this * 1000.0
-}
+public fun Double.Companion.toKm() : Double =
+ this * 1000.0
public val Double.Companion.m: Double
- get() {
- return this
- }
+ get() = this
diff --git a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/functions.kt b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/functions.kt
index 7c9ea3b..17acff7 100644
--- a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/functions.kt
+++ b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/functions.kt
@@ -7,7 +7,14 @@ fun method(param: String) : String {}
fun method(param: (Int) -> Unit) {}
-fun findRestaurant(restaurantId: Int) : ServiceTask {
- return NetworkRequestServiceTask(networkSession = networkSession, endpoint = "restaurants/")
-}
+fun findRestaurant(restaurantId: Int) : ServiceTask =
+ NetworkRequestServiceTask(networkSession = networkSession, endpoint = "restaurants/")
restaurantService.findRestaurant(restaurantId = restaurant.id, param = param)
+
+fun tokenize(codeBlock: String?) : List {
+ val statement = codeBlock ?: return listOf()
+ return someOtherMethod(statement = statement)
+}
+
+public fun whenAll(promises: List>) : Promise> =
+ Promise()
diff --git a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/functions.swift b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/functions.swift
index 99289e5..0bdbe6e 100644
--- a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/functions.swift
+++ b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/functions.swift
@@ -11,3 +11,14 @@ func findRestaurant(restaurantId: Int) -> ServiceTask {
}
restaurantService.findRestaurant(restaurantId: restaurant.id, param: param)
+
+func tokenize(_ codeBlock: String?) -> [String] {
+ guard let statement = codeBlock else {
+ return []
+ }
+ return someOtherMethod(statement: statement)
+}
+
+public func whenAll(promises: [Promise]) -> Promise<[T]> {
+ return Promise()
+}
diff --git a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/lambdas.kt b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/lambdas.kt
index d2d8656..2608c41 100644
--- a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/lambdas.kt
+++ b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/lambdas.kt
@@ -1,12 +1,14 @@
userService.updateUser(picture = picture).always {
- this?.hasPhoto = true
+ this.hasPhoto = true
}
userService.autoLinkTenant(tenantId = tenant.id).then { _ ->
- this?.startPayment(paymentMethod, true)
+ this.startPayment(paymentMethod, true)
}.catchError { _ ->
- val intent = this?.coordinator?.autoRegisterIntent(tenant = tenant, onComplete = { this?.startPayment(paymentMethod, true) })
- this?.navigationManager?.show(intent, animation = .push)
+ val intent = this.coordinator.autoRegisterIntent(tenant = tenant, onComplete = { this.startPayment(paymentMethod, true) })
+ this.navigationManager.show(intent, animation = .push)
}
item.selectCallback = { option ->
presenter.selectPaymentMethod(option)
}
+item.selectCallback?.invoke(option)
+item.selectCallback!!.invoke(option)
diff --git a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/lambdas.swift b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/lambdas.swift
index 2e25711..196cd60 100644
--- a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/lambdas.swift
+++ b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/lambdas.swift
@@ -16,3 +16,6 @@ userService.autoLinkTenant(tenantId: tenant.id).then { [weak self] _ in
item.selectCallback = { option in
presenter.selectPaymentMethod(option)
}
+
+item.selectCallback?(option)
+item.selectCallback!(option)
diff --git a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/properties.kt b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/properties.kt
index ba0bfbd..9349506 100644
--- a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/properties.kt
+++ b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/properties.kt
@@ -6,9 +6,7 @@ interface Hello {
class A {
val stateObservable1: Observable
- get() {
- return state.asObservable()
- }
+ get() = state.asObservable()
val stateObservable2: Observable
get() {
return state.asObservable()
diff --git a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/statics.kt b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/statics.kt
index c275b08..3e57a3a 100644
--- a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/statics.kt
+++ b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/statics.kt
@@ -25,13 +25,11 @@ class A {
fun method() {}
- fun create() : A? {
- return null
- }
+ fun create() : A? =
+ null
- fun withParams(param: Int) : A? {
- return null
- }
+ fun withParams(param: Int) : A? =
+ null
}
}