Skip to content

Commit

Permalink
run swift-format
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Nov 27, 2024
1 parent af3ddf8 commit e3f50d5
Show file tree
Hide file tree
Showing 16 changed files with 610 additions and 405 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let package = Package(
name: "swift-mustache",
platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6)],
products: [
.library(name: "Mustache", targets: ["Mustache"]),
.library(name: "Mustache", targets: ["Mustache"])
],
dependencies: [],
targets: [
Expand Down
6 changes: 3 additions & 3 deletions Sources/Mustache/ContentType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public protocol MustacheContentType: Sendable {
/// Text content type where no character is escaped
struct TextContentType: MustacheContentType {
func escapeText(_ text: String) -> String {
return text
text
}
}

/// HTML content where text is escaped for HTML output
struct HTMLContentType: MustacheContentType {
func escapeText(_ text: String) -> String {
return text.htmlEscape()
text.htmlEscape()
}
}

Expand All @@ -39,7 +39,7 @@ struct HTMLContentType: MustacheContentType {
/// with `MustacheContentTypes.register`.
public enum MustacheContentTypes {
static func get(_ name: String) -> MustacheContentType? {
return self.types[name]
self.types[name]
}

/// Register new content type
Expand Down
2 changes: 1 addition & 1 deletion Sources/Mustache/Context.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ struct MustacheContext {

/// return context with sequence info and sequence element added to stack
func withContentType(_ contentType: MustacheContentType) -> MustacheContext {
return .init(
.init(
stack: self.stack,
sequenceContext: self.sequenceContext,
indentation: self.indentation,
Expand Down
2 changes: 1 addition & 1 deletion Sources/Mustache/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ public struct MustacheLambda {
}

internal func callAsFunction(_ s: String) -> Any? {
return self.callback(s)
self.callback(s)
}
}
2 changes: 1 addition & 1 deletion Sources/Mustache/Parent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ public protocol MustacheParent {
/// Extend dictionary where the key is a string so that it uses the key values to access
/// it values
extension Dictionary: MustacheParent where Key == String {
public func child(named: String) -> Any? { return self[named] }
public func child(named: String) -> Any? { self[named] }
}
26 changes: 16 additions & 10 deletions Sources/Mustache/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct Parser {
self.position = string.startIndex
}

var buffer: String { return self._storage.buffer }
var buffer: String { self._storage.buffer }
private(set) var position: String.Index
}

Expand All @@ -59,7 +59,10 @@ extension Parser {
/// - Returns: If current character was the one we expected
mutating func read(_ char: Character) throws -> Bool {
let c = try character()
guard c == char else { unsafeRetreat(); return false }
guard c == char else {
unsafeRetreat()
return false
}
return true
}

Expand All @@ -84,7 +87,10 @@ extension Parser {
/// - Returns: If current character is in character set
mutating func read(_ characterSet: Set<Character>) throws -> Bool {
let c = try character()
guard characterSet.contains(c) else { unsafeRetreat(); return false }
guard characterSet.contains(c) else {
unsafeRetreat()
return false
}
return true
}

Expand Down Expand Up @@ -236,7 +242,7 @@ extension Parser {
@discardableResult mutating func read(while: Character) -> Int {
var count = 0
while !self.reachedEnd(),
unsafeCurrent() == `while`
unsafeCurrent() == `while`
{
unsafeAdvance()
count += 1
Expand All @@ -250,7 +256,7 @@ extension Parser {
@discardableResult mutating func read(while keyPath: KeyPath<Character, Bool>) -> Substring {
let startIndex = self.position
while !self.reachedEnd(),
unsafeCurrent()[keyPath: keyPath]
unsafeCurrent()[keyPath: keyPath]
{
unsafeAdvance()
}
Expand All @@ -263,7 +269,7 @@ extension Parser {
@discardableResult mutating func read(while cb: (Character) -> Bool) -> Substring {
let startIndex = self.position
while !self.reachedEnd(),
cb(unsafeCurrent())
cb(unsafeCurrent())
{
unsafeAdvance()
}
Expand All @@ -276,7 +282,7 @@ extension Parser {
@discardableResult mutating func read(while characterSet: Set<Character>) -> Substring {
let startIndex = self.position
while !self.reachedEnd(),
characterSet.contains(unsafeCurrent())
characterSet.contains(unsafeCurrent())
{
unsafeAdvance()
}
Expand All @@ -286,13 +292,13 @@ extension Parser {
/// Return whether we have reached the end of the buffer
/// - Returns: Have we reached the end
func reachedEnd() -> Bool {
return self.position == self.buffer.endIndex
self.position == self.buffer.endIndex
}

/// Return whether we are at the start of the buffer
/// - Returns: Are we are the start
func atStart() -> Bool {
return self.position == self.buffer.startIndex
self.position == self.buffer.startIndex
}
}

Expand Down Expand Up @@ -378,7 +384,7 @@ extension Parser {
// unsafe versions without checks
extension Parser {
func unsafeCurrent() -> Character {
return self.buffer[self.position]
self.buffer[self.position]
}

mutating func unsafeAdvance() {
Expand Down
7 changes: 4 additions & 3 deletions Sources/Mustache/Template+Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ extension MustacheTemplate {
}
if self.isStandalone(&parser, state: state) {
setNewLine = true
} else if whiteSpaceBefore.count > 0 {}
} else if whiteSpaceBefore.count > 0 {
}
let sectionTemplate = try parse(&parser, state: state.withSectionName(name, newLine: setNewLine))
tokens.append(.blockExpansion(name: name, default: sectionTemplate, indentation: String(whiteSpaceBefore)))
whiteSpaceBefore = ""
Expand Down Expand Up @@ -415,7 +416,7 @@ extension MustacheTemplate {
nameParser.unsafeAdvance()
// We need to have a `)` for each transform that we've parsed
guard nameParser.read(while: ")") + 1 == existing.count,
nameParser.reachedEnd()
nameParser.reachedEnd()
else {
throw Error.unfinishedName
}
Expand Down Expand Up @@ -512,7 +513,7 @@ extension MustacheTemplate {
}

static func isStandalone(_ parser: inout Parser, state: ParserState) -> Bool {
return state.newLine && self.hasLineFinished(&parser)
state.newLine && self.hasLineFinished(&parser)
}

private static let sectionNameCharsWithoutBrackets = Set<Character>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_?*")
Expand Down
2 changes: 1 addition & 1 deletion Sources/Mustache/Template+Render.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ extension MustacheTemplate {
// run transform on the current child
for transform in transforms.reversed() {
if let runnable = child as? MustacheTransformable,
let transformed = runnable.transform(transform)
let transformed = runnable.transform(transform)
{
child = transformed
continue
Expand Down
8 changes: 4 additions & 4 deletions Sources/Mustache/Transform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public protocol MustacheTransformable {
func transform(_ name: String) -> Any?
}

public extension StringProtocol {
extension StringProtocol {
/// Transform String/Substring
///
/// Transforms available are `capitalized`, `lowercased`, `uppercased` and `reversed`
/// - Parameter name: transform name
/// - Returns: Result
func transform(_ name: String) -> Any? {
public func transform(_ name: String) -> Any? {
switch name {
case "empty":
return isEmpty
Expand Down Expand Up @@ -209,13 +209,13 @@ extension Dictionary: ComparableSequence where Key: Comparable {
}
}

public extension FixedWidthInteger {
extension FixedWidthInteger {
/// Transform FixedWidthInteger
///
/// Transforms available are `plusone`, `minusone`, `odd`, `even`
/// - Parameter name: transform name
/// - Returns: Result
func transform(_ name: String) -> Any? {
public func transform(_ name: String) -> Any? {
switch name {
case "equalzero":
return self == 0
Expand Down
54 changes: 35 additions & 19 deletions Tests/MustacheTests/ErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ import XCTest

final class ErrorTests: XCTestCase {
func testSectionCloseNameIncorrect() {
XCTAssertThrowsError(try MustacheTemplate(string: """
{{#test}}
{{.}}
{{/test2}}
""")) { error in
XCTAssertThrowsError(
try MustacheTemplate(
string: """
{{#test}}
{{.}}
{{/test2}}
"""
)
) { error in
switch error {
case let error as MustacheTemplate.ParserError:
XCTAssertEqual(error.error as? MustacheTemplate.Error, .sectionCloseNameIncorrect)
Expand All @@ -36,11 +40,15 @@ final class ErrorTests: XCTestCase {
}

func testUnfinishedName() {
XCTAssertThrowsError(try MustacheTemplate(string: """
{{#test}}
{{name}
{{/test2}}
""")) { error in
XCTAssertThrowsError(
try MustacheTemplate(
string: """
{{#test}}
{{name}
{{/test2}}
"""
)
) { error in
switch error {
case let error as MustacheTemplate.ParserError:
XCTAssertEqual(error.error as? MustacheTemplate.Error, .unfinishedName)
Expand All @@ -55,10 +63,14 @@ final class ErrorTests: XCTestCase {
}

func testExpectedSectionEnd() {
XCTAssertThrowsError(try MustacheTemplate(string: """
{{#test}}
{{.}}
""")) { error in
XCTAssertThrowsError(
try MustacheTemplate(
string: """
{{#test}}
{{.}}
"""
)
) { error in
switch error {
case let error as MustacheTemplate.ParserError:
XCTAssertEqual(error.error as? MustacheTemplate.Error, .expectedSectionEnd)
Expand All @@ -73,11 +85,15 @@ final class ErrorTests: XCTestCase {
}

func testInvalidSetDelimiter() {
XCTAssertThrowsError(try MustacheTemplate(string: """
{{=<% %>=}}
<%.%>
<%={{}}=%>
""")) { error in
XCTAssertThrowsError(
try MustacheTemplate(
string: """
{{=<% %>=}}
<%.%>
<%={{}}=%>
"""
)
) { error in
switch error {
case let error as MustacheTemplate.ParserError:
XCTAssertEqual(error.error as? MustacheTemplate.Error, .invalidSetDelimiter)
Expand Down
15 changes: 9 additions & 6 deletions Tests/MustacheTests/LibraryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
//
//===----------------------------------------------------------------------===//

@testable import Mustache
import XCTest

@testable import Mustache

final class LibraryTests: XCTestCase {
func testDirectoryLoad() async throws {
let fs = FileManager()
Expand Down Expand Up @@ -54,11 +55,13 @@ final class LibraryTests: XCTestCase {
let mustache = Data("<test>{{#value}}<value>{{.}}</value>{{/value}}</test>".utf8)
try mustache.write(to: URL(fileURLWithPath: "templates/test.mustache"))
defer { XCTAssertNoThrow(try fs.removeItem(atPath: "templates/test.mustache")) }
let mustache2 = Data("""
{{#test}}
{{{name}}
{{/test2}}
""".utf8)
let mustache2 = Data(
"""
{{#test}}
{{{name}}
{{/test2}}
""".utf8
)
try mustache2.write(to: URL(fileURLWithPath: "templates/error.mustache"))
defer { XCTAssertNoThrow(try fs.removeItem(atPath: "templates/error.mustache")) }

Expand Down
Loading

0 comments on commit e3f50d5

Please sign in to comment.