ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [iOS] SwiftLint ์ ์šฉํ•˜๊ธฐ
    iOS 2022. 5. 10. 16:59

    SwiftLint๋ž€?

    SwiftLint๋Š” ์ฝ”๋“œ์˜ ์Šคํƒ€์ผ ์˜ค๋ฅ˜๋ฅผ ํ‘œ์‹œํ•˜๋Š”๋ฐ ๋„์›€์ด ๋˜๋Š” ์‹ ์†ํ•œ ์ฝ”๋“œ ์Šคํƒ€์ผ ๋ถ„์„ ๋„๊ตฌ์ด๋‹ค.

    ์Šคํƒ€์ผ์„ ๋”ฐ๋ฅด์ง€ ์•Š์„ ๋•Œ, ๋นŒ๋“œ๋ฅผ ์‹คํŒจ๋กœ ํ‘œ์‹œํ•˜๊ฑฐ๋‚˜ ๊ฒฝ๊ณ ๋ฅผ ํ‘œ์‹œํ•˜์—ฌ ์Šคํƒ€์ผ์„ ์ ์šฉํ•˜๋Š”๋ฐ ๋„์›€์„ ์ค€๋‹ค.

     

    1. ์ฝ”์ฝ”์•„ํŒŸ ์„ค์น˜ํ•˜๊ธฐ

    pod 'SwiftLint'
    

    2. Script ์ถ”๊ฐ€ํ•˜๊ธฐ

    Target → Build Phase → + → New Run Script Phase

     

    Run Script์— ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

    ${PODS_ROOT}/SwiftLint/swiftlint
    

    ๊ทธ๋ฆฌ๊ณ  Run Script ์ด๋ฆ„์„ SwiftLint Script๋กœ ๋ณ€๊ฒฝํ–ˆ๋‹ค.

    ๊ทธ๋ฆฌ๊ณ  ์œ„์น˜๋ฅผ Compile Sources ์œ„๋กœ ์˜ฌ๋ ค์ฃผ์—ˆ๋‹ค!

    ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด, ์ปดํŒŒ์ผ ์ „์— ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•ด์„œ SwiftLint ๋ฃฐ ๊ฒ€์‚ฌ๋ฅผ ๋จผ์ € ์ง„ํ–‰ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

     

    ๊ทธ๋ฆฌ๊ณ  ๋นŒ๋“œ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด!

    ์ด๋ ‡๊ฒŒ ๋งŽ์€ Warning๋“ค์ด ์ƒ๊ฒจ๋‚œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

    ์ง€๊ธˆ์€ ์ง์ ‘ ๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ๊ฒƒ์ด ์•„๋‹Œ, Default Rules๋ฅผ ๋ชจ๋‘ ์ ์šฉ์‹œํ‚จ ์ƒํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ ‡๊ฒŒ ๋งŽ์€ ๊ฒฝ๊ณ ๋“ค์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

    Default Rules๋Š” ์—ฌ๊ธฐ์„œ ํ™•์ธ ๊ฐ€๋Šฅ!

     

    3. SwiftLint ์‚ฌ์šฉํ•˜๊ธฐ

    ์šฐ๋ฆฌ๊ฐ€ ์ด์ œ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

    • disable rules (๊ทœ์น™ ๋ฌดํšจํ™”)
    • opt-in on non-default rules (๊ธฐ๋ณธ ์•„๋‹Œ ๊ทœ์น™์— ๋Œ€ํ•œ ์„ ํƒ)
    • specify only the rules you want to enable (ignoring all default rules) (๋ชจ๋“  ๊ทœ์น™ ๋ฌด์‹œํ•˜๊ณ , ์‚ฌ์šฉํ•˜๋ ค๋Š” ๊ทœ์น™๋งŒ ์ง€์ •)

     

    ๋จผ์ € .swiftlint.yml ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์•ผํ•œ๋‹ค.

    ํ”„๋กœ์ ํŠธ์˜ ๊ฐ€์žฅ ์ƒ์œ„ ํด๋”์—์„œ Empty ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค. ํŒŒ์ผ ์ด๋ฆ„์€ .swiftlint.yml์œผ๋กœ ์ €์žฅํ•œ๋‹ค.

     

    ๊ทธ๋Ÿฌ๋ฉด ์ด๋ ‡๊ฒŒ ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ๋‹ค!

    ์—ฌ๊ธฐ์„œ ์›ํ•˜๋Š” ๋Œ€๋กœ ์ž‘์„ฑํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

    disabled_rules: # Default Rules์—์„œ ๋น„ํ™œ์„ฑํ•  ๊ทœ์น™
    - trailing_whitespace # Lines should not have trailing whitespace.
    
    opt_in_rules: # ๊ธฐ๋ณธ์ ์œผ๋กœ ๋น„ํ™œ์„ฑํ™”๋˜์–ด์žˆ์œผ๋‚˜ ํ™œ์„ฑํ™”ํ•  ๊ทœ์น™
    - yoda_condition
    
    excluded: # File or Directory Lint should exclude.
    - Pods
    

     

     

    Opt-In Rules

    ๊ธฐ๋ณธ์ ์œผ๋กœ ๋น„ํ™œ์„ฑํ™”๋˜์–ด์žˆ์œผ๋‚˜, ํ™œ์„ฑํ™”ํ•˜๊ณ  ์‹ถ์€ ๊ทœ์น™์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

    Guidelines on when to mark a rule as opt-in:

    Disable Rules in code

    ์•„๋ž˜์™€ ๊ฐ™์€ ํฌ๋งท์„ ์‚ฌ์šฉํ•˜์—ฌ ๋น„ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค.

    // swiftlint:disable <rule1> [<rule2> <rule3>...]
    

    ์ด ๊ทœ์น™์€ ํŒŒ์ผ์˜ ๋์ด๋‚˜ ํ™œ์„ฑํ™” ์ปค๋ฉ˜ํŠธ๊ฐ€ ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ ๋น„ํ™œ์„ฑํ™”๋œ๋‹ค.

    // swiftlint:enable <rule1> [<rule2> <rule3>...]
    

    ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

    // swiftlint:disable colon
    let noWarning :String = "" // ๋ณ€์ˆ˜ ์ด๋ฆ„ ์งํ›„์— ์ฝœ๋ก ์— ๋Œ€ํ•œ ๊ฒฝ๊ณ  X
    
    // swiftlint:enable colon
    let hasWarning :String = "" // ๋ณ€์ˆ˜ ์ด๋ฆ„ ์งํ›„์— ์ฝœ๋ก ์— ๋Œ€ํ•œ ๊ฒฝ๊ณ  O
    

    all ํ‚ค์›Œ๋“œ๋ฅผ ํฌํ•จํ•˜๋ฉด, ํ™œ์„ฑํ™” ์ปค๋ฉ˜ํŠธ๊ฐ€ ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ ๋ชจ๋“  ๊ทœ์น™์ด ๋น„ํ™œ์„ฑํ™”๋œ๋‹ค.

    // swiftlint:disable all
    // swiftlint:enable all
    

    Configuration

    SwiftLint๋ฅผ ์‹คํ–‰ํ•  ๋””๋ ‰ํ† ๋ฆฌ์—์„œ .swiftlint.yml ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•ด์„œ SwiftLint๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

    • disabled_rules
      • Default Rules ์ค‘์— ๋น„ํ™œ์„ฑํ™”ํ•˜๊ณ  ์‹ถ์€ ๊ทœ์น™ ์ถ”๊ฐ€
      disabled_rules: # Default Rules์—์„œ ๋น„ํ™œ์„ฑํ•  ๊ทœ์น™
      - trailing_whitespace # Lines should not have trailing whitespace.
      
    • opt_in_rules
      • ๊ธฐ๋ณธ์ ์œผ๋กœ ๋น„ํ™œ์„ฑํ™”๋˜์–ด์žˆ์ง€๋งŒ ํ™œ์„ฑํ™”ํ•˜๊ณ  ์‹ถ์€ ๊ทœ์น™ ์ถ”๊ฐ€
      opt_in_rules: # ๊ธฐ๋ณธ์ ์œผ๋กœ ๋น„ํ™œ์„ฑํ™”๋˜์–ด์žˆ์œผ๋‚˜ ํ™œ์„ฑํ™”ํ•  ๊ทœ์น™
      - yoda_condition
      
    • only_rules
      • ์ด ๋ชฉ๋ก์— ์ง€์ •๋œ ๊ทœ์น™๋งŒ ํ™œ์„ฑํ™”. disabled_rules ๋˜๋Š” opt_in_rules์™€ ํ•จ๊ป˜ ์ง€์ •ํ•  ์ˆ˜ ์—†๋‹ค.
    • analyzer_rules
      • ์˜ค์ง analyze ๋ช…๋ น์— ์˜ํ•ด์„œ๋งŒ ์‹คํ–‰๋˜๋Š” ์™„์ „ํžˆ ๋ณ„๋„์˜ ๊ทœ์น™ ๋ชฉ๋ก์ด๋‹ค. ๋ชจ๋“  analyzer ๊ทœ์น™์€ opt-in์ด๋ฏ€๋กœ ์ด ๊ทœ์น™ ๋ชฉ๋ก๋งŒ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, disabled_rules, only_rules์™€ ๋™์ผํ•œ ๊ทœ์น™์ด ์—†๋‹ค.
    • included
      • SwiftLint ๊ฒ€์‚ฌ์—์„œ ํฌํ•จํ•  ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
    • excluded
      • SwiftLint ๊ฒ€์‚ฌ์—์„œ ์ œ์™ธํ•  ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. included์— ํฌํ•จ๋œ ๊ฒƒ๋ณด๋‹ค ์šฐ์„ ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋œ๋‹ค.
      excluded: # SwiftLint ๊ฒ€์‚ฌ์—์„œ ์ œ์™ธํ•  ํŒŒ์ผ ๊ฒฝ๋กœ
        - Pods
        - ProjectName/AppDelegate.swift
        - ProjectName/SceneDelegate.swift
      

     

     

     

    ์ฐธ๊ณ  ์ž๋ฃŒ

     

    SwiftLintFramework Reference

    SwiftLint A tool to enforce Swift style and conventions, loosely based on the now archived GitHub Swift Style Guide. SwiftLint enforces the style guide rules that are generally accepted by the Swift community. These rules are well described in popular styl

    realm.github.io

     

    Improving Swift Code Readability With SwiftLint

    Easily enforce Swift code styling rules with SwiftLint

    betterprogramming.pub

     

    ๋Œ“๊ธ€

Designed by Tistory.