Skip to content

Commit

Permalink
ADD swiftlint
Browse files Browse the repository at this point in the history
  • Loading branch information
mflknr committed Mar 9, 2024
1 parent fca144f commit a315af9
Show file tree
Hide file tree
Showing 18 changed files with 374 additions and 117 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/checks.yml → .github/workflows/builds.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: checks
name: builds

on:
push:
Expand All @@ -21,7 +21,6 @@ jobs:
- uses: fwal/setup-swift@v1
with:
swift-version: ${{ matrix.swift }}

- run: swift --version

- run: swift build -v
- run: swift test -v
17 changes: 17 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: lint

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
runs-on: macos-latest
steps:
- uses: actions/checkout@v4
- uses: jdx/mise-action@v2
- run: swiftlint --strict
2 changes: 2 additions & 0 deletions .mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
swiftlint = "0.54.0"
220 changes: 220 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
excluded:
- Package.swift
- .swiftpm
- .build

disabled_rules:

analyzer_rules:
- capture_variable
#- explicit_self
#- typesafe_array_init
- unused_declaration
- unused_import

# rules that are commented out are explicitly opted out unless told otherwise
opt_in_rules:
- accessibility_label_for_image
- accessibility_trait_for_button
#- anonymous_argument_in_multiline_closure
#- anyobject_protocol # deprecated
- array_init
- attributes
- balanced_xctest_lifecycle
- closure_end_indentation
- closure_spacing
- collection_alignment
- comma_inheritance
- conditional_returns_on_newline
- contains_over_filter_count
- contains_over_filter_is_empty
- contains_over_first_not_nil
- contains_over_range_nil_comparison
- convenience_type
#- direct_return
- discarded_notification_center_observer
- discouraged_assert
- discouraged_none_name
- discouraged_object_literal
#- discouraged_optional_boolean
#- discouraged_optional_collection
- empty_collection_literal
- empty_count
- empty_string
- empty_xctest_method
- enum_case_associated_values_count
#- explicit_enum_raw_value
- expiring_todo
#- explicit_acl
- explicit_enum_raw_value
- explicit_init
#- explicit_top_level_acl
- explicit_type_interface
- extension_access_modifier
- fallthrough
- fatal_error_message
- file_header
#- file_name
- file_name_no_space
#- file_types_order
- final_test_case # has been added to the source, but has not been released yet
- first_where
- flatmap_over_map_reduce
- force_unwrapping
#- function_default_parameter_at_end
- ibinspectable_in_extension
#- identical_operands
- implicit_return
- implicitly_unwrapped_optional
#- indentation_width # has conflicts with default xcode settings. use strg+i to indent correctly
#- inert_defer # deprecated
- joined_default_parameter
- last_where
#- legacy_multiple
#- legacy_objc_type # not suitable due to third party libs
- let_var_whitespace
- literal_expression_end_indentation
- local_doc_comment
- lower_acl_than_parent
- missing_docs
- modifier_order
- multiline_arguments
- multiline_arguments_brackets
- multiline_function_chains
- multiline_literal_brackets
- multiline_parameters
- multiline_parameters_brackets
#- nimble_operator
#- no_extension_access_modifier
#- no_grouping_extension
- no_magic_numbers
- non_overridable_class_declaration
- notification_center_detachment
#- nslocalizedstring_key
#- nslocalizedstring_require_bundle
- number_separator
#- object_literal
- one_declaration_per_file # has been added to the source, but has not been released yet
- opening_brace
- operator_usage_whitespace
- optional_enum_case_matching
- overridden_super_call
- override_in_extension
- pattern_matching_keywords
#- prefer_nimble
- prefer_self_in_static_references
- prefer_self_type_over_type_of_self
- prefer_zero_over_explicit_init
- prefixed_toplevel_constant
- private_swiftui_state
#- prohibited_interface_builder
#- prohibited_super_call
#- quick_discouraged_call
#- quick_discouraged_focused_test
#- quick_discouraged_pending_test
#- raw_value_for_camel_cased_codable_enum
- reduce_into
- redundant_nil_coalescing
- redundant_self_in_closure
#- redundant_type_annotation
#- required_deinit
#- required_enum_case
- return_value_from_void_function
- self_binding
- shorthand_argument # has been added to the source, but has not been released yet
- shorthand_optional_binding
- single_test_class
#- sorted_enum_cases
- sorted_first_last
#- sorted_imports # see #1295 on github, conflicts with testable, also managed with swiftformat
- static_operator
- strict_fileprivate
#- strong_iboutlet
- superfluous_else
- switch_case_on_newline
- test_case_accessibility
- toggle_bool
- trailing_closure
- type_contents_order
- unavailable_function
- unhandled_throwing_task
- unneeded_parentheses_in_closure_argument
- unowned_variable_capture
- untyped_error_in_catch
#- unused_capture_list # deprecated
#- vertical_parameter_alignment_on_call
#- vertical_whitespace_between_cases
- vertical_whitespace_closing_braces
- vertical_whitespace_opening_braces
- weak_delegate
- xct_specific_matcher
- yoda_condition

attributes:
always_on_same_line:
- "@IBSegueAction"
- "@IBAction"
- "@NSManaged"
- "@objc"
always_on_line_above:
- "@discardableResult"

force_cast: error

force_try: error

function_body_length:
warning: 150

legacy_hashing: error

identifier_name:
min_length: 2
max_length:
warning: 60
error: 80
excluded:
- id

multiline_arguments:
first_argument_location: any_line
only_enforce_after_first_closure_on_first_line: true

number_separator:
minimum_length: 5

overridden_super_call:
excluded:
- setUp()
- setUpWithError()
- tearDown()
- tearDownWithError()

private_over_fileprivate:
validate_extensions: true

trailing_whitespace:
ignores_empty_lines: true
ignores_comments: true

type_name:
min_length: 3
max_length:
warning: 70
error: 80
allowed_symbols:
- "_"

trailing_closure:
only_single_muted_parameter: true

cyclomatic_complexity:
ignores_case_statements: true

function_parameter_count:
warning: 6
error: 8

type_body_length:
warning: 300
error: 400
16 changes: 8 additions & 8 deletions Sources/Helper/String+Regex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
//

internal extension String {
var isAlphaNumericString: Bool {
matches("^[a-zA-Z0-9-]+$")
}

var isNumericString: Bool {
matches("[0-9]+$")
}

func matches(_ regex: String) -> Bool {
range(of: regex, options: .regularExpression, range: nil, locale: nil) != nil
}
Expand All @@ -15,12 +23,4 @@ internal extension String {
matches("^([0-9a-zA-Z]+)\\.([0-9a-zA-Z]+)$") ||
matches("^([0-9a-zA-Z]+)$")
}

var isAlphaNumericString: Bool {
matches("^[a-zA-Z0-9-]+$")
}

var isNumericString: Bool {
matches("[0-9]+$")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
//

extension BuildMetaData: LosslessStringConvertible {
public var description: String { value }

public init?(_ string: String) {
self.init(private: string)
}

public var description: String { value }
}

extension BuildMetaData: ExpressibleByStringLiteral {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public enum BuildMetaData: Comparable {
case unknown

init(private string: String) {
if let _ = Int(string) {
if Int(string) != nil {
self = .digits(string)
} else if string.isAlphaNumericString {
self = .alphaNumeric(string)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// Created by Marius Felkner on 12.03.21.
//

extension PrereleaseIdentifier {
public extension PrereleaseIdentifier {
/// Compares pre-release identifiers for equality.
///
/// - Returns: `true` if pre-release identifiers are equal.
public static func == (lhs: Self, rhs: Self) -> Bool {
static func == (lhs: Self, rhs: Self) -> Bool {
lhs.value == rhs.value
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
//

extension PrereleaseIdentifier: LosslessStringConvertible {
public var description: String { value }

public init?(_ string: String) {
self.init(private: string)
}

public var description: String { value }
}

extension PrereleaseIdentifier: ExpressibleByStringLiteral {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ public extension SemanticVersionComparable {
guard lhs.hasEqualVersionCore(as: rhs) else {
// cast UInt to Int for each identifier to compare ordering lexicographically. missing
// identifier for minor or patch versions (e. g. "1" or "2.0") are handled as zeros.
let lhsAsIntSequence = [Int(lhs.major), Int(lhs.minor ?? 0), Int(lhs.patch ?? 0)]
let rhsAsIntSequence = [Int(rhs.major), Int(rhs.minor ?? 0), Int(rhs.patch ?? 0)]
let lhsAsIntSequence: [Int] = [Int(lhs.major), Int(lhs.minor ?? 0), Int(lhs.patch ?? 0)]
let rhsAsIntSequence: [Int] = [Int(rhs.major), Int(rhs.minor ?? 0), Int(rhs.patch ?? 0)]
return lhsAsIntSequence.lexicographicallyPrecedes(rhsAsIntSequence)
}

// non-pre-release lhs version is always >= than rhs version
guard
let lhspr = lhs.prerelease,
lhspr.count > 0 else {
!lhspr.isEmpty else {
return false
}

// same goes for rhs vise versa
guard
let rhspr = rhs.prerelease,
rhspr.count > 0 else {
!rhspr.isEmpty else {
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
// Created by Marius Felkner on 13.03.21.
//

extension SemanticVersionComparable {
public extension SemanticVersionComparable {
/// Conformance to `Hashable` protocol.
///
/// - Note: Since build-meta-data are not considered in ranking semantic version, it won't be considered
/// here either.
public func hash(into hasher: inout Hasher) {
func hash(into hasher: inout Hasher) {
hasher.combine(major)
hasher.combine(minor)
hasher.combine(patch)
Expand Down
Loading

0 comments on commit a315af9

Please sign in to comment.