Skip to content

Commit ea02880

Browse files
committed
Merge pull request #220 from dylansturg/better_diagnostic_locs
Fix invalid location references in some diagnostics.
1 parent 40bab4e commit ea02880

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

Sources/SwiftFormatRules/ValidateDocumentationComments.swift

+10-6
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ public final class ValidateDocumentationComments: SyntaxLintRule {
5858
$0.trimmingCharacters(in: .whitespaces).starts(with: "- Parameters")
5959
}
6060

61-
validateThrows(throwsOrRethrowsKeyword, name: name, throwsDesc: commentInfo.throwsDescription)
62-
validateReturn(returnClause, name: name, returnDesc: commentInfo.returnsDescription)
61+
validateThrows(
62+
throwsOrRethrowsKeyword, name: name, throwsDesc: commentInfo.throwsDescription, node: node)
63+
validateReturn(
64+
returnClause, name: name, returnDesc: commentInfo.returnsDescription, node: node)
6365
let funcParameters = funcParametersIdentifiers(in: parameters)
6466

6567
// If the documentation of the parameters is wrong 'docCommentInfo' won't
@@ -89,10 +91,11 @@ public final class ValidateDocumentationComments: SyntaxLintRule {
8991
private func validateReturn(
9092
_ returnClause: ReturnClauseSyntax?,
9193
name: String,
92-
returnDesc: String?
94+
returnDesc: String?,
95+
node: DeclSyntax
9396
) {
9497
if returnClause == nil && returnDesc != nil {
95-
diagnose(.removeReturnComment(funcName: name), on: returnClause)
98+
diagnose(.removeReturnComment(funcName: name), on: node)
9699
} else if returnClause != nil && returnDesc == nil {
97100
diagnose(.documentReturnValue(funcName: name), on: returnClause)
98101
}
@@ -103,15 +106,16 @@ public final class ValidateDocumentationComments: SyntaxLintRule {
103106
private func validateThrows(
104107
_ throwsOrRethrowsKeyword: TokenSyntax?,
105108
name: String,
106-
throwsDesc: String?
109+
throwsDesc: String?,
110+
node: DeclSyntax
107111
) {
108112
// If a function is marked as `rethrows`, it doesn't have any errors of its
109113
// own that should be documented. So only require documentation for
110114
// functions marked `throws`.
111115
let needsThrowsDesc = throwsOrRethrowsKeyword?.tokenKind == .throwsKeyword
112116

113117
if !needsThrowsDesc && throwsDesc != nil {
114-
diagnose(.removeThrowsComment(funcName: name), on: throwsOrRethrowsKeyword)
118+
diagnose(.removeThrowsComment(funcName: name), on: throwsOrRethrowsKeyword ?? node.firstToken)
115119
} else if needsThrowsDesc && throwsDesc == nil {
116120
diagnose(.documentErrorsThrown(funcName: name), on: throwsOrRethrowsKeyword)
117121
}

Tests/SwiftFormatRulesTests/ValidateDocumentationCommentsTests.swift

+18-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import SwiftFormatRules
22

33
final class ValidateDocumentationCommentsTests: LintOrFormatRuleTestCase {
4+
override func setUp() {
5+
super.setUp()
6+
shouldCheckForUnassertedDiagnostics = true
7+
}
8+
49
func testParameterDocumentation() {
510
let input =
611
"""
@@ -32,9 +37,9 @@ final class ValidateDocumentationCommentsTests: LintOrFormatRuleTestCase {
3237
func testInvalidParameterDesc(command: String, stdin: String) -> String {}
3338
"""
3439
performLint(ValidateDocumentationComments.self, input: input)
35-
XCTAssertDiagnosed(.useSingularParameter)
36-
XCTAssertDiagnosed(.usePluralParameters)
37-
XCTAssertDiagnosed(.usePluralParameters)
40+
XCTAssertDiagnosed(.useSingularParameter, line: 6, column: 1)
41+
XCTAssertDiagnosed(.usePluralParameters, line: 15, column: 1)
42+
XCTAssertDiagnosed(.usePluralParameters, line: 26, column: 1)
3843
}
3944

4045
func testParametersName() {
@@ -57,8 +62,8 @@ final class ValidateDocumentationCommentsTests: LintOrFormatRuleTestCase {
5762
func foo(p1: Int, p2: Int, p3: Int) -> Int {}
5863
"""
5964
performLint(ValidateDocumentationComments.self, input: input)
60-
XCTAssertDiagnosed(.parametersDontMatch(funcName: "sum"))
61-
XCTAssertDiagnosed(.parametersDontMatch(funcName: "foo"))
65+
XCTAssertDiagnosed(.parametersDontMatch(funcName: "sum"), line: 7, column: 1)
66+
XCTAssertDiagnosed(.parametersDontMatch(funcName: "foo"), line: 15, column: 1)
6267
}
6368

6469
func testThrowsDocumentation() {
@@ -88,9 +93,9 @@ final class ValidateDocumentationCommentsTests: LintOrFormatRuleTestCase {
8893
func doesRethrow(p1: (() throws -> ())) rethrows {}
8994
"""
9095
performLint(ValidateDocumentationComments.self, input: input)
91-
XCTAssertDiagnosed(.removeThrowsComment(funcName: "doesNotThrow"))
92-
XCTAssertDiagnosed(.documentErrorsThrown(funcName: "doesThrow"))
93-
XCTAssertDiagnosed(.removeThrowsComment(funcName: "doesRethrow"))
96+
XCTAssertDiagnosed(.removeThrowsComment(funcName: "doesNotThrow"), line: 8, column: 1)
97+
XCTAssertDiagnosed(.documentErrorsThrown(funcName: "doesThrow"), line: 16, column: 43)
98+
XCTAssertDiagnosed(.removeThrowsComment(funcName: "doesRethrow"), line: 22, column: 41)
9499
}
95100

96101
func testReturnDocumentation() {
@@ -114,8 +119,8 @@ final class ValidateDocumentationCommentsTests: LintOrFormatRuleTestCase {
114119
func foo(p1: Int, p2: Int, p3: Int) -> Int {}
115120
"""
116121
performLint(ValidateDocumentationComments.self, input: input)
117-
XCTAssertDiagnosed(.removeReturnComment(funcName: "noReturn"))
118-
XCTAssertDiagnosed(.documentReturnValue(funcName: "foo"))
122+
XCTAssertDiagnosed(.removeReturnComment(funcName: "noReturn"), line: 8, column: 1)
123+
XCTAssertDiagnosed(.documentReturnValue(funcName: "foo"), line: 16, column: 37)
119124
}
120125

121126
func testValidDocumentation() {
@@ -214,7 +219,7 @@ final class ValidateDocumentationCommentsTests: LintOrFormatRuleTestCase {
214219
XCTAssertNotDiagnosed(.useSingularParameter)
215220
XCTAssertNotDiagnosed(.usePluralParameters)
216221

217-
XCTAssertDiagnosed(.parametersDontMatch(funcName: "incorrectParam"))
222+
XCTAssertDiagnosed(.parametersDontMatch(funcName: "incorrectParam"), line: 6, column: 1)
218223

219224
XCTAssertNotDiagnosed(.documentReturnValue(funcName: "singularParam"))
220225
XCTAssertNotDiagnosed(.removeReturnComment(funcName: "singularParam"))
@@ -268,8 +273,8 @@ final class ValidateDocumentationCommentsTests: LintOrFormatRuleTestCase {
268273
XCTAssertNotDiagnosed(.useSingularParameter)
269274
XCTAssertNotDiagnosed(.usePluralParameters)
270275

271-
XCTAssertDiagnosed(.parametersDontMatch(funcName: "init"))
272-
XCTAssertDiagnosed(.removeReturnComment(funcName: "init"))
276+
XCTAssertDiagnosed(.parametersDontMatch(funcName: "init"), line: 6, column: 3)
277+
XCTAssertDiagnosed(.removeReturnComment(funcName: "init"), line: 6, column: 3)
273278

274279
XCTAssertNotDiagnosed(.documentReturnValue(funcName: "init"))
275280
XCTAssertNotDiagnosed(.removeReturnComment(funcName: "init"))

0 commit comments

Comments
 (0)