swiftlint
SwiftLint is the industry-standard linter and formatter for Swift code that enforces style guidelines and best practices based on the community-driven Swift Style Guide. It serves as an essential tool for maintaining high-quality, consistent Swift codebases.
Key Features:
- Comprehensive Rule Set: Over 200 built-in rules covering code style, performance, best practices, and potential bug detection
- SourceKit Integration: Leverages Apple's SourceKit for accurate AST-based analysis and precise error detection
- Automatic Fixing: Intelligent auto-correction for many rule violations, saving manual effort
- Highly Configurable: Extensive configuration options with support for enabling/disabling rules and custom severity levels
- Custom Rules: Support for project-specific custom rules using regular expressions and AST patterns
- Xcode Integration: Seamless integration with Xcode projects through build phases and scheme actions
- Performance Optimized: Fast analysis even for large Swift projects with incremental linting support
- Multiple Output Formats: Supports various report formats including JSON, HTML, and checkstyle for tool integration
swiftlint documentation
- Version in MegaLinter: 0.61.0
- Visit Official Web Site
- Docker image: ghcr.io/realm/swiftlint:SWIFT_SWIFTLINT_VERSION
- arguments:
-v {{WORKSPACE}}:/tmp/lint:rw -w /tmp/lint
- arguments:
- See How to configure swiftlint rules
- See How to disable swiftlint rules in files
- See Index of problems detected by swiftlint
Configuration in MegaLinter
- Enable swiftlint by adding
SWIFT_SWIFTLINT
in ENABLE_LINTERS variable - Disable swiftlint by adding
SWIFT_SWIFTLINT
in DISABLE_LINTERS variable
- Enable autofixes by adding
SWIFT_SWIFTLINT
in APPLY_FIXES variable
Variable | Description | Default value |
---|---|---|
SWIFT_SWIFTLINT_DOCKER_IMAGE_VERSION | Docker image version | SWIFT_SWIFTLINT_VERSION |
SWIFT_SWIFTLINT_ARGUMENTS | User custom arguments to add in linter CLI call Ex: -s --foo "bar" |
|
SWIFT_SWIFTLINT_COMMAND_REMOVE_ARGUMENTS | User custom arguments to remove from command line before calling the linter Ex: -s --foo "bar" |
|
SWIFT_SWIFTLINT_CLI_LINT_MODE | Override default CLI lint mode ⚠️ As default value is project, overriding might not work - file : Calls the linter for each file- list_of_files : Call the linter with the list of files as argument- project : Call the linter from the root of the project |
project |
SWIFT_SWIFTLINT_FILE_EXTENSIONS | Allowed file extensions. "*" matches any extension, "" matches empty extension. Empty list excludes all filesEx: [".py", ""] |
[".swift"] |
SWIFT_SWIFTLINT_FILE_NAMES_REGEX | File name regex filters. Regular expression list for filtering files by their base names using regex full match. Empty list includes all files Ex: ["Dockerfile(-.+)?", "Jenkinsfile"] |
Include every file |
SWIFT_SWIFTLINT_PRE_COMMANDS | List of bash commands to run before the linter | None |
SWIFT_SWIFTLINT_POST_COMMANDS | List of bash commands to run after the linter | None |
SWIFT_SWIFTLINT_UNSECURED_ENV_VARIABLES | List of env variables explicitly not filtered before calling SWIFT_SWIFTLINT and its pre/post commands | None |
SWIFT_SWIFTLINT_CONFIG_FILE | swiftlint configuration file name Use LINTER_DEFAULT to let the linter find it |
.swiftlint.yml |
SWIFT_SWIFTLINT_RULES_PATH | Path where to find linter configuration file | Workspace folder, then MegaLinter default rules |
SWIFT_SWIFTLINT_DISABLE_ERRORS | Run linter but consider errors as warnings | false |
SWIFT_SWIFTLINT_DISABLE_ERRORS_IF_LESS_THAN | Maximum number of errors allowed | 0 |
SWIFT_SWIFTLINT_CLI_EXECUTABLE | Override CLI executable | ['swiftlint'] |
IDE Integration
Use swiftlint in your favorite IDE to catch errors before MegaLinter !
IDE | Extension Name | Install | |
---|---|---|---|
IDEA | swiftlint | ||
xcode | Configure manually | Visit Web Site |
MegaLinter Flavors
This linter is available in the following flavors
Flavor | Description | Embedded linters | Info | |
---|---|---|---|---|
![]() |
all | Default MegaLinter Flavor | 126 | |
cupcake | MegaLinter for the most commonly used languages | 87 | ||
swift | Optimized for SWIFT based projects | 50 |
Behind the scenes
How are identified applicable files
- Activated only if one of these files is found:
.swiftlint.yml
- File extensions:
.swift
How the linting is performed
swiftlint is called once on the whole project directory (project
CLI lint mode)
- filtering can not be done using MegaLinter configuration variables,it must be done using swiftlint configuration or ignore file (if existing)
VALIDATE_ALL_CODEBASE: false
doesn't make swiftlint analyze only updated files
Example calls
docker run -v /tmp/lint:/tmp/lint:rw ghcr.io/realm/swiftlint:latest swiftlint --strict
docker run -v /tmp/lint:/tmp/lint:rw ghcr.io/realm/swiftlint:latest swiftlint --fix --strict
Help content
OVERVIEW: Print lint warnings and errors
USAGE: swiftlint lint [<options>] [<paths> ...]
ARGUMENTS:
<paths> List of paths to the files or directories to lint.
OPTIONS:
--config <config> The path to one or more SwiftLint configuration
files, evaluated as a parent-child hierarchy.
--fix, --autocorrect Correct violations whenever possible.
--format Should reformat the Swift files using the same
mechanism used by Xcode (via SourceKit).
Only applied with `--fix`/`--autocorrect`.
--use-alternative-excluding
Use an alternative algorithm to exclude paths for
`excluded`, which may be faster in some cases.
--use-script-input-files
Read SCRIPT_INPUT_FILE* environment variables as
files.
--use-script-input-file-lists
Read SCRIPT_INPUT_FILE_LIST* environment variables as
file lists.
--strict Upgrades warnings to serious violations (errors).
--lenient Downgrades serious violations to warnings, warning
threshold is disabled.
--force-exclude Exclude files in config `excluded` even if their
paths are explicitly specified.
--benchmark Save benchmarks to `benchmark_files.txt` and
`benchmark_rules.txt`.
--reporter <reporter> The reporter used to log errors and warnings.
--baseline <baseline> The path to a baseline file, which will be used to
filter out detected violations.
--write-baseline <write-baseline>
The path to save detected violations to as a new
baseline.
--working-directory <working-directory>
The working directory to use when running SwiftLint.
--output <output> The file where violations should be saved. Prints to
stdout by default.
--progress Show a live-updating progress bar instead of each
file being processed.
--check-for-updates Check whether a later version of SwiftLint is
available after processing all files.
--only-rule <only-rule> Run only the specified rule, ignoring `only_rules`,
`opt_in_rules` and `disabled_rules`.
Can be specified repeatedly to run multiple rules.
--use-stdin Lint standard input.
--quiet Don't print status logs like 'Linting <file>' & 'Done
linting'.
--silence-deprecation-warnings
Don't print deprecation warnings.
--cache-path <cache-path>
The directory of the cache used when linting.
--no-cache Ignore cache when linting.
--enable-all-rules Run all rules, even opt-in and disabled ones,
ignoring `only_rules`.
--version Show the version.
-h, --help Show help information.
Installation on mega-linter Docker image
- Dockerfile commands :
# renovate: datasource=docker depName=ghcr.io/realm/swiftlint
ENV SWIFT_SWIFTLINT_VERSION=0.61.0