Skip to content

Commit 4f0ec76

Browse files
authored
Merge pull request #1331 from kimdv/kimdv/remove-sapce-after-switch-case-label-colon-and-break-stmt
2 parents d689cae + b7cfc6b commit 4f0ec76

File tree

8 files changed

+97
-9
lines changed

8 files changed

+97
-9
lines changed

CodeGeneration/Sources/SyntaxSupport/gyb_generated/ExprNodes.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ public let EXPR_NODES: [Node] = [
456456
Child(name: "CaseItems",
457457
kind: .collection(kind: "CaseItemList", collectionElementName: "CaseItem")),
458458
Child(name: "Colon",
459-
kind: .token(choices: [.token(tokenKind: "ColonToken")]))
459+
kind: .token(choices: [.token(tokenKind: "ColonToken")], requiresTrailingSpace: false))
460460
]),
461461

462462
Node(name: "SwitchDefaultLabel",
@@ -466,7 +466,7 @@ public let EXPR_NODES: [Node] = [
466466
Child(name: "DefaultKeyword",
467467
kind: .token(choices: [.keyword(text: "default")])),
468468
Child(name: "Colon",
469-
kind: .token(choices: [.token(tokenKind: "ColonToken")]))
469+
kind: .token(choices: [.token(tokenKind: "ColonToken")], requiresTrailingSpace: false))
470470
]),
471471

472472
Node(name: "CaseItem",

CodeGeneration/Sources/SyntaxSupport/gyb_generated/StmtNodes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public let STMT_NODES: [Node] = [
231231
kind: "Stmt",
232232
children: [
233233
Child(name: "BreakKeyword",
234-
kind: .token(choices: [.keyword(text: "break")])),
234+
kind: .token(choices: [.keyword(text: "break")], requiresTrailingSpace: false)),
235235
Child(name: "Label",
236236
kind: .token(choices: [.token(tokenKind: "IdentifierToken")]),
237237
nameForDiagnostics: "label",

Sources/SwiftBasicFormat/generated/BasicFormat.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,18 @@ open class BasicFormat: SyntaxRewriter {
178178
switch keyPath {
179179
case \AvailabilityArgumentSyntax.entry:
180180
return false
181+
case \BreakStmtSyntax.breakKeyword:
182+
return false
181183
case \DeclNameArgumentSyntax.colon:
182184
return false
183185
case \DictionaryExprSyntax.content:
184186
return false
185187
case \DynamicReplacementArgumentsSyntax.forLabel:
186188
return false
189+
case \SwitchCaseLabelSyntax.colon:
190+
return false
191+
case \SwitchDefaultLabelSyntax.colon:
192+
return false
187193
case \TryExprSyntax.questionOrExclamationMark:
188194
return true
189195
default:

Tests/SwiftSyntaxBuilderTest/Assertions.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@ import _SwiftSyntaxTestSupport
1616

1717
import XCTest
1818

19-
func AssertBuildResult<T: SyntaxProtocol>(_ buildable: T, _ expectedResult: String, file: StaticString = #file, line: UInt = #line) {
19+
func AssertBuildResult<T: SyntaxProtocol>(_ buildable: T, _ expectedResult: String, trimTrailingWhitespace: Bool = true, file: StaticString = #file, line: UInt = #line) {
20+
var buildableDescription = buildable.formatted().description
21+
var expectedResult = expectedResult
22+
if trimTrailingWhitespace {
23+
buildableDescription = buildableDescription.trimmingTrailingWhitespace()
24+
expectedResult = expectedResult.trimmingTrailingWhitespace()
25+
}
2026
AssertStringsEqualWithDiff(
21-
buildable.formatted().description.trimmingTrailingWhitespace(),
22-
expectedResult.trimmingTrailingWhitespace(),
27+
buildableDescription,
28+
expectedResult,
2329
file: file,
2430
line: line
2531
)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 BreakStmtSyntaxTests: XCTestCase {
18+
func testBreakStmtSyntax() {
19+
let testCases: [UInt: (StmtSyntax, String)] = [
20+
#line: (BreakStmtSyntax().as(StmtSyntax.self)!, "break"),
21+
#line: (StmtSyntax("break"), "break"),
22+
]
23+
24+
for (line, testCase) in testCases {
25+
let (builder, expected) = testCase
26+
AssertBuildResult(builder, expected, trimTrailingWhitespace: false, line: line)
27+
}
28+
}
29+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 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 SwitchCaseLabelSyntaxTests: XCTestCase {
18+
func testSwitchCaseLabelSyntax() {
19+
let testCases: [UInt: (SwitchCaseSyntax, String)] = [
20+
#line: (
21+
SwitchCaseSyntax("default:") {
22+
StmtSyntax("return nil")
23+
24+
},
25+
"""
26+
default:
27+
return nil
28+
"""
29+
),
30+
#line: (
31+
SwitchCaseSyntax("case .foo:") {
32+
StmtSyntax("return nil")
33+
34+
},
35+
"""
36+
case .foo:
37+
return nil
38+
"""
39+
),
40+
]
41+
42+
for (line, testCase) in testCases {
43+
let (builder, expected) = testCase
44+
AssertBuildResult(builder, expected, trimTrailingWhitespace: false, line: line)
45+
}
46+
}
47+
}

gyb_syntax_support/ExprNodes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,14 +347,14 @@
347347
Child('CaseKeyword', kind='KeywordToken', token_choices=['KeywordToken|case']),
348348
Child('CaseItems', kind='CaseItemList',
349349
collection_element_name='CaseItem'),
350-
Child('Colon', kind='ColonToken'),
350+
Child('Colon', kind='ColonToken', requires_trailing_space=False),
351351
]),
352352

353353
# switch-default-label -> 'default' ':'
354354
Node('SwitchDefaultLabel', name_for_diagnostics=None, kind='Syntax',
355355
children=[
356356
Child('DefaultKeyword', kind='KeywordToken', token_choices=['KeywordToken|default']),
357-
Child('Colon', kind='ColonToken'),
357+
Child('Colon', kind='ColonToken', requires_trailing_space=False),
358358
]),
359359

360360
# case-item -> pattern where-clause? ','?

gyb_syntax_support/StmtNodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@
143143
# break-stmt -> 'break' identifier? ';'?
144144
Node('BreakStmt', name_for_diagnostics="'break' statement", kind='Stmt',
145145
children=[
146-
Child('BreakKeyword', kind='KeywordToken', token_choices=['KeywordToken|break']),
146+
Child('BreakKeyword', kind='KeywordToken', token_choices=['KeywordToken|break'], requires_trailing_space=False),
147147
Child('Label', kind='IdentifierToken', name_for_diagnostics='label',
148148
is_optional=True),
149149
]),

0 commit comments

Comments
 (0)