-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from angelolloqui/feature/fixes
Feature/fixes
- Loading branch information
Showing
17 changed files
with
219 additions
and
18 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
74 changes: 74 additions & 0 deletions
74
Sources/SwiftKotlinFramework/plugins/FoundationMethodsTransformPlugin.swift
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,74 @@ | ||
// | ||
// FoundationMethodsTransformPlugin.swift | ||
// SwiftKotlinPackageDescription | ||
// | ||
// Created by Angel Luis Garcia on 10/12/2017. | ||
// | ||
|
||
import Foundation | ||
import Transform | ||
import AST | ||
|
||
public class FoundationMethodsTransformPlugin: TokenTransformPlugin { | ||
public var name: String { | ||
return "Foundation transformations" | ||
} | ||
|
||
public var description: String { | ||
return "Transforms methods on lists, maps,... like `first`, `count`... to their Kotlin variant" | ||
} | ||
|
||
public init() {} | ||
|
||
public func transform(tokens: [Token], topDeclaration: TopLevelDeclaration) throws -> [Token] { | ||
var newTokens = [Token]() | ||
|
||
for token in tokens { | ||
if token.kind == .identifier, | ||
let memberExpression = token.origin as? ExplicitMemberExpression, | ||
case ExplicitMemberExpression.Kind.namedType(let expression, let identifier) = memberExpression.kind, | ||
let inferredType = inferTypeFor(expression: expression, topDeclaration: topDeclaration), | ||
let replace = memberStringMappings[inferredType]?[identifier] { | ||
newTokens.append(memberExpression.newToken(.identifier, replace)) | ||
} else if token.kind == .identifier, token.value == "fatalError", let origin = token.origin, let node = token.node { | ||
newTokens.append(origin.newToken(.keyword, "throw", node)) | ||
newTokens.append(origin.newToken(.space, " ", node)) | ||
newTokens.append(origin.newToken(.identifier, "Exception", node)) | ||
} else { | ||
newTokens.append(token) | ||
} | ||
} | ||
|
||
return newTokens | ||
} | ||
|
||
func inferTypeFor(expression: PostfixExpression, topDeclaration: TopLevelDeclaration) -> String? { | ||
// TODO: Right now there is no way to infer types. Will be fixed in future versions of AST | ||
return "List" | ||
} | ||
|
||
let memberStringMappings = [ | ||
"List": [ | ||
"first": "firstOrNull()", | ||
"last": "lastOrNull()", | ||
"count": "size", | ||
"isEmpty": "isEmpty()" | ||
], | ||
"String": [ | ||
"count": "length", | ||
"uppercased": "toUpperCase", | ||
"lowercased": "toLowerCase" | ||
] | ||
] | ||
|
||
// TODO: How to map regex expressions that affect multiple tokens? | ||
let memberRegexMappings = [ | ||
"List": [ | ||
"index(of: \\(.+))": "indexOf($1)", | ||
"append(\\(.+))": "add($1)", | ||
"remove(at: \\(.+))": "removeAt($1)", | ||
"sorted(by: \\(.+))": "sortedWith(comparator = Comparator($1))", | ||
"joined(separator: \\(.+))": "joinToString(separator = $1)" | ||
] | ||
] | ||
} |
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
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
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
8 changes: 8 additions & 0 deletions
8
Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/expressions.kt
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,8 @@ | ||
|
||
val value = if (isTrue) "yes" else "no" | ||
val label = if (x > 0) "Positive" else "negative" | ||
button.color = if (item.deleted) red else green | ||
val text = label ?: "default" | ||
service.deleteObject() | ||
this.service.fetchData()?.user?.name?.size | ||
this.data.filter { it.item?.value == 1 }.map { it.key }.firstOrNull()?.name?.size |
14 changes: 14 additions & 0 deletions
14
Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/expressions.swift
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,14 @@ | ||
|
||
|
||
// Ternary and coalescing operators | ||
let value = isTrue ? "yes" : "no" | ||
let label = x > 0 ? "Positive" : "negative" | ||
button.color = item.deleted ? red : green | ||
let text = label ?? "default" | ||
|
||
// Wilcard assignments | ||
_ = service.deleteObject() | ||
|
||
// Optional chaning | ||
self.service.fetchData()?.user.name?.count | ||
self.data.filter { $0.item?.value == 1 }.map { $0.key }.first?.name.count |
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
6 changes: 6 additions & 0 deletions
6
Tests/SwiftKotlinFrameworkTests/Tests/plugins/FoundationMethodsTransformPlugin.kt
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,6 @@ | ||
val list = listOf(1, 2, 3) | ||
list.firstOrNull() | ||
list.lastOrNull() | ||
list.size | ||
list.isEmpty() | ||
throw Exception("An error ${error} occurred") |
19 changes: 19 additions & 0 deletions
19
Tests/SwiftKotlinFrameworkTests/Tests/plugins/FoundationMethodsTransformPlugin.swift
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,19 @@ | ||
let list = [1, 2, 3] | ||
|
||
list.first | ||
list.last | ||
list.count | ||
list.isEmpty | ||
//list.index(of: a) --> list.indexOf(a) | ||
//list.append(element) --> list.add(element) | ||
//list.remove(at: index) --> list.removeAt(index) | ||
//list.sorted(by: lambda) --> sortedWith(comparator = Comparator(lambda)) | ||
//list.joined(separator: joiner) --> elements. joinToString(separator = joiner) | ||
|
||
//let string = "A string" | ||
//string.count --> string.length | ||
//string.uppercased() --> string.toUpperCase() | ||
//string.lowercased() --> string.toLowerCase() | ||
|
||
fatalError("An error \(error) occurred") | ||
|
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