SwiftLint - DevOli/Marvel-chars GitHub Wiki

SwiftLint

A detail to setup SwiftLint over Xcode

  1. First add the line “pod 'SwiftLint' “ into pod File.

  2. On XCode go to project file in the left menu, the first file on directory tree. Then go to Build Phases

SwiftLint1

  1. Push the Add button and select New Run Script Phase

SwiftLint2

  1. Set the name in the Label and insert the script

SwiftLint3

  1. Leave the script as the last item of list and Build the app.

SwiftLint4

  1. SwiftLint give us the detail of warning and error to fix.

SwiftLint5

  1. On Xcode, select "Preferences -> Locations". Chances are that your Command Line Tools are not set. Select the suggested Xcode-tools location and you are done.

SwiftLint6

  1. Finally, include the .swiftlint.yml file on the root of the Xcode project. In the file is necessary define the directories and rules.

SwiftLint7

Pre-commit

SwiftLint can be run as a pre-commit hook. Once installed, add this to the “.pre-commit-config.yaml” in the root of your repository.

  1. For install pre-commit package manager is necessary homebrew. On terminal use this two commands

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

    git -C $(brew --repo homebrew/core) checkout master

  2. Then install pre-commit with the line

brew install pre-commit

  1. Create the file in the root of project and add the follow lines:

repos: - repo : https://github.com/realm/SwiftLint rev: v2.3.0 hooks: - id: swiftlint

Inside this file, add the lines defined for the hook, for example (extracted from Realm Repository):

	- id: swiftlint
	name: SwiftLint
	description: "Check Swift files for issues with SwiftLint"
	entry: "swiftlint --quiet"

language: swift types: [swift]

  1. Create the git hook pre-commit with the command in the root of project, now pre-commit will run automatically on git commit:

       pre-commit install
    
  2. Configure SwiftLint by adding a .swiftlint.yml file from the directory you'll run SwiftLint from. The following parameters can be configured:

Rule inclusion:

disabled_rules: Disable rules from the default enabled set.

opt_in_rules: Enable rules not from the default set.

only_rules: Only the rules specified in this list will be enabled. Cannot be specified alongside disabled_rules or opt_in_rules.

analyzer_rules: This is an entirely separate list of rules that are only run by the analyze command. All analyzer rules are opt-in, so this is the only configurable rule list, there are no equivalents for disabled_rules only_rules.

  1. Now is possible check swiftlint using the command:

    pre-commit run –all-files

Rules

Over 200 rules are included in SwiftLint and the Swift community (that's you!) continues to contribute more over time. You can find an updated list of rules and more information about them:

https://realm.github.io/SwiftLint/rule-directory.html

You can also check Source/SwiftLintFramework/Rules directory to see their implementation.

https://github.com/realm/SwiftLint/tree/master/Source/SwiftLintFramework/Rules

Opt-In Rules opt_in_rules are disabled by default (i.e., you have to explicitly enable them in your configuration file).

Guidelines on when to mark a rule as opt-in:

  • A rule that can have many false positives (e.g. empty_count)
  • A rule that is too slow
  • A rule that is not general consensus or is only useful in some cases (e.g. force_unwrapping)

Disable rules in code

Rules can be disabled with a comment inside a source file with the following format:

// swiftlint:disable [ ...]

The rules will be disabled until the end of the file or until the linter sees a matching enable comment:

// swiftlint:enable [ ...]

For example:

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

Including the all keyword will disable all rules until the linter sees a matching enable comment:

// swiftlint:disable all // swiftlint:enable all

For example:

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

⚠️ **GitHub.com Fallback** ⚠️