-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Allow prefer_key_paths
to ignore identity closures ({ $0 }
)
#6068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Allow prefer_key_paths
to ignore identity closures ({ $0 }
)
#6068
Conversation
Add new `ignore_identity_closures` parameter to `prefer_key_paths` rule to skip conversion of identity closures (`{ $0 }`) to identity key paths (`\self`). Add a small note to the rule description stating that identity key path conversion is Swift 6+ only.
Generated by 🚫 Danger |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good in general. Thanks!
Please see my comments and rebase your branch to make all tests run (there are relevant fixes on main
).
* None. | ||
* Add new `ignore_identity_closures` parameter to `prefer_key_paths` rule to skip conversion of identity closures | ||
(`{ $0 }`) to identity key paths (`\self`). | ||
Add a small note to the rule description stating that identity key path conversion is Swift 6+ only. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's have the note here as well instead of a reference to the note:
Add a small note to the rule description stating that identity key path conversion is Swift 6+ only. | |
Note that identity key paths are only support from Swift 6 on, hence this option | |
will be implicitly ignored/set to `true` when SwiftLint detects a Swift <6 compiler | |
to avoid causing compilation errors. |
* Add new `ignore_identity_closures` parameter to `prefer_key_paths` rule to skip conversion of identity closures | ||
(`{ $0 }`) to identity key paths (`\self`). | ||
Add a small note to the rule description stating that identity key path conversion is Swift 6+ only. | ||
[p4checo](https://github.com/p4checo) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please reference the issue here as well.
private static let extendedModeAndIgnoreIdentity = [ | ||
"restrict_to_standard_functions": false, | ||
"ignore_identity_closures": true, | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should work, shouldn't it?
private static let extendedModeAndIgnoreIdentity = [ | |
"restrict_to_standard_functions": false, | |
"ignore_identity_closures": true, | |
] | |
private static let extendedModeAndIgnoreIdentity = extendedMode + ignoreIdentity |
|
||
static let description = RuleDescription( | ||
identifier: "prefer_key_path", | ||
name: "Prefer Key Path", | ||
description: "Use a key path argument instead of a closure with property access", | ||
description: """ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep the existing description as it's currently also used as the violation message and add a rational
property instead to explain this detail.
@@ -12,6 +17,8 @@ final class PreferKeyPathRuleTests: SwiftLintTestCase { | |||
.with(nonTriggeringExamples: [ | |||
Example("f.filter { a in b }"), | |||
Example("f.g { $1 }", configuration: Self.extendedMode), | |||
Example("f { $0 }", configuration: Self.extendedModeAndIgnoreIdentity), | |||
Example("f.map { $0 }", configuration: Self.ignoreIdentity), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also add a correction
example to proof that nothing is changed with the option enabled.
@@ -25,6 +35,8 @@ struct PreferKeyPathRule: Rule { | |||
Example("f { (a, b) in a.b }", configuration: extendedMode), | |||
Example("f { $0.a } g: { $0.b }", configuration: extendedMode), | |||
Example("[1, 2, 3].reduce(1) { $0 + $1 }", configuration: extendedMode), | |||
Example("f { $0 }", configuration: extendedModeAndIgnoreIdentity), | |||
Example("f.map { $0 }", configuration: ignoreIdentity), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also add a correction
example.
Add new
ignore_identity_closures
parameter toprefer_key_paths
rule to skip conversion of identity closures ({ $0 }
) to identity key paths (\self
).Add a small note to the rule description stating that identity key path conversion is Swift 6+ only.