【问题标题】:SwiftLint: Exclude file for specific ruleSwiftLint:排除特定规则的文件
【发布时间】:2017-02-01 14:38:57
【问题描述】:

我试图在我的 .swiftlint.yml 文件中做这样的事情:

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

我有这段代码,但我不喜欢我收到的 force_try 警告:

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

我想通过从规则中排除这个文件来禁止这个文件的警告。

有没有办法做到这一点?

【问题讨论】:

    标签: swiftlint


    【解决方案1】:

    好吧,如果您不想将某些特定规则应用于特定文件,您可以使用@Benno Kress 提到的技术。为此,您需要在 swift 文件中添加注释,如下所示。

    规则将被禁用,直到文件结束或 linter 看到匹配的启用注释:

    // swiftlint:disable <rule1> 
    
       YOUR CODE WHERE NO rule1 is applied
    
    // swiftlint:enable <rule1>
    

    也可以通过配置 swiftlint 来跳过一些文件。 在运行 SwiftLint 的目录中添加一个“.swiftlint.yml”文件。

    添加以下内容以排除某些文件。让我们说file1,file2 ...等

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

    要完全禁用某些规则,请将以下内容添加到同一个“.swiftlint.yml”文件中。

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

    有关更多信息,请参阅以下链接。

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

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

    【讨论】:

      【解决方案2】:

      您可以在文件的开头写上// swiftlint:disable force_cast,在该文件中您要取消此规则的警告。在文件末尾或添加// swiftlint:enable force_cast 行之前,它会被禁用。

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

      【讨论】:

        【解决方案3】:

        我只是摆脱了 force_cast

        第 1 步:

        cd path-to-your-project
        

        第 2 步:

        touch .swiftlint.yml
        

        第 3 步: open .swiftlint.yml 并添加规则

        disabled_rules: # rule identifiers to exclude from running
         - force_cast
        

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

        【讨论】:

        • 如何打开 .swiftlint.yml ?
        • @RajeshKumarR 您可以使用 Subline 文本编辑器打开它。
        • 这是一个隐藏文件,所以运行open .swiftlint.yml
        • @Andrew 是的。或者如果 finder 有显示隐藏文件设置,那么直接双击也可以。
        【解决方案4】:

        通过从运行 SwiftLint 的目录中添加 .swiftlint.yml 文件来配置 SwiftLint。这是您可以在 .swiftlint.yml 文件中使用的完整选项集

        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)
        

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

        【讨论】:

          【解决方案5】:

          也许这是一个更好的方法:

          guard let cell = tableView.dequeueReusableCellWithIdentifier(Constants.dogViewCellReuseIdentifier,
                                                                         forIndexPath: indexPath) as? DogViewCell else {
          return UITableviewCell()
          }
          

          【讨论】:

            【解决方案6】:

            您可以将新的.swiftlint.yml 文件添加到排除的文件夹并覆盖那里的规则: project_root/.swiftlint.yml:

            opt_in_rules:
              force_unwrapping
            .. and other rules
            

            project_root/your_excluded_folder/.swiftlint.yml

            disabled_rules:
              - force_unwrapping
            

            那么force_unwrapping 将不会应用于your_excluded_folder 和所有子文件夹

            子文件夹中的配置文件将“覆盖”根配置文件中的规则。

            这对于单元测试文件夹很有用

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2020-07-04
              • 2022-06-10
              • 1970-01-01
              • 1970-01-01
              • 2017-01-07
              • 2021-02-09
              • 2020-12-01
              • 2016-07-22
              相关资源
              最近更新 更多