From 19646bcddf787f0b01beb9b8efe261c587d586e5 Mon Sep 17 00:00:00 2001 From: David Jennes Date: Wed, 12 Aug 2020 22:52:00 +0200 Subject: [PATCH] Update SwiftLint to 0.39.2 (#295) * Update SwiftLint to 0.39.2 * Enable a bunch of extra rules * Fix all warnings * Ignore this Xcode generated folder Co-authored-by: Olivier Halligon --- .gitignore | 1 + .swiftlint.yml | 49 +++++++++++++++++++++++- .travis.yml | 2 +- Sources/Environment.swift | 11 +++--- Sources/Errors.swift | 4 +- Sources/Expression.swift | 1 - Sources/Filters.swift | 4 +- Sources/Inheritence.swift | 8 +--- Sources/Lexer.swift | 1 - Sources/Parser.swift | 2 - Sources/Template.swift | 6 +-- Sources/Variable.swift | 1 - Tests/StencilTests/EnvironmentSpec.swift | 26 +++++++------ Tests/StencilTests/FilterSpec.swift | 24 ++++++++---- Tests/StencilTests/LexerSpec.swift | 3 +- 15 files changed, 95 insertions(+), 48 deletions(-) diff --git a/.gitignore b/.gitignore index 088c653..417edf6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .conche/ .build/ +.swiftpm/ Packages/ Package.pins *.xcodeproj diff --git a/.swiftlint.yml b/.swiftlint.yml index e84a57f..627f305 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -1,45 +1,90 @@ +swiftlint_version: 0.39.2 + +disabled_rules: + # Remove this once we remove old swift support + - implicit_return + opt_in_rules: - anyobject_protocol - array_init - attributes + - closure_body_length - closure_end_indentation - closure_spacing + - collection_alignment + - contains_over_filter_count + - contains_over_filter_is_empty - contains_over_first_not_nil + - contains_over_range_nil_comparison - convenience_type - discouraged_optional_boolean - discouraged_optional_collection + - duplicate_enum_cases + - duplicate_imports + - empty_collection_literal - empty_count - empty_string - fallthrough - fatal_error_message - first_where + - flatmap_over_map_reduce - force_unwrapping - - implicit_return - - implicitly_unwrapped_optional + - identical_operands + - inert_defer - joined_default_parameter + - last_where + - legacy_hashing + - legacy_random - literal_expression_end_indentation - lower_acl_than_parent - modifier_order - multiline_arguments - multiline_function_chains + - multiline_literal_brackets - multiline_parameters + - multiline_parameters_brackets + - nslocalizedstring_key + - nsobject_prefer_isequal - number_separator + - object_literal - operator_usage_whitespace - overridden_super_call - override_in_extension + - prefer_self_type_over_type_of_self - private_action - private_outlet - prohibited_super_call + - raw_value_for_camel_cased_codable_enum + - reduce_boolean + - reduce_into - redundant_nil_coalescing + - redundant_objc_attribute - sorted_first_last - sorted_imports + - static_operator + - strong_iboutlet + - toggle_bool - trailing_closure - unavailable_function - unneeded_parentheses_in_closure_argument + - unowned_variable_capture + - unused_capture_list + - unused_control_flow_label + - unused_declaration + - unused_setter_value - vertical_parameter_alignment_on_call + - vertical_whitespace_closing_braces + - vertical_whitespace_opening_braces + - xct_specific_matcher - yoda_condition + # Enable this again once we remove old swift support + # - optional_enum_case_matching + # - legacy_multiple # Rules customization +closure_body_length: + warning: 25 + line_length: warning: 120 error: 200 diff --git a/.travis.yml b/.travis.yml index ade0b07..871283e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,7 +15,7 @@ sudo: required dist: trusty install: - if [ "$TRAVIS_OS_NAME" == "linux" ]; then eval "$(curl -sL https://swiftenv.fuller.li/install.sh)"; fi - - if [ "$TRAVIS_OS_NAME" == "osx" ]; then wget --output-document /tmp/SwiftLint.pkg https://github.com/realm/SwiftLint/releases/download/0.27.0/SwiftLint.pkg && + - if [ "$TRAVIS_OS_NAME" == "osx" ]; then wget --output-document /tmp/SwiftLint.pkg https://github.com/realm/SwiftLint/releases/download/0.39.2/SwiftLint.pkg && sudo installer -pkg /tmp/SwiftLint.pkg -target /; fi script: - swift test diff --git a/Sources/Environment.swift b/Sources/Environment.swift index 0c2c72e..bdc0fbd 100644 --- a/Sources/Environment.swift +++ b/Sources/Environment.swift @@ -1,13 +1,13 @@ public struct Environment { public let templateClass: Template.Type public var extensions: [Extension] - public var loader: Loader? - public init(loader: Loader? = nil, - extensions: [Extension] = [], - templateClass: Template.Type = Template.self) { - + public init( + loader: Loader? = nil, + extensions: [Extension] = [], + templateClass: Template.Type = Template.self + ) { self.templateClass = templateClass self.loader = loader self.extensions = extensions + [DefaultExtension()] @@ -44,5 +44,4 @@ public struct Environment { template.environment = self return try template.render(context) } - } diff --git a/Sources/Errors.swift b/Sources/Errors.swift index 9c1b584..2c54519 100644 --- a/Sources/Errors.swift +++ b/Sources/Errors.swift @@ -55,7 +55,6 @@ public protocol ErrorReporter: AnyObject { } open class SimpleErrorReporter: ErrorReporter { - open func renderError(_ error: Error) -> String { guard let templateError = error as? TemplateSyntaxError else { return error.localizedDescription } @@ -74,10 +73,9 @@ open class SimpleErrorReporter: ErrorReporter { """ } - var descriptions = templateError.stackTrace.reduce([]) { $0 + [describe(token: $1)] } + var descriptions = templateError.stackTrace.reduce(into: []) { $0.append(describe(token: $1)) } let description = templateError.token.map(describe(token:)) ?? templateError.reason descriptions.append(description) return descriptions.joined(separator: "\n") } - } diff --git a/Sources/Expression.swift b/Sources/Expression.swift index 045b34c..1505209 100644 --- a/Sources/Expression.swift +++ b/Sources/Expression.swift @@ -113,7 +113,6 @@ final class InExpression: Expression, InfixOperator, CustomStringConvertible { return false } - } final class OrExpression: Expression, InfixOperator, CustomStringConvertible { diff --git a/Sources/Filters.swift b/Sources/Filters.swift index a456299..b44f93e 100644 --- a/Sources/Filters.swift +++ b/Sources/Filters.swift @@ -108,8 +108,8 @@ func indent(_ content: String, indentation: String, indentFirst: Bool) -> String var lines = content.components(separatedBy: .newlines) let firstLine = (indentFirst ? indentation : "") + lines.removeFirst() - let result = lines.reduce([firstLine]) { result, line in - result + [(line.isEmpty ? "" : "\(indentation)\(line)")] + let result = lines.reduce(into: [firstLine]) { result, line in + result.append(line.isEmpty ? "" : "\(indentation)\(line)") } return result.joined(separator: "\n") } diff --git a/Sources/Inheritence.swift b/Sources/Inheritence.swift index 611d28c..fcacdf0 100644 --- a/Sources/Inheritence.swift +++ b/Sources/Inheritence.swift @@ -63,11 +63,8 @@ class ExtendsNode: NodeType { } let blockNodes = parsedNodes.compactMap { $0 as? BlockNode } - - let nodes = blockNodes.reduce([String: BlockNode]()) { accumulator, node -> [String: BlockNode] in - var dict = accumulator - dict[node.name] = node - return dict + let nodes = blockNodes.reduce(into: [String: BlockNode]()) { accumulator, node in + accumulator[node.name] = node } return ExtendsNode(templateName: Variable(bits[1]), blocks: nodes, token: token) @@ -185,5 +182,4 @@ class BlockNode: NodeType { } return childContext } - } diff --git a/Sources/Lexer.swift b/Sources/Lexer.swift index f6fe0a2..d24533a 100644 --- a/Sources/Lexer.swift +++ b/Sources/Lexer.swift @@ -108,7 +108,6 @@ struct Lexer { let offset = templateString.distance(from: line.range.lowerBound, to: range.lowerBound) return (line.content, line.number, offset) } - } class Scanner { diff --git a/Sources/Parser.swift b/Sources/Parser.swift index 404b8e2..0181552 100644 --- a/Sources/Parser.swift +++ b/Sources/Parser.swift @@ -87,7 +87,6 @@ public class TokenParser { public func compileResolvable(_ token: String, containedIn containingToken: Token) throws -> Resolvable { return try environment.compileResolvable(token, containedIn: containingToken) } - } extension Environment { @@ -180,7 +179,6 @@ extension Environment { public func compileExpression(components: [String], containedIn token: Token) throws -> Expression { return try IfExpressionParser.parser(components: components, environment: self, token: token).parse() } - } // https://en.wikipedia.org/wiki/Levenshtein_distance#Iterative_with_two_matrix_rows diff --git a/Sources/Template.swift b/Sources/Template.swift index d4d1868..2063e8c 100644 --- a/Sources/Template.swift +++ b/Sources/Template.swift @@ -50,17 +50,17 @@ open class Template: ExpressibleByStringLiteral { // MARK: ExpressibleByStringLiteral // Create a templaVte with a template string literal - public convenience required init(stringLiteral value: String) { + public required convenience init(stringLiteral value: String) { self.init(templateString: value) } // Create a template with a template string literal - public convenience required init(extendedGraphemeClusterLiteral value: StringLiteralType) { + public required convenience init(extendedGraphemeClusterLiteral value: StringLiteralType) { self.init(stringLiteral: value) } // Create a template with a template string literal - public convenience required init(unicodeScalarLiteral value: StringLiteralType) { + public required convenience init(unicodeScalarLiteral value: StringLiteralType) { self.init(stringLiteral: value) } diff --git a/Sources/Variable.swift b/Sources/Variable.swift index 44569df..2da56d9 100644 --- a/Sources/Variable.swift +++ b/Sources/Variable.swift @@ -189,7 +189,6 @@ public struct RangeVariable: Resolvable { let range = min(lower, upper)...max(lower, upper) return lower > upper ? Array(range.reversed()) : Array(range) } - } func normalize(_ current: Any?) -> Any? { diff --git a/Tests/StencilTests/EnvironmentSpec.swift b/Tests/StencilTests/EnvironmentSpec.swift index f5b829f..f077ef2 100644 --- a/Tests/StencilTests/EnvironmentSpec.swift +++ b/Tests/StencilTests/EnvironmentSpec.swift @@ -133,7 +133,7 @@ final class EnvironmentTests: XCTestCase { token: "name|unknown" ) } - + it("reports error in variable tag") { self.template = "{{ }}" try self.expectError(reason: "Missing variable name", token: " ") @@ -261,11 +261,13 @@ final class EnvironmentIncludeTemplateTests: XCTestCase { function: String = #function ) throws { var expectedError = expectedSyntaxError(token: token, template: template, description: reason) - expectedError.stackTrace = [expectedSyntaxError( - token: includedToken, - template: includedTemplate, - description: reason - ).token].compactMap { $0 } + expectedError.stackTrace = [ + expectedSyntaxError( + token: includedToken, + template: includedTemplate, + description: reason + ).token + ].compactMap { $0 } let error = try expect( self.environment.render(template: self.template, context: ["target": "World"]), @@ -368,11 +370,13 @@ final class EnvironmentBaseAndChildTemplateTests: XCTestCase { ) throws { var expectedError = expectedSyntaxError(token: childToken, template: childTemplate, description: reason) if let baseToken = baseToken { - expectedError.stackTrace = [expectedSyntaxError( - token: baseToken, - template: baseTemplate, - description: reason - ).token].compactMap { $0 } + expectedError.stackTrace = [ + expectedSyntaxError( + token: baseToken, + template: baseTemplate, + description: reason + ).token + ].compactMap { $0 } } let error = try expect( self.environment.render(template: self.childTemplate, context: ["target": "World"]), diff --git a/Tests/StencilTests/FilterSpec.swift b/Tests/StencilTests/FilterSpec.swift index d2ac102..9b7b9bb 100644 --- a/Tests/StencilTests/FilterSpec.swift +++ b/Tests/StencilTests/FilterSpec.swift @@ -325,10 +325,12 @@ final class FilterTests: XCTestCase { let template = Template(templateString: """ {{ value|indent:2 }} """) - let result = try template.render(Context(dictionary: ["value": """ + let result = try template.render(Context(dictionary: [ + "value": """ One Two - """])) + """ + ])) try expect(result) == """ One Two @@ -339,10 +341,12 @@ final class FilterTests: XCTestCase { let template = Template(templateString: """ {{ value|indent:2,"\t" }} """) - let result = try template.render(Context(dictionary: ["value": """ + let result = try template.render(Context(dictionary: [ + "value": """ One Two - """])) + """ + ])) try expect(result) == """ One \t\tTwo @@ -353,10 +357,12 @@ final class FilterTests: XCTestCase { let template = Template(templateString: """ {{ value|indent:2," ",true }} """) - let result = try template.render(Context(dictionary: ["value": """ + let result = try template.render(Context(dictionary: [ + "value": """ One Two - """])) + """ + ])) try expect(result) == """ One Two @@ -367,14 +373,16 @@ final class FilterTests: XCTestCase { let template = Template(templateString: """ {{ value|indent }} """) - let result = try template.render(Context(dictionary: ["value": """ + let result = try template.render(Context(dictionary: [ + "value": """ One Two - """])) + """ + ])) try expect(result) == """ One diff --git a/Tests/StencilTests/LexerSpec.swift b/Tests/StencilTests/LexerSpec.swift index 60b8cf7..4eb7e6b 100644 --- a/Tests/StencilTests/LexerSpec.swift +++ b/Tests/StencilTests/LexerSpec.swift @@ -127,7 +127,8 @@ final class LexerTests: XCTestCase { } func testCombiningDiaeresis() throws { - // the symbol "ü" in the `templateString` is unusually encoded as 0x75 0xCC 0x88 (LATIN SMALL LETTER U + COMBINING DIAERESIS) instead of 0xC3 0xBC (LATIN SMALL LETTER U WITH DIAERESIS) + // the symbol "ü" in the `templateString` is unusually encoded as 0x75 0xCC 0x88 (LATIN SMALL LETTER U + COMBINING + // DIAERESIS) instead of 0xC3 0xBC (LATIN SMALL LETTER U WITH DIAERESIS) let templateString = "ü\n{% if test %}ü{% endif %}\n{% if ü %}ü{% endif %}\n" let lexer = Lexer(templateString: templateString) let tokens = lexer.tokenize()