Skip to content

Commit

Permalink
Merge pull request #117 from angelolloqui/fix/116
Browse files Browse the repository at this point in the history
#116 Fixed implicit return statements from Swift 5.2
  • Loading branch information
angelolloqui authored May 17, 2020
2 parents 530a4db + 09af2d1 commit 6cb381e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 18 deletions.
3 changes: 3 additions & 0 deletions Assets/Tests/KotlinTokenizer/functions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ fun tokenize(codeBlock: String?) : List<String> {
public fun <T> whenAll(promises: List<Promise<T>>) : Promise<List<T>> =
Promise<T>()

public fun <T> whenAny(promises: List<Promise<T>>) : Promise<List<T>> =
Promise<T>()

fun sumOf(vararg numbers: Int) : Int {
var sum = 0
for (number in numbers) {
Expand Down
5 changes: 5 additions & 0 deletions Assets/Tests/KotlinTokenizer/functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public func whenAll<T>(promises: [Promise<T>]) -> Promise<[T]> {
return Promise<T>()
}


public func whenAny<T>(promises: [Promise<T>]) -> Promise<[T]> {
Promise<T>()
}

func sumOf(_ numbers: Int...) -> Int {
var sum = 0
for number in numbers {
Expand Down
22 changes: 13 additions & 9 deletions Assets/Tests/KotlinTokenizer/properties.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,21 @@ class A {
val stateObservable1: Observable<RestaurantsListState>
get() = state.asObservable()
val stateObservable2: Observable<RestaurantsListState>
get() {
return state.asObservable()
}
get() = state.asObservable()
val stateObservable3: Observable<RestaurantsListState>
get() = state.asObservable()
val stateObservable4: Observable<RestaurantsListState>
get() {
NSLog("Multiple statements")
return state.asObservable()
}
var center: Point
get() {
return Point(x = centerX, y = centerY)
}
get() = Point(x = centerX, y = centerY)
set(newValue) {
origin.x = newValue.x - 100
}
var top: Point
get() {
return Point(x = topX, y = topY)
}
get() = Point(x = topX, y = topY)
set(val) {
origin.y = 0
origin.x = val.x
Expand All @@ -53,6 +49,14 @@ class A {
private set(newValue) {
field = newValue
}
val myVar: String
get() {
if (a == 5) {
return "test"
} else {
return "b"
}
}
}

data class Rect(
Expand Down
13 changes: 10 additions & 3 deletions Assets/Tests/KotlinTokenizer/properties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class A {
return state.asObservable()
}
}
var stateObservable3: Observable<RestaurantsListState> {
var stateObservable3: Observable<RestaurantsListState> { state.asObservable() }
var stateObservable4: Observable<RestaurantsListState> {
NSLog("Multiple statements")
return state.asObservable()
}
Expand All @@ -28,7 +29,7 @@ class A {

var top: Point {
get {
return Point(x: topX, y: topY)
Point(x: topX, y: topY)
}
set(val) {
origin.y = 0
Expand Down Expand Up @@ -59,7 +60,13 @@ class A {
private(set) var anotherNameWithDidSet: String = "a value" {
didSet {}
}

var myVar: String {
if a == 5 {
return "test"
} else {
return "b"
}
}
}

struct Rect {
Expand Down
18 changes: 12 additions & 6 deletions Sources/SwiftKotlinFramework/KotlinTokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ public class KotlinTokenizer: SwiftTokenizer {
}

open override func tokenize(_ block: GetterSetterBlock, node: ASTNode) -> [Token] {
block.getter.codeBlock.setLexicalParent(node)
let getterTokens = tokenize(block.getter, node: node)
.replacing({ $0.kind == .keyword && $0.value == "get" }, with: [block.newToken(.keyword, "get()", node)])
let setterTokens = block.setter.map { tokenize($0, node: node) } ?? []
Expand Down Expand Up @@ -453,17 +454,22 @@ 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)
guard codeBlock.statements.count == 1, let statement = codeBlock.statements.first, let parent = codeBlock.lexicalParent,
!(statement is SwitchStatement), !(statement is IfStatement), // Conditional statements have returns inside that are not compatible with the = optimization
parent is VariableDeclaration || (parent as? FunctionDeclaration)?.signature.result != nil
else { return super.tokenize(codeBlock) }

let bodyTokens: [Token]
if let returnStatement = statement as? ReturnStatement {
bodyTokens = returnStatement.expression.map { tokenize($0) } ?? []
} else {
bodyTokens = tokenize(statement)
}
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)
sameLine ? bodyTokens : indent(bodyTokens)
].joined(token: separator)
}

Expand Down

0 comments on commit 6cb381e

Please sign in to comment.