### SwiftLint Configuration Example Source: https://github.com/realm/swiftlint/blob/main/README.md This snippet shows a comprehensive .swiftlint.yml configuration file. It demonstrates how to disable/enable rules, specify included/excluded paths, set severity levels for rules, and configure the reporter format. Use this as a template for your project's SwiftLint setup. ```yaml # By default, SwiftLint uses a set of sensible default rules you can adjust. Find all the available rules # by running `swiftlint rules` or visiting https://realm.github.io/SwiftLint/rule-directory.html. # Rules turned on by default can be disabled. disabled_rules: - colon - comma - control_statement # Rules turned off by default can be enabled. opt_in_rules: - empty_count # Alternatively, specify all rules explicitly by uncommenting this option and removing the above two. # only_rules: # - empty_parameters # - vertical_whitespace # Rules only run by `swiftlint analyze`. These are all opt-in. analyzer_rules: - explicit_self # Case-sensitive paths to include during linting. Directory paths supplied on the # command line will be ignored. Wildcards are supported. included: - Sources # Case-sensitive paths to ignore during linting. Takes precedence over `included`. Wildcards # are supported. excluded: - Carthage - Pods - Sources/ExcludedFolder - Sources/ExcludedFile.swift - Sources/*/ExcludedFile.swift # If true, SwiftLint will not fail if no lintable files are found. allow_zero_lintable_files: false # If true, SwiftLint will treat all warnings as errors. strict: false # If true, SwiftLint will treat all errors as warnings. lenient: false # The path to a baseline file, which will be used to filter out detected violations. baseline: Baseline.json # The path to save detected violations to as a new baseline. write_baseline: Baseline.json # If true, SwiftLint will check for updates after linting or analyzing. check_for_updates: true # Configurable rules can be customized. All rules support setting their severity level. force_cast: warning # implicitly force_try: severity: warning # explicitly # Rules that have both warning and error levels can set just the warning level implicitly. line_length: 110 # To set both levels implicitly, use an array. type_body_length: - 300 # warning - 400 # error # To set both levels explicitly, use a dictionary. file_length: warning: 500 error: 1200 # Naming rules can set warnings/errors for `min_length` and `max_length`. Additionally, they can # set excluded names and allowed symbols. type_name: min_length: 4 # warning max_length: # warning and error warning: 40 error: 50 excluded: i(Phone|Pad|Pod) # regex pattern allowed_symbols: ["_"] identifier_name: min_length: error: 4 # only error excluded: # excluded via string array - id - URL - GlobalAPIKey # The default reporter (SwiftLint's output format) can be configured as `checkstyle`, `codeclimate`, `csv`, # `emoji`, `github-actions-logging`, `gitlab`, `html`, `json`, `junit`, `markdown`, `relative-path`, `sarif`, # `sonarqube`, `summary`, or `xcode` (default). reporter: "xcode" ``` -------------------------------- ### Mint Installation Source: https://github.com/realm/swiftlint/blob/main/README.md Install SwiftLint using the Mint package manager. ```bash mint install realm/SwiftLint ``` -------------------------------- ### Homebrew Installation Source: https://github.com/realm/swiftlint/blob/main/README.md Install SwiftLint using the Homebrew package manager. ```bash brew install swiftlint ``` -------------------------------- ### CocoaPods Installation Source: https://github.com/realm/swiftlint/blob/main/README.md Add SwiftLint to your Podfile for installation via CocoaPods. ```ruby pod 'SwiftLint' ``` -------------------------------- ### SwiftLint YAML Configuration Examples Source: https://github.com/realm/swiftlint/blob/main/CONTRIBUTING.md Examples of how to configure SwiftLint rules in a .swiftlint.yml file. Supports basic severity and more complex options like min/max lengths and exclusions. ```yaml force_cast: warning file_length: warning: 800 error: 1200 identifier_name: min_length: warning: 3 error: 2 max_length: 20 excluded: id ``` -------------------------------- ### SwiftLint Configuration: Opt-in Rule Source: https://github.com/realm/swiftlint/blob/main/README.md Example of a project-specific configuration file that opts into the `force_cast` rule. ```yaml opt_in_rules: - force_cast ``` -------------------------------- ### Install Buildkite Agent and Tools Source: https://github.com/realm/swiftlint/blob/main/CONTRIBUTING.md Installs essential tools for Buildkite workers, including aria2, bazelisk, htop, and the Buildkite agent itself, using Homebrew. ```shell brew install aria2 bazelisk htop buildkite/buildkite/buildkite-agent robotsandpencils/made/xcodes ``` -------------------------------- ### Remote Parent Configuration Example Source: https://github.com/realm/swiftlint/blob/main/README.md Reference remote configuration files using URLs starting with 'http://' or 'https://'. SwiftLint caches remote configurations and respects specified timeouts. ```yaml parent_config: https://myteamserver.com/our-base-swiftlint-config.yml ``` -------------------------------- ### SwiftLint Rule Examples Source: https://github.com/realm/swiftlint/blob/main/CONTRIBUTING.md Examples of non-triggering and triggering code for SwiftLint rules. Use `focused()` to run only specific examples during testing. ```swift nonTriggeringExamples: [ Example("let x: [Int]"), Example("let x: [Int: String]").focused() // Only this one will be run in tests. ], triggeringExamples: [ Example("let x: ↓Array"), Example("let x: ↓Dictionary") ] ``` -------------------------------- ### Bazel WORKSPACE Setup for SwiftLint Source: https://github.com/realm/swiftlint/blob/main/README.md This WORKSPACE configuration loads necessary Apple and Swift rules, then adds SwiftLint as an HTTP archive and loads its specific repositories and dependencies. ```bzl load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( name = "build_bazel_rules_apple", sha256 = "390841dd5f8a85fc25776684f4793d56e21b098dfd7243cd145b9831e6ef8be6", url = "https://github.com/bazelbuild/rules_apple/releases/download/2.4.1/rules_apple.2.4.1.tar.gz", ) load( "@build_bazel_rules_apple//apple:repositories.bzl", "apple_rules_dependencies", ) apple_rules_dependencies() load( "@bazel_build_rules_swift//swift:repositories.bzl", "swift_rules_dependencies", ) swift_rules_dependencies() load( "@bazel_build_rules_swift//swift:extras.bzl", "swift_rules_extra_dependencies", ) swift_rules_extra_dependencies() http_archive( name = "SwiftLint", sha256 = "c6ea58b9c72082cdc1ada4a2d48273ecc355896ed72204cedcc586b6ccb8aca6", url = "https://github.com/realm/SwiftLint/releases/download/0.52.4/bazel.tar.gz", ) load("@SwiftLint//bazel:repos.bzl", "swiftlint_repos") swiftlint_repos() load("@SwiftLint//bazel:deps.bzl", "swiftlint_deps") swiftlint_deps() ``` -------------------------------- ### Local Child and Parent Configuration Example Source: https://github.com/realm/swiftlint/blob/main/README.md Use 'child_config' and 'parent_config' to reference local configuration files relative to the current file. Child configurations refine and override parent configurations. ```yaml child_config: .swiftlint_refinement.yml parent_config: Base/.swiftlint_base.yml ``` -------------------------------- ### Basic SwiftLint Run Script Phase Source: https://github.com/realm/swiftlint/blob/main/README.md Use this script in an Xcode Run Script phase to execute SwiftLint. It checks if the command is available and provides an installation warning if not. ```bash if command -v swiftlint >/dev/null 2>&1 then swiftlint else echo "warning: `swiftlint` command not found - See https://github.com/realm/SwiftLint#installation for installation instructions." fi ``` -------------------------------- ### Install Specific Xcode Version Source: https://github.com/realm/swiftlint/blob/main/CONTRIBUTING.md Updates available Xcode versions and installs Xcode version 14.0.0. This is part of the Buildkite worker setup. ```shell xcodes update && xcodes install 14.0.0 ``` -------------------------------- ### Symbolic Link for SwiftLint on Apple Silicon Source: https://github.com/realm/swiftlint/blob/main/README.md Creates a symbolic link to make the SwiftLint binary installed by Homebrew on Apple Silicon accessible in a standard location. ```bash ln -s /opt/homebrew/bin/swiftlint /usr/local/bin/swiftlint ``` -------------------------------- ### SwiftLint Run Script for CocoaPods Installation Source: https://github.com/realm/swiftlint/blob/main/README.md Use this script if SwiftLint was installed via CocoaPods. It directly calls the SwiftLint executable from the Pods directory. ```bash "${PODS_ROOT}/SwiftLint/swiftlint" ``` -------------------------------- ### SwiftLint Configuration: Disable Rule Source: https://github.com/realm/swiftlint/blob/main/README.md Example of a team-wide configuration file that disables the `force_cast` rule. ```yaml disabled_rules: - force_cast ``` -------------------------------- ### Publish CocoaPods Manually Source: https://github.com/realm/swiftlint/blob/main/CONTRIBUTING.md Use this command to manually publish a CocoaPod release if the automated process fails. Ensure you have the latest Xcode and SDKs installed. ```shell make pod_publish ``` -------------------------------- ### Define Custom Regex Rule with String Match Kind Source: https://github.com/realm/swiftlint/blob/main/README.md Example of a custom regex rule that specifically targets 'ninja' within string literals. ```yaml custom_rules: no_hiding_in_strings: regex: "([nN]inja)" match_kinds: string ``` -------------------------------- ### Define Custom Regex Rule with SwiftLint Source: https://github.com/realm/swiftlint/blob/main/README.md Example of a custom regex rule named 'Pirates Beat Ninjas' that flags occurrences of 'ninja' in Swift files, excluding test files. It specifies the rule name, message, severity, and matching syntax kinds. ```yaml custom_rules: pirates_beat_ninjas: included: - ".*\.swift" excluded: - ".*Test\.swift" name: "Pirates Beat Ninjas" regex: "([nN]inja)" capture_group: 0 match_kinds: - comment - identifier message: "Pirates are better than ninjas." severity: error ``` -------------------------------- ### Running SwiftLint with Bazel Source: https://github.com/realm/swiftlint/blob/main/README.md Execute SwiftLint from the command line using Bazel. Ensure you are in the directory containing the Bazel configuration. ```console bazel run -c opt @SwiftLint//:swiftlint ``` -------------------------------- ### SwiftLint Command Line Help Source: https://github.com/realm/swiftlint/blob/main/README.md Display the main help information for the SwiftLint command-line tool, showing available subcommands and options. ```txt $ swiftlint help OVERVIEW: A tool to enforce Swift style and conventions. USAGE: swiftlint OPTIONS: --version Show the version. -h, --help Show help information. SUBCOMMANDS: analyze Run analysis rules docs Open SwiftLint documentation website in the default web browser generate-docs Generates markdown documentation for selected group of rules lint (default) Print lint warnings and errors baseline Operations on existing baselines reporters Display the list of reporters and their identifiers rules Display the list of rules and their identifiers version Display the current version of SwiftLint See 'swiftlint help ' for detailed help. ``` -------------------------------- ### Run SwiftLint Binary Source: https://github.com/realm/swiftlint/blob/main/CONTRIBUTING.md Execute the SwiftLint binary. This can be done using 'swift run' or by directly invoking the binary from the build directory. ```shell swift run [-c release] [swiftlint] [arguments] ``` ```shell .build/[release|debug]/swiftlint ``` -------------------------------- ### Build SwiftLint with Swift Package Manager Source: https://github.com/realm/swiftlint/blob/main/CONTRIBUTING.md Build the SwiftLint binary using Swift Package Manager. Use '-c release' for a release build. ```shell swift build [-c release] ``` -------------------------------- ### SwiftLint Run Script for Apple Silicon (Homebrew) Source: https://github.com/realm/swiftlint/blob/main/README.md This script ensures SwiftLint is found on Apple Silicon by adding Homebrew's bin directory to the PATH. It includes a check for the command's availability. ```bash if [[ "$(uname -m)" == arm64 ]] then export PATH="/opt/homebrew/bin:$PATH" fi if command -v swiftlint >/dev/null 2>&1 then swiftlint else echo "warning: `swiftlint` command not found - See https://github.com/realm/SwiftLint#installation for installation instructions." fi ``` -------------------------------- ### Run SwiftLint Tests with Bazel Source: https://github.com/realm/swiftlint/blob/main/CONTRIBUTING.md Execute SwiftLint tests using Bazel. This is a specific build tool integration. ```shell make bazel_test ``` -------------------------------- ### Swift Package Command Plugin Source: https://github.com/realm/swiftlint/blob/main/README.md Run SwiftLint directly from the command line using the Swift Package Manager's plugin system. ```shell swift package plugin swiftlint ``` -------------------------------- ### Command Line Configuration Hierarchy Source: https://github.com/realm/swiftlint/blob/main/README.md Specify multiple configuration files on the command line, where the first is the parent and the last is the highest-priority child. This overrides any .swiftlint.yml files found in the directory structure. ```bash swiftlint --config .swiftlint.yml --config .swiftlint_child.yml ``` -------------------------------- ### Run SwiftLint Tests with Swift Package Manager Source: https://github.com/realm/swiftlint/blob/main/CONTRIBUTING.md Run SwiftLint tests using Swift Package Manager. This command works on both macOS and Linux. ```shell swift test ``` -------------------------------- ### Run SwiftLint Tests with Docker Source: https://github.com/realm/swiftlint/blob/main/CONTRIBUTING.md Run SwiftLint tests within a Docker container. This ensures a consistent testing environment. ```shell make docker_test ``` -------------------------------- ### Run SwiftLint Tests with Xcode Source: https://github.com/realm/swiftlint/blob/main/CONTRIBUTING.md Execute SwiftLint tests using Xcode's build system. This command is specific to macOS. ```shell xcodebuild -scheme swiftlint test -destination 'platform=macOS' ``` -------------------------------- ### Debug SwiftLint with LLDB Source: https://github.com/realm/swiftlint/blob/main/CONTRIBUTING.md Attach LLDB to the SwiftLint binary for debugging purposes. Include any necessary arguments. ```shell lldb -- .build/[release|debug]/swiftlint [arguments] ``` -------------------------------- ### Bazel Dependency in MODULE.bazel Source: https://github.com/realm/swiftlint/blob/main/README.md Add this line to your MODULE.bazel file to include SwiftLint as a dependency. ```bzl bazel_dep(name = "swiftlint", version = "0.52.4", repo_name = "SwiftLint") ``` -------------------------------- ### Pull SwiftLint Docker Image Source: https://github.com/realm/swiftlint/blob/main/README.md Download the latest SwiftLint Docker image from the GitHub Container Registry. This is a one-time operation before first use. ```bash docker pull ghcr.io/realm/swiftlint:latest ``` -------------------------------- ### Integrate Custom Rules with `only_rules` Source: https://github.com/realm/swiftlint/blob/main/README.md Demonstrates how to include custom rules when using the `only_rules` configuration option by adding the literal string `custom_rules` to the list. ```yaml only_rules: - custom_rules custom_rules: no_hiding_in_strings: regex: "([nN]inja)" match_kinds: string ``` -------------------------------- ### SwiftLint Run Script for Swift Packages Source: https://github.com/realm/swiftlint/blob/main/README.md This script is for Swift packages, locating the SwiftLint executable within the package's artifact directory. It includes a fallback warning if the command is not found. ```bash SWIFT_PACKAGE_DIR="${SWIFT_PACKAGE_DIR:-"${BUILD_DIR%Build/*}"SourcePackages}" SWIFTLINT_CMD="$SWIFT_PACKAGE_DIR/artifacts/swiftlintplugins/SwiftLintBinary/SwiftLintBinary.artifactbundle/macos/swiftlint" if test -f "$SWIFTLINT_CMD" 2>&1 then "$SWIFTLINT_CMD" else echo "warning: `swiftlint` command not found - See https://github.com/realm/SwiftLint#xcode-run-script-build-phase for installation instructions." fi ``` -------------------------------- ### Configure SwiftLint as a pre-commit Hook Source: https://github.com/realm/swiftlint/blob/main/README.md Add SwiftLint to your .pre-commit-config.yaml to automatically lint code before commits. ```yaml repos: - repo: https://github.com/realm/SwiftLint rev: 0.57.1 hooks: - id: swiftlint ``` -------------------------------- ### Swift Package Manager - Latest Release Source: https://github.com/realm/swiftlint/blob/main/README.md Add SwiftLint as a package dependency to consume the latest release automatically. ```swift .package(url: "https://github.com/SimplyDanny/SwiftLintPlugins", from: "") ``` -------------------------------- ### Xcode Package Dependency Source: https://github.com/realm/swiftlint/blob/main/README.md Use this URL to add SwiftLint as a Package Dependency to an Xcode project. ```bash https://github.com/SimplyDanny/SwiftLintPlugins ``` -------------------------------- ### Fastlane SwiftLint Action Configuration Source: https://github.com/realm/swiftlint/blob/main/README.md Configure the swiftlint action in Fastlane to lint or autocorrect Swift code. Specify paths, reporters, and configuration files as needed. ```ruby swiftlint( mode: :lint, # SwiftLint mode: :lint (default) or :autocorrect executable: "Pods/SwiftLint/swiftlint", # The SwiftLint binary path (optional). Important if you've installed it via CocoaPods path: "/path/to/lint", # Specify path to lint (optional) output_file: "swiftlint.result.json", # The path of the output file (optional) reporter: "json", # The custom reporter to use (optional) config_file: ".swiftlint-ci.yml", # The path of the configuration file (optional) files: [ # List of files to process (optional) "AppDelegate.swift", "path/to/project/Model.swift" ], ignore_exit_status: true, # Allow fastlane to continue even if SwiftLint returns a non-zero exit status (Default: false) quiet: true, # Don't print status logs like 'Linting ' & 'Done linting' (Default: false) strict: true # Fail on warnings? (Default: false) ) ``` -------------------------------- ### Configure SwiftLint pre-commit Hook with Fixes and Strictness Source: https://github.com/realm/swiftlint/blob/main/README.md Configure the SwiftLint pre-commit hook to automatically fix issues and fail on strict violations. ```yaml - repo: https://github.com/realm/SwiftLint rev: 0.57.1 hooks: - id: swiftlint entry: swiftlint --fix --strict ``` -------------------------------- ### Swift Package Manager Target Plugin Source: https://github.com/realm/swiftlint/blob/main/README.md Add the SwiftLintBuildToolPlugin to your target's plugins to enable linting during builds. This is for Swift Package projects. ```swift .target( ... plugins: [.plugin(name: "SwiftLintBuildToolPlugin", package: "SwiftLintPlugins")] ), ``` -------------------------------- ### Run SwiftLint within Docker Source: https://github.com/realm/swiftlint/blob/main/README.md Execute SwiftLint commands inside a Docker container, mounting the current directory to persist changes and output. This command ensures SwiftLint runs in the context of your project's directory. ```bash docker run -it -v `pwd`:`pwd` -w `pwd` ghcr.io/realm/swiftlint:latest ``` -------------------------------- ### Swift Package Manager - Specific Version Source: https://github.com/realm/swiftlint/blob/main/README.md Add SwiftLint as a package dependency and pin it to a specific version. ```swift .package(url: "https://github.com/SimplyDanny/SwiftLintPlugins", exact: "") ``` -------------------------------- ### Set Swift Toolchain for SwiftLint Source: https://github.com/realm/swiftlint/blob/main/README.md Override SwiftLint's default Swift toolchain by setting the TOOLCHAINS environment variable. ```shell TOOLCHAINS=com.apple.dt.toolchain.Swift_2_3 swiftlint --fix ``` -------------------------------- ### SwiftLint Run Script with Auto-Fix Source: https://github.com/realm/swiftlint/blob/main/README.md This script attempts to fix correctable SwiftLint violations before running the linter again, ensuring warnings are shown for remaining issues. ```bash swiftlint --fix && swiftlint ``` -------------------------------- ### SwiftLint Severity Configuration Source: https://github.com/realm/swiftlint/blob/main/CONTRIBUTING.md Default severity configuration for a SwiftLint rule. This sets the rule's severity to warning. ```swift var configuration = SeverityConfiguration(.warning) ``` -------------------------------- ### Disabling Xcode Build Plugin Validation (defaults) Source: https://github.com/realm/swiftlint/blob/main/README.md Set these Xcode defaults to disable package plugin and macro validation, useful for unattended builds. ```bash defaults write com.apple.dt.Xcode IDESkipPackagePluginFingerprintValidatation -bool YES defaults write com.apple.dt.Xcode IDESkipMacroFingerprintValidation -bool YES ``` -------------------------------- ### Control Rule Disabling Scope with :next, :this, :previous Source: https://github.com/realm/swiftlint/blob/main/README.md Modify SwiftLint disable/enable commands to apply to the next, current, or previous line. ```swift // swiftlint:disable:next force_cast let noWarning = NSNumber() as! Int let hasWarning = NSNumber() as! Int let noWarning2 = NSNumber() as! Int // swiftlint:disable:this force_cast let noWarning3 = NSNumber() as! Int // swiftlint:disable:previous force_cast ``` -------------------------------- ### Disabling Xcode Build Plugin Validation (xcodebuild) Source: https://github.com/realm/swiftlint/blob/main/README.md Use these xcodebuild options to bypass package plugin and macro validation for unattended builds on CI. ```bash -skipPackagePluginValidation -skipMacroValidation ``` -------------------------------- ### Disable All Rules in Swift Code Source: https://github.com/realm/swiftlint/blob/main/README.md Disable all SwiftLint rules for a section of code using 'all'. Rules are re-enabled at the end of the file or by an enable comment. ```swift // swiftlint:disable all let noWarning :String = "" // No warning about colons immediately after variable names. let i = "" // Also no warning about short identifier names. // swiftlint:enable all let hasWarning :String = "" // Warning generated about colons immediately after variable names. let y = "" // Warning generated about short identifier names. ``` -------------------------------- ### Disable Specific Rules in Swift Code Source: https://github.com/realm/swiftlint/blob/main/README.md Temporarily disable specified rules within a Swift file using a comment. Rules are re-enabled at the end of the file or by an enable comment. ```swift // swiftlint:disable colon let noWarning :String = "" // No warning about colons immediately after variable names. // swiftlint:enable colon let hasWarning :String = "" // Warning generated about colons immediately after variable names. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.