Contents

gayledunham/swiftlintplugin

A Swift Package Manager plugin for SwiftLint, supporting both Swift Packages and Xcode Projects.

Requirements

  • Recommended: Xcode 16 or later, but you can use Xcode 14 or 15 by specifying branch swift-5.7
  • Recommended: Swift 6.0 or later, but you can use Swift 5.7 or later by specifying branch swift-5.7
  • Required: You must have a SwiftLint configuration file (swiftlint.yml or .swiftlint.yml) in the root folder of your package or project.

Installation

This package contains a binaryTarget for the current released version 0.63.2 of SwiftLint from realm/SwiftLint, used by all the plugin commands and the build tool command. No installation of SwiftLint or configuring of PATH is required.

For more information on SwiftLint rules, see the Rule Directory Reference.

Adding Linting to a Swift Package

  1. Add SwiftLintPlugin as a dependency of your package.
    dependencies: [
        .package(url: "https://github.com/GayleDunham/SwiftLintPlugin.git", branch: "main"),
    ],
  1. Optionally, add the SwiftLintBuildTool plugin to your main target. All files in the package will be evaluated by the linter.
    targets: [
        .target(
            name: "YOUR_TARGET",
            dependencies: [],
            plugins: [ .plugin(name: "SwiftLintBuildTool", package: "SwiftLintPlugin") ]
        ),

Adding Linting to an Xcode Project

TL;DR

  1. Add the package https://github.com/GayleDunham/SwiftLintPlugin to the project.
  2. Optionally, add the SwiftLintBuildTool plugin to the targets you want to be evaluated by the linter.

Add the Package to Your Project

  1. In the Project Navigator, select the first item (the project).
  2. In the Project Settings editor, select the project.
  3. Select the Package Dependencies tab.
  4. Click the + under the "Add package here" text.

[Edit the Project Package Settings]

  1. Paste https://github.com/GayleDunham/SwiftLintPlugin in the search box.
  2. Click Add Package.

[Paste the link and Add Package]

Optionally: Add the Build Tool to Your Target

  1. Select the target.
  2. Select the Build Phases tab.
  3. Expand Run Build Tool Plug-in and click the +.

[Edit the Target Build Settings]

  1. Select SwiftLintBuildTool and click Add.

[Select the Build Tool and Add]

Features

Build Tool

The SwiftLintBuildTool is a pre-build command that runs SwiftLint against a target's sources directory. All lint issues are displayed in the Xcode Issue Navigator and code editor.

Commands

Commands are run against one or more selected targets. Output appears in the Report Navigator, labeled with the command name (e.g., "SwiftLintFix").

  • SwiftLintFix: Modifies your files to correct lint violations if possible
  • SwiftLintLinter: Prints lint warnings and errors
  • SwiftLintRules: Displays the list of rules and their identifiers
  • SwiftLintVersion: Displays the current version of SwiftLint

Running Plugin Commands from Xcode

  • From the Project Navigator: Right-click on the first item (the project or package) in the Project Navigator, then click the command to run.

[Run Command from Project Navigator]

  • From the Menu: Select the first item (the project or package) in the Project Navigator. Then in the menu bar, select File > Packages and the command to run.

[Run Command from Menu]

Command Line Usage for Swift Packages

In the top-level directory of the Swift Package, execute any of the following commands.

swift package swiftlint-fix
swift package swiftlint-lint
swift package swiftlint-rules
swift package swiftlint-version

xcodebuild Usage and CI Systems

xcodebuild  \
    -scheme "YOUR_PROJECT" \
    -destination "platform=macOS" \
    -skipPackagePluginValidation \
    clean build

[!NOTE] For CI systems, specify -skipPackagePluginValidation to skip the validation prompt that occurs in Xcode.

References

SwiftLint

Example SwiftLint Configuration Files from Industry Leaders

Swift Package Plugins

Package Metadata

Repository: gayledunham/swiftlintplugin

Default branch: main

README: README.md