250x250
반응형
Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

개발자 수니

[iOS/Swift] SwiftLint 적용하기 (pod 'SwiftLint') 본문

iOS - Swift

[iOS/Swift] SwiftLint 적용하기 (pod 'SwiftLint')

개발자 수니 2022. 1. 19. 18:10
728x90
반응형

📌  이번 글은

Xcode 프로젝트에 SwiftLint 적용 방법입니다.

 

이번에 토이 프로젝트 리팩터링을 하면서 SwiftLint를 적용해보기로 했어요.

정말 유용해서 한번 익히면 자주 사용할 것 같아 적용 법도 정리해봅니다!

 

SwiftLint
- Swift 스타일 및 규칙을 적용하는 도구입니다.
- 일반적으로 Swift 커뮤니티에서 허용되는 스타일 가이드 규칙을 적용합니다.

 

 1. 프로젝트에 SwiftLint 설치 : pod 'SwiftLint' 

 

GitHub - realm/SwiftLint: A tool to enforce Swift style and conventions.

A tool to enforce Swift style and conventions. Contribute to realm/SwiftLint development by creating an account on GitHub.

github.com

 

 

2. Xcode에 SwiftLint 적용

Targets > Build Phases > + > New Run Script Phase

 

이름을 SwiftLint Script로 변경

Complie Sources 위로 이동 (컴파일하기 전에 스크립트를 실행시켜 SwiftLint 검사를 먼저 하는 것이 효율적)

 

Run Script 입력

${PODS_ROOT}/SwiftLint/swiftlint

 

 

3. .swiftlint.yml 파일 생성

SwiftLint 실행에 대한 Rule을 작성하기 위한 파일

 

.swiftlint.yml 이름의 Empty 파일 생성 (드시 프로젝트 최상위에 생성해야 합니다.)

 

3. Rule

disabled_rules (규칙 이름 목록 보러 가기)

디폴트로 활성화되어있는 규칙 중 비활성화할 규칙을 정하는 키워드

 

opt_in_rules (규칙 이름 목록 보러 가기)

기본 규칙이 아닌 규칙들을 활성화

 

whitelist_rules

지정된 규칙들만 활성화되도록 화이트리스트로 등록 (disalbed_rules, opt_in_rules은 같이 사용 불가)

 

included

SwiftLint 검사에서 포함할 파일 경로

 

excluded

SwiftLint 검사에서 제외할 파일 경로 - included에 포함한 것보다 우선적으로 처리

 

excluded에 자주 포함하는 경로

excluded:
  - Pods
  - ProjectName/AppDelegate.swift
  - ProjectName/SceneDelegate.swift

 

현재 ToDoRim에 적용 중인 rules

# Git URL : https://github.com/realm/SwiftLint
# Reference : https://realm.github.io/SwiftLint/index.html
# 린트 과정에서 무시할 파일 경로.
excluded:
  - Pods
  - Todorim/AppDelegate.swift
included:
  - Todorim
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

 

3. 빌드, 오류 해결

위 룰을 적용하고 빌드하면 오류들이 발생합니다.

항상 commit 전에, Lint 오류들을 해결하여 코드 컨벤션을 잡고 하는 게 바람직합니다! 

 

 

 

🙋🏻‍♀️ 참고

[iOS/Swift] XCode 프로젝트에 SwiftLint 적용하기

[iOS - swift] SwiftLint 적용 방법

728x90
반응형
Comments