-
Notifications
You must be signed in to change notification settings - Fork 0
4. 원티드 프로젝트에 SwiftFormat 적용기
이 설정 파일은 Wantedlab Swift Style Guide에 맞춰 작성했습니다.
가장 최근 설정파일과 내용이 많이 다른데, swiftlint를 적용하는 과정에서 개선되었습니다.
SwiftLint에서 자동 수정을 지원하지 않는 규칙을 제공하고 있습니다.
SwiftLint의 linting으로 인한 워닝 또는 경고를 줄이고 싶었습니다.
그래서, SwiftLint로 autocorrect를 수행한 코드에서 swiftformat을 실행해서 추가 수정했습니다.
우선 wrapparameters규칙을 주석처리하고 나머지 규칙을 수행한 후, wrapparameters를 다시 살리고 전체 규칙을 적용했습니다.
서로 관련있는 2개의 규칙중에 wrap 규칙을 먼저 적용하고 wrapparameters를 적용하기 위해서 아래와 같이 두 단계로 처리했습니다.
> swiftformat --config .wanted.swiftformat .
Running SwiftFormat...
SwiftFormat completed in 26.17s.
607/637 files formatted, 4 files skipped.
> swiftformat --config .wanted.swiftformat .
Running SwiftFormat...
SwiftFormat completed in 16.43s.
339/637 files formatted, 4 files skipped.
아직 정확한 동작방식을 파악하지는 못했지만, 모든 규칙을 한꺼번에 적용하는 것 보다는 관련있는 규칙을 단계적으로 수행했을 때 이슈가 더 줄었습니다. 규칙을 적용하는 순서에 따라서도 결과가 달라졌습니다.
그리고나서, SwiftLint를 실행해서 이슈의 개수를 확인해봤습니다.
> swiftlint --config .swiftlint.yml
Loading configuration from '.swiftlint.yml'
Linting Swift files at paths
Linting 'SearchJobsIntentHandler.swift' (1/637)
.
.
.
Done linting! Found 2717 violations, 670 serious in 637 files.
전체 이슈가 8407개에서 2717개로 5690개가 감소했습니다.
세부적으로 어떻게 변화했는지 살펴봤습니다.
- 전체 - 8407 → 2717
- line_length - 5630 → 582
- multiline 류 - 1695 → 1309
- multiline_arguments
- multiline_arguments_brackets
- multiline_parameters
- multiline_parameters_brackets
- trailing_closure - 615 → 509
- implicitly_unwrapped_optional - 122 → 122
- identifier_name - 85 → 84
- implicit_getter - 80 → 0
- control_statement - 54 → 0
- unused_optional_binding - 46 → 46
- 나머지 - 80 → 65
multiline 류 규칙을 해결할 수 있는 자동 수정 규칙들이 여러개 있는데, 이런 규칙을 한꺼번에 적용하지 않고 순차적으로 적용했을 때는 1695개가 791개로 줄어들었습니다. 서로 관련있는 자동수정되는 규칙들을 하나씩 순차적으로 적용해볼 필요가 있을 것 같습니다.
컴파일 에러가 SwiftFormat의 수정으로 Utils.swift파일에 2개 발생했습니다.
SwiftFormat의 redundantSelf
규칙에 의해 인스턴스 변수의 self.
를 모두 제거했는데, extension에서도 제거를 해버려서 self.boundingRect
가 boundingRect
가 되어 컴파일 에러가 발생했습니다.
그래서, 해당 extension에서는 redundantSelf
규칙이 적용되지 않게 했습니다.
/// extension에서는 self를 붙여야하기때문에(항상 그런 것은 아니지만 아래 케이스에서는), 여기서만 규칙을 비활성화해서 self 제거를 막음.
// swiftformat:disable redundantSelf
extension String {
func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {
let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [kCTFontAttributeName as NSAttributedString.Key: font], context: nil)
return ceil(boundingBox.height)
}
func width(withConstrainedHeight height: CGFloat, font: UIFont) -> CGFloat {
let constraintRect = CGSize(width: .greatestFiniteMagnitude, height: height)
let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [kCTFontAttributeName as NSAttributedString.Key: font], context: nil)