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

Fix false positive on operator_usage_whitespace #1168

Merged
merged 1 commit into from
Jan 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
[Marcelo Fabri](https://github.com/marcelofabri)
[#1159](https://github.com/realm/SwiftLint/issues/1159)

* Fix false positive on `operator_usage_whitespace` rule with decimal
literals in exponent format.
[Marcelo Fabri](https://github.com/marcelofabri)
[#1153](https://github.com/realm/SwiftLint/issues/1153)

## 0.16.0: Maximum Energy Efficiency Setting

##### Breaking
Expand Down
38 changes: 26 additions & 12 deletions Source/SwiftLintFramework/Rules/OperatorUsageWhitespaceRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public struct OperatorUsageWhitespaceRule: OptInRule, CorrectableRule, Configura
"#if swift(>=3.0)\n",
"array.removeAtIndex(-200)\n",
"let name = \"image-1\"\n",
"button.setImage(#imageLiteral(resourceName: \"image-1\"), for: .normal)\n"
"button.setImage(#imageLiteral(resourceName: \"image-1\"), for: .normal)\n",
"let doubleValue = -9e-11\n"
],
triggeringExamples: [
"let foo = 1↓+2\n",
Expand Down Expand Up @@ -107,36 +108,49 @@ public struct OperatorUsageWhitespaceRule: OptInRule, CorrectableRule, Configura
zeroSpaces + trailingVariableOrNumber
let excludingPattern = "(?:\(genericPattern)|\(validRangePattern))"

let kinds = SyntaxKind.commentAndStringKinds() + [.objectLiteral]
let excludingKinds = SyntaxKind.commentAndStringKinds() + [.objectLiteral]

return file.match(pattern: pattern, excludingSyntaxKinds: kinds,
excludingPattern: excludingPattern).flatMap {
return file.match(pattern: pattern, excludingSyntaxKinds: excludingKinds,
excludingPattern: excludingPattern).flatMap { range in

// if it's only a number (i.e. -9e-11), it shouldn't trigger
guard kinds(in: range, file: file) != [.number] else {
return nil
}

let spacesPattern = oneSpace + "*"
let rangeRegex = regex(spacesPattern + rangePattern + spacesPattern)

// if it's a range operator, the correction shouldn't have spaces
if let range = rangeRegex.firstMatch(in: file.contents,
options: [], range: $0)?.range {
let correction = operatorInRange(file: file, range: range)
return (range, correction)
if let matchRange = rangeRegex.firstMatch(in: file.contents, options: [], range: range)?.range {
let correction = operatorInRange(file: file, range: matchRange)
return (matchRange, correction)
}

let pattern = spacesPattern + operators + spacesPattern
let operatorsRegex = regex(pattern)

guard let range = operatorsRegex.firstMatch(in: file.contents,
options: [], range: $0)?.range else {
guard let matchRange = operatorsRegex.firstMatch(in: file.contents,
options: [], range: range)?.range else {
return nil
}

let operatorContent = operatorInRange(file: file, range: range)
let operatorContent = operatorInRange(file: file, range: matchRange)
let correction = " " + operatorContent + " "

return (range, correction)
return (matchRange, correction)
}
}

private func kinds(in range: NSRange, file: File) -> [SyntaxKind] {
let contents = file.contents.bridge()
guard let byteRange = contents.NSRangeToByteRange(start: range.location, length: range.length) else {
return []
}

return file.syntaxMap.tokens(inByteRange: byteRange).flatMap { SyntaxKind(rawValue: $0.type) }
}

private func operatorInRange(file: File, range: NSRange) -> String {
return file.contents.bridge().substring(with: range).trimmingCharacters(in: .whitespaces)
}
Expand Down