Skip to content

Commit

Permalink
Merge pull request #228 from stencilproject/swift_4
Browse files Browse the repository at this point in the history
Swift 4.1
  • Loading branch information
yonaskolb authored Sep 10, 2018
2 parents acda1b0 + 93ccc56 commit d71fe2a
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 52 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
Packages/
Package.resolved
Package.pins
*.xcodeproj
14 changes: 3 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
matrix:
include:
- os: osx
osx_image: xcode8.3
env: SWIFT_VERSION=3.1.1
- os: osx
osx_image: xcode9
env: SWIFT_VERSION=4.0
- os: osx
osx_image: xcode9.1
env: SWIFT_VERSION=4.0
- os: linux
env: SWIFT_VERSION=3.1.1
osx_image: xcode9.3
env: SWIFT_VERSION=4.1
- os: linux
env: SWIFT_VERSION=4.0
env: SWIFT_VERSION=4.1
language: generic
sudo: required
dist: trusty
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

### Breaking Changes

_None_
- Now requires Swift 4.1 or newer.
[Yonas Kolb](https://github.com/yonaskolb)
[#228](https://github.com/stencilproject/Stencil/pull/228)

### New Features

Expand Down
18 changes: 15 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
// swift-tools-version:3.1
// swift-tools-version:4.0
import PackageDescription

let package = Package(
name: "Stencil",
products: [
.library(name: "Stencil", targets: ["Stencil"]),
],
dependencies: [
.Package(url: "https://github.com/kylef/PathKit.git", majorVersion: 0, minor: 9),
.Package(url: "https://github.com/kylef/Spectre.git", majorVersion: 0, minor: 8),
.package(url: "https://github.com/kylef/PathKit.git", from: "0.9.0"),
.package(url: "https://github.com/kylef/Spectre.git", from: "0.8.0"),
],
targets: [
.target(name: "Stencil", dependencies: [
"PathKit",
], path: "Sources"),
.testTarget(name: "StencilTests", dependencies: [
"Stencil",
"Spectre",
])
]
)
10 changes: 0 additions & 10 deletions [email protected]

This file was deleted.

2 changes: 1 addition & 1 deletion Sources/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ open class SimpleErrorReporter: ErrorReporter {
func describe(token: Token) -> String {
let templateName = token.sourceMap.filename ?? ""
let location = token.sourceMap.location
let highlight = "\(String(Array(repeating: " ", count: location.lineOffset)))^\(String(Array(repeating: "~", count: max(token.contents.characters.count - 1, 0))))"
let highlight = "\(String(Array(repeating: " ", count: location.lineOffset)))^\(String(Array(repeating: "~", count: max(token.contents.count - 1, 0))))"

return "\(templateName)\(location.lineNumber):\(location.lineOffset): error: \(templateError.reason)\n"
+ "\(location.content)\n"
Expand Down
2 changes: 1 addition & 1 deletion Sources/ForTag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ForNode : NodeType {
throw TemplateSyntaxError("'for' statements should use the syntax: `for <x> in <y> [where <condition>]`.")
}

let loopVariables = components[1].characters
let loopVariables = components[1]
.split(separator: ",")
.map(String.init)
.map { $0.trim(character: " ") }
Expand Down
6 changes: 3 additions & 3 deletions Sources/Inheritence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ExtendsNode : NodeType {
throw TemplateSyntaxError("'extends' cannot appear more than once in the same template")
}

let blockNodes = parsedNodes.flatMap { $0 as? BlockNode }
let blockNodes = parsedNodes.compactMap { $0 as? BlockNode }

let nodes = blockNodes.reduce([String: BlockNode]()) { (accumulator, node) -> [String: BlockNode] in
var dict = accumulator
Expand Down Expand Up @@ -159,8 +159,8 @@ class BlockNode : NodeType {
}

// child node is a block node from child template that extends this node (has the same name)
func childContext(_ child: BlockNode, blockContext: BlockContext, context: Context) throws -> [String: Any?] {
var childContext: [String: Any?] = [BlockContext.contextKey: blockContext]
func childContext(_ child: BlockNode, blockContext: BlockContext, context: Context) throws -> [String: Any] {
var childContext: [String: Any] = [BlockContext.contextKey: blockContext]

if let blockSuperNode = child.nodes.first(where: {
if case .variable(let variable, _)? = $0.token, variable == "block.super" { return true }
Expand Down
2 changes: 1 addition & 1 deletion Sources/KeyPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class KeyPath {
subscriptLevel = 0
}

for c in variable.characters {
for c in variable {
switch c {
case "." where subscriptLevel == 0:
try foundSeparator()
Expand Down
16 changes: 8 additions & 8 deletions Sources/Lexer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ struct Lexer {
self.templateName = templateName
self.templateString = templateString

self.lines = templateString.components(separatedBy: .newlines).enumerated().flatMap {
self.lines = templateString.components(separatedBy: .newlines).enumerated().compactMap {
guard !$0.element.isEmpty else { return nil }
return (content: $0.element, number: UInt($0.offset + 1), templateString.range(of: $0.element)!)
}
}

func createToken(string: String, at range: Range<String.Index>) -> Token {
func strip() -> String {
guard string.characters.count > 4 else { return "" }
guard string.count > 4 else { return "" }
let start = string.index(string.startIndex, offsetBy: 2)
let end = string.index(string.endIndex, offsetBy: -2)
let trimmed = String(string[start..<end])
Expand Down Expand Up @@ -114,14 +114,14 @@ class Scanner {

range = range.upperBound..<range.upperBound
while index != content.endIndex {
let substring = content.substring(from: index)
let substring = String(content[index...])

if substring.hasPrefix(until) {
let result = content.substring(to: index)
let result = String(content[..<index])

if returnUntil {
range = range.lowerBound..<originalContent.index(range.upperBound, offsetBy: until.characters.count)
content = substring.substring(from: until.endIndex)
range = range.lowerBound..<originalContent.index(range.upperBound, offsetBy: until.count)
content = String(substring[until.endIndex...])
return result + until
}

Expand All @@ -145,10 +145,10 @@ class Scanner {
var index = content.startIndex
range = range.upperBound..<range.upperBound
while index != content.endIndex {
let substring = content.substring(from: index)
let substring = String(content[index...])
for string in until {
if substring.hasPrefix(string) {
let result = content.substring(to: index)
let result = String(content[..<index])
content = substring
return (string, result)
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public class TokenParser {
let filtersWithDistance = allFilters
.map({ (filterName: $0, distance: $0.levenshteinDistance(name)) })
// do not suggest filters which names are shorter than the distance
.filter({ $0.filterName.characters.count > $0.distance })
.filter({ $0.filterName.count > $0.distance })
guard let minDistance = filtersWithDistance.min(by: { $0.distance < $1.distance })?.distance else {
return []
}
Expand Down Expand Up @@ -167,18 +167,18 @@ extension String {
// initialize v0 (the previous row of distances)
// this row is A[0][i]: edit distance for an empty s
// the distance is just the number of characters to delete from t
last = [Int](0...target.characters.count)
current = [Int](repeating: 0, count: target.characters.count + 1)
last = [Int](0...target.count)
current = [Int](repeating: 0, count: target.count + 1)

for i in 0..<self.characters.count {
for i in 0..<self.count {
// calculate v1 (current row distances) from the previous row v0

// first element of v1 is A[i+1][0]
// edit distance is delete (i+1) chars from s to match empty t
current[0] = i + 1

// use formula to fill in the rest of the row
for j in 0..<target.characters.count {
for j in 0..<target.count {
current[j+1] = Swift.min(
last[j+1] + 1,
current[j] + 1,
Expand All @@ -190,7 +190,7 @@ extension String {
last = current
}

return current[target.characters.count]
return current[target.count]
}

}
4 changes: 2 additions & 2 deletions Sources/Tokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extension String {
let specialCharacters = ",|:"
func appendWord(_ word: String) {
if components.count > 0 {
if let precedingChar = components.last?.characters.last, specialCharacters.characters.contains(precedingChar) {
if let precedingChar = components.last?.last, specialCharacters.contains(precedingChar) {
components[components.count-1] += word
} else if specialCharacters.contains(word) {
components[components.count-1] += word
Expand All @@ -25,7 +25,7 @@ extension String {
}
}

for character in self.characters {
for character in self {
if character == "'" { singleQuoteCount += 1 }
else if character == "\"" { doubleQuoteCount += 1 }

Expand Down
4 changes: 2 additions & 2 deletions Sources/Variable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class FilterExpression : Resolvable {
let variable: Variable

init(token: String, parser: TokenParser) throws {
let bits = token.characters.split(separator: "|").map({ String($0).trim(character: " ") })
let bits = token.split(separator: "|").map({ String($0).trim(character: " ") })
if bits.isEmpty {
throw TemplateSyntaxError("Variable tags must include at least 1 argument")
}
Expand Down Expand Up @@ -60,7 +60,7 @@ public struct Variable : Equatable, Resolvable {

if (variable.hasPrefix("'") && variable.hasSuffix("'")) || (variable.hasPrefix("\"") && variable.hasSuffix("\"")) {
// String literal
return String(variable[variable.characters.index(after: variable.startIndex) ..< variable.characters.index(before: variable.endIndex)])
return String(variable[variable.index(after: variable.startIndex) ..< variable.index(before: variable.endIndex)])
}

// Number literal
Expand Down
2 changes: 2 additions & 0 deletions Stencil.podspec.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"osx": "10.9",
"tvos": "9.0"
},
"cocoapods_version": "1.4.0",
"swift_version": "4.1",
"requires_arc": true,
"dependencies": {
"PathKit": [
Expand Down
6 changes: 3 additions & 3 deletions Tests/StencilTests/ForNodeSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func testForNode() {
let template = Template(templateString: templateString)
let result = try template.render(context)

let sortedResult = result.characters.split(separator: ",").map(String.init).sorted(by: <)
let sortedResult = result.split(separator: ",").map(String.init).sorted(by: <)
try expect(sortedResult) == ["one: I", "two: II"]
}

Expand All @@ -202,7 +202,7 @@ func testForNode() {
let node = ForNode(resolvable: Variable("dict"), loopVariables: ["key"], nodes: nodes, emptyNodes: emptyNodes, where: nil)
let result = try node.render(context)

let sortedResult = result.characters.split(separator: ",").map(String.init).sorted(by: <)
let sortedResult = result.split(separator: ",").map(String.init).sorted(by: <)
try expect(sortedResult) == ["one", "two"]
}

Expand All @@ -218,7 +218,7 @@ func testForNode() {

let result = try node.render(context)

let sortedResult = result.characters.split(separator: ",").map(String.init).sorted(by: <)
let sortedResult = result.split(separator: ",").map(String.init).sorted(by: <)
try expect(sortedResult) == ["one=I", "two=II"]
}

Expand Down

0 comments on commit d71fe2a

Please sign in to comment.