SwiftLint: Exclude file for specific rule

孤街浪徒 提交于 2019-12-20 11:04:14

问题


I'm trying to do something like this in my .swiftlint.yml file:

force_cast:
  severity: warning # explicitly
  excluded:
    - Dog.swift

I have this code and I don't like the force_try warning I'm getting for it:

let cell = tableView.dequeueReusableCellWithIdentifier(Constants.dogViewCellReuseIdentifier,
                                                               forIndexPath: indexPath) as! DogViewCell

I want to suppress the warning for this file by excluding this file from the rule.

Is there a way to do that ?


回答1:


Well, if you don't want some specific rules to be applied to a specific file you can use the technique mentioned by @Benno Kress. For that you need to add a comment in your swift file as given below.

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

// swiftlint:disable <rule1> 

   YOUR CODE WHERE NO rule1 is applied

// swiftlint:enable <rule1>

It is also possible to skip some files by configuring swiftlint. add a ".swiftlint.yml" file in the directory where you'll run SwiftLint.

Add the following content to exclude some files. Lets say file1, file2 ... etc

excluded: 
  - file1
  - file2
  - folder1
  - folder1/ExcludedFile.swift

To disable some rules completely add the following to the same ".swiftlint.yml" file.

disabled_rules: # rule identifiers to exclude from running
  - colon
  - comma
  - control_statement

for more information, refer the following links.

https://swifting.io/blog/2016/03/29/11-swiftlint/

https://github.com/realm/SwiftLint#disable-rules-in-code




回答2:


You can write // swiftlint:disable force_cast at the beginning of the file in which you want to supress the warning for this rule. It gets disabled until the end of the file or until you add the line // swiftlint:enable force_cast.

Source: https://github.com/realm/SwiftLint#disable-rules-in-code




回答3:


I just get rid with force_cast

Step 1:

cd path-to-your-project

Step 2:

touch .swiftlint.yml

Step 3: open .swiftlint.yml and add

disabled_rules: # rule identifiers to exclude from running
 - force_cast

Reference - https://github.com/realm/SwiftLint#disable-rules-in-code




回答4:


Configure SwiftLint by adding a .swiftlint.yml file from the directory you'll run SwiftLint from. Here is the complete set of options you can use in your .swiftlint.yaml file

disabled_rules: # rule identifiers to exclude from running
  - colon
  - comma
  - control_statement
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
  - Source/ExcludedFolder
  - Source/ExcludedFile.swift
  - Source/*/ExcludedFile.swift # Exclude files with a wildcard
analyzer_rules: # Rules run by `swiftlint analyze` (experimental)
  - explicit_self

# configurable rules can be customized 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: 110
# they can set both implicitly with an array
type_body_length:
  - 300 # warning
  - 400 # error
# or they can set both explicitly
file_length:
  warning: 500
  error: 1200
# 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
identifier_name:
  min_length: # only min_length
    error: 4 # only error
  excluded: # excluded via string array
    - id
    - URL
    - GlobalAPIKey
reporter: "xcode" # reporter type (xcode, json, csv, checkstyle, junit, html, emoji, sonarqube, markdown)

Reference: github.com/realm/SwiftLint#disable-rules-in-code



来源:https://stackoverflow.com/questions/39665790/swiftlint-exclude-file-for-specific-rule

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!