realm / realm/SwiftLint

TypeBodyLengthRule should not count conditional directives

Open
#3,139 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Swift
Stars
19.7k
Forks
2.3k
Avg merge
1d 1h
Merged PRs (30d)
11

Description

New Issue Checklist
Describe the bug

type_body_length rule counts lines with conditional directives as lines of code, which is not correct. Example:

class Test {
  func foo() {
    #if DEBUG
    print("test")
    #endif 
  }
}

How many lines are in foo function? swiftlint thinks there are 5 lines, but there's 3 lines of code.
This rule ignores comments and whitespaces already since they are not an actual lines of code (#369). So, why a conditional directives does count?
I believe it's an incorrect rule behaviour.

Environment
  • SwiftLint version (run swiftlint version to be sure)?
    0.39.1
  • Installation method used (Homebrew, CocoaPods, building from source, etc)?
    CocoaPods

swiftlint ouptut for sample above:

$ pbpaste | swiftlint lint --no-cache --use-stdin --enable-all-rules
Loading configuration from '.swiftlint.yml'
<nopath>:1:1: warning: Type Body Length Violation: Type body should span 3 lines or less excluding comments and whitespace: currently spans 4 lines (type_body_length)

using sample yml:


type_body_length: 3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the TypeBodyLengthRule entry point and reproduce the reported result using the SwiftLint stdin command and sample configuration from the issue. The work is done when conditional directives are excluded from the counted lines while comments and whitespace remain excluded, and the sample reports the expected three lines.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.