Skip to content

Commit

Permalink
Merge pull request #92 from squareup/bradfol/5.9
Browse files Browse the repository at this point in the history
Upgrade SwiftSyntax to 509.0.1
  • Loading branch information
bradfol authored Oct 27, 2023
2 parents ee697b1 + 08003c2 commit bc19cdc
Show file tree
Hide file tree
Showing 19 changed files with 306 additions and 303 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ jobs:
- name: Checkout
uses: actions/checkout@v3
- name: Create Release
uses: swift-actions/setup-swift@v1.25.0
with:
swift-version: "5.9"
run: |
set -euo pipefail
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ jobs:

steps:
- uses: actions/checkout@v3
- uses: swift-actions/setup-swift@v1.25.0
with:
swift-version: "5.9"
- name: Build
run: swift build -v
- name: Run tests
Expand Down
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "2c49d66d34dfd6f8130afdba889de77504b58ec0",
"version" : "508.0.1"
"revision" : "ffa3cd6fc2aa62adbedd31d3efaf7c0d86a9f029",
"version" : "509.0.1"
}
},
{
Expand Down
10 changes: 5 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.7
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand All @@ -13,7 +13,7 @@ let package = Package(
.executable(name: "knit-cli", targets: ["KnitCommand"])
],
dependencies: [
.package(url: "https://github.com/apple/swift-syntax.git", from: "508.0.1"),
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.1"),
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.2.0"),
.package(url: "https://github.com/Swinject/Swinject.git", from: "2.8.3"),
.package(url: "https://github.com/Swinject/SwinjectAutoregistration.git", from: "2.8.3"),
Expand All @@ -29,17 +29,17 @@ let package = Package(
.executableTarget(
name: "KnitCommand",
dependencies: [
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
.product(name: "SwiftSyntaxParser", package: "swift-syntax"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.target(name: "KnitCodeGen"),
]
),
.target(
name: "KnitCodeGen",
dependencies: [
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
.product(name: "SwiftSyntaxParser", package: "swift-syntax"),
]
),
.testTarget(
Expand Down
27 changes: 14 additions & 13 deletions Sources/KnitCodeGen/AssemblyParsing.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import Foundation
import SwiftSyntax
import SwiftSyntaxParser
import SwiftParser

public func parseAssemblies(at paths: [String]) throws -> ConfigurationSet {
var configs = [Configuration]()
for path in paths {
let url = URL(fileURLWithPath: path, isDirectory: false)
var errorsToPrint = [Error]()

let syntaxTree: SourceFileSyntax
let source: String
do {
syntaxTree = try SwiftSyntaxParser.SyntaxParser.parse(url)
source = try String(contentsOf: url)
} catch {
throw AssemblyParsingError.syntaxParsingError(error, path: path)
throw AssemblyParsingError.fileReadError(error, path: path)
}
let syntaxTree = Parser.parse(source: source)
let configuration = try parseSyntaxTree(syntaxTree, errorsToPrint: &errorsToPrint)
configs.append(configuration)
printErrors(errorsToPrint, filePath: path, syntaxTree: syntaxTree)
Expand Down Expand Up @@ -79,11 +80,11 @@ private class AssemblyFileVisitor: SyntaxVisitor {
}

override func visit(_ node: ImportDeclSyntax) -> SyntaxVisitorContinueKind {
imports.append(node.withoutTrivia())
imports.append(node.trimmed)
return .skipChildren
}

private func visitAssemblyType(_ node: IdentifiedDeclSyntax) -> SyntaxVisitorContinueKind {
private func visitAssemblyType(_ node: NamedDeclSyntax) -> SyntaxVisitorContinueKind {
guard classDeclVisitor == nil else {
// Only the first class declaration should be visited
return .skipChildren
Expand Down Expand Up @@ -139,12 +140,12 @@ private class ClassDeclVisitor: SyntaxVisitor {

}

extension IdentifiedDeclSyntax {
extension NamedDeclSyntax {

/// Returns the module name for the assembly class.
/// If the class is not an assembly returns `nil`.
var moduleNameForAssembly: String? {
let className = identifier.text
let className = name.text
let assemblySuffx = "Assembly"
guard className.hasSuffix(assemblySuffx) else {
return nil
Expand All @@ -157,17 +158,17 @@ extension IdentifiedDeclSyntax {
// MARK: - Errors

enum AssemblyParsingError: Error {
case syntaxParsingError(Error, path: String)
case fileReadError(Error, path: String)
case missingModuleName
}

extension AssemblyParsingError: LocalizedError {

var errorDescription: String? {
switch self {
case let .syntaxParsingError(error, path: path):
case let .fileReadError(error, path: path):
return """
Error parsing assembly file: \(error)
Error reading file: \(error.localizedDescription)
File path: \(path)
"""

Expand All @@ -184,12 +185,12 @@ func printErrors(_ errors: [Error], filePath: String, syntaxTree: SyntaxProtocol
guard !errors.isEmpty else {
return
}
let lineConverter = SourceLocationConverter(file: filePath, tree: syntaxTree)
let lineConverter = SourceLocationConverter(fileName: filePath, tree: syntaxTree)

for error in errors {
if let syntaxError = error as? SyntaxError {
let position = syntaxError.syntax.startLocation(converter: lineConverter, afterLeadingTrivia: true)
let line = position.line ?? 1
let line = position.line
print("\(filePath):\(line): error: \(error.localizedDescription)")
} else {
print("\(filePath): error: \(error.localizedDescription)")
Expand Down
18 changes: 11 additions & 7 deletions Sources/KnitCodeGen/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ public struct Configuration: Encodable {

public extension Configuration {

func makeTypeSafetySourceFile() -> SourceFileSyntax {
return TypeSafetySourceFile.make(
func makeTypeSafetySourceFile() throws -> SourceFileSyntax {
return try TypeSafetySourceFile.make(
assemblyName: "\(name)Assembly",
extensionTarget: "Resolver",
registrations: registrations
)
}

func makeUnitTestSourceFile() -> SourceFileSyntax {
func makeUnitTestSourceFile() throws -> SourceFileSyntax {
var allImports = imports
allImports.append("@testable import \(raw: self.name)")
allImports.append("import XCTest")
allImports.append(try ImportDeclSyntax("@testable import \(raw: self.name)"))
allImports.append(try ImportDeclSyntax("import XCTest"))

return UnitTestSourceFile.make(
return try UnitTestSourceFile.make(
configuration: self
)
}
Expand Down Expand Up @@ -76,7 +76,11 @@ public extension ImportDeclSyntax {

init(moduleName: String) {
self.init(
path: [ AccessPathComponentSyntax(name: moduleName) ]
path: [
ImportPathComponentSyntax(
name: "\(raw: moduleName)"
)
]
)
}

Expand Down
27 changes: 13 additions & 14 deletions Sources/KnitCodeGen/ConfigurationSet.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Created by Alexander skorulis on 4/8/2023.

import Foundation
import SwiftSyntax
import SwiftSyntaxBuilder

// Multiple assemblies that are grouped together
public struct ConfigurationSet {
Expand All @@ -16,17 +15,17 @@ public struct ConfigurationSet {
public func writeGeneratedFiles(
typeSafetyExtensionsOutputPath: String?,
unitTestOutputPath: String?
) {
) throws {
if let typeSafetyExtensionsOutputPath {
write(
text: makeTypeSafetySourceFile(),
text: try makeTypeSafetySourceFile(),
to: typeSafetyExtensionsOutputPath
)
}

if let unitTestOutputPath {
write(
text: makeUnitTestSourceFile(),
text: try makeUnitTestSourceFile(),
to: unitTestOutputPath
)
}
Expand All @@ -41,24 +40,24 @@ public struct ConfigurationSet {

public extension ConfigurationSet {

func makeTypeSafetySourceFile() -> String {
func makeTypeSafetySourceFile() throws -> String {
var allImports = allImports
allImports.append("import Swinject")
allImports.append(try ImportDeclSyntax("import Swinject"))
let header = HeaderSourceFile.make(importDecls: sortImports(allImports), comment: Self.typeSafetyIntro)
let body = assemblies.map { $0.makeTypeSafetySourceFile() }
let body = try assemblies.map { try $0.makeTypeSafetySourceFile() }
let sourceFiles = [header] + body
return Self.join(sourceFiles: sourceFiles)
}

func makeUnitTestSourceFile() -> String {
func makeUnitTestSourceFile() throws -> String {
var allImports = allImports
allImports.append("@testable import \(raw: primaryAssembly.name)")
allImports.append("import XCTest")
allImports.append(try ImportDeclSyntax("@testable import \(raw: primaryAssembly.name)"))
allImports.append(try ImportDeclSyntax("import XCTest"))
let header = HeaderSourceFile.make(importDecls: sortImports(allImports), comment: nil)
let body = assemblies.map { $0.makeUnitTestSourceFile() }
let body = try assemblies.map { try $0.makeUnitTestSourceFile() }
let allRegistrations = assemblies.flatMap { $0.registrations }
let allRegistrationsIntoCollections = assemblies.flatMap { $0.registrationsIntoCollections }
let resolverExtensions = UnitTestSourceFile.resolverExtensions(
let resolverExtensions = try UnitTestSourceFile.resolverExtensions(
registrations: allRegistrations,
registrationsIntoCollections: allRegistrationsIntoCollections
)
Expand All @@ -68,7 +67,7 @@ public extension ConfigurationSet {

private static func join(sourceFiles: [SourceFileSyntax]) -> String {
let result = sourceFiles.map { $0.formatted().description }.joined(separator: "\n")
return result.replacingOccurrences(of: ", \n", with: ",\n")
return result
}

private func sortImports(_ imports: [ImportDeclSyntax]) -> [ImportDeclSyntax] {
Expand Down
Loading

0 comments on commit bc19cdc

Please sign in to comment.