Skip to content

Commit 17ea484

Browse files
authored
Merge pull request swiftlang#441 from allevato/fold-operators-in-rule-tests
Update rules to use the operator-folded syntax tree.
2 parents da31c21 + 8e28076 commit 17ea484

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

Sources/SwiftFormatRules/UseSynthesizedInitializer.swift

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -142,27 +142,35 @@ public final class UseSynthesizedInitializer: SyntaxLintRule {
142142

143143
var statements: [String] = []
144144
for statement in initBody.statements {
145-
guard let exp = statement.item.as(SequenceExprSyntax.self) else { return false }
145+
guard
146+
let expr = statement.item.as(InfixOperatorExprSyntax.self),
147+
expr.operatorOperand.is(AssignmentExprSyntax.self)
148+
else {
149+
return false
150+
}
151+
146152
var leftName = ""
147153
var rightName = ""
148154

149-
for element in exp.elements {
150-
switch Syntax(element).as(SyntaxEnum.self) {
151-
case .memberAccessExpr(let element):
152-
guard let base = element.base,
153-
base.description.trimmingCharacters(in: .whitespacesAndNewlines) == "self"
154-
else {
155-
return false
156-
}
157-
leftName = element.name.text
158-
case .assignmentExpr(let element):
159-
guard element.assignToken.tokenKind == .equal else { return false }
160-
case .identifierExpr(let element):
161-
rightName = element.identifier.text
162-
default:
155+
if let memberAccessExpr = expr.leftOperand.as(MemberAccessExprSyntax.self) {
156+
guard
157+
let base = memberAccessExpr.base,
158+
base.description.trimmingCharacters(in: .whitespacesAndNewlines) == "self"
159+
else {
163160
return false
164161
}
162+
163+
leftName = memberAccessExpr.name.text
164+
} else {
165+
return false
165166
}
167+
168+
if let identifierExpr = expr.rightOperand.as(IdentifierExprSyntax.self) {
169+
rightName = identifierExpr.identifier.text
170+
} else {
171+
return false
172+
}
173+
166174
guard leftName == rightName else { return false }
167175
statements.append(leftName)
168176
}

Tests/SwiftFormatRulesTests/LintOrFormatRuleTestCase.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import SwiftFormatConfiguration
22
import SwiftFormatCore
33
import SwiftFormatTestSupport
4-
import SwiftSyntax
4+
import SwiftOperators
55
import SwiftParser
6+
import SwiftSyntax
67
import XCTest
78

89
class LintOrFormatRuleTestCase: DiagnosingTestCase {
@@ -19,7 +20,9 @@ class LintOrFormatRuleTestCase: DiagnosingTestCase {
1920
file: StaticString = #file,
2021
line: UInt = #line
2122
) {
22-
let sourceFileSyntax = restoringLegacyTriviaBehavior(Parser.parse(source: input))
23+
let sourceFileSyntax = try! restoringLegacyTriviaBehavior(
24+
OperatorTable.standardOperators.foldAll(Parser.parse(source: input))
25+
.as(SourceFileSyntax.self)!)
2326

2427
// Force the rule to be enabled while we test it.
2528
var configuration = Configuration()
@@ -56,7 +59,9 @@ class LintOrFormatRuleTestCase: DiagnosingTestCase {
5659
file: StaticString = #file,
5760
line: UInt = #line
5861
) {
59-
let sourceFileSyntax = restoringLegacyTriviaBehavior(Parser.parse(source: input))
62+
let sourceFileSyntax = try! restoringLegacyTriviaBehavior(
63+
OperatorTable.standardOperators.foldAll(Parser.parse(source: input))
64+
.as(SourceFileSyntax.self)!)
6065

6166
// Force the rule to be enabled while we test it.
6267
var configuration = configuration ?? Configuration()

Tests/SwiftFormatRulesTests/NeverForceUnwrapTests.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@ final class NeverForceUnwrapTests: LintOrFormatRuleTestCase {
1616
}
1717
"""
1818
performLint(NeverForceUnwrap.self, input: input)
19-
XCTAssertDiagnosed(.doNotForceUnwrap(name: "(someValue())"))
20-
XCTAssertDiagnosed(.doNotForceUnwrap(name: "String(a)"))
19+
XCTAssertDiagnosed(.doNotForceCast(name: "Int"), line: 3, column: 11)
20+
XCTAssertDiagnosed(.doNotForceUnwrap(name: "(someValue())"), line: 4, column: 11)
21+
XCTAssertDiagnosed(.doNotForceUnwrap(name: "String(a)"), line: 5, column: 11)
2122
XCTAssertNotDiagnosed(.doNotForceCast(name: "try"))
2223
XCTAssertNotDiagnosed(.doNotForceUnwrap(name: "try"))
23-
XCTAssertDiagnosed(.doNotForceUnwrap(name: "a"))
24-
XCTAssertDiagnosed(.doNotForceUnwrap(name: "[1: a, 2: b, 3: c][4]"))
25-
// FIXME: These diagnostics will be emitted once NeverForceUnwrap is taught
26-
// how to interpret Unresolved* components in sequence expressions.
27-
// XCTAssertDiagnosed(.doNotForceCast(name: "Int"))
28-
// XCTAssertDiagnosed(.doNotForceCast(name: "FooBarType"))
24+
XCTAssertDiagnosed(.doNotForceUnwrap(name: "[1: a, 2: b, 3: c][4]"), line: 7, column: 35)
25+
XCTAssertDiagnosed(.doNotForceCast(name: "FooBarType"), line: 8, column: 11)
26+
XCTAssertDiagnosed(.doNotForceUnwrap(name: "a"), line: 9, column: 10)
2927
}
3028

3129
func testIgnoreTestCode() {

0 commit comments

Comments
 (0)