From b2fa60f2d8bb76a8b626d72457d465709b4cd241 Mon Sep 17 00:00:00 2001 From: Angel Garcia Date: Sat, 3 Feb 2018 12:36:00 +0100 Subject: [PATCH 1/6] #26 Removed optional chaining in lambdas --- Sources/SwiftKotlinFramework/KotlinTokenizer.swift | 9 ++++++++- .../Tests/KotlinTokenizer/lambdas.kt | 8 ++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Sources/SwiftKotlinFramework/KotlinTokenizer.swift b/Sources/SwiftKotlinFramework/KotlinTokenizer.swift index 2e535b6..1f1c5ac 100644 --- a/Sources/SwiftKotlinFramework/KotlinTokenizer.swift +++ b/Sources/SwiftKotlinFramework/KotlinTokenizer.swift @@ -692,7 +692,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/lambdas.kt b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/lambdas.kt index d2d8656..9261ac1 100644 --- a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/lambdas.kt +++ b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/lambdas.kt @@ -1,11 +1,11 @@ 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) From e0a49e4138b0a89e7f6a3f448e1d611e5206ff4b Mon Sep 17 00:00:00 2001 From: Angel Garcia Date: Sat, 3 Feb 2018 14:29:10 +0100 Subject: [PATCH 2/6] #20 Optimized body returns to single line --- .../KotlinTokenizer.swift | 15 +++++++++++ .../Tests/KotlinTokenizer/annotations.kt | 5 ++-- .../Tests/KotlinTokenizer/extensions.kt | 27 +++++++------------ .../Tests/KotlinTokenizer/functions.kt | 10 ++++--- .../Tests/KotlinTokenizer/functions.swift | 7 +++++ .../Tests/KotlinTokenizer/properties.kt | 4 +-- .../Tests/KotlinTokenizer/statics.kt | 10 +++---- 7 files changed, 45 insertions(+), 33 deletions(-) diff --git a/Sources/SwiftKotlinFramework/KotlinTokenizer.swift b/Sources/SwiftKotlinFramework/KotlinTokenizer.swift index 1f1c5ac..1b8c42f 100644 --- a/Sources/SwiftKotlinFramework/KotlinTokenizer.swift +++ b/Sources/SwiftKotlinFramework/KotlinTokenizer.swift @@ -394,6 +394,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] { 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/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..4cfcc94 100644 --- a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/functions.kt +++ b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/functions.kt @@ -7,7 +7,11 @@ 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) +} diff --git a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/functions.swift b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/functions.swift index 99289e5..d19f793 100644 --- a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/functions.swift +++ b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/functions.swift @@ -11,3 +11,10 @@ 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) +} 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 } } From 27d019d7bb779922f785f0cb6a710dfe47aa9372 Mon Sep 17 00:00:00 2001 From: Angel Garcia Date: Sat, 3 Feb 2018 16:32:17 +0100 Subject: [PATCH 3/6] #53 Added inheritance support to enums --- Sources/SwiftKotlinFramework/KotlinTokenizer.swift | 9 +++++---- .../Tests/KotlinTokenizer/enums.kt | 5 +++++ .../Tests/KotlinTokenizer/enums.swift | 6 ++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Sources/SwiftKotlinFramework/KotlinTokenizer.swift b/Sources/SwiftKotlinFramework/KotlinTokenizer.swift index 1b8c42f..7683907 100644 --- a/Sources/SwiftKotlinFramework/KotlinTokenizer.swift +++ b/Sources/SwiftKotlinFramework/KotlinTokenizer.swift @@ -323,14 +323,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 +352,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 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?) +} From 34fd658f390fdb92f2788ebeb2082cff29895ea7 Mon Sep 17 00:00:00 2001 From: Angel Garcia Date: Sat, 3 Feb 2018 16:43:52 +0100 Subject: [PATCH 4/6] #60 Fixed position of generics in function definitions --- .../KotlinTokenizer.swift | 24 ++++++++++++++++--- .../Tests/KotlinTokenizer/functions.kt | 3 +++ .../Tests/KotlinTokenizer/functions.swift | 4 ++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Sources/SwiftKotlinFramework/KotlinTokenizer.swift b/Sources/SwiftKotlinFramework/KotlinTokenizer.swift index 7683907..b2c0c3f 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] { diff --git a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/functions.kt b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/functions.kt index 4cfcc94..17acff7 100644 --- a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/functions.kt +++ b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/functions.kt @@ -15,3 +15,6 @@ 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 d19f793..0bdbe6e 100644 --- a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/functions.swift +++ b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/functions.swift @@ -18,3 +18,7 @@ func tokenize(_ codeBlock: String?) -> [String] { } return someOtherMethod(statement: statement) } + +public func whenAll(promises: [Promise]) -> Promise<[T]> { + return Promise() +} From 321db9e1a28aa89d0f74b6ce334b274c30fe1b8d Mon Sep 17 00:00:00 2001 From: Angel Garcia Date: Sat, 3 Feb 2018 16:47:57 +0100 Subject: [PATCH 5/6] Updated version --- Sources/SwiftKotlinApp/Info.plist | 2 +- Sources/SwiftKotlinCommandLine/main.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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", From 6e6015c73a5ff2b9de5c2af10c97ec25ec3ea811 Mon Sep 17 00:00:00 2001 From: Angel Garcia Date: Sat, 3 Feb 2018 17:05:13 +0100 Subject: [PATCH 6/6] #41 Added invoke to optional lambdas --- Sources/SwiftKotlinFramework/KotlinTokenizer.swift | 12 ++++++++++++ .../Tests/KotlinTokenizer/lambdas.kt | 2 ++ .../Tests/KotlinTokenizer/lambdas.swift | 3 +++ 3 files changed, 17 insertions(+) diff --git a/Sources/SwiftKotlinFramework/KotlinTokenizer.swift b/Sources/SwiftKotlinFramework/KotlinTokenizer.swift index b2c0c3f..326773f 100644 --- a/Sources/SwiftKotlinFramework/KotlinTokenizer.swift +++ b/Sources/SwiftKotlinFramework/KotlinTokenizer.swift @@ -608,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 }, diff --git a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/lambdas.kt b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/lambdas.kt index 9261ac1..2608c41 100644 --- a/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/lambdas.kt +++ b/Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/lambdas.kt @@ -10,3 +10,5 @@ userService.autoLinkTenant(tenantId = tenant.id).then { _ -> 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)