Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Swift 4 features #239

Merged
merged 4 commits into from Sep 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion .travis.yml
@@ -1,10 +1,15 @@
matrix:
include:
- os: osx
osx_image: xcode9.3
osx_image: xcode9.4
env: SWIFT_VERSION=4.1
- os: osx
osx_image: xcode10
env: SWIFT_VERSION=4.2
- os: linux
env: SWIFT_VERSION=4.1
- os: linux
env: SWIFT_VERSION=4.2
language: generic
sudo: required
dist: trusty
Expand Down
6 changes: 4 additions & 2 deletions CHANGELOG.md
Expand Up @@ -16,11 +16,13 @@

### New Features

_None_
_None_

### Internal Changes

_None_
- Updated the codebase to use Swift 4 features.
[David Jennes](https://github.com/djbe)
[#239](https://github.com/stencilproject/Stencil/pull/239)


## 0.12.1
Expand Down
20 changes: 10 additions & 10 deletions Sources/Errors.swift
Expand Up @@ -37,11 +37,6 @@ public struct TemplateSyntaxError : Error, Equatable, CustomStringConvertible {
public init(_ description: String) {
self.init(reason: description)
}

public static func ==(lhs:TemplateSyntaxError, rhs:TemplateSyntaxError) -> Bool {
return lhs.description == rhs.description && lhs.token == rhs.token && lhs.stackTrace == rhs.stackTrace
}

}

extension Error {
Expand All @@ -67,11 +62,16 @@ 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.count - 1, 0))))"

return "\(templateName)\(location.lineNumber):\(location.lineOffset): error: \(templateError.reason)\n"
+ "\(location.content)\n"
+ "\(highlight)\n"
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)
\(location.content)
\(highlight)
"""
}

var descriptions = templateError.stackTrace.reduce([]) { $0 + [describe(token: $1)] }
Expand Down
8 changes: 6 additions & 2 deletions Sources/Filters.swift
Expand Up @@ -74,15 +74,19 @@ func indentFilter(value: Any?, arguments: [Any?]) throws -> Any? {
var indentWidth = 4
if arguments.count > 0 {
guard let value = arguments[0] as? Int else {
throw TemplateSyntaxError("'indent' filter width argument must be an Integer (\(String(describing: arguments[0])))")
throw TemplateSyntaxError("""
'indent' filter width argument must be an Integer (\(String(describing: arguments[0])))
""")
}
indentWidth = value
}

var indentationChar = " "
if arguments.count > 1 {
guard let value = arguments[1] as? String else {
throw TemplateSyntaxError("'indent' filter indentation argument must be a String (\(String(describing: arguments[1]))")
throw TemplateSyntaxError("""
'indent' filter indentation argument must be a String (\(String(describing: arguments[1]))
""")
}
indentationChar = value
}
Expand Down
6 changes: 5 additions & 1 deletion Sources/Include.swift
Expand Up @@ -10,7 +10,11 @@ class IncludeNode : NodeType {
let bits = token.components()

guard bits.count == 2 || bits.count == 3 else {
throw TemplateSyntaxError("'include' tag requires one argument, the template file to be included. A second optional argument can be used to specify the context that will be passed to the included file")
throw TemplateSyntaxError("""
'include' tag requires one argument, the template file to be included. \
A second optional argument can be used to specify the context that will \
be passed to the included file
""")
}

return IncludeNode(templateName: Variable(bits[1]), includeContext: bits.count == 3 ? bits[2] : nil, token: token)
Expand Down
5 changes: 4 additions & 1 deletion Sources/Parser.swift
Expand Up @@ -98,7 +98,10 @@ public class TokenParser {
if suggestedFilters.isEmpty {
throw TemplateSyntaxError("Unknown filter '\(name)'.")
} else {
throw TemplateSyntaxError("Unknown filter '\(name)'. Found similar filters: \(suggestedFilters.map({ "'\($0)'" }).joined(separator: ", ")).")
throw TemplateSyntaxError("""
Unknown filter '\(name)'. \
Found similar filters: \(suggestedFilters.map({ "'\($0)'" }).joined(separator: ", ")).
""")
}
}

Expand Down
17 changes: 0 additions & 17 deletions Sources/Tokenizer.swift
Expand Up @@ -114,21 +114,4 @@ public enum Token : Equatable {
return sourceMap
}
}

}


public func == (lhs: Token, rhs: Token) -> Bool {
switch (lhs, rhs) {
case let (.text(lhsValue, lhsAt), .text(rhsValue, rhsAt)):
return lhsValue == rhsValue && lhsAt == rhsAt
case let (.variable(lhsValue, lhsAt), .variable(rhsValue, rhsAt)):
return lhsValue == rhsValue && lhsAt == rhsAt
case let (.block(lhsValue, lhsAt), .block(rhsValue, rhsAt)):
return lhsValue == rhsValue && lhsAt == rhsAt
case let (.comment(lhsValue, lhsAt), .comment(rhsValue, rhsAt)):
return lhsValue == rhsValue && lhsAt == rhsAt
default:
return false
}
}
4 changes: 0 additions & 4 deletions Sources/Variable.swift
Expand Up @@ -128,10 +128,6 @@ public struct Variable : Equatable, Resolvable {
}
}

public func ==(lhs: Variable, rhs: Variable) -> Bool {
return lhs.variable == rhs.variable
}

/// A structure used to represet range of two integer values expressed as `from...to`.
/// Values should be numbers (they will be converted to integers).
/// Rendering this variable produces array from range `from...to`.
Expand Down