Skip to content

Commit 188418c

Browse files
authored
Merge pull request #390 from allevato/latest-fixes
Fix build errors, test failures, and move to non-deprecated APIs in latest swift-syntax.
2 parents fa880f3 + 9916694 commit 188418c

26 files changed

+131
-131
lines changed

Sources/SwiftFormat/Pipelines+Generated.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class LintPipeline: SyntaxVisitor {
3131
/// Creates a new lint pipeline.
3232
init(context: Context) {
3333
self.context = context
34+
super.init(viewMode: .sourceAccurate)
3435
}
3536

3637
override func visit(_ node: AsExprSyntax) -> SyntaxVisitorContinueKind {

Sources/SwiftFormat/SyntaxValidatingVisitor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fileprivate class SyntaxValidatingVisitor: SyntaxVisitor {
6969
///
7070
/// - Parameter syntax: The root of a tree of syntax nodes to check for compatibility.
7171
public func _firstInvalidSyntaxPosition(in syntax: Syntax) -> AbsolutePosition? {
72-
let visitor = SyntaxValidatingVisitor()
72+
let visitor = SyntaxValidatingVisitor(viewMode: .sourceAccurate)
7373
visitor.walk(syntax)
7474
return visitor.invalidSyntaxStartPosition
7575
}

Sources/SwiftFormatCore/RuleMask.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ fileprivate class RuleStatusCollectionVisitor: SyntaxVisitor {
130130
ignoreFileRegex = try! NSRegularExpression(pattern: ignoreFilePattern, options: [])
131131

132132
self.sourceLocationConverter = sourceLocationConverter
133+
super.init(viewMode: .sourceAccurate)
133134
}
134135

135136
// MARK: - Syntax Visitation Methods

Sources/SwiftFormatCore/SyntaxLintRule.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ open class SyntaxLintRule: SyntaxVisitor, Rule {
2727
/// Creates a new rule in a given context.
2828
public required init(context: Context) {
2929
self.context = context
30+
super.init(viewMode: .sourceAccurate)
3031
}
3132
}

Sources/SwiftFormatCore/Trivia+Convenience.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ extension Trivia {
112112
prev = .carriageReturnLineFeeds(l + r)
113113
case (.verticalTabs(let l), .verticalTabs(let r)):
114114
prev = .verticalTabs(l + r)
115-
case (.garbageText(let l), .garbageText(let r)):
116-
prev = .garbageText(l + r)
115+
case (.unexpectedText(let l), .unexpectedText(let r)):
116+
prev = .unexpectedText(l + r)
117117
case (.formfeeds(let l), .formfeeds(let r)):
118118
prev = .formfeeds(l + r)
119119
default:

Sources/SwiftFormatPrettyPrint/SequenceExprFolding.swift

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ extension SequenceExprSyntax {
8282
case 1:
8383
// A sequence with one element will not be changed by folding unless that
8484
// element is a ternary expression.
85-
return elements.first!.is(TernaryExprSyntax.self)
85+
return elements.first!.is(UnresolvedTernaryExprSyntax.self)
8686

8787
case 2:
8888
// A sequence with two elements might be changed by folding if the first
@@ -91,7 +91,7 @@ extension SequenceExprSyntax {
9191
var elementsIterator = elements.makeIterator()
9292
let first = elementsIterator.next()!
9393
let second = elementsIterator.next()!
94-
return first.is(TernaryExprSyntax.self)
94+
return first.is(UnresolvedTernaryExprSyntax.self)
9595
|| !(second.is(AsExprSyntax.self) || second.is(IsExprSyntax.self))
9696

9797
case 3:
@@ -100,7 +100,8 @@ extension SequenceExprSyntax {
100100
// be more inclusive than it needs to be.)
101101
return elements.contains {
102102
$0.is(AsExprSyntax.self) || $0.is(IsExprSyntax.self)
103-
|| $0.is(TernaryExprSyntax.self) || $0.is(AwaitExprSyntax.self)
103+
|| $0.is(UnresolvedTernaryExprSyntax.self)
104+
|| $0.is(AwaitExprSyntax.self)
104105
|| $0.is(TryExprSyntax.self)
105106
}
106107

@@ -283,19 +284,6 @@ extension SequenceExprSyntax {
283284
// (or begins with) an operator.
284285
elements.append(expr)
285286
elements.append(expr)
286-
} else if let ternaryExpr = expr.as(TernaryExprSyntax.self) {
287-
// In the compiler implementation, ternary expressions have their
288-
// condition and false choice appear in the main sequence, with the true
289-
// choice nested inside an `if-expr` with null values for the other two
290-
// parts. In order to match that behavior, we extract the condition and
291-
// false choice from the ternary and put them directly in the sequence.
292-
// We can't null out those properties of a `TernaryExprSyntax` because
293-
// they are non-optional, so instead we simply insert the original
294-
// ternary in that slot and the rest of the algorithm will ignore
295-
// everything except for the true choice.
296-
normalizeExpression(ternaryExpr.conditionExpression, into: &elements)
297-
elements.append(ExprSyntax(ternaryExpr))
298-
normalizeExpression(ternaryExpr.secondChoice, into: &elements)
299287
} else {
300288
elements.append(expr)
301289
}
@@ -317,7 +305,7 @@ extension SequenceExprSyntax {
317305
case .binaryOperatorExpr(let binOpExpr):
318306
let infixOpName = binOpExpr.operatorToken.text
319307
return context.infixOperator(named: infixOpName)?.precedenceGroup
320-
case .ternaryExpr:
308+
case .unresolvedTernaryExpr:
321309
return context.precedenceGroup(named: .ternary)
322310
default:
323311
// This branch will cover any potential new nodes that might arise in the
@@ -378,26 +366,26 @@ extension SequenceExprSyntax {
378366
}
379367

380368
switch Syntax(op).as(SyntaxEnum.self) {
381-
case .ternaryExpr(let ternaryExpr):
369+
case .unresolvedTernaryExpr(let unresolvedTernaryExpr):
382370
// Resolve the ternary expression by pulling the LHS and RHS that we
383371
// actually want into it.
384-
385-
let result = ternaryExpr
386-
.withConditionExpression(lhs)
387-
.withSecondChoice(rhs)
372+
let result = TernaryExprSyntax(
373+
conditionExpression: lhs,
374+
questionMark: unresolvedTernaryExpr.questionMark,
375+
firstChoice: unresolvedTernaryExpr.firstChoice,
376+
colonMark: unresolvedTernaryExpr.colonMark,
377+
secondChoice: rhs)
388378
return makeResultExpression(ExprSyntax(result))
389379
case .asExpr, .isExpr:
390380
// A normalized cast expression will have a regular LHS, then `as/is Type`
391381
// as the operator *and* the RHS. We resolve it by returning a new
392382
// sequence expression that discards the extra RHS.
393-
let result = SyntaxFactory.makeSequenceExpr(
394-
elements: SyntaxFactory.makeExprList([lhs, op]))
383+
let result = SequenceExprSyntax(elements: ExprListSyntax([lhs, op]))
395384
return makeResultExpression(ExprSyntax(result))
396385
default:
397386
// For any other binary operator, we simply return a sequence that has the
398387
// three elements.
399-
let result = SyntaxFactory.makeSequenceExpr(
400-
elements: SyntaxFactory.makeExprList([lhs, op, rhs]))
388+
let result = SequenceExprSyntax(elements: ExprListSyntax([lhs, op, rhs]))
401389
return makeResultExpression(ExprSyntax(result))
402390
}
403391
}

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
6262
self.config = configuration
6363
self.operatorContext = operatorContext
6464
self.maxlinelength = config.lineLength
65+
super.init(viewMode: .sourceAccurate)
6566
}
6667

6768
func makeStream(from node: Syntax) -> [Token] {
@@ -486,14 +487,18 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
486487

487488
// MARK: - Control flow statement nodes
488489

490+
override func visit(_ node: LabeledStmtSyntax) -> SyntaxVisitorContinueKind {
491+
after(node.labelColon, tokens: .space)
492+
return .visitChildren
493+
}
494+
489495
override func visit(_ node: IfStmtSyntax) -> SyntaxVisitorContinueKind {
490496
// There may be a consistent breaking group around this node, see `CodeBlockItemSyntax`. This
491497
// group is necessary so that breaks around and inside of the conditions aren't forced to break
492498
// when the if-stmt spans multiple lines.
493499
before(node.conditions.firstToken, tokens: .open)
494500
after(node.conditions.lastToken, tokens: .close)
495501

496-
after(node.labelColon, tokens: .space)
497502
after(node.ifKeyword, tokens: .space)
498503

499504
// Add break groups, using open continuation breaks, around any conditions after the first so
@@ -553,8 +558,6 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
553558
}
554559

555560
override func visit(_ node: ForInStmtSyntax) -> SyntaxVisitorContinueKind {
556-
after(node.labelColon, tokens: .space)
557-
558561
// If we have a `(try) await` clause, allow breaking after the `for` so that the `(try) await`
559562
// can fall onto the next line if needed, and if both `try await` are present, keep them
560563
// together. Otherwise, keep `for` glued to the token after it so that we break somewhere later
@@ -589,7 +592,6 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
589592
}
590593

591594
override func visit(_ node: WhileStmtSyntax) -> SyntaxVisitorContinueKind {
592-
after(node.labelColon, tokens: .space)
593595
after(node.whileKeyword, tokens: .space)
594596

595597
// Add break groups, using open continuation breaks, around any conditions after the first so
@@ -611,7 +613,6 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
611613
}
612614

613615
override func visit(_ node: RepeatWhileStmtSyntax) -> SyntaxVisitorContinueKind {
614-
after(node.labelColon, tokens: .space)
615616
arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)
616617

617618
if config.lineBreakBeforeControlFlowKeywords {
@@ -631,7 +632,6 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
631632
}
632633

633634
override func visit(_ node: DoStmtSyntax) -> SyntaxVisitorContinueKind {
634-
after(node.labelColon, tokens: .space)
635635
arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)
636636
return .visitChildren
637637
}
@@ -686,7 +686,6 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
686686
}
687687

688688
override func visit(_ node: SwitchStmtSyntax) -> SyntaxVisitorContinueKind {
689-
after(node.labelColon, tokens: .space)
690689
before(node.switchKeyword, tokens: .open)
691690
after(node.switchKeyword, tokens: .space)
692691
before(node.leftBrace, tokens: .break(.reset))
@@ -2498,14 +2497,14 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
24982497
/// same place but still let surrounding formatting occur somewhat as expected.
24992498
private func appendTrailingTrivia(_ token: TokenSyntax) {
25002499
let trailingTrivia = Array(token.trailingTrivia)
2501-
guard let lastGarbageIndex = trailingTrivia.lastIndex(where: { $0.isGarbageText }) else {
2500+
guard let lastUnexpectedIndex = trailingTrivia.lastIndex(where: { $0.isUnexpectedText }) else {
25022501
return
25032502
}
25042503

25052504
var verbatimText = ""
2506-
for piece in trailingTrivia[...lastGarbageIndex] {
2505+
for piece in trailingTrivia[...lastUnexpectedIndex] {
25072506
switch piece {
2508-
case .shebang, .garbageText, .spaces, .tabs, .formfeeds, .verticalTabs:
2507+
case .shebang, .unexpectedText, .spaces, .tabs, .formfeeds, .verticalTabs:
25092508
piece.write(to: &verbatimText)
25102509
default:
25112510
// The implementation of the lexer today ensures that newlines, carriage returns, and
@@ -2956,7 +2955,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
29562955
}
29572956
}
29582957

2959-
case .shebang(let text), .garbageText(let text):
2958+
case .shebang(let text), .unexpectedText(let text):
29602959
// Garbage text in leading trivia might be something meaningful that would be disruptive to
29612960
// throw away when formatting the file, like a hashbang line or Unicode byte-order marker at
29622961
// the beginning of a file, or source control conflict markers. Keep it as verbatim text so
@@ -3610,10 +3609,10 @@ class CommentMovingRewriter: SyntaxRewriter {
36103609
}
36113610

36123611
extension TriviaPiece {
3613-
/// True if the trivia piece is garbage text.
3614-
fileprivate var isGarbageText: Bool {
3612+
/// True if the trivia piece is unexpected text.
3613+
fileprivate var isUnexpectedText: Bool {
36153614
switch self {
3616-
case .garbageText: return true
3615+
case .unexpectedText: return true
36173616
default: return false
36183617
}
36193618
}

Sources/SwiftFormatRules/DoNotUseSemicolons.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,16 @@ public final class DoNotUseSemicolons: SyntaxFormatRule {
7070
// If there's a semicolon, diagnose and remove it.
7171
if let semicolon = item.semicolon {
7272

73-
// Exception: do not remove the semicolon if it is separating a 'do' statement from a 'while' statement.
74-
if Syntax(item).as(CodeBlockItemSyntax.self)?.children.first?.is(DoStmtSyntax.self) == true,
73+
// Exception: do not remove the semicolon if it is separating a 'do' statement from a
74+
// 'while' statement.
75+
if Syntax(item).as(CodeBlockItemSyntax.self)?
76+
.children(viewMode: .sourceAccurate).first?.is(DoStmtSyntax.self) == true,
7577
idx < node.count - 1
7678
{
77-
let nextItem = node.children[node.children.index(after: item.index)]
79+
let children = node.children(viewMode: .sourceAccurate)
80+
let nextItem = children[children.index(after: item.index)]
7881
if Syntax(nextItem).as(CodeBlockItemSyntax.self)?
79-
.children.first?.is(WhileStmtSyntax.self) == true
82+
.children(viewMode: .sourceAccurate).first?.is(WhileStmtSyntax.self) == true
8083
{
8184
continue
8285
}
@@ -96,11 +99,11 @@ public final class DoNotUseSemicolons: SyntaxFormatRule {
9699
}
97100

98101
public override func visit(_ node: CodeBlockItemListSyntax) -> Syntax {
99-
return Syntax(nodeByRemovingSemicolons(from: node, nodeCreator: SyntaxFactory.makeCodeBlockItemList))
102+
return Syntax(nodeByRemovingSemicolons(from: node, nodeCreator: CodeBlockItemListSyntax.init))
100103
}
101104

102105
public override func visit(_ node: MemberDeclListSyntax) -> Syntax {
103-
return Syntax(nodeByRemovingSemicolons(from: node, nodeCreator: SyntaxFactory.makeMemberDeclList))
106+
return Syntax(nodeByRemovingSemicolons(from: node, nodeCreator: MemberDeclListSyntax.init))
104107
}
105108
}
106109

Sources/SwiftFormatRules/FileScopedDeclarationPrivacy.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public final class FileScopedDeclarationPrivacy: SyntaxFormatRule {
9797
return codeBlockItem
9898
}
9999
}
100-
return SyntaxFactory.makeCodeBlockItemList(newCodeBlockItems)
100+
return CodeBlockItemListSyntax(newCodeBlockItems)
101101
}
102102

103103
/// Returns a new `IfConfigDeclSyntax` equivalent to the given node, but where any file-scoped
@@ -116,7 +116,7 @@ public final class FileScopedDeclarationPrivacy: SyntaxFormatRule {
116116
return clause
117117
}
118118
}
119-
return ifConfigDecl.withClauses(SyntaxFactory.makeIfConfigClauseList(newClauses))
119+
return ifConfigDecl.withClauses(IfConfigClauseListSyntax(newClauses))
120120
}
121121

122122
/// Returns a rewritten version of the given declaration if its modifier list contains `private`
@@ -163,7 +163,7 @@ public final class FileScopedDeclarationPrivacy: SyntaxFormatRule {
163163
}
164164
return modifier
165165
}
166-
return factory(SyntaxFactory.makeModifierList(newModifiers))
166+
return factory(ModifierListSyntax(newModifiers))
167167
}
168168
}
169169

Sources/SwiftFormatRules/FullyIndirectEnum.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ public final class FullyIndirectEnum: SyntaxFormatRule {
6666
newEnumDecl = node
6767
}
6868

69-
let newModifier = SyntaxFactory.makeDeclModifier(
70-
name: SyntaxFactory.makeIdentifier(
69+
let newModifier = DeclModifierSyntax(
70+
name: TokenSyntax.identifier(
7171
"indirect", leadingTrivia: leadingTrivia, trailingTrivia: .spaces(1)), detail: nil)
7272

73-
let newMemberBlock = node.members.withMembers(SyntaxFactory.makeMemberDeclList(newMembers))
73+
let newMemberBlock = node.members.withMembers(MemberDeclListSyntax(newMembers))
7474
return DeclSyntax(newEnumDecl.addModifier(newModifier).withMembers(newMemberBlock))
7575
}
7676

Sources/SwiftFormatRules/GroupNumericLiterals.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public final class GroupNumericLiterals: SyntaxFormatRule {
6060

6161
newDigits = isNegative ? "-" + newDigits : newDigits
6262
let result = node.withDigits(
63-
SyntaxFactory.makeIntegerLiteral(
63+
TokenSyntax.integerLiteral(
6464
newDigits,
6565
leadingTrivia: node.digits.leadingTrivia,
6666
trailingTrivia: node.digits.trailingTrivia))

Sources/SwiftFormatRules/ImportsXCTestVisitor.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ fileprivate class ImportsXCTestVisitor: SyntaxVisitor {
1919

2020
init(context: Context) {
2121
self.context = context
22+
super.init(viewMode: .sourceAccurate)
2223
}
2324

2425
override func visit(_ node: SourceFileSyntax) -> SyntaxVisitorContinueKind {

Sources/SwiftFormatRules/ModifierListSyntax+Convenience.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ extension ModifierListSyntax {
4848

4949
/// Returns a foramatted declaration modifier token with the given name.
5050
func createModifierToken(name: String) -> DeclModifierSyntax {
51-
let id = SyntaxFactory.makeIdentifier(name, trailingTrivia: .spaces(1))
52-
let newModifier = SyntaxFactory.makeDeclModifier(
53-
name: id, detail: nil)
51+
let id = TokenSyntax.identifier(name, trailingTrivia: .spaces(1))
52+
let newModifier = DeclModifierSyntax(name: id, detail: nil)
5453
return newModifier
5554
}
5655

@@ -86,7 +85,7 @@ extension ModifierListSyntax {
8685
leadingTrivia: [],
8786
trailingTrivia: .spaces(1))
8887
newModifiers.insert(formattedMod, at: 0)
89-
return SyntaxFactory.makeModifierList(newModifiers)
88+
return ModifierListSyntax(newModifiers)
9089
} else {
9190
return inserting(modifier, at: index)
9291
}

Sources/SwiftFormatRules/NoAccessLevelOnExtensionDeclaration.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public final class NoAccessLevelOnExtensionDeclaration: SyntaxFormatRule {
4444
accessKeywordToAdd = accessKeyword
4545
}
4646

47-
let newMembers = SyntaxFactory.makeMemberDeclBlock(
47+
let newMembers = MemberDeclBlockSyntax(
4848
leftBrace: node.members.leftBrace,
4949
members: addMemberAccessKeywords(memDeclBlock: node.members, keyword: accessKeywordToAdd),
5050
rightBrace: node.members.rightBrace)
@@ -96,7 +96,7 @@ public final class NoAccessLevelOnExtensionDeclaration: SyntaxFormatRule {
9696
else { continue }
9797
newMembers.append(memberItem.withDecl(newDecl))
9898
}
99-
return SyntaxFactory.makeMemberDeclList(newMembers)
99+
return MemberDeclListSyntax(newMembers)
100100
}
101101
}
102102

0 commit comments

Comments
 (0)