Notice
Recent Posts
Recent Comments
Link
관리 메뉴

김종권의 iOS 앱 개발 알아가기

[iOS - swift] SwiftLint 적용 방법 본문

iOS 응용 (swift)

[iOS - swift] SwiftLint 적용 방법

jake-kim 2021. 8. 17. 23:37

SwiftLint

Xcode에 SwfitLint 적용 설정

  • Targets > build phase > new run script phase

  • run script 입력하면 완료
${PODS_ROOT}/SwiftLint/swiftlint

  • 적용이 되었는지 확인 > 경고 메세지가 생긴것을 확인

 

Rule Directory Reference

 

realm.github.io

swiftlint 커스텀

  • empty 파일로 최상위 경로에 생성

  • Rule 정의
    •  disabled_rules: 디폴트로 활성화되어있는 규칙 중 비활성화할 규칙을 정하는 키워드
    • opt_in_rules: 기본 규칙이 아닌 규칙들을 활성화
    • whitelist_rules: 지정한 규칙들만 활성화 되도록 화이트리스트로 등록 (disalbed_rules, opt_in_rules은 같이 사용 불가)
excluded: # 린트 과정에서 무시할 파일 경로. `included`보다 우선순위 높음
  - Pods
  - SignUp/AppDelegate.swift
  - SignUp/SceneDelegate.swift
  • 일반적으로 사용되는 rules를 적용한 예제
# Git URL : https://github.com/realm/SwiftLint
# Reference : https://realm.github.io/SwiftLint/index.html
excluded:
  - Pods
analyzer_rules:
  - unused_declaration
  - unused_import
opt_in_rules:
  - anyobject_protocol
  - array_init
  - attributes
  - closure_end_indentation
  - closure_spacing
  - collection_alignment
  - contains_over_first_not_nil
  - empty_string
  - empty_xctest_method
  - explicit_init
  - extension_access_modifier
  - fallthrough
  - fatal_error_message
  - file_header
  - first_where
  - identical_operands
  - joined_default_parameter
  - let_var_whitespace
  - last_where
  - literal_expression_end_indentation
  - lower_acl_than_parent
  - nimble_operator
  - operator_usage_whitespace
  - overridden_super_call
  - override_in_extension
  - pattern_matching_keywords
  - private_action
  - private_outlet
  - prohibited_interface_builder
  - prohibited_super_call
  - quick_discouraged_call
  - quick_discouraged_focused_test
  - quick_discouraged_pending_test
  - redundant_nil_coalescing
  - single_test_class
  - sorted_first_last
  - static_operator
  - unavailable_function
  - unneeded_parentheses_in_closure_argument
  - untyped_error_in_catch
  - vertical_parameter_alignment_on_call
  - xct_specific_matcher
  - yoda_condition
disabled_rules:
# 추가한 rules 
  - todo
  - orphaned_doc_comment
  - empty_xctest_method
  - redundant_type_annotation
  - number_separator
  - empty_count
#---------------------------
  - object_literal
  - file_name
  - trailing_whitespace
  - sorted_imports
  - file_header
  - vertical_whitespace_opening_braces
  - vertical_whitespace_closing_braces

# Customized Configurable Rules
line_length: 140
force_cast: error
nesting:
  type_level:
    warning: 3
  statement_level:
    warning: 5
identifier_name:
  excluded:
    - id
# number_separator:
#   minimum_length: 5
# file_name:
#   excluded:
#     - main.swift
#     - LinuxMain.swift
#     - TestHelpers.swift
#     - shim.swift
#     - AutomaticRuleTests.generated.swift

# Defining Custom Rules    
custom_rules:
  rule_id:
    included: Source/SwiftLintFramework/Rules/.+/\w+\.swift
    name: Rule ID
    message: Rule IDs must be all lowercase, snake case and not end with `rule`
    regex: identifier:\s*("\w+_rule"|"\S*[^a-z_]\S*")
    severity: error
  rule_test_function:
    included: Tests/SwiftLintFrameworkTests/RulesTests.swift
    name: Rule Test Function
    message: Rule Test Function mustn't end with `rule`
    regex: func\s*test\w+(r|R)ule\(\)
    severity: error

* 주의- cocoapods에 경고 메시지 해제 방법

# podfile

source 'https://cdn.cocoapods.org/'
platform :ios, '13.0' # cocoapods 프레임워크들이 13.0 버전에 맞추도록 설정 (경고 메시지 해제)

inhibit_all_warnings! # cocoapods의 swiftlint 경고 메시지 해제

* 참고

- https://realm.github.io/SwiftLint/rule-directory.html

- https://yagom.net/forums/topic/swift-lint-%EC%8D%A8%EB%B3%B4%EA%B8%B0/

- https://zeddios.tistory.com/447

- https://github.com/realm/SwiftLint

Comments