From 9e2f8f548a5d467eeff75a0e1ad29a17e100613a Mon Sep 17 00:00:00 2001 From: Krzysztof Zablocki Date: Wed, 21 Dec 2016 14:53:07 +0100 Subject: [PATCH] fix: workaround for typealias parsing causing crash --- CHANGELOG.md | 4 ++++ Sourcery.podspec | 2 +- Sourcery/Parsing/Parser.swift | 23 ++++++++++++----------- Sourcery/Sourcery.swift | 2 +- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73eb35355..69240d5b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Sourcery CHANGELOG --- +## 0.4.6 + +### Bug Fixes +- Typealiases parsing could cause crash, implemented a workaround for that until we can find a little more reliable solution ## 0.4.5 diff --git a/Sourcery.podspec b/Sourcery.podspec index 03dad9e0d..5b704a083 100644 --- a/Sourcery.podspec +++ b/Sourcery.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "Sourcery" - s.version = "0.4.5" + s.version = "0.4.6" s.summary = "A tool that brings meta-programming to Swift, allowing you to code generate Swift code." s.description = <<-DESC diff --git a/Sourcery/Parsing/Parser.swift b/Sourcery/Parsing/Parser.swift index b5b337f4c..9d1ecf63f 100644 --- a/Sourcery/Parsing/Parser.swift +++ b/Sourcery/Parsing/Parser.swift @@ -587,23 +587,24 @@ extension Parser { // replace all processed substructures with whitespaces so that we don't process their typealiases again for substructure in processed { - if let range = SubstringIdentifier.key.range(for: substructure) - .flatMap({ contentToParse.byteRangeToNSRange(start: Int($0.offset), length: Int($0.length)) }) { + if let substring = extract(.key, from: substructure) { - let replacement = String(repeating: " ", count: range.length) - contentToParse = contentToParse.bridge().replacingCharacters(in: range, with: replacement) + let replacementCharacter = " " + let count = substring.lengthOfBytes(using: .utf8) / replacementCharacter.lengthOfBytes(using: .utf8) + let replacement = String(repeating: replacementCharacter, count: count) + contentToParse = contentToParse.bridge().replacingOccurrences(of: substring, with: replacement) } } - if containingType != nil { - if let body = extract(.body, from: source, contents: contentToParse) { - return parseTypealiases(SyntaxMap(file: File(contents: body)).tokens, contents: body) - } else { - return [] - } - } else { + guard containingType != nil else { return parseTypealiases(SyntaxMap(file: File(contents: contentToParse)).tokens, contents: contentToParse) } + + if let body = extract(.body, from: source, contents: contentToParse) { + return parseTypealiases(SyntaxMap(file: File(contents: body)).tokens, contents: body) + } else { + return [] + } } private func parseTypealiases(_ tokens: [SyntaxToken], contents: String, existingTypealiases: [Typealias] = []) -> [Typealias] { diff --git a/Sourcery/Sourcery.swift b/Sourcery/Sourcery.swift index a2c22884d..a6fa0d2cc 100644 --- a/Sourcery/Sourcery.swift +++ b/Sourcery/Sourcery.swift @@ -37,7 +37,7 @@ internal class SourceryTemplate: Template { /// If you specify templatePath as a folder, it will create a Generated[TemplateName].swift file /// If you specify templatePath as specific file, it will put all generated results into that single file public class Sourcery { - public static let version: String = inUnitTests ? "Major.Minor.Patch" : "0.4.5" + public static let version: String = inUnitTests ? "Major.Minor.Patch" : "0.4.6" public static let generationMarker: String = "// Generated using Sourcery" let verbose: Bool