Skip to content

Commit 96f44d0

Browse files
authored
Merge pull request #397 from allevato/prep-5.7-release
Prepare for 0.50700.0 release.
2 parents bd89f0d + 5c46519 commit 96f44d0

File tree

10 files changed

+266
-10
lines changed

10 files changed

+266
-10
lines changed

Package.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,15 @@ if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
179179
package.dependencies += [
180180
.package(
181181
url: "https://github.com/apple/swift-argument-parser.git",
182-
branch: "main"
182+
.upToNextMinor(from: "1.1.4")
183183
),
184184
.package(
185185
url: "https://github.com/apple/swift-syntax",
186-
branch: "main"
186+
branch: "swift-5.7-RELEASE"
187187
),
188188
.package(
189189
url: "https://github.com/apple/swift-tools-support-core.git",
190-
branch: "main"
190+
.upToNextMinor(from: "0.2.7")
191191
),
192192
]
193193
} else {

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,15 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
12891289
before(tokenToOpenWith.nextToken, tokens: .break(breakKindClose, newlines: .soft), .close)
12901290
}
12911291

1292-
if let condition = node.condition {
1292+
if isNestedInPostfixIfConfig(node: Syntax(node)) {
1293+
before(
1294+
node.firstToken,
1295+
tokens: [
1296+
.printerControl(kind: .enableBreaking),
1297+
.break(.reset),
1298+
]
1299+
)
1300+
} else if let condition = node.condition {
12931301
before(condition.firstToken, tokens: .printerControl(kind: .disableBreaking))
12941302
after(
12951303
condition.lastToken,
@@ -3441,7 +3449,11 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
34413449

34423450
if let calledMemberAccessExpr = calledExpression.as(MemberAccessExprSyntax.self) {
34433451
if calledMemberAccessExpr.base != nil {
3444-
before(calledMemberAccessExpr.dot, tokens: [.break(.contextual, size: 0)])
3452+
if isNestedInPostfixIfConfig(node: Syntax(calledMemberAccessExpr)) {
3453+
before(calledMemberAccessExpr.dot, tokens: [.break(.same, size: 0)])
3454+
} else {
3455+
before(calledMemberAccessExpr.dot, tokens: [.break(.contextual, size: 0)])
3456+
}
34453457
}
34463458
before(calledMemberAccessExpr.dot, tokens: beforeTokens)
34473459
after(expr.lastToken, tokens: afterTokens)
@@ -3466,6 +3478,20 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
34663478
}
34673479
}
34683480

3481+
private func isNestedInPostfixIfConfig(node: Syntax) -> Bool {
3482+
var this: Syntax? = node
3483+
3484+
while this?.parent != nil {
3485+
if this?.parent?.is(PostfixIfConfigExprSyntax.self) == true {
3486+
return true
3487+
}
3488+
3489+
this = this?.parent
3490+
}
3491+
3492+
return false
3493+
}
3494+
34693495
extension Syntax {
34703496
/// Creates a pretty-printable token stream for the provided Syntax node.
34713497
func makeTokenStream(configuration: Configuration, operatorContext: OperatorContext) -> [Token] {

Sources/SwiftFormatRules/AlwaysUseLowerCamelCase.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ public final class AlwaysUseLowerCamelCase: SyntaxLintRule {
5151
}
5252

5353
public override func visit(_ node: VariableDeclSyntax) -> SyntaxVisitorContinueKind {
54+
// Don't diagnose any issues when the variable is overriding, because this declaration can't
55+
// rename the variable. If the user analyzes the code where the variable is really declared,
56+
// then the diagnostic can be raised for just that location.
57+
if let modifiers = node.modifiers, modifiers.has(modifier: "override") {
58+
return .visitChildren
59+
}
60+
5461
for binding in node.bindings {
5562
guard let pat = binding.pattern.as(IdentifierPatternSyntax.self) else {
5663
continue
@@ -94,6 +101,13 @@ public final class AlwaysUseLowerCamelCase: SyntaxLintRule {
94101
}
95102

96103
public override func visit(_ node: FunctionDeclSyntax) -> SyntaxVisitorContinueKind {
104+
// Don't diagnose any issues when the function is overriding, because this declaration can't
105+
// rename the function. If the user analyzes the code where the function is really declared,
106+
// then the diagnostic can be raised for just that location.
107+
if let modifiers = node.modifiers, modifiers.has(modifier: "override") {
108+
return .visitChildren
109+
}
110+
97111
// We allow underscores in test names, because there's an existing convention of using
98112
// underscores to separate phrases in very detailed test names.
99113
let allowUnderscores = testCaseFuncs.contains(node)

Sources/SwiftFormatRules/UseWhereClausesInForLoops.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public final class UseWhereClausesInForLoops: SyntaxFormatRule {
4141
// with a single condition whose body is just `continue`.
4242
switch stmt.item.as(SyntaxEnum.self) {
4343
case .ifStmt(let ifStmt)
44-
where ifStmt.conditions.count == 1 && node.body.statements.count == 1:
44+
where ifStmt.conditions.count == 1 && ifStmt.elseKeyword == nil
45+
&& node.body.statements.count == 1:
4546
// Extract the condition of the IfStmt.
4647
let conditionElement = ifStmt.conditions.first!
4748
guard let condition = conditionElement.condition.as(ExprSyntax.self) else {

Sources/swift-format/Frontend/FormatFrontend.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ class FormatFrontend: Frontend {
4949
to: &buffer,
5050
parsingDiagnosticHandler: diagnosticsEngine.consumeParserDiagnostic)
5151

52-
let bufferData = buffer.data(using: .utf8)! // Conversion to UTF-8 cannot fail
53-
try bufferData.write(to: url, options: .atomic)
52+
if buffer != source {
53+
let bufferData = buffer.data(using: .utf8)! // Conversion to UTF-8 cannot fail
54+
try bufferData.write(to: url, options: .atomic)
55+
}
5456
} else {
5557
try formatter.format(
5658
source: source,

Sources/swift-format/VersionOptions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct VersionOptions: ParsableArguments {
2020
func validate() throws {
2121
if version {
2222
// TODO: Automate updates to this somehow.
23-
print("0.50500.0")
23+
print("0.50700.0")
2424
throw ExitCode.success
2525
}
2626
}

Tests/SwiftFormatPrettyPrintTests/IfConfigTests.swift

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,164 @@ final class IfConfigTests: PrettyPrintTestCase {
230230

231231
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40)
232232
}
233+
234+
func testPostfixPoundIfAfterParentheses() {
235+
let input =
236+
"""
237+
VStack {
238+
Text("something")
239+
#if os(iOS)
240+
.iOSSpecificModifier()
241+
#endif
242+
.commonModifier()
243+
}
244+
"""
245+
246+
let expected =
247+
"""
248+
VStack {
249+
Text("something")
250+
#if os(iOS)
251+
.iOSSpecificModifier()
252+
#endif
253+
.commonModifier()
254+
}
255+
256+
"""
257+
258+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
259+
}
260+
261+
func testPostfixPoundIfAfterParenthesesMultipleMembers() {
262+
let input =
263+
"""
264+
VStack {
265+
Text("something")
266+
#if os(iOS)
267+
.iOSSpecificModifier()
268+
.anotherModifier()
269+
.anotherAnotherModifier()
270+
#endif
271+
.commonModifier()
272+
.anotherCommonModifier()
273+
}
274+
"""
275+
276+
let expected =
277+
"""
278+
VStack {
279+
Text("something")
280+
#if os(iOS)
281+
.iOSSpecificModifier()
282+
.anotherModifier()
283+
.anotherAnotherModifier()
284+
#endif
285+
.commonModifier()
286+
.anotherCommonModifier()
287+
}
288+
289+
"""
290+
291+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
292+
}
293+
294+
func testPostfixPoundIfNested() {
295+
let input =
296+
"""
297+
VStack {
298+
Text("something")
299+
#if os(iOS) || os(watchOS)
300+
#if os(iOS)
301+
.iOSModifier()
302+
#else
303+
.watchOSModifier()
304+
#endif
305+
.iOSAndWatchOSModifier()
306+
#endif
307+
}
308+
"""
309+
310+
let expected =
311+
"""
312+
VStack {
313+
Text("something")
314+
#if os(iOS) || os(watchOS)
315+
#if os(iOS)
316+
.iOSModifier()
317+
#else
318+
.watchOSModifier()
319+
#endif
320+
.iOSAndWatchOSModifier()
321+
#endif
322+
}
323+
324+
"""
325+
326+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
327+
}
328+
329+
330+
func testPostfixPoundIfAfterVariables() {
331+
let input =
332+
"""
333+
VStack {
334+
textView
335+
#if os(iOS)
336+
.iOSSpecificModifier()
337+
#endif
338+
.commonModifier()
339+
}
340+
"""
341+
342+
let expected =
343+
"""
344+
VStack {
345+
textView
346+
#if os(iOS)
347+
.iOSSpecificModifier()
348+
#endif
349+
.commonModifier()
350+
}
351+
352+
"""
353+
354+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
355+
}
356+
357+
func testPostfixPoundIfAfterClosingBrace() {
358+
let input =
359+
"""
360+
HStack {
361+
Toggle(isOn: binding) {
362+
Text("Some text")
363+
}
364+
#if !os(tvOS)
365+
.toggleStyle(SwitchToggleStyle(tint: Color.blue))
366+
#endif
367+
.accessibilityValue(
368+
binding.wrappedValue == true ? "On" : "Off"
369+
)
370+
}
371+
"""
372+
373+
let expected =
374+
"""
375+
HStack {
376+
Toggle(isOn: binding) {
377+
Text("Some text")
378+
}
379+
#if !os(tvOS)
380+
.toggleStyle(
381+
SwitchToggleStyle(tint: Color.blue))
382+
#endif
383+
.accessibilityValue(
384+
binding.wrappedValue == true
385+
? "On" : "Off"
386+
)
387+
}
388+
389+
"""
390+
391+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
392+
}
233393
}

Tests/SwiftFormatRulesTests/AlwaysUseLowerCamelCaseTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,25 @@ final class AlwaysUseLowerCamelCaseTests: LintOrFormatRuleTestCase {
123123
XCTAssertNotDiagnosed(
124124
.nameMustBeLowerCamelCase("test_HappyPath_Through_GoodCode_Throws", description: "function"))
125125
}
126+
127+
func testIgnoresFunctionOverrides() {
128+
let input =
129+
"""
130+
class ParentClass {
131+
var poorly_named_variable: Int = 5
132+
func poorly_named_method() {}
133+
}
134+
135+
class ChildClass: ParentClass {
136+
override var poorly_named_variable: Int = 5
137+
override func poorly_named_method() {}
138+
}
139+
"""
140+
141+
performLint(AlwaysUseLowerCamelCase.self, input: input)
142+
XCTAssertDiagnosed(
143+
.nameMustBeLowerCamelCase("poorly_named_variable", description: "variable"), line: 2, column: 7)
144+
XCTAssertDiagnosed(
145+
.nameMustBeLowerCamelCase("poorly_named_method", description: "function"), line: 3, column: 8)
146+
}
126147
}

Tests/SwiftFormatRulesTests/UseWhereClausesInForLoopsTests.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ final class UseWhereClausesInForLoopsTests: LintOrFormatRuleTestCase {
1111
}
1212
}
1313
14+
for i in [0, 1, 2, 3] {
15+
if i > 30 {
16+
print(i)
17+
} else {
18+
print(i)
19+
}
20+
}
21+
22+
for i in [0, 1, 2, 3] {
23+
if i > 30 {
24+
print(i)
25+
} else if i > 40 {
26+
print(i)
27+
}
28+
}
29+
1430
for i in [0, 1, 2, 3] {
1531
if i > 30 {
1632
print(i)
@@ -36,6 +52,22 @@ final class UseWhereClausesInForLoopsTests: LintOrFormatRuleTestCase {
3652
print(i)
3753
}
3854
55+
for i in [0, 1, 2, 3] {
56+
if i > 30 {
57+
print(i)
58+
} else {
59+
print(i)
60+
}
61+
}
62+
63+
for i in [0, 1, 2, 3] {
64+
if i > 30 {
65+
print(i)
66+
} else if i > 40 {
67+
print(i)
68+
}
69+
}
70+
3971
for i in [0, 1, 2, 3] {
4072
if i > 30 {
4173
print(i)

Tests/SwiftFormatTests/SyntaxValidatingVisitorTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final class SyntaxValidatingVisitorTests: XCTestCase {
2828
var bar = 0
2929
}
3030
"""
31-
assertInvalidSyntax(in: input, atLine: 1, column: 1)
31+
assertInvalidSyntax(in: input, atLine: 1, column: 7)
3232

3333
input =
3434
"""

0 commit comments

Comments
 (0)