Skip to content

Commit

Permalink
Merge pull request #32 from RakuyoKit/feature/synchronize-upstream
Browse files Browse the repository at this point in the history
Feature/synchronize-upstream
  • Loading branch information
rakuyoMo authored Jun 26, 2024
2 parents 426104d + b23e576 commit f1c1e30
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 64 deletions.
3 changes: 3 additions & 0 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ runs:
run: sudo xcode-select --switch /Applications/Xcode_${{ inputs.xcode }}.app
if: ${{ inputs.xcode }}
shell: bash
- name: Install Ruby Gems
run: bundle install
shell: bash
9 changes: 7 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ jobs:
fail-fast: false
matrix:
xcode:
- '14.2' # Swift 5.7
- '14.3' # Swift 5.8
- '15.0' # Swift 5.9
steps:
- uses: actions/checkout@v2
- uses: ./.github/actions/setup
with:
xcode: ${{ matrix.xcode }}
- name: Test Package Plugin
run: swift package --allow-writing-to-package-directory format --lint
run: bundle exec rake swift:lint

unit-tests:
name: Unit Tests
Expand All @@ -32,5 +34,8 @@ jobs:
- '15.0' # Swift 5.9
steps:
- uses: actions/checkout@v2
- uses: ./.github/actions/setup
with:
xcode: ${{ matrix.xcode }}
- name: Run Unit Tests
run: swift test
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.build
.swiftpm
.DS_Store
.DS_Store
.vscode
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org' do
gem "rake", "~> 13.0.0"
end
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
GEM
remote: https://rubygems.org/
specs:
rake (13.0.6)

PLATFORMS
ruby

DEPENDENCIES
rake (~> 13.0.0)!

BUNDLED WITH
2.1.4
170 changes: 158 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ This repo includes a Swift Package Manager command plugin that you can use to au

```swift
dependencies: [
.package(url: "https://github.com/RakuyoKit/swift", from: "1.0.0"),
.package(url: "https://github.com/RakuyoKit/swift", from: "1.1.4"),
]
```

Expand Down Expand Up @@ -341,25 +341,125 @@ _You can enable the following settings in Xcode by running [this script](resourc

```swift
// WRONG
let host: Host = Host()
let sun: Star = Star(mass: 1.989e30)
let earth: Planet = Planet.earth

// RIGHT
let host = Host()
let sun = Star(mass: 1.989e30)
let earth = Planet.earth

// NOT RECOMMENDED. However, since the linter doesn't have full type information, this is not enforced automatically.
let moon: Moon = earth.moon // returns `Moon`

// RIGHT
let moon = earth.moon
let moon: PlanetaryBody? = earth.moon

// WRONG: Most literals provide a default type that can be inferred.
let enableGravity: Bool = true
let numberOfPlanets: Int = 8
let sunMass: Double = 1.989e30

// RIGHT
let enableGravity = true
let numberOfPlanets = 8
let sunMass = 1.989e30

// WRONG: Types can be inferred from if/switch expressions as well if each branch has the same explicit type.
let smallestPlanet: Planet =
if treatPlutoAsPlanet {
Planet.pluto
} else {
Planet.mercury
}

// RIGHT
let smallestPlanet =
if treatPlutoAsPlanet {
Planet.pluto
} else {
Planet.mercury
}
```

</details>

* <a id='infer-property-types'></a>(<a href='#infer-property-types'>link</a>) **Prefer letting the type of a variable or property be inferred from the right-hand-side value rather than writing the type explicitly on the left-hand side.** [![SwiftFormat: propertyType](https://img.shields.io/badge/SwiftFormat-propertyType-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#propertyType)

<details>

Prefer using inferred types when the right-hand-side value is a static member with a leading dot (e.g. an `init`, a `static` property / function, or an enum case). This applies to both local variables and property declarations:

```swift
enum Direction {
case left
case right
// WRONG
struct SolarSystemBuilder {
let sun: Star = .init(mass: 1.989e30)
let earth: Planet = .earth

func setUp() {
let galaxy: Galaxy = .andromeda
let system: SolarSystem = .init(sun, earth)
galaxy.add(system)
}
}

// RIGHT
struct SolarSystemBuilder {
let sun = Star(mass: 1.989e30)
let earth = Planet.earth

func someDirection() -> Direction {
// WRONG
return Direction.left
func setUp() {
let galaxy = Galaxy.andromeda
let system = SolarSystem(sun, earth)
galaxy.add(system)
}
}
```

// RIGHT
return .left
Explicit types are still permitted in other cases:

```swift
// RIGHT: There is no right-hand-side value, so an explicit type is required.
let sun: Star

// RIGHT: The right-hand-side is not a static member of the left-hand type.
let moon: PlantaryBody = earth.moon
let sunMass: Float = 1.989e30
let planets: [Planet] = []
let venusMoon: Moon? = nil
```

There are some rare cases where the inferred type syntax has a different meaning than the explicit type syntax. In these cases, the explicit type syntax is still permitted:

```swift
extension String {
static let earth = "Earth"
}

// WRONG: fails with "error: type 'String?' has no member 'earth'"
let planetName = String?.earth

// RIGHT
let planetName: String? = .earth
```

```swift
struct SaturnOutline: ShapeStyle { ... }

extension ShapeStyle where Self == SaturnOutline {
static var saturnOutline: SaturnOutline {
SaturnOutline()
}
}

// WRONG: fails with "error: static member 'saturnOutline' cannot be used on protocol metatype '(any ShapeStyle).Type'"
let myShape2 = (any ShapeStyle).myShape

// RIGHT: If the property's type is an existential / protocol type, moving the type
// to the right-hand side will result in invalid code if the value is defined in an
// extension like `extension ShapeStyle where Self == SaturnOutline`.
// SwiftFormat autocorrect detects this case by checking for the existential `any` keyword.
let myShape1: any ShapeStyle = .saturnOutline
```

</details>
Expand Down Expand Up @@ -1244,7 +1344,7 @@ _You can enable the following settings in Xcode by running [this script](resourc

</details>

* <a id='blank-line-after-multiline-switch-case'></a>(<a href='#blank-line-after-multiline-switch-case'>link</a>) **Insert a blank line following a switch case with a multi-line body.** Spacing within an individual switch statement should be consistent. If any case has a multi-line body then all cases should include a trailing blank line. The last switch case doesn't need a blank line, since it is already followed by a closing brace. [![SwiftFormat: blankLineAfterMultilineSwitchCase](https://img.shields.io/badge/SwiftFormat-blankLineAfterMultilineSwitchCase-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#blankLineAfterMultilineSwitchCase) [![SwiftFormat: consistentSwitchStatementSpacing](https://img.shields.io/badge/SwiftFormat-consistentSwitchStatementSpacing-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#consistentSwitchStatementSpacing)
* <a id='blank-line-after-multiline-switch-case'></a>(<a href='#blank-line-after-multiline-switch-case'>link</a>) **Insert a blank line following a switch case with a multi-line body.** Spacing within an individual switch statement should be consistent. If any case has a multi-line body then all cases should include a trailing blank line. The last switch case doesn't need a blank line, since it is already followed by a closing brace. [![SwiftFormat: blankLineAfterSwitchCase](https://img.shields.io/badge/SwiftFormat-blankLineAfterSwitchCase-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#blankLineAfterSwitchCase) [![SwiftFormat: consistentSwitchCaseSpacing](https://img.shields.io/badge/SwiftFormat-consistentSwitchCaseSpacing-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#consistentSwitchCaseSpacing)

<details>

Expand Down Expand Up @@ -2093,6 +2193,50 @@ _You can enable the following settings in Xcode by running [this script](resourc

</details>

* <a id='remove-blank-lines-between-chained-functions'></a>(<a href='#remove-blank-lines-between-chained-functions'>link</a>) **Remove blank lines between chained functions.** [![SwiftFormat: blanklinesbetweenchainedfunctions](https://img.shields.io/badge/SwiftFormat-blankLinesBetweenChainedFunctions-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#blanklinesbetweenchainedfunctions)

<details>

#### Why?

Improves readability and maintainability, making it easier to see the sequence of functions that are applied to the object.

```swift
// WRONG
var innerPlanetNames: [String] {
planets
.filter { $0.isInnerPlanet }

.map { $0.name }
}

// WRONG
var innerPlanetNames: [String] {
planets
.filter { $0.isInnerPlanet }

// Gets the name of the inner planet
.map { $0.name }
}

// RIGHT
var innerPlanetNames: [String] {
planets
.filter { $0.isInnerPlanet }
.map { $0.name }
}

// RIGHT
var innerPlanetNames: [String] {
planets
.filter { $0.isInnerPlanet }
// Gets the name of the inner planet
.map { $0.name }
}
```

</details>

### Closures

* <a id='favor-void-closure-return'></a>(<a href='#favor-void-closure-return'>link</a>) **Favor `Void` return types over `()` in closure declarations.** If you must specify a `Void` return type in a function declaration, use `Void` rather than `()` to improve readability. [![SwiftLint: void_return](https://img.shields.io/badge/SwiftLint-void__return-007A87.svg)](https://realm.github.io/SwiftLint/void_return)
Expand Down Expand Up @@ -3008,6 +3152,7 @@ _You can enable the following settings in Xcode by running [this script](resourc
return "💥 Critical Error"
} else {
return "ℹ️ Info"
}
}

func type(of planet: Planet) -> PlanetType {
Expand Down Expand Up @@ -3040,6 +3185,7 @@ _You can enable the following settings in Xcode by running [this script](resourc
"💥 Critical Error"
} else {
"ℹ️ Info"
}
}

func type(of planet: Planet) -> PlanetType {
Expand Down
52 changes: 52 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require 'json'
require 'net/http'
require 'json'
require 'tempfile'

namespace :swift do
desc 'Lints swift files'
task :lint do
sh 'swift package --allow-writing-to-package-directory format --lint'
end

desc 'Formats swift files'
task :format do
sh 'swift package --allow-writing-to-package-directory format'
end
end

namespace :update do
desc 'Updates SwiftFormat to the latest version'
task :swiftformat do
# Find the most recent release of SwiftFormat in the https://github.com/calda/SwiftFormat repo.
response = Net::HTTP.get(URI('https://api.github.com/repos/calda/SwiftFormat/releases/latest'))
latest_release_info = JSON.parse(response)

latest_version_number = latest_release_info['tag_name']

# Download the artifact bundle for the latest release and compute its checksum.
temp_dir = Dir.mktmpdir
artifact_bundle_url = "https://github.com/calda/SwiftFormat/releases/download/#{latest_version_number}/swiftformat.artifactbundle.zip"
artifact_bundle_zip_path = "#{temp_dir}/swiftformat.artifactbundle.zip"

sh "curl #{artifact_bundle_url} -L --output #{artifact_bundle_zip_path}"
checksum = `swift package compute-checksum #{artifact_bundle_zip_path}`

# Update the Package.swift file to reference this version
package_manifest_path = 'Package.swift'
package_manifest_content = File.read(package_manifest_path)

updated_swift_format_reference = <<-EOS
.binaryTarget(
name: "swiftformat",
url: "https://github.com/calda/SwiftFormat/releases/download/#{latest_version_number}/SwiftFormat.artifactbundle.zip",
checksum: "#{checksum.strip}"),
EOS

regex = /[ ]*.binaryTarget\([\S\s]*name: "swiftformat"[\S\s]*?\),\s/
updated_package_manifest = package_manifest_content.gsub(regex, updated_swift_format_reference)
File.open(package_manifest_path, "w") { |file| file.puts updated_package_manifest }

puts "Updated Package.swift to reference SwiftFormat #{latest_version_number}"
end
end
30 changes: 14 additions & 16 deletions Sources/RakuyoSwiftFormatTool/rakuyo.swiftformat
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
--wrapparameters before-first # wrapArguments
--wrapcollections before-first # wrapArguments
--wrapconditions before-first # wrapArguments
--wrapreturntype preserve #wrapArguments
--wrapeffects preserve #wrapArguments
--wrapreturntype preserve # wrapArguments
--wrapeffects preserve # wrapArguments
--closingparen balanced # wrapArguments
--wraptypealiases before-first # wrapArguments
--funcattributes prev-line # wrapAttributes
--computedvarattrs prev-line # wrapAttributes
--storedvarattrs prev-line # wrapAttributes
--complexattrs prev-line # wrapAttributes
--typeattributes prev-line # wrapAttributes
--wrapternary before-operators # wrap
--extensionacl on-declarations # extensionAccessControl
Expand All @@ -27,15 +30,12 @@
--typeblanklines preserve # blankLinesAtStartOfScope, blankLinesAtEndOfScope
--emptybraces spaced # emptyBraces
--someAny disabled # opaqueGenericParameters
--elseposition same-line #elseOnSameLine
--guardelse next-line #elseOnSameLine
--onelineforeach convert #preferForLoop
--shortoptionals always #typeSugar
--semicolons never #semicolons
--doccomments preserve #docComments
--computedvarattrs prev-line # wrapAttributes
--storedvarattrs prev-line # wrapAttributes
--complexattrs prev-line # wrapAttributes
--elseposition same-line # elseOnSameLine
--guardelse next-line # elseOnSameLine
--onelineforeach convert # preferForLoop
--shortoptionals always # typeSugar
--semicolons never # semicolons
--doccomments preserve # docComments

# We recommend a max width of 100 but _strictly enforce_ a max width of 130
--maxwidth 130 # wrap
Expand Down Expand Up @@ -95,18 +95,16 @@
--rules preferForLoop
--rules conditionalAssignment
--rules wrapMultilineConditionalAssignment
--rules blankLineAfterSwitchCase
--rules consistentSwitchCaseSpacing
--rules semicolons
--rules blankLinesBetweenChainedFunctions
--rules isEmpty
--rules acronyms
--rules modifierOrder
--rules spaceAroundOperators
--rules assertionFailures
--rules blankLinesAroundMark
--rules blankLinesBetweenChainedFunctions
--rules redundantTypedThrows
--rules consistentSwitchCaseSpacing
--rules blankLineAfterSwitchCase

# Not released yet, will be used after release
# --rules blankLineAfterMultilineSwitchCase
# --rules consistentSwitchStatementSpacing
Loading

0 comments on commit f1c1e30

Please sign in to comment.