Skip to content

Commit e2c5240

Browse files
authored
Merge pull request #214 from dylansturg/no_trivia_diagnostics
Trim trivia from node descriptions used in diagnostics.
2 parents 2825f93 + 8c7b0b7 commit e2c5240

8 files changed

+28
-8
lines changed

Sources/SwiftFormatRules/NeverForceUnwrap.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public final class NeverForceUnwrap: SyntaxLintRule {
2626

2727
public override func visit(_ node: ForcedValueExprSyntax) -> SyntaxVisitorContinueKind {
2828
guard context.importsXCTest == .doesNotImportXCTest else { return .skipChildren }
29-
diagnose(.doNotForceUnwrap(name: node.expression.description), on: node)
29+
diagnose(.doNotForceUnwrap(name: node.expression.withoutTrivia().description), on: node)
3030
return .skipChildren
3131
}
3232

@@ -36,7 +36,7 @@ public final class NeverForceUnwrap: SyntaxLintRule {
3636
guard context.importsXCTest == .doesNotImportXCTest else { return .skipChildren }
3737
guard let questionOrExclamation = node.questionOrExclamationMark else { return .skipChildren }
3838
guard questionOrExclamation.tokenKind == .exclamationMark else { return .skipChildren }
39-
diagnose(.doNotForceCast(name: node.typeName.description), on: node)
39+
diagnose(.doNotForceCast(name: node.typeName.withoutTrivia().description), on: node)
4040
return .skipChildren
4141
}
4242
}

Sources/SwiftFormatRules/NeverUseImplicitlyUnwrappedOptionals.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ public final class NeverUseImplicitlyUnwrappedOptionals: SyntaxLintRule {
5151

5252
private func diagnoseImplicitWrapViolation(_ type: TypeSyntax) {
5353
guard let violation = type.as(ImplicitlyUnwrappedOptionalTypeSyntax.self) else { return }
54-
diagnose(.doNotUseImplicitUnwrapping(identifier: "\(violation.wrappedType)"), on: type)
54+
diagnose(
55+
.doNotUseImplicitUnwrapping(
56+
identifier: violation.wrappedType.withoutTrivia().description), on: type)
5557
}
5658
}
5759

Sources/SwiftFormatRules/NoCasesWithOnlyFallthrough.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public final class NoCasesWithOnlyFallthrough: SyntaxFormatRule {
4545

4646
if isFallthroughOnly(switchCase), let label = switchCase.label.as(SwitchCaseLabelSyntax.self) {
4747
// If the case is fallthrough-only, store it as a violation that we will merge later.
48-
diagnose(.collapseCase(name: "\(label)"), on: switchCase)
48+
diagnose(
49+
.collapseCase(name: label.caseItems.withoutTrivia().description), on: switchCase)
4950
fallthroughOnlyCases.append(switchCase)
5051
} else {
5152
guard !fallthroughOnlyCases.isEmpty else {

Sources/SwiftFormatRules/NoLabelsInCasePatterns.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public final class NoLabelsInCasePatterns: SyntaxFormatRule {
5050
}
5151

5252
// Remove label if it's the same as the value identifier
53-
let name = valueBinding.valuePattern.description.trimmingCharacters(in: .whitespaces)
53+
let name = valueBinding.valuePattern.withoutTrivia().description
5454
guard name == label.text else {
5555
newArgs.append(argument)
5656
continue

Tests/SwiftFormatRulesTests/NeverForceUnwrapTests.swift

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ final class NeverForceUnwrapTests: LintOrFormatRuleTestCase {
1010
let c = (someValue())!
1111
let d = String(a)!
1212
let regex = try! NSRegularExpression(pattern: "a*b+c?")
13+
let e = /*comment about stuff*/ [1: a, 2: b, 3: c][4]!
14+
var f = a as! /*comment about this type*/ FooBarType
1315
return a!
1416
}
1517
"""
@@ -20,6 +22,8 @@ final class NeverForceUnwrapTests: LintOrFormatRuleTestCase {
2022
XCTAssertNotDiagnosed(.doNotForceCast(name: "try"))
2123
XCTAssertNotDiagnosed(.doNotForceUnwrap(name: "try"))
2224
XCTAssertDiagnosed(.doNotForceUnwrap(name: "a"))
25+
XCTAssertDiagnosed(.doNotForceUnwrap(name: "[1: a, 2: b, 3: c][4]"))
26+
XCTAssertDiagnosed(.doNotForceCast(name: "FooBarType"))
2327
}
2428

2529
func testIgnoreTestCode() {

Tests/SwiftFormatRulesTests/NeverUseImplicitlyUnwrappedOptionalsTests.swift

+2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ final class NeverUseImplicitlyUnwrappedOptionalsTests: LintOrFormatRuleTestCase
1010
1111
var foo: Int?
1212
var s: String!
13+
var f: /*this is a Foo*/Foo!
1314
var c, d, e: Float
1415
@IBOutlet var button: UIButton!
1516
"""
1617
performLint(NeverUseImplicitlyUnwrappedOptionals.self, input: input)
1718
XCTAssertNotDiagnosed(.doNotUseImplicitUnwrapping(identifier: "Int"))
1819
XCTAssertDiagnosed(.doNotUseImplicitUnwrapping(identifier: "String"))
20+
XCTAssertDiagnosed(.doNotUseImplicitUnwrapping(identifier: "Foo"))
1921
XCTAssertNotDiagnosed(.doNotUseImplicitUnwrapping(identifier: "Float"))
2022
XCTAssertNotDiagnosed(.doNotUseImplicitUnwrapping(identifier: "UIButton"))
2123
}

Tests/SwiftFormatRulesTests/NoCasesWithOnlyFallthroughTests.swift

+12-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,18 @@ final class NoCasesWithOnlyFallthroughTests: LintOrFormatRuleTestCase {
5353
case .empty: fallthrough
5454
default: break
5555
}
56-
""")
56+
""",
57+
checkForUnassertedDiagnostics: true)
58+
59+
XCTAssertDiagnosed(.collapseCase(name: "2"), line: 3, column: 1)
60+
XCTAssertDiagnosed(.collapseCase(name: "3"), line: 4, column: 1)
61+
XCTAssertDiagnosed(.collapseCase(name: "5"), line: 6, column: 1)
62+
XCTAssertDiagnosed(.collapseCase(name: "\"a\""), line: 11, column: 1)
63+
XCTAssertDiagnosed(.collapseCase(name: "\"b\", \"c\""), line: 12, column: 1)
64+
XCTAssertDiagnosed(.collapseCase(name: "\"f\""), line: 15, column: 1)
65+
XCTAssertDiagnosed(.collapseCase(name: ".rightBrace"), line: 21, column: 1)
66+
XCTAssertDiagnosed(.collapseCase(name: ".leftBrace"), line: 22, column: 1)
67+
XCTAssertDiagnosed(.collapseCase(name: ".empty"), line: 25, column: 1)
5768
}
5869

5970
func testFallthroughCasesWithCommentsAreNotCombined() {

Tests/SwiftFormatRulesTests/NoLabelsInCasePatternsTests.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ final class NoLabelsInCasePatternsTests: LintOrFormatRuleTestCase {
88
switch treeNode {
99
case .root(let data):
1010
break
11-
case .subtree(left: let left, right: let right):
11+
case .subtree(left: let /*hello*/left, right: let right):
1212
break
1313
case .leaf(element: let element):
1414
break
@@ -18,7 +18,7 @@ final class NoLabelsInCasePatternsTests: LintOrFormatRuleTestCase {
1818
switch treeNode {
1919
case .root(let data):
2020
break
21-
case .subtree(let left, let right):
21+
case .subtree(let /*hello*/left, let right):
2222
break
2323
case .leaf(let element):
2424
break

0 commit comments

Comments
 (0)