Skip to content

Commit f646a1f

Browse files
committed
Merge pull request #278 from allevato/property-effects
Support `async` and `throws` on property/subscript getters.
1 parent 3e924a8 commit f646a1f

File tree

3 files changed

+284
-0
lines changed

3 files changed

+284
-0
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

+16
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,22 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
441441

442442
override func visit(_ node: AccessorDeclSyntax) -> SyntaxVisitorContinueKind {
443443
arrangeAttributeList(node.attributes)
444+
445+
if let asyncKeyword = node.asyncKeyword {
446+
if node.throwsKeyword != nil {
447+
before(asyncKeyword, tokens: .break, .open)
448+
} else {
449+
before(asyncKeyword, tokens: .break)
450+
}
451+
}
452+
453+
if let throwsKeyword = node.throwsKeyword {
454+
before(node.throwsKeyword, tokens: .break)
455+
if node.asyncKeyword != nil {
456+
after(throwsKeyword, tokens: .close)
457+
}
458+
}
459+
444460
arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)
445461
return .visitChildren
446462
}

Tests/SwiftFormatPrettyPrintTests/AccessorTests.swift

+157
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,161 @@ final class AccessorTests: PrettyPrintTestCase {
295295

296296
assertPrettyPrintEqual(input: input, expected: expected20, linelength: 20)
297297
}
298+
299+
func testPropertyEffectsWithBodyAfter() {
300+
let input =
301+
"""
302+
var x: T {
303+
get async {
304+
foo()
305+
bar()
306+
}
307+
}
308+
var x: T {
309+
get throws {
310+
foo()
311+
bar()
312+
}
313+
}
314+
var x: T {
315+
get async throws {
316+
foo()
317+
bar()
318+
}
319+
}
320+
"""
321+
322+
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 80)
323+
324+
let expected16 =
325+
"""
326+
var x: T {
327+
get async {
328+
foo()
329+
bar()
330+
}
331+
}
332+
var x: T {
333+
get throws {
334+
foo()
335+
bar()
336+
}
337+
}
338+
var x: T {
339+
get
340+
async throws
341+
{
342+
foo()
343+
bar()
344+
}
345+
}
346+
347+
"""
348+
349+
assertPrettyPrintEqual(input: input, expected: expected16, linelength: 16)
350+
351+
let expected10 =
352+
"""
353+
var x: T {
354+
get
355+
async
356+
{
357+
foo()
358+
bar()
359+
}
360+
}
361+
var x: T {
362+
get
363+
throws
364+
{
365+
foo()
366+
bar()
367+
}
368+
}
369+
var x: T {
370+
get
371+
async
372+
throws
373+
{
374+
foo()
375+
bar()
376+
}
377+
}
378+
379+
"""
380+
381+
assertPrettyPrintEqual(input: input, expected: expected10, linelength: 10)
382+
}
383+
384+
func testPropertyEffectsWithNoBodyAfter() {
385+
let input =
386+
"""
387+
protocol P {
388+
var x: T { get async }
389+
var x: T { get throws }
390+
var x: T { get async throws }
391+
}
392+
"""
393+
394+
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 80)
395+
396+
let expected20 =
397+
"""
398+
protocol P {
399+
var x: T {
400+
get async
401+
}
402+
var x: T {
403+
get throws
404+
}
405+
var x: T {
406+
get async throws
407+
}
408+
}
409+
410+
"""
411+
412+
assertPrettyPrintEqual(input: input, expected: expected20, linelength: 20)
413+
414+
let expected18 =
415+
"""
416+
protocol P {
417+
var x: T {
418+
get async
419+
}
420+
var x: T {
421+
get throws
422+
}
423+
var x: T {
424+
get
425+
async throws
426+
}
427+
}
428+
429+
"""
430+
431+
assertPrettyPrintEqual(input: input, expected: expected18, linelength: 18)
432+
433+
let expected12 =
434+
"""
435+
protocol P {
436+
var x: T {
437+
get
438+
async
439+
}
440+
var x: T {
441+
get
442+
throws
443+
}
444+
var x: T {
445+
get
446+
async
447+
throws
448+
}
449+
}
450+
451+
"""
452+
453+
assertPrettyPrintEqual(input: input, expected: expected12, linelength: 12)
454+
}
298455
}

Tests/SwiftFormatPrettyPrintTests/SubscriptDeclTests.swift

+111
Original file line numberDiff line numberDiff line change
@@ -497,4 +497,115 @@ final class SubscriptDeclTests: PrettyPrintTestCase {
497497
"""
498498
assertPrettyPrintEqual(input: input, expected: wrapped, linelength: 28)
499499
}
500+
501+
func testAccessorEffectsWithBodyAfter() {
502+
let input =
503+
"""
504+
struct X {
505+
subscript(i: Int) -> T {
506+
get async throws {
507+
foo()
508+
bar()
509+
}
510+
}
511+
}
512+
"""
513+
514+
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 80)
515+
516+
let expected18 =
517+
"""
518+
struct X {
519+
subscript(
520+
i: Int
521+
) -> T {
522+
get
523+
async throws
524+
{
525+
foo()
526+
bar()
527+
}
528+
}
529+
}
530+
531+
"""
532+
533+
assertPrettyPrintEqual(input: input, expected: expected18, linelength: 18)
534+
535+
let expected12 =
536+
"""
537+
struct X {
538+
subscript(
539+
i: Int
540+
) -> T {
541+
get
542+
async
543+
throws
544+
{
545+
foo()
546+
bar()
547+
}
548+
}
549+
}
550+
551+
"""
552+
553+
assertPrettyPrintEqual(input: input, expected: expected12, linelength: 12)
554+
}
555+
556+
func testAccessorEffectsWithNoBodyAfter() {
557+
let input =
558+
"""
559+
protocol P {
560+
subscript(i: Int) -> T { get async throws }
561+
}
562+
"""
563+
564+
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 80)
565+
566+
let expected20 =
567+
"""
568+
protocol P {
569+
subscript(i: Int)
570+
-> T
571+
{
572+
get async throws
573+
}
574+
}
575+
576+
"""
577+
578+
assertPrettyPrintEqual(input: input, expected: expected20, linelength: 20)
579+
580+
let expected18 =
581+
"""
582+
protocol P {
583+
subscript(
584+
i: Int
585+
) -> T {
586+
get
587+
async throws
588+
}
589+
}
590+
591+
"""
592+
593+
assertPrettyPrintEqual(input: input, expected: expected18, linelength: 18)
594+
595+
let expected16 =
596+
"""
597+
protocol P {
598+
subscript(
599+
i: Int
600+
) -> T {
601+
get
602+
async
603+
throws
604+
}
605+
}
606+
607+
"""
608+
609+
assertPrettyPrintEqual(input: input, expected: expected16, linelength: 16)
610+
}
500611
}

0 commit comments

Comments
 (0)