Fix false positives related to multiline strings in `indentation_width` rule (#4862)
This commit is contained in:
parent
bd444fcd77
commit
a2facce70c
|
@ -29,6 +29,9 @@
|
|||
[SimplyDanny](https://github.com/SimplyDanny)
|
||||
[#4860](https://github.com/realm/SwiftLint/issues/4860)
|
||||
|
||||
* Fix false positives in `indentation_width` rule.
|
||||
[Sven Münnich](https://github.com/svenmuennich)
|
||||
|
||||
## 0.51.0: bzllint
|
||||
|
||||
#### Breaking
|
||||
|
|
|
@ -172,12 +172,23 @@ struct IndentationWidthRule: ConfigurationProviderRule, OptInRule {
|
|||
if configuration.includeMultilineStrings {
|
||||
return false
|
||||
}
|
||||
if file.syntaxMap.tokens(inByteRange: line.byteRange).kinds == [.string] {
|
||||
return true
|
||||
}
|
||||
|
||||
// A multiline string content line is characterized by beginning with a token of kind string whose range's lower
|
||||
// bound is smaller than that of the line itself.
|
||||
let tokensInLine = file.syntaxMap.tokens(inByteRange: line.byteRange)
|
||||
guard
|
||||
let firstToken = tokensInLine.first,
|
||||
firstToken.kind == .string,
|
||||
firstToken.range.lowerBound < line.byteRange.lowerBound else {
|
||||
return false
|
||||
}
|
||||
|
||||
// Closing delimiters of a multiline string should follow the defined indentation. The Swift compiler requires
|
||||
// those delimiters to be on their own line so we need to consider the number of tokens as well as the upper
|
||||
// bounds.
|
||||
return tokensInLine.count > 1 || line.byteRange.upperBound < firstToken.range.upperBound
|
||||
}
|
||||
|
||||
/// Validates whether the indentation of a specific line is valid based on the indentation of the previous line.
|
||||
///
|
||||
/// - parameter indentation: The indentation of the line to validate.
|
||||
|
|
|
@ -196,18 +196,57 @@ class IndentationWidthRuleTests: XCTestCase {
|
|||
""", includeCompilerDirectives: true)
|
||||
}
|
||||
|
||||
func testIgnoredMultilineStrings() {
|
||||
assertNoViolation(
|
||||
in: "let x = \"\"\"\nstring1\n string2\n string3\n\"\"\"\n",
|
||||
includeMultilineStrings: false
|
||||
)
|
||||
assert1Violation(
|
||||
in: "let x = \"\"\"\nstring1\n string2\n string3\n\"\"\"\n"
|
||||
)
|
||||
assertViolations(
|
||||
in: "let x = \"\"\"\nstring1\n string2\n string3\n string4\n\"\"\"\n",
|
||||
equals: 2
|
||||
)
|
||||
func testIncludeMultilineStrings() {
|
||||
let example0 = #"""
|
||||
let x = """
|
||||
string1
|
||||
string2
|
||||
string3
|
||||
"""
|
||||
"""#
|
||||
assertNoViolation(in: example0, includeMultilineStrings: false)
|
||||
assert1Violation(in: example0, includeMultilineStrings: true)
|
||||
|
||||
let example1 = #"""
|
||||
let x = """
|
||||
string1
|
||||
string2
|
||||
string3
|
||||
string4
|
||||
"""
|
||||
"""#
|
||||
assertNoViolation(in: example1, includeMultilineStrings: false)
|
||||
assertViolations(in: example1, equals: 2, includeMultilineStrings: true)
|
||||
|
||||
let example2 = ##"""
|
||||
let x = #"""
|
||||
string1
|
||||
"""#
|
||||
"""##
|
||||
assert1Violation(in: example2, includeMultilineStrings: false)
|
||||
assert1Violation(in: example2, includeMultilineStrings: true)
|
||||
|
||||
let example3 = """
|
||||
let x = [
|
||||
"key": [
|
||||
["nestedKey": "string"],
|
||||
],
|
||||
]
|
||||
"""
|
||||
assertNoViolation(in: example3, includeMultilineStrings: false)
|
||||
assertNoViolation(in: example3, includeMultilineStrings: true)
|
||||
|
||||
let example4 = #"""
|
||||
func test() -> String {
|
||||
"""
|
||||
▿ Type:
|
||||
- property: \(123) + \(456)
|
||||
\(true)
|
||||
"""
|
||||
}
|
||||
"""#
|
||||
assertNoViolation(in: example4, includeMultilineStrings: false)
|
||||
assert1Violation(in: example4, includeMultilineStrings: true)
|
||||
}
|
||||
|
||||
// MARK: Helpers
|
||||
|
|
Loading…
Reference in New Issue