|
| 1 | +// |
| 2 | +// FoundationMethodsTransformPlugin.swift |
| 3 | +// SwiftKotlinPackageDescription |
| 4 | +// |
| 5 | +// Created by Angel Luis Garcia on 10/12/2017. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import Transform |
| 10 | +import AST |
| 11 | + |
| 12 | +public class FoundationMethodsTransformPlugin: TokenTransformPlugin { |
| 13 | + public var name: String { |
| 14 | + return "Foundation transformations" |
| 15 | + } |
| 16 | + |
| 17 | + public var description: String { |
| 18 | + return "Transforms methods on lists, maps,... like `first`, `count`... to their Kotlin variant" |
| 19 | + } |
| 20 | + |
| 21 | + public init() {} |
| 22 | + |
| 23 | + public func transform(tokens: [Token], topDeclaration: TopLevelDeclaration) throws -> [Token] { |
| 24 | + var newTokens = [Token]() |
| 25 | + |
| 26 | + for token in tokens { |
| 27 | + if token.kind == .identifier, |
| 28 | + let memberExpression = token.origin as? ExplicitMemberExpression, |
| 29 | + case ExplicitMemberExpression.Kind.namedType(let expression, let identifier) = memberExpression.kind, |
| 30 | + let inferredType = inferTypeFor(expression: expression, topDeclaration: topDeclaration), |
| 31 | + let replace = memberStringMappings[inferredType]?[identifier] { |
| 32 | + newTokens.append(memberExpression.newToken(.identifier, replace)) |
| 33 | + } else if token.kind == .identifier, token.value == "fatalError", let origin = token.origin, let node = token.node { |
| 34 | + newTokens.append(origin.newToken(.keyword, "throw", node)) |
| 35 | + newTokens.append(origin.newToken(.space, " ", node)) |
| 36 | + newTokens.append(origin.newToken(.identifier, "Exception", node)) |
| 37 | + } else { |
| 38 | + newTokens.append(token) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + return newTokens |
| 43 | + } |
| 44 | + |
| 45 | + func inferTypeFor(expression: PostfixExpression, topDeclaration: TopLevelDeclaration) -> String? { |
| 46 | + // TODO: Right now there is no way to infer types. Will be fixed in future versions of AST |
| 47 | + return "List" |
| 48 | + } |
| 49 | + |
| 50 | + let memberStringMappings = [ |
| 51 | + "List": [ |
| 52 | + "first": "firstOrNull()", |
| 53 | + "last": "lastOrNull()", |
| 54 | + "count": "size", |
| 55 | + "isEmpty": "isEmpty()" |
| 56 | + ], |
| 57 | + "String": [ |
| 58 | + "count": "length", |
| 59 | + "uppercased": "toUpperCase", |
| 60 | + "lowercased": "toLowerCase" |
| 61 | + ] |
| 62 | + ] |
| 63 | + |
| 64 | + // TODO: How to map regex expressions that affect multiple tokens? |
| 65 | + let memberRegexMappings = [ |
| 66 | + "List": [ |
| 67 | + "index(of: \\(.+))": "indexOf($1)", |
| 68 | + "append(\\(.+))": "add($1)", |
| 69 | + "remove(at: \\(.+))": "removeAt($1)", |
| 70 | + "sorted(by: \\(.+))": "sortedWith(comparator = Comparator($1))", |
| 71 | + "joined(separator: \\(.+))": "joinToString(separator = $1)" |
| 72 | + ] |
| 73 | + ] |
| 74 | +} |
0 commit comments