How do I create .swiftlint.yml file & where I need to put it?

I want to use Swiftlint in my Swift project. I followed the Realm instruction and installed Swiftlint by brew install swiftlint. Further I face the problem to create .swiftlint.yml file.

So please suggest me how I proceed?

Upvotes: 46

Views: 30275

Answers (5)

Jogendra.Com
Jogendra.Com

Reputation: 6454

Create this file in your project main directory and name should .swiftlint.yml

File example

disabled_rules: # rule identifiers to exclude from running
  - colon
  - comma
  - control_statement
  - identifier_name #rule for checking variable conditions (Upper case , lower case , underscore )
  - force_cast
  - shorthand_operator

cyclomatic_complexity:
  warning: 25 # two nested ifs are acceptable
  error: 50   # six nested ifs shows warning, 6 causes compile error


opt_in_rules: # some rules are only opt-in
  # - empty_count
  # Find all the available rules by running:
  # swiftlint rules

#included: # paths to include during linting. `--path` is ignored if present.
#  - Source

excluded: # paths to ignore during linting. Takes precedence over `included`.
  - Carthage
  - Pods
  - AppFolder\ App/Class/*
 # - AppFolder\ App/ViewController/* //Enabled for this

analyzer_rules: # Rules run by `swiftlint analyze` (experimental)
  - explicit_self

# configurable rules can be customised from this configuration file
# binary rules can set 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: 200
# they can set both implicitly with an array

type_body_length:
  - 300 # warning
  - 600 # error
# or they can set both explicitly

file_length:
  warning: 500
  error: 2500

function_body_length:
  - 200 #warning
  - 300 #error

# naming rules can set warnings/errors for min_length and max_length
# additionally they can set excluded names

type_name:
  min_length: 4 # only warning
  max_length: # warning and error
    warning: 40
    error: 50
  excluded: iPhone # excluded via string
  allowed_symbols: ["_"] # these are allowed in type names
identifier_name:
  min_length: # only min_length
    error: 4 # only error
  excluded: # excluded via string array
    - id
    - URL
    - GlobalAPIKey

identifier_name:
#  allowed_symbols: "_"
  max_length:
    warning: 45
    error: 60
  min_length:
    warning: 1


reporter: "xcode" # reporter type (xcode, json, csv, checkstyle, junit, html, emoji, sonarqube, markdown)

Upvotes: 9

Shahbaz Akram
Shahbaz Akram

Reputation: 1657

I install swift lint via cocoapods and then add new file with name .swiftlint.yml in root project directory. For more detail visit youtube link.
I found this video helpful: https://www.youtube.com/watch?v=cEA9BDVbjfI OR https://www.youtube.com/watch?v=3MAlqOVIAwI

Upvotes: 5

Lehlohonolo_Isaac
Lehlohonolo_Isaac

Reputation: 1504

If you are using the terminal:

    cd your_project_directory
    touch .swiftlint.yml

Upvotes: 50

Greg Robertson
Greg Robertson

Reputation: 2347

I found this video helpful: https://www.youtube.com/watch?v=3MAlqOVIAwI

You can create a .swiftlint.yml in XCode and save it in your project directory. Just select File -> New -> File -> Empty

Upvotes: 13

Raphael
Raphael

Reputation: 10589

I suggest you read the documentation:

Configure SwiftLint by adding a .swiftlint.yml file from the directory you'll run SwiftLint from.

I'd suggest your project or source root.

An extensive example file follows; start from there.

As for the available rules and defaults, there does not seem to be good documentation besides running

swiftlint rules > swiftlint_rules.txt

and have very wide screen.

Upvotes: -6

Related Questions