Skip to content

Commit 8c4689b

Browse files
committed
Add newline for pund-keywords in IfConfigDeclSyntax
1 parent f5d6072 commit 8c4689b

File tree

4 files changed

+80
-4
lines changed

4 files changed

+80
-4
lines changed

CodeGeneration/Sources/SyntaxSupport/gyb_generated/DeclNodes.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,8 @@ public let DECL_NODES: [Node] = [
523523
children: [
524524
Child(name: "PoundKeyword",
525525
kind: .token(choices: [.token(tokenKind: "PoundIfToken"), .token(tokenKind: "PoundElseifToken"), .token(tokenKind: "PoundElseToken")]),
526-
classification: "BuildConfigId"),
526+
classification: "BuildConfigId",
527+
requiresLeadingNewline: true),
527528
Child(name: "Condition",
528529
kind: .node(kind: "Expr"),
529530
nameForDiagnostics: "condition",
@@ -553,7 +554,8 @@ public let DECL_NODES: [Node] = [
553554
kind: .collection(kind: "IfConfigClauseList", collectionElementName: "Clause")),
554555
Child(name: "PoundEndif",
555556
kind: .token(choices: [.token(tokenKind: "PoundEndifToken")]),
556-
classification: "BuildConfigId")
557+
classification: "BuildConfigId",
558+
requiresLeadingNewline: true)
557559
]),
558560

559561
Node(name: "ImportDecl",

Sources/SwiftBasicFormat/generated/BasicFormat.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ open class BasicFormat: SyntaxRewriter {
129129
return true
130130
case \CodeBlockSyntax.rightBrace:
131131
return true
132+
case \IfConfigClauseSyntax.poundKeyword:
133+
return true
134+
case \IfConfigDeclSyntax.poundEndif:
135+
return true
132136
case \MemberDeclBlockSyntax.rightBrace:
133137
return true
134138
case \SwitchExprSyntax.rightBrace:
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import XCTest
14+
import SwiftSyntax
15+
import SwiftSyntaxBuilder
16+
17+
final class IfConfigDeclSyntaxTests: XCTestCase {
18+
func testIfConfigClauseSyntax() {
19+
let buildable = IfConfigDeclSyntax(
20+
clauses: IfConfigClauseListSyntax {
21+
IfConfigClauseSyntax(
22+
poundKeyword: .poundIfKeyword(),
23+
condition: ExprSyntax("DEBUG"),
24+
elements: .statements(
25+
CodeBlockItemListSyntax {
26+
DeclSyntax(
27+
"""
28+
public func debug(_ data: Foo) -> String {
29+
return data.debugDescription
30+
}
31+
"""
32+
)
33+
}
34+
)
35+
)
36+
IfConfigClauseSyntax(
37+
poundKeyword: .poundElseKeyword(leadingTrivia: .newline),
38+
elements: .statements(
39+
CodeBlockItemListSyntax {
40+
DeclSyntax(
41+
"""
42+
public func debug(_ data: Foo) -> String {
43+
return data.description
44+
}
45+
"""
46+
)
47+
}
48+
)
49+
)
50+
},
51+
poundEndif: .poundEndifKeyword(leadingTrivia: .newline)
52+
)
53+
54+
AssertBuildResult(
55+
buildable,
56+
"""
57+
58+
#if DEBUG
59+
public func debug(_ data: Foo) -> String {
60+
return data.debugDescription
61+
}
62+
#else
63+
public func debug(_ data: Foo) -> String {
64+
return data.description
65+
}
66+
#endif
67+
"""
68+
)
69+
}
70+
}

gyb_syntax_support/DeclNodes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
'PoundIfToken',
9595
'PoundElseifToken',
9696
'PoundElseToken',
97-
]),
97+
], requires_leading_newline=True),
9898
Child('Condition', kind='Expr', name_for_diagnostics='condition', classification='BuildConfigId',
9999
is_optional=True),
100100
Child('Elements', is_optional=True, kind='Syntax',
@@ -118,7 +118,7 @@
118118
Child('Clauses', kind='IfConfigClauseList',
119119
collection_element_name='Clause'),
120120
Child('PoundEndif', kind='PoundEndifToken',
121-
classification='BuildConfigId'),
121+
classification='BuildConfigId', requires_leading_newline=True),
122122
]),
123123

124124
Node('PoundSourceLocation', name_for_diagnostics="'#sourceLocation' directive",

0 commit comments

Comments
 (0)