Skip to content

Commit

Permalink
Swiftlint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tevelee committed Apr 3, 2018
1 parent 034d75a commit 6260997
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 57 deletions.
4 changes: 2 additions & 2 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ opt_in_rules:
- empty_enum_arguments
- empty_parameters
- empty_parentheses_with_trailing_closure
- explicit_acl
# - explicit_acl
- explicit_enum_raw_value
- explicit_init
- explicit_top_level_acl
- explicit_type_interface
# - explicit_type_interface
- extension_access_modifier
- fallthrough
- fatal_error_message
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import AppKit
@_exported import Eval
import Foundation
@_exported import class Eval.Pattern
import Foundation

public class ColorParser: EvaluatorWithLocalContext {
let interpreter: TypedInterpreter
Expand Down Expand Up @@ -48,23 +48,23 @@ extension String {

extension NSColor {
func blend(with other: NSColor, using factor: CGFloat = 0.5) -> NSColor {
let rightFactor = 1.0 - factor
let inverseFactor = 1.0 - factor

var lr: CGFloat = 0
var lg: CGFloat = 0
var lb: CGFloat = 0
var la: CGFloat = 0
getRed(&lr, green: &lg, blue: &lb, alpha: &la)
var leftRed: CGFloat = 0
var leftGreen: CGFloat = 0
var leftBlue: CGFloat = 0
var leftAlpha: CGFloat = 0
getRed(&leftRed, green: &leftGreen, blue: &leftBlue, alpha: &leftAlpha)

var rr: CGFloat = 0
var rg: CGFloat = 0
var rb: CGFloat = 0
var ra: CGFloat = 0
other.getRed(&rr, green: &rg, blue: &rb, alpha: &ra)
var rightRed: CGFloat = 0
var rightGreen: CGFloat = 0
var rightBlue: CGFloat = 0
var rightAlpha: CGFloat = 0
other.getRed(&rightRed, green: &rightGreen, blue: &rightBlue, alpha: &rightAlpha)

return NSColor(calibratedRed: lr * factor + rr * rightFactor,
green: lg * factor + rg * rightFactor,
blue: lb * factor + rb * rightFactor,
alpha: la * factor + ra * rightFactor)
return NSColor(calibratedRed: leftRed * factor + rightRed * inverseFactor,
green: leftGreen * factor + rightGreen * inverseFactor,
blue: leftBlue * factor + rightBlue * inverseFactor,
alpha: leftAlpha * factor + rightAlpha * inverseFactor)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@_exported import Eval
import Foundation
@_exported import class Eval.Pattern
import Foundation

public class TemplateLanguage: EvaluatorWithLocalContext {
public typealias EvaluatedType = String
Expand Down Expand Up @@ -67,7 +67,7 @@ public class TemplateLanguage: EvaluatorWithLocalContext {
}
return convert(value)
}

func replaceWhitespaces(_ input: String) -> String {
let tag = "{-}"
var input = input
Expand Down Expand Up @@ -106,8 +106,8 @@ public class TemplateLanguage: EvaluatorWithLocalContext {
}
}

typealias Macro = (arguments: [String], body: String)
typealias BlockRenderer = (_ context: Context) -> String
internal typealias Macro = (arguments: [String], body: String)
internal typealias BlockRenderer = (_ context: Context) -> String

extension Context {
static let macrosKey: String = "__macros"
Expand Down Expand Up @@ -181,7 +181,7 @@ public class TemplateLibrary {
}
}
}

public static var printStatement: Pattern<String, TemplateInterpreter<String>> {
return Pattern([OpenKeyword("{{"), Variable<Any>("body"), CloseKeyword("}}")]) { variables, interpreter, _ in
guard let body = variables["body"] else { return nil }
Expand Down Expand Up @@ -411,12 +411,12 @@ public class StandardLibrary {

public static var numericType: DataType<Double> {
let numberLiteral = Literal { value, _ in Double(value) }
let pi = Literal("pi", convertsTo: Double.pi)
return DataType(type: Double.self, literals: [numberLiteral, pi]) { value, _ in String(format: "%g", value) }
let piLiteral = Literal("pi", convertsTo: Double.pi)
return DataType(type: Double.self, literals: [numberLiteral, piLiteral]) { value, _ in String(format: "%g", value) }
}

public static var stringType: DataType<String> {
let singleQuotesLiteral = literal(opening: "'", closing: "'") { (input, _) in input }
let singleQuotesLiteral = literal(opening: "'", closing: "'") { input, _ in input }
return DataType(type: String.self, literals: [singleQuotesLiteral]) { value, _ in value }
}

Expand Down Expand Up @@ -994,7 +994,7 @@ public class StandardLibrary {
}

public static var methodCallWithIntResult: Function<Double> {
return Function([Variable<Any>("lhs"), Keyword("."), Variable<String>("rhs", options: .notInterpreted)]) { (arguments, _, _) -> Double? in
return Function([Variable<Any>("lhs"), Keyword("."), Variable<String>("rhs", options: .notInterpreted)]) { arguments, _, _ -> Double? in
if let lhs = arguments["lhs"] as? NSObjectProtocol,
let rhs = arguments["rhs"] as? String,
let result = lhs.perform(Selector(rhs)) {
Expand Down Expand Up @@ -1170,11 +1170,11 @@ extension String {

var html: String {
var html = ""
for c in self {
if let entity = String.enc[c] {
for character in self {
if let entity = String.enc[character] {
html.append(entity)
} else {
html.append(c)
html.append(character)
}
}
return html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ class TemplateExampleTests: XCTestCase {
func testIfStatement() {
XCTAssertEqual(eval("{% if true %}Hello{% endif %} {{ name }}!", ["name": "Teve"]), "Hello Teve!")
}

func testEmbeddedIfStatement() {
XCTAssertEqual(eval("Result: {% if x > 1 %}{% if x < 5 %}1<x<5{% endif %}{% endif %}", ["x": 2]), "Result: 1<x<5")

XCTAssertEqual(eval("Result: {% if x > 1 %}{% if x < 5 %}1<x<5{% endif %}{% else %}x<=1{% endif %}", ["x": 2]), "Result: 1<x<5")
XCTAssertEqual(eval("Result: {% if x >= 5 %}x>=5{% else %}{% if x > 1 %}1<x<5{% endif %}{% endif %}", ["x": 2]), "Result: 1<x<5")

XCTAssertEqual(eval("Result: {% if x > 1 %}{% if x < 5 %}1<x<5{% else %}x>=5{% endif %}{% else %}x<=1{% endif %}", ["x": 2]), "Result: 1<x<5")
XCTAssertEqual(eval("Result: {% if x >= 5 %}x>=5{% else %}{% if x > 1 %}1<x<5{% else %}x<=1{% endif %}{% endif %}", ["x": 2]), "Result: 1<x<5")
}
Expand Down Expand Up @@ -66,12 +66,12 @@ class TemplateExampleTests: XCTestCase {
XCTAssertEqual(eval("{% block title5 %}Hello {{name}}{% endblock %}{% block title5 %}{{ parent() }}!{% endblock %}", ["name": "George"]), "Hello George!")
XCTAssertEqual(eval("{% block title6 %}Hello {{name}}{% endblock %}{% block title6 %}{{ parent(name='Laszlo') }}!{% endblock %}", ["name": "Geroge"]), "Hello Laszlo!")
}

func testSpaceElimination() {
XCTAssertEqual(eval("asd {-} jkl"), "asdjkl")
XCTAssertEqual(eval("{-} jkl"), "jkl")
XCTAssertEqual(eval("asd {-}"), "asd")

XCTAssertEqual(eval("asd {-}{% if true %} Hello {% endif %} "), "asd Hello ")
XCTAssertEqual(eval("asd {-}{% if true %}{-} Hello {% endif %} "), "asdHello ")
XCTAssertEqual(eval("asd {% if true %} Hello {-} {% endif %} "), "asd Hello ")
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Sources/Automation/Eval.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class Eval {
if matches > 0, let currentTag = try Shell.execute("git show HEAD~1:.version")?.output {
let currentTag = currentTag.trimmingCharacters(in: .whitespacesAndNewlines)
let tag = message.replacingOccurrences(of: "Version ", with: "")

guard let tags = try Shell.execute("git tag -l")?.output?.components(separatedBy: .whitespacesAndNewlines),
!tags.contains(tag) else { return }

Expand Down
6 changes: 3 additions & 3 deletions Scripts/Sources/Automation/Shell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ class Shell {
}

let observer = NotificationCenter.default.addObserver(forName: Notification.Name.NSFileHandleDataAvailable, object: fileHandle, queue: nil) { notification in
if let fh = notification.object as? FileHandle {
process(data: fh.availableData)
fh.waitForDataInBackgroundAndNotify()
if let noitificationFileHandle = notification.object as? FileHandle {
process(data: noitificationFileHandle.availableData)
noitificationFileHandle.waitForDataInBackgroundAndNotify()
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Eval/Common.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public class Context {
/// - returns: A new `InterpreterContext` instance with the current and the parameter variables merged inside
public func merging(with other: Context?) -> Context {
if let other = other {
return Context(variables: other.variables.merging(self.variables) { (eixstingValue, _) in eixstingValue })
return Context(variables: other.variables.merging(self.variables) { eixstingValue, _ in eixstingValue })
} else {
return self
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Eval/Elements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public class GenericVariable<T, E: Interpreter> : VariableProtocol, PatternEleme
/// - parameter map: If provided, then the result of the evaluated variable will be running through this map function. By default the map tries to convert the matched value to the expected type, using the `as?` operator. Defaults to identical map, using the `as?` operator for value transformation
public init(_ name: String,
options: VariableOptions = [],
map: @escaping VariableMapper<T, E> = { (value, _) in value as? T }) {
map: @escaping VariableMapper<T, E> = { value, _ in value as? T }) {
self.name = name
self.options = options
self.map = map
Expand Down
3 changes: 1 addition & 2 deletions Sources/Eval/TemplateInterpreter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,11 @@ public class TemplateVariable: GenericVariable<String, StringTemplateInterpreter
/// - parameter options: Options that modify the behaviour of the variable matching, and the output that the framework provides
/// - parameter map: If provided, then the result of the evaluated variable will be running through this map function
/// Whether the processed variable sould be trimmed (removing whitespaces from both sides). Defaults to `true`
public override init(_ name: String, options: VariableOptions = [], map: @escaping VariableMapper<String, StringTemplateInterpreter> = { (value, _) in value as? String }) {
public override init(_ name: String, options: VariableOptions = [], map: @escaping VariableMapper<String, StringTemplateInterpreter> = { value, _ in value as? String }) {
super.init(name, options: options.union(.notInterpreted)) { value, interpreter in
guard let stringValue = value as? String else { return "" }
let result = options.interpreted ? interpreter.evaluate(stringValue) : stringValue
return map(result, interpreter)
}
}
}

6 changes: 3 additions & 3 deletions Sources/Eval/Utilities/Matcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import Foundation
/// A tuple with the variable metadata and its value
/// - parameter metadata: Name, options, mapping information
/// - parameter value: The value of the variable
typealias VariableValue = (metadata: VariableProtocol, value: String)
internal typealias VariableValue = (metadata: VariableProtocol, value: String)

/// A processor that can process a raw value with extra information, such as interpreter and context
internal protocol VariableProcessorProtocol {
Expand Down Expand Up @@ -181,7 +181,7 @@ internal class Matcher {
}
} while true
}

/// Removes whitespaces characters from the upcoming consecutive input characters, when the context allows to do so
/// - parameter remainder: The input to remove whitespaces from
/// - parameter index: The index of the current element
Expand Down Expand Up @@ -237,7 +237,7 @@ internal class Matcher {
nextElement(&elementIndex)
}
} else {
variables.merge(embeddedVariables) { (key, _) in key }
variables.merge(embeddedVariables) { key, _ in key }
if currentlyActiveVariable != nil {
guard registerAndValidateVariable(variables: &variables) else { return .noMatch }
currentlyActiveVariable = nil
Expand Down
14 changes: 7 additions & 7 deletions Tests/EvalTests/IntegrationTests/InterpreterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ class InterpreterTests: XCTestCase {
XCTAssertNil(interpreter.evaluate("add(1,'a')"))
XCTAssertNil(interpreter.evaluate("hello"))

let c = Context()
_ = interpreter.evaluate("Date(1009 * 2, sqrt(144), 10 + 3).format('yyyy-MM-dd')", context: c)
let context = Context()
_ = interpreter.evaluate("Date(1009 * 2, sqrt(144), 10 + 3).format('yyyy-MM-dd')", context: context)

c.debugInfo.forEach {
context.debugInfo.forEach {
print("DEBUG STEP: '\($0.value.pattern)', where \($0.value.variables), rendered to \($0.value.output) from input \($0.key)")
}

let ifStatement = Pattern<String, TemplateInterpreter<String>>([Keyword("{%"), Keyword("if"), Variable<Bool>("condition"), Keyword("%}"), TemplateVariable("body"), Keyword("{% endif %}")]) { (variables, _, _) -> String? in
let ifStatement = Pattern<String, TemplateInterpreter<String>>([Keyword("{%"), Keyword("if"), Variable<Bool>("condition"), Keyword("%}"), TemplateVariable("body"), Keyword("{% endif %}")]) { variables, _, _ -> String? in
guard let condition = variables["condition"] as? Bool, let body = variables["body"] as? String else { return nil }
if condition {
return body
Expand Down Expand Up @@ -207,7 +207,7 @@ class InterpreterTests: XCTestCase {
}

func stringDataType() -> DataType<String> {
let singleQuotesLiteral = Literal { (input, _) -> String? in
let singleQuotesLiteral = Literal { input, _ -> String? in
guard let first = input.first, let last = input.last, first == last, first == "'" else { return nil }
let trimmed = input.trimmingCharacters(in: CharacterSet(charactersIn: "'"))
return trimmed.contains("'") ? nil : trimmed
Expand Down Expand Up @@ -243,7 +243,7 @@ class InterpreterTests: XCTestCase {

func methodCallFunction() -> Function<Double> {
return Function(patterns: [
Pattern(Variable<Any>("lhs") + Keyword(".") + Variable<String>("rhs", options: .notInterpreted)) { (arguments, _, _) -> Double? in
Pattern(Variable<Any>("lhs") + Keyword(".") + Variable<String>("rhs", options: .notInterpreted)) { arguments, _, _ -> Double? in
if let lhs = arguments["lhs"] as? NSObjectProtocol,
let rhs = arguments["rhs"] as? String,
let result = lhs.perform(Selector(rhs)) {
Expand Down Expand Up @@ -280,7 +280,7 @@ class InterpreterTests: XCTestCase {
}

func incrementFunction() -> Function<Double> {
return Function([Variable<Any>("value", options: .notInterpreted), Keyword("++")]) { (arguments, interpreter, _) -> Double? in
return Function([Variable<Any>("value", options: .notInterpreted), Keyword("++")]) { arguments, interpreter, _ -> Double? in
if let argument = arguments["value"] as? String {
if let variable = interpreter.context.variables.first(where: { argument == $0.key }), let value = variable.value as? Double {
let incremented = value + 1
Expand Down
8 changes: 4 additions & 4 deletions Tests/EvalTests/IntegrationTests/TemplateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TemplateTests: XCTestCase {
functions: [concat, parenthesis, plusOperator, lessThan],
context: Context(variables: ["name": "Laszlo Teveli"]))

let ifStatement = Pattern<String, TemplateInterpreter<String>>([Keyword("{%"), Keyword("if"), Variable<Bool>("condition"), Keyword("%}"), TemplateVariable("body"), Keyword("{% endif %}")]) { (variables, _, _) -> String? in
let ifStatement = Pattern<String, TemplateInterpreter<String>>([Keyword("{%"), Keyword("if"), Variable<Bool>("condition"), Keyword("%}"), TemplateVariable("body"), Keyword("{% endif %}")]) { variables, _, _ -> String? in
guard let condition = variables["condition"] as? Bool, let body = variables["body"] as? String else { return nil }
if condition {
return body
Expand All @@ -38,10 +38,10 @@ class TemplateTests: XCTestCase {
let lessThan = infixOperator("<") { (lhs: Double, rhs: Double) in lhs < rhs }
let interpreter = TypedInterpreter(dataTypes: [numberDataType(), stringDataType(), booleanDataType()], functions: [parenthesis, lessThan], context: Context())

let braces = Pattern<String, TemplateInterpreter<String>>([OpenKeyword("("), TemplateVariable("body"), CloseKeyword(")")]) { (variables, _, _) -> String? in
let braces = Pattern<String, TemplateInterpreter<String>>([OpenKeyword("("), TemplateVariable("body"), CloseKeyword(")")]) { variables, _, _ -> String? in
return variables["body"] as? String
}
let ifStatement = Pattern<String, TemplateInterpreter<String>>([OpenKeyword("{% if"), Variable<Bool>("condition"), Keyword("%}"), TemplateVariable("body"), CloseKeyword("{% endif %}")]) { (variables, _, _) -> String? in
let ifStatement = Pattern<String, TemplateInterpreter<String>>([OpenKeyword("{% if"), Variable<Bool>("condition"), Keyword("%}"), TemplateVariable("body"), CloseKeyword("{% endif %}")]) { variables, _, _ -> String? in
guard let condition = variables["condition"] as? Bool, let body = variables["body"] as? String else { return nil }
if condition {
return body
Expand All @@ -66,7 +66,7 @@ class TemplateTests: XCTestCase {
}

func stringDataType() -> DataType<String> {
let singleQuotesLiteral = Literal { (input, _) -> String? in
let singleQuotesLiteral = Literal { input, _ -> String? in
guard let first = input.first, let last = input.last, first == last, first == "'" else { return nil }
let trimmed = input.trimmingCharacters(in: CharacterSet(charactersIn: "'"))
return trimmed.contains("'") ? nil : trimmed
Expand Down
1 change: 1 addition & 0 deletions Tests/EvalTests/UnitTests/PatternTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class PatternTests: XCTestCase {

// swiftlint:disable:next function_body_length
func test_whenMatching_thenExpectingAppropriateResults() {
// swiftlint:disable:next nesting
typealias TestCase = (elements: [PatternElement], input: String, expectedResult: MatchResult<Int>)

let keyword = Keyword("ok")
Expand Down

0 comments on commit 6260997

Please sign in to comment.