Skip to content

Commit

Permalink
realm#3167: Fixes false positives for multiline_parameters_brackets a…
Browse files Browse the repository at this point in the history
…nd multiline_arguments_brackets
  • Loading branch information
noahsark769 committed Apr 5, 2020
1 parent 1e1807b commit a614d35
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,25 @@ public struct MultilineArgumentsBracketsRule: ASTRule, OptInRule, ConfigurationP
AlertViewModel.AlertAction(title: "some title", style: .default) {
AlertManager.shared.presentNextDebugAlert()
}
""")
"""),
Example("""
public final class Logger {
public static let shared = Logger(outputs: [
OSLoggerOutput(),
ErrorLoggerOutput()
])
}
"""),
Example("""
let errors = try self.download([
(description: description, priority: priority),
])
"""),
Example("""
return SignalProducer({ observer, _ in
observer.sendCompleted()
}).onMainQueue()
"""),
],
triggeringExamples: [
Example("""
Expand Down Expand Up @@ -86,8 +104,35 @@ public struct MultilineArgumentsBracketsRule: ASTRule, OptInRule, ConfigurationP
return []
}

let body = file.contents.substring(from: range.location, length: range.length)
let isMultiline = body.contains("\n")
func body(ofParameter parameter: SourceKittenDictionary) -> String? {
guard
let byteRange = parameter.byteRange,
let range = file.stringView.byteRangeToNSRange(byteRange)
else {
return nil
}

let body = file.stringView.nsString.substring(with: range)
return String(body)
}

let callBody = file.contents.substring(from: range.location, length: range.length)

let parameters = dictionary.substructure.filter {
[.argument, .array, .dictionary, .closure].contains($0.expressionKind)
}
let parameterBodies = parameters.compactMap { body(ofParameter: $0) }
let parameterNewlineCounts = parameterBodies.map { body in
return body.reduce(0, {
$1 == "\n" ? $0 + 1 : $0
})
}
let parameterNewlineCount = parameterNewlineCounts.reduce(0, { $0 + $1 })
let callNewlineCount = callBody.reduce(0, {
$1 == "\n" ? $0 + 1 : $0
})
let isMultiline = callNewlineCount > parameterNewlineCount

guard isMultiline else {
return []
}
Expand All @@ -96,11 +141,11 @@ public struct MultilineArgumentsBracketsRule: ASTRule, OptInRule, ConfigurationP
let expectedBodyEndRegex = regex("\\n[ \\t]*\\z")

var violatingByteOffsets = [ByteCount]()
if expectedBodyBeginRegex.firstMatch(in: body, options: [], range: body.fullNSRange) == nil {
if expectedBodyBeginRegex.firstMatch(in: callBody, options: [], range: callBody.fullNSRange) == nil {
violatingByteOffsets.append(bodyRange.location)
}

if expectedBodyEndRegex.firstMatch(in: body, options: [], range: body.fullNSRange) == nil {
if expectedBodyEndRegex.firstMatch(in: callBody, options: [], range: callBody.fullNSRange) == nil {
violatingByteOffsets.append(bodyRange.upperBound)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public struct MultilineParametersBracketsRule: OptInRule, ConfigurationProviderR
"""),
Example("""
func foo<T>(param1: T, param2: String, param3: String) -> T { /* some code */ }
"""),
Example("""
func foo(a: [Int] = [
1
])
""")
],
triggeringExamples: [
Expand Down Expand Up @@ -106,9 +111,31 @@ public struct MultilineParametersBracketsRule: OptInRule, ConfigurationProviderR
return []
}

let isMultiline = functionName.contains("\n")
func body(ofParameter parameter: SourceKittenDictionary) -> String? {
guard
let byteRange = parameter.byteRange,
let range = file.stringView.byteRangeToNSRange(byteRange)
else {
return nil
}

let body = file.stringView.nsString.substring(with: range)
return String(body)
}

let parameters = substructure.substructure.filter { $0.declarationKind == .varParameter }
let parameterBodies = parameters.compactMap { body(ofParameter: $0) }
let parameterNewlineCounts = parameterBodies.map { body in
return body.reduce(0, {
$1 == "\n" ? $0 + 1 : $0
})
}
let parameterNewlineCount = parameterNewlineCounts.reduce(0, { $0 + $1 })
let declarationNewlineCount = functionName.reduce(0, {
$1 == "\n" ? $0 + 1 : $0
})
let isMultiline = declarationNewlineCount > parameterNewlineCount

if isMultiline && !parameters.isEmpty {
if let openingBracketViolation = openingBracketViolation(parameters: parameters, file: file) {
violations.append(openingBracketViolation)
Expand Down

0 comments on commit a614d35

Please sign in to comment.