realm / realm/SwiftLint

Check function calling inside functions using `custom_rules`

Open
#3,500 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

New Issue Checklist
Describe the bug

I use custom_rule to show warnings for all @IBAction functions which don't call handleActionButton function inside these functions and it doesn't work correctly.
In that case, SwiftLint doesn't work correctly.

Here is the custom rule:

custom_rules:
  button_action_tracking:
    name: "Button Action Tracking"
    regex: "@IBAction\\s+func\\s.*\\s(?!.*handleButtonAction)\\s"
    message: "Don't forget to call `handleButtonAction` inside the method"
    severity: warning

Input:

class Test {
    
    @IBOutlet weak var button1: UIButton!
    @IBOutlet weak var button2: UIButton!
    @IBOutlet weak var button3: UIButton!

    func handleButtonAction(with button: UIButton) {
    }

    @IBAction func button1Tapped() {
        handleButtonAction(with: button2)
    }

    @IBAction func button2Tapped() {
    }

    @IBAction func button3Tapped() {
    }

}

Linting:

$ swiftlint lint --path file1.swift
Loading configuration from '.swiftlint.yml'
Linting Swift files at path file1.swift
Linting 'file.swift' (1/1)
file1.swift:10:5: warning: Button Action Tracking Violation: Don't forget to call `handleButtonAction` inside the method (button_action_tracking)
file1.swift:20:1: warning: Trailing Newline Violation: Files should have a single trailing newline. (trailing_newline)
file1.swift:2:1: warning: Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)
Done linting! Found 3 violations, 0 serious in 1 file.

However, SwiftLint highlights the first function instead of the second and third.

Expected behavior

https://regex101.com/r/TdkiNC/32

Screen Shot 2021-01-22 at 10 52 43

Environment
  • SwiftLint version (run swiftlint version to be sure)?
    0.42.0
  • Installation method used (Homebrew, CocoaPods, building from source, etc)?
    CocoaPods
  • Configuration file:
excluded:
  - Pods
  - vendor

disabled_rules:
  - line_length
  - identifier_name
  - function_parameter_count

force_cast: warning
force_try: warning
cyclomatic_complexity:
  error: 40
type_body_length:
  error: 1000
function_body_length:
  error: 200
file_length:
  error: 1600

custom_rules:
  button_action_tracking:
    name: "Button Action Tracking"
    regex: "@IBAction\\s+func\\s.*\\s(?!.*handleButtonAction)\\s"
    message: "Don't forget to call `handleButtonAction` inside the method"
    severity: warning
  • Are you using nested configurations?
    No
  • Which Xcode version are you using (check xcodebuild -version)?
    12.3 (12C33)

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

Reproduce the warning placement with the supplied Swift input, custom_rules configuration, and swiftlint lint --path file1.swift command. Investigate how custom-rule regex matches function bodies and verify the result against the expected behavior: warnings should identify the @IBAction functions that do not call handleButtonAction.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.