Skip to content

[5.6] Async fixes and README update #285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ Swift toolchain that is installed and used to build and run the formatter:
| Xcode Release | Swift Version | `swift-format` Branch / Tags |
|:----------------|:-----------------------|:---------------------------------|
| – | Swift at `main` | `main` |
| Xcode 13.3 | Swift 5.6 | `release/5.6` / `0.50600.x` |
| Xcode 13.0–13.2 | Swift 5.5 | `swift-5.5-branch` / `0.50500.x` |
| Xcode 12.5 | Swift 5.4 | `swift-5.4-branch` / `0.50400.x` |
| Xcode 12.0–12.4 | Swift 5.3 | `swift-5.3-branch` / `0.50300.x` |
| Xcode 11.4–11.7 | Swift 5.2 | `swift-5.2-branch` / `0.50200.x` |
| Xcode 11.0–11.3 | Swift 5.1 | `swift-5.1-branch` |

For example, if you are using Xcode 13.1 (Swift 5.5), you will need
`swift-format` 0.50500.0.
For example, if you are using Xcode 13.3 (Swift 5.6), you will need
`swift-format` 0.50600.0.

## Getting swift-format

Expand All @@ -44,7 +45,7 @@ then once you have identified the version you need, you can check out the
source and build it using the following commands:

```sh
VERSION=0.50500.0 # replace this with the version you need
VERSION=0.50600.0 # replace this with the version you need
git clone https://github.com/apple/swift-format.git
cd swift-format
git checkout "tags/$VERSION"
Expand Down
16 changes: 16 additions & 0 deletions Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,22 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {

override func visit(_ node: AccessorDeclSyntax) -> SyntaxVisitorContinueKind {
arrangeAttributeList(node.attributes)

if let asyncKeyword = node.asyncKeyword {
if node.throwsKeyword != nil {
before(asyncKeyword, tokens: .break, .open)
} else {
before(asyncKeyword, tokens: .break)
}
}

if let throwsKeyword = node.throwsKeyword {
before(node.throwsKeyword, tokens: .break)
if node.asyncKeyword != nil {
after(throwsKeyword, tokens: .close)
}
}

arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)
return .visitChildren
}
Expand Down
4 changes: 3 additions & 1 deletion Sources/SwiftFormatRules/UseSingleLinePropertyGetter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public final class UseSingleLinePropertyGetter: SyntaxFormatRule {
accessorBlock.accessors.count == 1,
acc.accessorKind.tokenKind == .contextualKeyword("get"),
acc.attributes == nil,
acc.modifier == nil
acc.modifier == nil,
acc.asyncKeyword == nil,
acc.throwsKeyword == nil
else { return Syntax(node) }

diagnose(.removeExtraneousGetBlock, on: acc)
Expand Down
157 changes: 157 additions & 0 deletions Tests/SwiftFormatPrettyPrintTests/AccessorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,161 @@ final class AccessorTests: PrettyPrintTestCase {

assertPrettyPrintEqual(input: input, expected: expected20, linelength: 20)
}

func testPropertyEffectsWithBodyAfter() {
let input =
"""
var x: T {
get async {
foo()
bar()
}
}
var x: T {
get throws {
foo()
bar()
}
}
var x: T {
get async throws {
foo()
bar()
}
}
"""

assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 80)

let expected16 =
"""
var x: T {
get async {
foo()
bar()
}
}
var x: T {
get throws {
foo()
bar()
}
}
var x: T {
get
async throws
{
foo()
bar()
}
}

"""

assertPrettyPrintEqual(input: input, expected: expected16, linelength: 16)

let expected10 =
"""
var x: T {
get
async
{
foo()
bar()
}
}
var x: T {
get
throws
{
foo()
bar()
}
}
var x: T {
get
async
throws
{
foo()
bar()
}
}

"""

assertPrettyPrintEqual(input: input, expected: expected10, linelength: 10)
}

func testPropertyEffectsWithNoBodyAfter() {
let input =
"""
protocol P {
var x: T { get async }
var x: T { get throws }
var x: T { get async throws }
}
"""

assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 80)

let expected20 =
"""
protocol P {
var x: T {
get async
}
var x: T {
get throws
}
var x: T {
get async throws
}
}

"""

assertPrettyPrintEqual(input: input, expected: expected20, linelength: 20)

let expected18 =
"""
protocol P {
var x: T {
get async
}
var x: T {
get throws
}
var x: T {
get
async throws
}
}

"""

assertPrettyPrintEqual(input: input, expected: expected18, linelength: 18)

let expected12 =
"""
protocol P {
var x: T {
get
async
}
var x: T {
get
throws
}
var x: T {
get
async
throws
}
}

"""

assertPrettyPrintEqual(input: input, expected: expected12, linelength: 12)
}
}
111 changes: 111 additions & 0 deletions Tests/SwiftFormatPrettyPrintTests/SubscriptDeclTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -497,4 +497,115 @@ final class SubscriptDeclTests: PrettyPrintTestCase {
"""
assertPrettyPrintEqual(input: input, expected: wrapped, linelength: 28)
}

func testAccessorEffectsWithBodyAfter() {
let input =
"""
struct X {
subscript(i: Int) -> T {
get async throws {
foo()
bar()
}
}
}
"""

assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 80)

let expected18 =
"""
struct X {
subscript(
i: Int
) -> T {
get
async throws
{
foo()
bar()
}
}
}

"""

assertPrettyPrintEqual(input: input, expected: expected18, linelength: 18)

let expected12 =
"""
struct X {
subscript(
i: Int
) -> T {
get
async
throws
{
foo()
bar()
}
}
}

"""

assertPrettyPrintEqual(input: input, expected: expected12, linelength: 12)
}

func testAccessorEffectsWithNoBodyAfter() {
let input =
"""
protocol P {
subscript(i: Int) -> T { get async throws }
}
"""

assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 80)

let expected20 =
"""
protocol P {
subscript(i: Int)
-> T
{
get async throws
}
}

"""

assertPrettyPrintEqual(input: input, expected: expected20, linelength: 20)

let expected18 =
"""
protocol P {
subscript(
i: Int
) -> T {
get
async throws
}
}

"""

assertPrettyPrintEqual(input: input, expected: expected18, linelength: 18)

let expected16 =
"""
protocol P {
subscript(
i: Int
) -> T {
get
async
throws
}
}

"""

assertPrettyPrintEqual(input: input, expected: expected16, linelength: 16)
}
}
30 changes: 30 additions & 0 deletions Tests/SwiftFormatRulesTests/UseSingleLinePropertyGetterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ final class UseSingleLinePropertyGetterTests: LintOrFormatRuleTestCase {
var j: Int {
mutating get { return 0 }
}
var k: Int {
get async {
return 4
}
}
var l: Int {
get throws {
return 4
}
}
var m: Int {
get async throws {
return 4
}
}
""",
expected: """
var g: Int { return 4 }
Expand All @@ -31,6 +46,21 @@ final class UseSingleLinePropertyGetterTests: LintOrFormatRuleTestCase {
var j: Int {
mutating get { return 0 }
}
var k: Int {
get async {
return 4
}
}
var l: Int {
get throws {
return 4
}
}
var m: Int {
get async throws {
return 4
}
}
""")
}
}