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)
|
[SimplyDanny](https://github.com/SimplyDanny)
|
||||||
[#4860](https://github.com/realm/SwiftLint/issues/4860)
|
[#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
|
## 0.51.0: bzllint
|
||||||
|
|
||||||
#### Breaking
|
#### Breaking
|
||||||
|
|
|
@ -172,10 +172,21 @@ struct IndentationWidthRule: ConfigurationProviderRule, OptInRule {
|
||||||
if configuration.includeMultilineStrings {
|
if configuration.includeMultilineStrings {
|
||||||
return false
|
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
|
||||||
}
|
}
|
||||||
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.
|
/// Validates whether the indentation of a specific line is valid based on the indentation of the previous line.
|
||||||
|
|
|
@ -196,18 +196,57 @@ class IndentationWidthRuleTests: XCTestCase {
|
||||||
""", includeCompilerDirectives: true)
|
""", includeCompilerDirectives: true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testIgnoredMultilineStrings() {
|
func testIncludeMultilineStrings() {
|
||||||
assertNoViolation(
|
let example0 = #"""
|
||||||
in: "let x = \"\"\"\nstring1\n string2\n string3\n\"\"\"\n",
|
let x = """
|
||||||
includeMultilineStrings: false
|
string1
|
||||||
)
|
string2
|
||||||
assert1Violation(
|
string3
|
||||||
in: "let x = \"\"\"\nstring1\n string2\n string3\n\"\"\"\n"
|
"""
|
||||||
)
|
"""#
|
||||||
assertViolations(
|
assertNoViolation(in: example0, includeMultilineStrings: false)
|
||||||
in: "let x = \"\"\"\nstring1\n string2\n string3\n string4\n\"\"\"\n",
|
assert1Violation(in: example0, includeMultilineStrings: true)
|
||||||
equals: 2
|
|
||||||
)
|
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
|
// MARK: Helpers
|
||||||
|
|
Loading…
Reference in New Issue