Skip to content
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

Identifier name #5140

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ attributes:
identifier_name:
excluded:
- id
previous_function_behavior: true
large_tuple: 3
number_separator:
minimum_length: 5
Expand Down
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

#### Breaking

* None.
* `identifier_name` now evaluates function names for both length and non
alphanumeric violations. Set the option
`previous_function_behavior: true` in your config file to revert to older
behavior

#### Experimental

Expand All @@ -19,6 +22,11 @@
* Rewrite `control_statement` rule using SwiftSyntax.
[SimplyDanny](https://github.com/SimplyDanny)

* `identifier_name` now has the option to ignore minimum variable length
within short closures.
[mredig](https://github.com/mredig)
[5140](https://github.com/realm/SwiftLint/pull/5140)

#### Bug Fixes

* Fix false positive in `control_statement` rule that triggered on conditions
Expand Down Expand Up @@ -52,6 +60,11 @@
[Martin Redington](https://github.com/mildm8nnered)
[#5120](https://github.com/realm/SwiftLint/issues/5120)

* `identifier_name` behavior corrected for accuracy to its description. See the
breaking change for more info
[mredig](https://github.com/mredig)
[5140](https://github.com/realm/SwiftLint/pull/5140)

## 0.52.4: Lid Switch

#### Breaking
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ with Swift Package Manager on Linux. When contributing code changes, please
ensure that all three supported build methods continue to work and pass tests.

```shell
$ xcodebuild -scheme swiftlint test
$ swift test
$ xcodebuild -scheme swiftlint clean test -destination 'platform=macOS' | xcpretty
$ swift test | xcpretty
$ make docker_test
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ struct NameConfiguration<Parent: Rule>: RuleConfiguration, Equatable {
private(set) var unallowedSymbolsSeverity = Severity.error
@ConfigurationElement(key: "validates_start_with_lowercase")
private(set) var validatesStartWithLowercase = StartWithLowercaseConfiguration.error
/// Only valid for `identifier_name`
@ConfigurationElement(key: "ignore_min_length_for_short_closure_content")
private(set) var ignoreMinLengthForShortClosureContent = false
/// Only valid for `identifier_name`
/// Before this update, the code skipped over functions in evaluating both their length and their special
/// characters. The code read `if !SwiftDeclarationKind.functionKinds.contains(kind) {` which is incredibly hard
/// to follow double negative logic, plus the description for the rule made no indication that functions were
/// excluded from this rule. And given the history of this rule as previously being variables only, but refactored
/// to the generic concept of identifiers (which should include function *identifiers*, this leads me to believe it
/// was unintentional. Hence, I'm defaulting to `false`.
@ConfigurationElement(key: "previous_function_behavior")
private(set) var previousFunctionBehavior = false

var minLengthThreshold: Int {
return max(minLength.warning, minLength.error ?? minLength.warning)
Expand All @@ -38,7 +50,9 @@ struct NameConfiguration<Parent: Rule>: RuleConfiguration, Equatable {
excluded: [String] = [],
allowedSymbols: [String] = [],
unallowedSymbolsSeverity: Severity = .error,
validatesStartWithLowercase: StartWithLowercaseConfiguration = .error) {
validatesStartWithLowercase: StartWithLowercaseConfiguration = .error,
ignoreMinLengthForShortClosureContent: Bool = false,
previousFunctionBehavior: Bool = false) {
minLength = SeverityLevels(warning: minLengthWarning, error: minLengthError)
maxLength = SeverityLevels(warning: maxLengthWarning, error: maxLengthError)
self.excludedRegularExpressions = Set(excluded.compactMap {
Expand All @@ -47,6 +61,8 @@ struct NameConfiguration<Parent: Rule>: RuleConfiguration, Equatable {
self.allowedSymbols = Set(allowedSymbols)
self.unallowedSymbolsSeverity = unallowedSymbolsSeverity
self.validatesStartWithLowercase = validatesStartWithLowercase
self.ignoreMinLengthForShortClosureContent = ignoreMinLengthForShortClosureContent
self.previousFunctionBehavior = previousFunctionBehavior
}

mutating func apply(configuration: Any) throws {
Expand Down Expand Up @@ -84,6 +100,12 @@ struct NameConfiguration<Parent: Rule>: RuleConfiguration, Equatable {
"""
).print()
}
if let ignoreMinLengthForClosures = configurationDict[$ignoreMinLengthForShortClosureContent] as? Bool {
self.ignoreMinLengthForShortClosureContent = ignoreMinLengthForClosures
}
if let previousFunctionBehavior = configurationDict[$previousFunctionBehavior] as? Bool {
self.previousFunctionBehavior = previousFunctionBehavior
}
}
}

Expand Down