Swiftlint 自动更正不修改文件

Max*_*eod 5 macos swift swiftlint

在运行swiftlint0.24.0版本时,报错。运行autocorrect表明文件已被更正。然而,在随后的证明运行中没有进行任何修改swiftlint

请参阅尝试检查并更正两个文件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$
Run Code Online (Sandbox Code Playgroud)

这是另一次运行,但这次一个文件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$ 
Run Code Online (Sandbox Code Playgroud)

请注意,文件是否存在.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                                                                                                                   |
+------------------------------------------+--------+-------------+------------------------+-------------+---------------------------------------------------------------------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)

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

Ati*_*dar 6

更新
swiftlint autocorrect --format
swiftlint --fix --format
swiftlint lint --fix --format

将为所有人工作。来源: https: //github.com/realm/SwiftLint/issues/3571


版本 如果swiftlint版本低于 0.42.0 那么你可以使用自动更正它swiftlint autocorrect --format

但如果它是 0.43.0 或更高,那么你需要使用swiftlint --fix

它会自动更正空格、尾随空格、初始格式、if else 中的括号。等等,这不会影响您的逻辑或代码。

swiftlint autocorrect --format//对于版本 < 0.42.0
swiftlint --fix//对于最新版本 >= 0.43.0


Dav*_*ist 2

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

\n\n

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

\n