Skip to content

Commit

Permalink
fix: workaround for typealias parsing causing crash
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztofzablocki committed Dec 21, 2016
1 parent 663f74b commit 9e2f8f5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion Sourcery.podspec
Original file line number Diff line number Diff line change
@@ -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
Expand Down
23 changes: 12 additions & 11 deletions Sourcery/Parsing/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down
2 changes: 1 addition & 1 deletion Sourcery/Sourcery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9e2f8f5

Please sign in to comment.