Swiftlint自动更正不会修改文件

时间:2018-01-10 14:05:26

标签: swift macos swiftlint

在运行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,但没有用。

1 个答案:

答案 0 :(得分:2)

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

我不确定您使用的是什么版本的SwiftLint,但我不认为line_lengthoperator_whitespace是可纠正的。你必须自己处理这些问题。