【问题标题】:Swiftlint Autocorrect Does Not Modify FileSwiftlint 自动更正不会修改文件
【发布时间】:2018-06-19 17:27:36
【问题描述】:

在运行swiftlint 0.24.0 版本时,报错。运行autocorrect 表明文件已更正。然而,正如swiftlint 的后续运行所证明的那样,没有进行任何修改。

查看 lint 和更正两个文件 Player.swiftPrize.swift 的尝试。

MacBook-Pro-5: Developer$ swiftlint 
Loading configuration from '.swiftlint.yml'
Linting Swift files in current working directory
Linting 'Prize.swift' (1/2)
Linting 'Player.swift' (2/2)
Player.swift:26:19: warning: Operator Function Whitespace Violation: Operators should be surrounded by a single whitespace when defining them. (operator_whitespace)
Player.swift:27: warning: Line Length Violation: Line should be 120 characters or less: currently 131 characters (line_length)
Player.swift:39: warning: Line Length Violation: Line should be 120 characters or less: currently 147 characters (line_length)
Prize.swift:44:34: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
Prize.swift:44: error: Line Length Violation: Line should be 120 characters or less: currently 210 characters (line_length)
Done linting! Found 5 violations, 1 serious in 2 files.
MacBook-Pro-5: Developer$ swiftlint autocorrect
Loading configuration from '.swiftlint.yml'
Correcting Swift files in current working directory
Correcting 'Prize.swift' (1/2)
Correcting 'Player.swift' (2/2)
Done correcting 2 files!
MacBook-Pro-5: Developer$ swiftlint 
Loading configuration from '.swiftlint.yml'
Linting Swift files in current working directory
Linting 'Prize.swift' (1/2)
Linting 'Player.swift' (2/2)
Prize.swift:44:34: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
Prize.swift:44: error: Line Length Violation: Line should be 120 characters or less: currently 210 characters (line_length)
Player.swift:26:19: warning: Operator Function Whitespace Violation: Operators should be surrounded by a single whitespace when defining them. (operator_whitespace)
Player.swift:27: warning: Line Length Violation: Line should be 120 characters or less: currently 131 characters (line_length)
Player.swift:39: warning: Line Length Violation: Line should be 120 characters or less: currently 147 characters (line_length)
Done linting! Found 5 violations, 1 serious in 2 files.
MacBook-Pro-5: Developer$

这是另一次运行,但这次只有一个文件 Prize.swift 包含一个可纠正的违规行为。同样,autocorrect 不会修改:

MacBook-Pro-5:P Developer$ swiftlint 
Linting Swift files in current working directory
Linting 'Prize.swift' (1/2)
Linting 'Player.swift' (2/2)
PPrize.swift:44:34: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
Done linting! Found 1 violation, 0 serious in 2 files.
MacBook-Pro-5:P Developer$ swiftlint autocorrect
Correcting Swift files in current working directory
Correcting 'Prize.swift' (1/2)
Correcting 'Player.swift' (2/2)
Done correcting 2 files!
MacBook-Pro-5:P Developer$ swiftlint 
Linting Swift files in current working directory
Linting 'Prize.swift' (1/2)
Linting 'Player.swift' (2/2)
PPrize.swift:44:34: warning: Colon Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)
Done linting! Found 1 violation, 0 serious in 2 files.
MacBook-Pro-5:P Developer$ 

请注意,.swiftlint.yml 文件的存在与否对此没有任何影响。

缩写-即仅显示相关规则-swiftlint rules 的输出如下所示。请注意,在这三个违规行为中,colon 绝对是可以纠正的。

MacBook-Pro-5: Developer$ swiftlint rules
+------------------------------------------+--------+-------------+------------------------+-------------+---------------------------------------------------------------------------------------------------------------------------+
| identifier                               | opt-in | correctable | enabled in your config | kind        | configuration                                                                                                             |
+------------------------------------------+--------+-------------+------------------------+-------------+---------------------------------------------------------------------------------------------------------------------------+
| colon                                    | no     | yes         | yes                    | style       | warning, flexible_right_spacing: false, apply_to_dictionaries: true                                                       |
| line_length                              | no     | no          | yes                    | metrics     | warning: 120, error: 200, ignores urls: false, ignores function declarations: false, ignores comments: false              |
| operator_whitespace                      | no     | no          | yes                    | style       | warning                                                                                                                   |
+------------------------------------------+--------+-------------+------------------------+-------------+---------------------------------------------------------------------------------------------------------------------------+

关于swiftlint autocorrect如何成功运行有什么想法吗?我可能缺少一些明显的配置或参数吗?我已咨询https://github.com/realm/SwiftLint 无济于事。

【问题讨论】:

    标签: swift macos swiftlint


    【解决方案1】:

    并非所有 SwiftLint 规则都是可纠正的,这意味着在您运行 swiftlint autocorrect 后它们仍将保留。如果你运行swiftlint rules,你可以看到一个表格——除其他外——显示哪些规则是可纠正的。

    我不确定您使用的是哪个版本的 SwiftLint,但我认为 line_lengthoperator_whitespace 是不可纠正的。您将不得不自己处理这些问题。

    【讨论】:

    • 为问题添加了详细信息。我想知道 - 例如 - 一个文件是否有三个违规行为,其中只有一个是可纠正的 - 自动更正只是忽略该文件。有时自动更正似乎确实有效。请注意,该问题是一个简短的示例。我一直在针对具有数百个文件和可纠正违规行为的工作区运行自动更正。比如说 620 个文件,自动更正可能会修复 4 个。我不知道为什么,也不知道为什么要更正这些特定文件。感觉好像缺少了一些明显的东西。就是不知道是什么。
    • 这可能是最新版本中的错误。我目前使用的是 0.23,它工作正常。有时它无法解决特定情况,但可以解决其他所有问题
    猜你喜欢
    • 2018-12-30
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 2014-08-16
    • 2020-09-06
    • 1970-01-01
    • 2020-04-28
    • 2014-09-24
    相关资源
    最近更新 更多