-
Notifications
You must be signed in to change notification settings - Fork 630
/
Examples.swift
1915 lines (1627 loc) · 34.4 KB
/
Examples.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Examples.swift
// SwiftFormat
//
// Created by Nick Lockwood on 07/02/2019.
// Copyright © 2019 Nick Lockwood. All rights reserved.
//
import Foundation
extension FormatRule {
var examples: String? {
examplesByRuleName[name]
}
}
extension _FormatRules {
var examplesByName: [String: String] {
examplesByRuleName
}
}
private let examplesByRuleName: [String: String] = {
var examples = [String: String]()
for (label, value) in Mirror(reflecting: Examples()).children {
guard let name = label, let text = value as? String, !text.isEmpty else {
continue
}
examples[name] = text
}
return examples
}()
private struct Examples {
let andOperator = """
```diff
- if true && true {
+ if true, true {
```
```diff
- guard true && true else {
+ guard true, true else {
```
```diff
- if functionReturnsBool() && true {
+ if functionReturnsBool(), true {
```
```diff
- if functionReturnsBool() && variable {
+ if functionReturnsBool(), variable {
```
"""
let anyObjectProtocol = """
```diff
- protocol Foo: class {}
+ protocol Foo: AnyObject {}
```
**NOTE:** The guideline to use `AnyObject` instead of `class` was only
introduced in Swift 4.1, so the `anyObjectProtocol` rule is disabled unless the
swift version is set to 4.1 or above.
"""
let blankLinesAtEndOfScope = """
```diff
func foo() {
// foo
-
}
func foo() {
// foo
}
```
```diff
array = [
foo,
bar,
baz,
-
]
array = [
foo,
bar,
baz,
]
```
"""
let blankLinesAtStartOfScope = """
```diff
func foo() {
-
// foo
}
func foo() {
// foo
}
```
```diff
array = [
-
foo,
bar,
baz,
]
array = [
foo,
bar,
baz,
]
```
"""
let blankLinesBetweenImports = """
```diff
import A
-
import B
import C
-
-
@testable import D
import E
```
"""
let blankLineAfterImports = """
```diff
import A
import B
@testable import D
+
class Foo {
// foo
}
```
"""
let blankLinesBetweenScopes = """
```diff
func foo() {
// foo
}
func bar() {
// bar
}
var baz: Bool
var quux: Int
func foo() {
// foo
}
+
func bar() {
// bar
}
+
var baz: Bool
var quux: Int
```
"""
let blankLinesAroundMark = """
```diff
func foo() {
// foo
}
// MARK: bar
func bar() {
// bar
}
func foo() {
// foo
}
+
// MARK: bar
+
func bar() {
// bar
}
```
"""
let braces = """
```diff
- if x
- {
// foo
}
- else
- {
// bar
}
+ if x {
// foo
}
+ else {
// bar
}
```
"""
let consecutiveBlankLines = """
```diff
func foo() {
let x = "bar"
-
print(x)
}
func foo() {
let x = "bar"
print(x)
}
```
"""
let consecutiveSpaces = """
```diff
- let foo = 5
+ let foo = 5
```
"""
let duplicateImports = """
```diff
import Foo
import Bar
- import Foo
```
```diff
import B
#if os(iOS)
import A
- import B
#endif
```
"""
let elseOnSameLine = """
```diff
if x {
// foo
- }
- else {
// bar
}
if x {
// foo
+ } else {
// bar
}
```
```diff
do {
// try foo
- }
- catch {
// bar
}
do {
// try foo
+ } catch {
// bar
}
```
```diff
repeat {
// foo
- }
- while {
// bar
}
repeat {
// foo
+ } while {
// bar
}
```
"""
let emptyBraces = """
```diff
- func foo() {
-
- }
+ func foo() {}
```
"""
let wrapConditionalBodies = """
```diff
- guard let foo = bar else { return baz }
+ guard let foo = bar else {
+ return baz
+ }
```
```diff
- if foo { return bar }
+ if foo {
+ return bar
+ }
```
"""
let wrapLoopBodies = """
```diff
- for foo in array { print(foo) }
+ for foo in array {
+ print(foo)
+ }
```
```diff
- while let foo = bar.next() { print(foo) }
+ while let foo = bar.next() {
+ print(foo)
+ }
```
"""
let hoistPatternLet = """
```diff
- (let foo, let bar) = baz()
+ let (foo, bar) = baz()
```
```diff
- if case .foo(let bar, let baz) = quux {
// inner foo
}
+ if case let .foo(bar, baz) = quux {
// inner foo
}
```
"""
let hoistAwait = """
```diff
- greet(await forename, await surname)
+ await greet(forename, surname)
```
```diff
- let foo = String(try await getFoo())
+ let foo = await String(try getFoo())
```
"""
let hoistTry = """
```diff
- foo(try bar(), try baz())
+ try foo(bar(), baz())
```
```diff
- let foo = String(try await getFoo())
+ let foo = try String(await getFoo())
```
"""
let indent = """
```diff
if x {
- // foo
} else {
- // bar
- }
if x {
+ // foo
} else {
+ // bar
+ }
```
```diff
let array = [
foo,
- bar,
- baz
- ]
let array = [
foo,
+ bar,
+ baz
+ ]
```
```diff
switch foo {
- case bar: break
- case baz: break
}
switch foo {
+ case bar: break
+ case baz: break
}
```
"""
let isEmpty = """
```diff
- if foo.count == 0 {
+ if foo.isEmpty {
- if foo.count > 0 {
+ if !foo.isEmpty {
- if foo?.count == 0 {
+ if foo?.isEmpty == true {
```
***NOTE:*** In rare cases, the `isEmpty` rule may insert an `isEmpty` call for
a type that doesn't implement that property, breaking the program. For this
reason, the rule is disabled by default, and must be manually enabled via the
`--enable isEmpty` option.
"""
let initCoderUnavailable = """
```diff
+ @available(*, unavailable)
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
```
"""
let numberFormatting = """
```diff
- let color = 0xFF77A5
+ let color = 0xff77a5
```
```diff
- let big = 123456.123
+ let big = 123_456.123
```
"""
let redundantBackticks = """
```diff
- let `infix` = bar
+ let infix = bar
```
```diff
- func foo(with `default`: Int) {}
+ func foo(with default: Int) {}
```
"""
let redundantBreak = """
```diff
switch foo {
case bar:
print("bar")
- break
default:
print("default")
- break
}
```
"""
let redundantGet = """
```diff
var foo: Int {
- get {
- return 5
- }
}
var foo: Int {
+ return 5
}
```
"""
let redundantExtensionACL = """
```diff
public extension URL {
- public func queryParameter(_ name: String) -> String { ... }
}
public extension URL {
+ func queryParameter(_ name: String) -> String { ... }
}
```
"""
let redundantFileprivate = """
```diff
- fileprivate let someConstant = "someConstant"
+ private let someConstant = "someConstant"
```
In Swift 4 and above, `fileprivate` can also be replaced with `private` for
members that are only accessed from extensions in the same file:
```diff
class Foo {
- fileprivate var foo = "foo"
+ private var foo = "foo"
}
extension Foo {
func bar() {
print(self.foo)
}
}
```
"""
let redundantLet = """
```diff
- let _ = foo()
+ _ = foo()
```
"""
let redundantLetError = """
```diff
- do { ... } catch let error { log(error) }
+ do { ... } catch { log(error) }
```
"""
let redundantNilInit = """
`--nilinit remove`
```diff
- var foo: Int? = nil
+ var foo: Int?
```
```diff
// doesn't apply to `let` properties
let foo: Int? = nil
```
```diff
// doesn't affect non-nil initialization
var foo: Int? = 0
```
`--nilinit insert`
```diff
- var foo: Int?
+ var foo: Int? = nil
```
"""
let redundantObjc = """
```diff
- @objc @IBOutlet var label: UILabel!
+ @IBOutlet var label: UILabel!
```
```diff
- @IBAction @objc func goBack() {}
+ @IBAction func goBack() {}
```
```diff
- @objc @NSManaged private var foo: String?
+ @NSManaged private var foo: String?
```
"""
let redundantParens = """
```diff
- if (foo == true) {}
+ if foo == true {}
```
```diff
- while (i < bar.count) {}
+ while i < bar.count {}
```
```diff
- queue.async() { ... }
+ queue.async { ... }
```
```diff
- let foo: Int = ({ ... })()
+ let foo: Int = { ... }()
```
"""
let redundantPattern = """
```diff
- if case .foo(_, _) = bar {}
+ if case .foo = bar {}
```
```diff
- let (_, _) = bar
+ let _ = bar
```
"""
let redundantRawValues = """
```diff
enum Foo: String {
- case bar = "bar"
case baz = "quux"
}
enum Foo: String {
+ case bar
case baz = "quux"
}
```
"""
let redundantReturn = """
```diff
- array.filter { return $0.foo == bar }
+ array.filter { $0.foo == bar }
// Swift 5.1+ (SE-0255)
var foo: String {
- return "foo"
+ "foo"
}
// Swift 5.9+ (SE-0380) and with conditionalAssignment rule enabled
func foo(_ condition: Bool) -> String {
if condition {
- return "foo"
+ "foo"
} else {
- return "bar"
+ "bar"
}
}
```
"""
let redundantSelf = """
```diff
func foobar(foo: Int, bar: Int) {
self.foo = foo
self.bar = bar
- self.baz = 42
}
func foobar(foo: Int, bar: Int) {
self.foo = foo
self.bar = bar
+ baz = 42
}
```
In the rare case of functions with `@autoclosure` arguments, `self` may be
required at the call site, but SwiftFormat is unable to detect this
automatically. You can use the `--selfrequired` command-line option to specify
a list of such methods, and the `redundantSelf` rule will then ignore them.
An example of such a method is the `expect()` function in the Nimble unit
testing framework (https://github.com/Quick/Nimble), which is common enough that
SwiftFormat excludes it by default.
There is also an option to always use explicit `self` but *only* inside `init`,
by using `--self init-only`:
```diff
init(foo: Int, bar: Int) {
self.foo = foo
self.bar = bar
- baz = 42
}
init(foo: Int, bar: Int) {
self.foo = foo
self.bar = bar
+ self.baz = 42
}
```
"""
let redundantVoidReturnType = """
```diff
- func foo() -> Void {
// returns nothing
}
+ func foo() {
// returns nothing
}
```
"""
let redundantInit = """
```diff
- String.init("text")
+ String("text")
```
"""
let semicolons = """
```diff
- let foo = 5;
+ let foo = 5
```
```diff
- let foo = 5; let bar = 6
+ let foo = 5
+ let bar = 6
```
```diff
// semicolon is not removed if it would affect the behavior of the code
return;
goto(fail)
```
"""
let sortImports = """
```diff
- import Foo
- import Bar
+ import Bar
+ import Foo
```
```diff
- import B
- import A
- #if os(iOS)
- import Foo-iOS
- import Bar-iOS
- #endif
+ import A
+ import B
+ #if os(iOS)
+ import Bar-iOS
+ import Foo-iOS
+ #endif
```
"""
let spaceAroundBraces = """
```diff
- foo.filter{ return true }.map{ $0 }
+ foo.filter { return true }.map { $0 }
```
```diff
- foo( {} )
+ foo({})
```
"""
let spaceAroundBrackets = """
```diff
- foo as[String]
+ foo as [String]
```
```diff
- foo = bar [5]
+ foo = bar[5]
```
"""
let spaceAroundComments = """
```diff
- let a = 5// assignment
+ let a = 5 // assignment
```
```diff
- func foo() {/* ... */}
+ func foo() { /* ... */ }
```
"""
let spaceAroundGenerics = """
```diff
- Foo <Bar> ()
+ Foo<Bar>()
```
"""
let spaceAroundOperators = """
```diff
- foo . bar()
+ foo.bar()
```
```diff
- a+b+c
+ a + b + c
```
```diff
- func ==(lhs: Int, rhs: Int) -> Bool
+ func == (lhs: Int, rhs: Int) -> Bool
```
"""
let spaceAroundParens = """
```diff
- init (foo)
+ init(foo)
```
```diff
- switch(x){
+ switch (x) {
```
"""
let spaceInsideBraces = """
```diff
- foo.filter {return true}
+ foo.filter { return true }
```
"""
let spaceInsideBrackets = """
```diff
- [ 1, 2, 3 ]
+ [1, 2, 3]
```
"""
let spaceInsideComments = """
```diff
- let a = 5 //assignment
+ let a = 5 // assignment
```
```diff
- func foo() { /*...*/ }
+ func foo() { /* ... */ }
```
"""
let spaceInsideGenerics = """
```diff
- Foo< Bar, Baz >
+ Foo<Bar, Baz>
```
"""
let spaceInsideParens = """
```diff
- ( a, b)
+ (a, b)
```
"""
let redundantType = """
```diff
// inferred
- let view: UIView = UIView()
+ let view = UIView()
// explicit
- let view: UIView = UIView()
+ let view: UIView = .init()
// infer-locals-only
class Foo {
- let view: UIView = UIView()
+ let view: UIView = .init()
func method() {
- let view: UIView = UIView()
+ let view = UIView()
}
}
// Swift 5.9+, inferred (SE-0380)
- let foo: Foo = if condition {
+ let foo = if condition {
Foo("foo")
} else {
Foo("bar")
}
// Swift 5.9+, explicit (SE-0380)
let foo: Foo = if condition {
- Foo("foo")
+ .init("foo")
} else {
- Foo("bar")
+ .init("foo")
}
```
"""
let modifierOrder = """
```diff
- lazy public weak private(set) var foo: UIView?
+ public private(set) lazy weak var foo: UIView?
```
```diff
- final public override func foo()
+ override public final func foo()
```
```diff
- convenience private init()
+ private convenience init()
```
**NOTE:** If the `--modifierorder` option isn't set, the default order will be:
`\(_FormatRules.defaultModifierOrder.flatMap { $0 }.joined(separator: "`, `"))`
"""
let strongifiedSelf = """
```diff
- guard let `self` = self else { return }
+ guard let self = self else { return }
```
**NOTE:** assignment to un-escaped `self` is only supported in Swift 4.2 and
above, so the `strongifiedSelf` rule is disabled unless the Swift version is
set to 4.2 or above.
"""
let strongOutlets = """
As per Apple's recommendation
(https://developer.apple.com/videos/play/wwdc2015/407/ @ 32:30).
```diff
- @IBOutlet weak var label: UILabel!
+ @IBOutlet var label: UILabel!
```
"""
let trailingClosures = """
```diff
- DispatchQueue.main.async(execute: { ... })
+ DispatchQueue.main.async {
```
```diff
- let foo = bar.map({ ... }).joined()
+ let foo = bar.map { ... }.joined()
```
"""
let trailingCommas = """
```diff
let array = [
foo,
bar,
- baz
]
let array = [
foo,
bar,
+ baz,
]
```
"""
let todos = """
```diff
- /* TODO fix this properly */
+ /* TODO: fix this properly */
```
```diff