Skip to content
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

Update minimum supported Swift version to Swift 5.7 #1837

Draft
wants to merge 52 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
e5692e1
`--conditionswrap` option to format condition in Xcode 12 style, in c…
Jan 27, 2021
44d79dd
Add preferInferredTypes rule
calda Mar 12, 2024
2b8d27d
Make preferInferredTypes rule more compatible with redundantType rule
calda Mar 15, 2024
b873d7d
Extend preferInferredTypes rule to support if/switch expressions
calda Mar 24, 2024
0bf5531
Fix issue where preferInferredTypes would cause build failure if prop…
calda Mar 28, 2024
08d52d5
Fix issue where preferInferredTypes could cause a build failure if th…
calda Mar 28, 2024
19e2685
Update preferInferedTypes rule to propertyType, add support for expli…
calda Mar 29, 2024
dc52243
Alphabetically sort organized declarations by name pattern
Jun 10, 2024
4667e23
Support automatic sorting by name pattern for `sortDeclarations` rule…
oiuhr Jun 18, 2024
152da3a
Add SwiftUI Property Wrapper declaration type (#1747)
miguel-jimenez-0529 Jul 3, 2024
fe644d5
Include original token range when parsing declarations (#1759)
calda Jul 17, 2024
6fe2247
Add unusedPrivateDeclaration rule to remove unused private and filepr…
mannylopez Jul 18, 2024
9f29782
`organizeDeclarations` custom naming & ordering (#1736)
oiuhr Jul 19, 2024
51b369a
Add allowList to unusedPrivateDeclaration (#1761)
mannylopez Jul 20, 2024
4781d16
Improve expressiveness of --typeorder and --visibilityorder options (…
calda Jul 20, 2024
758d010
Add typealias to unusedPrivateDeclaration allowlist (#1764)
calda Jul 20, 2024
4f76454
Add docCommentBeforeAttributes rule (#1766)
calda Jul 22, 2024
c26282b
Improve code organization of organizeDeclarations rule and related he…
calda Jul 22, 2024
bbe2bee
Fix build of CommandLineTool product
calda Jul 22, 2024
435ce82
Improve docCommentsBeforeAttributes rule, fix support for declaration…
calda Jul 22, 2024
5cb234f
Fix parseDeclarations bug where incorrect tokens could cause rules to…
calda Jul 22, 2024
c74333a
Update unusedPrivateDeclaration and handle false positives (#1771)
mannylopez Jul 23, 2024
a9507a1
Update docCommentsBeforeAttributes rule to only apply to doc comments…
calda Jul 26, 2024
a3005a7
Add exceptions to unusedPrivateDeclaration rule (#1779)
mannylopez Jul 27, 2024
2172c0a
Fix FocusState typo (#1785)
miguel-jimenez-0529 Jul 28, 2024
d79cb43
Implement each rule in a separate file (#1782)
calda Jul 29, 2024
8ae2da9
Update `exclude` to reference rules using static member lookup (#1787)
calda Jul 29, 2024
e4947df
Implement tests for each rule in a separate file (#1789)
calda Jul 29, 2024
a23b916
Update organizeDeclarations to avoid inserting blank lines inside con…
calda Jul 30, 2024
1fd1040
Ensure SwiftUI property reorder doesn't affect synthesized init (#1796)
miguel-jimenez-0529 Jul 30, 2024
6a43b76
Add emptyExtension rule + tests (#1792)
mannylopez Jul 30, 2024
657bb1d
Fix issue where closing brace could turn into a comment unexpectedly …
calda Jul 31, 2024
593c31c
Move rule-specific helpers from DeclarationHelpers.swift to OrganizeD…
calda Jul 31, 2024
c4f7823
Add SwiftUI properties subcategory alphabetical sort (#1794)
miguel-jimenez-0529 Jul 31, 2024
ce9af67
[New rule] spacingGuards (#1804)
NikKovIos Aug 1, 2024
c01d68e
Add test case to unusedPrivateDeclaration (#1805)
mannylopez Aug 1, 2024
651b4d0
Updated readme about how to test own rule. (#1807)
NikKovIos Aug 2, 2024
e7bb487
Update `MetadataTests.testRulesOptions()` to check entire rule file, …
calda Aug 3, 2024
c780352
Add --outputtokens option to print output tokens instead of source co…
calda Aug 6, 2024
a4cf33b
Fix issue where organizeDeclarations would add extra blank lines if t…
calda Aug 9, 2024
6310701
Fix issue where unusedArguments didn't handle conditional assignment …
calda Aug 11, 2024
ab4d327
Add tests related to code organization and rule helper usage (#1808)
calda Aug 11, 2024
fc6857d
Add preserve option to spaceAroundRangeOperators and spaceAroundOpera…
calda Aug 12, 2024
bc39514
Add option to sort SwiftUI properties by the first property appearanc…
miguel-jimenez-0529 Aug 20, 2024
16af7f5
Remove redundant Void from protocol definitions (#1824)
miguel-jimenez-0529 Aug 22, 2024
7689140
Fix wrapped array type extension mistaken for collection
nicklockwood Aug 24, 2024
d94a4a7
Move examples into rules files
nicklockwood Aug 24, 2024
5df3377
Fix missing lint output for organizeDeclarations rule (#1826)
calda Aug 25, 2024
df3106b
Fix typos discovered by codespell (#1835)
cclauss Aug 25, 2024
d2be392
Update minimum supported Swift version to Swift 5.7
calda Aug 26, 2024
0202f5e
Reformat code with --swiftversion 5.7
calda Aug 26, 2024
dbd22c8
We only need one Package.swift now
calda Aug 26, 2024
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
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ jobs:
fail-fast: false
matrix:
swiftver:
- swift:5.2
- swift:5.6
- swift:5.7
- swift:5.10
swiftos:
- focal
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .swiftformat
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
--self remove
--semicolons inline
--stripunusedargs always
--swiftversion 5.1
--swiftversion 5.7
--trimwhitespace always
--wraparguments preserve
--wrapcollections preserve
Expand Down
8 changes: 4 additions & 4 deletions EditorExtension/Application/Source/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import Cocoa

@NSApplicationMain
@main
class AppDelegate: NSObject, NSApplicationDelegate {
var window: NSWindow? {
NSApp.mainWindow
Expand Down Expand Up @@ -87,7 +87,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

@IBAction func openConfiguration(_: NSMenuItem) {
guard let window = window else {
guard let window else {
return
}

Expand All @@ -108,7 +108,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

@IBAction func saveConfiguration(_: NSMenuItem) {
guard let window = window else {
guard let window else {
return
}

Expand Down Expand Up @@ -137,7 +137,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

private func showError(_ error: Error) {
guard let window = window else {
guard let window else {
return
}

Expand Down
2 changes: 1 addition & 1 deletion EditorExtension/Extension/FormatFileCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class FormatFileCommand: NSObject, XCSourceEditorCommand {

let output: [Token]
do {
output = try format(input, rules: rules, options: formatOptions)
output = try format(input, rules: rules, options: formatOptions).tokens
} catch {
return completionHandler(error)
}
Expand Down
2 changes: 1 addition & 1 deletion EditorExtension/Extension/FormatSelectionCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class FormatSelectionCommand: NSObject, XCSourceEditorCommand {
let start = tokenIndex(for: startOffset, in: output, tabWidth: formatOptions.tabWidth)
let end = tokenIndex(for: endOffset, in: output, tabWidth: formatOptions.tabWidth)
do {
output = try format(output, rules: rules, options: formatOptions, range: start ..< end)
output = try format(output, rules: rules, options: formatOptions, range: start ..< end).tokens
} catch {
return completionHandler(error)
}
Expand Down
2 changes: 1 addition & 1 deletion EditorExtension/Shared/RulesStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ extension Rule: Comparable {
/// Space-separated, lowercased text terms that this rule might by found by.
var searchableText: String {
var items = [name]
if let formatRule = formatRule {
if let formatRule {
items.append(formatRule.help.keywords.joined(separator: " "))
items.append(formatRule.options.joined(separator: " "))
items.append(formatRule.sharedOptions.joined(separator: " "))
Expand Down
29 changes: 25 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
// swift-tools-version:4.2
// swift-tools-version:5.6
import PackageDescription

let package = Package(
name: "SwiftFormat",
products: [
.executable(name: "swiftformat", targets: ["CommandLineTool"]),
.library(name: "SwiftFormat", targets: ["SwiftFormat"]),
.plugin(name: "SwiftFormatPlugin", targets: ["SwiftFormatPlugin"]),
],
targets: [
.target(name: "CommandLineTool", dependencies: ["SwiftFormat"], path: "CommandLineTool"),
.target(name: "SwiftFormat", path: "Sources"),
.testTarget(name: "SwiftFormatTests", dependencies: ["SwiftFormat"], path: "Tests"),
.executableTarget(
name: "CommandLineTool", dependencies: ["SwiftFormat"], path: "CommandLineTool",
exclude: ["swiftformat"]
),
.target(name: "SwiftFormat", path: "Sources", exclude: ["Info.plist"]),
.testTarget(
name: "SwiftFormatTests",
dependencies: ["SwiftFormat"],
path: "Tests",
exclude: ["Info.plist", "GlobTest[5].txt"]
),
.plugin(
name: "SwiftFormatPlugin",
capability: .command(
intent: .custom(
verb: "swiftformat", description: "Formats Swift source files using SwiftFormat"
),
permissions: [
.writeToPackageDirectory(reason: "This command reformats source files"),
]
),
dependencies: [.target(name: "CommandLineTool")]
),
]
)
36 changes: 0 additions & 36 deletions [email protected]

This file was deleted.

16 changes: 8 additions & 8 deletions PerformanceTests/PerformanceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ class PerformanceTests: XCTestCase {
let tokens = files.map { tokenize($0) }
let options = FormatOptions(
linebreak: "\r\n",
spaceAroundRangeOperators: false,
spaceAroundOperatorDeclarations: false,
spaceAroundRangeOperators: .remove,
spaceAroundOperatorDeclarations: .remove,
useVoid: false,
indentCase: true,
trailingCommas: false,
Expand Down Expand Up @@ -137,7 +137,7 @@ class PerformanceTests: XCTestCase {
let files = PerformanceTests.files
let tokens = files.map { tokenize($0) }
measure {
_ = tokens.map { try! format($0, rules: [FormatRules.indent]) }
_ = tokens.map { try! format($0, rules: [.indent]) }
}
}

Expand All @@ -146,15 +146,15 @@ class PerformanceTests: XCTestCase {
let tokens = files.map { tokenize($0) }
let options = FormatOptions(indent: "\t", allmanBraces: true)
measure {
_ = tokens.map { try! format($0, rules: [FormatRules.indent], options: options) }
_ = tokens.map { try! format($0, rules: [.indent], options: options) }
}
}

func testRedundantSelf() {
let files = PerformanceTests.files
let tokens = files.map { tokenize($0) }
measure {
_ = tokens.map { try! format($0, rules: [FormatRules.redundantSelf]) }
_ = tokens.map { try! format($0, rules: [.redundantSelf]) }
}
}

Expand All @@ -163,15 +163,15 @@ class PerformanceTests: XCTestCase {
let tokens = files.map { tokenize($0) }
let options = FormatOptions(explicitSelf: .insert)
measure {
_ = tokens.map { try! format($0, rules: [FormatRules.redundantSelf], options: options) }
_ = tokens.map { try! format($0, rules: [.redundantSelf], options: options) }
}
}

func testNumberFormatting() {
let files = PerformanceTests.files
let tokens = files.map { tokenize($0) }
measure {
_ = tokens.map { try! format($0, rules: [FormatRules.numberFormatting]) }
_ = tokens.map { try! format($0, rules: [.numberFormatting]) }
}
}

Expand All @@ -187,7 +187,7 @@ class PerformanceTests: XCTestCase {
hexGrouping: .group(1, 1)
)
measure {
_ = tokens.map { try! format($0, rules: [FormatRules.numberFormatting], options: options) }
_ = tokens.map { try! format($0, rules: [.numberFormatting], options: options) }
}
}
}
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,13 @@ Q. I don't want to be surprised by new rules added when I upgrade SwiftFormat. H

> A. Yes, the SwiftFormat framework can be included in an app or test target, and used for many kinds of parsing and processing of Swift source code besides formatting. The SwiftFormat framework is available as a [CocoaPod](https://cocoapods.org/pods/SwiftFormat) for easy integration.

*Q. How to create own rule?*

> A. 1) Open `SwiftFormat.xcodeproj`; 2) Add a rule in `Sources/Rules/..`; 3) Add a test in `Tests/Rules/..`; 4) Add an example in `Sources/Examples.swift`; 5) Run all tests.

*Q. How do I run and debug the command line tool in Xcode while developing a new rule?*

> A. You can run the `swiftformat` command line tool via the `Swift Format (Command Line Tool)` scheme, and you can pass in arguments like `/path/to/my/code --config /path/to/my/config` as the `Arguments Passed On Launch` in Xcode's scheme editor. More instructions are available [here](https://github.com/nicklockwood/SwiftFormat/pull/1804#issuecomment-2263079432).

Known issues
---------------
Expand Down Expand Up @@ -951,7 +958,13 @@ Known issues

* If you have a generic typealias that defines a closure (e.g. `typealias ResultCompletion<T> = (Result<T, Error>) -> Void`) and use this closure as an argument in a generic function (e.g. `func handle<T: Decodable>(_ completion: ResultCompletion<T>)`), the `opaqueGenericParameters` rule may update the function definition to use `some` syntax (e.g. `func handle(_ completion: ResultCompletion<some Decodable>)`). `some` syntax is not permitted in closure parameters, so this will no longer compile. Workarounds include spelling out the closure explicitly in the generic function (instead of using a `typealias`) or disabling the `opaqueGenericParameters` rule (e.g. with `// swiftformat:disable:next opaqueGenericParameters`).

* If compiling for macOS with Xcode 14.0 and configuring SwiftFormat with `--swift-version 5.7`, the `genericExtensions` rule may cause a build failure by updating extensions of the format `extension Collection where Element == Foo` to `extension Collection<Foo>`. This fails to compile for macOS in Xcode 14.0, because the macOS SDK in that version of Xcode [does not include](https://forums.swift.org/t/xcode-14-rc-cannot-specialize-protocol-type/60171) the Swift 5.7 standard library. Workarounds include using `--swift-version 5.6` instead, updating to Xcode 14.1+, or disabling the `genericExtensions` rule (e.g. with `// swiftformat:next:disable genericExtensions`).
* If compiling for macOS with Xcode 14.0 and configuring SwiftFormat with `--swift-version 5.7`, the `genericExtensions` rule may cause a build failure by updating extensions of the format `extension Collection where Element == Foo` to `extension Collection<Foo>`. This fails to compile for macOS in Xcode 14.0, because the macOS SDK in that version of Xcode [does not include](https://forums.swift.org/t/xcode-14-rc-cannot-specialize-protocol-type/60171) the Swift 5.7 standard library. Workarounds include using `--swift-version 5.6` instead, updating to Xcode 14.1+, or disabling the `genericExtensions` rule (e.g. with `// swiftformat:disable:next genericExtensions`).

* The `propertyType` rule can cause a build failure in cases where there are multiple static overloads with the same name but different return types. As a workaround you can rename the overloads to no longer conflict, or exclude the property name with `--preservesymbols propertyName,otherPropertyName,etc`.

* The `propertyType` rule can cause a build failure in cases where the property's type is a protocol / existential like `let shapeStyle: ShapeStyle = .myShapeStyle`, and the value used on the right-hand side is defined in an extension like `extension ShapeStyle where Self == MyShapeStyle { static var myShapeStyle: MyShapeStyle { ... } }`. As a workaround you can use the existential `any` syntax (`let shapeStyle: any ShapeStyle = .myShapeStyle`), which the rule will preserve as-is, or exclude the type name and/or property name with `--preservesymbols ShapeStyle,myShapeStyle,etc`.

* The `propertyType` rule can cause a build failure in cases like `let foo = Foo.bar` where the value is a static member that doesn't return the same time. For example, `let foo: Foo = .bar` would be invalid if the `bar` property was defined as `static var bar: Bar`. As a workaround you can write the name of the type explicitly, like `let foo: Bar = Foo.bar`, or exclude the type name and/or property name with `--preservesymbols Bar,bar,etc`.


Tip Jar
Expand Down
Loading