Skip to content

Commit 9c4d3b3

Browse files
authored
Updates SwiftSyntax to 600.0.0 with explicit import access (#52)
* Updates `swift-syntax` package to v600 * Adopts import access level from SE-0409 * Resolves deprecations and use more generalized return type * Removes `String.isSwiftKeyword` extension ...and thus `@_spi` dependencies on `SwiftSyntax` and `SwiftParser` * Resolves regressions found * Specifies minimal import access level with Swift 6.0+ * Removes styling workarounds * Resolves Swift 5.x regressions
1 parent f02455d commit 9c4d3b3

File tree

28 files changed

+287
-100
lines changed

28 files changed

+287
-100
lines changed

Package.resolved

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ let package = Package(
2929
],
3030
dependencies: [
3131
// Dependencies declare other packages that this package depends on.
32-
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0"),
32+
.package(url: "https://github.com/swiftlang/swift-syntax.git", from: "600.0.0"),
3333
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.0.0"),
3434
.package(url: "https://github.com/apple/swift-collections.git", from: "1.0.0"),
3535
.package(url: "https://github.com/teco-project/teco-core.git", .upToNextMinor(from: "0.5.3")),

Sources/TecoCodeGeneratorCommons/CodeGenerationFormat.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@
2222
//
2323
//===----------------------------------------------------------------------===//
2424

25+
#if compiler(>=6.0)
26+
public import SwiftBasicFormat
27+
public import SwiftSyntax
28+
#else
2529
import SwiftBasicFormat
2630
import SwiftSyntax
31+
#endif
2732

2833
/// A format style for files generated by TecoCodeGenerator.
2934
public class CodeGenerationFormat: BasicFormat {

Sources/TecoCodeGeneratorCommons/TecoCodeGenerator.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
#if compiler(>=6.0)
2+
public import ArgumentParser
3+
private import struct Foundation.Calendar
4+
private import struct Foundation.Date
5+
private import class Foundation.FileManager
6+
private import struct Foundation.URL
7+
#else
18
import ArgumentParser
29
import struct Foundation.Calendar
310
import struct Foundation.Date
411
import class Foundation.FileManager
5-
@_exported import struct Foundation.URL
12+
import struct Foundation.URL
13+
#endif
614

715
public struct GeneratorContext {
816
@TaskLocal

Sources/TecoCodeGeneratorCommons/Utils.swift

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
import Foundation
1+
#if compiler(>=6.0)
2+
private import ArgumentParser
3+
public import Foundation
4+
private import OrderedCollections
5+
private import SwiftParser
6+
public import SwiftSyntax
7+
private import SwiftSyntaxBuilder
8+
#else
29
import ArgumentParser
3-
@_spi(Diagnostics) import SwiftParser
4-
@_spi(RawSyntax) import SwiftSyntax
5-
import SwiftSyntaxBuilder
10+
import Foundation
611
@_implementationOnly import OrderedCollections
12+
import SwiftParser
13+
import SwiftSyntax
14+
import SwiftSyntaxBuilder
15+
#endif
716

817
extension String {
918
public func lowerFirst() -> String {
@@ -24,20 +33,12 @@ extension String {
2433
return String(self[...startIndex]).uppercased() + self[index(after: startIndex)...]
2534
}
2635

27-
public var isSwiftKeyword: Bool {
28-
var string = self
29-
if let keyword = string.withSyntaxText(Keyword.init) {
30-
return TokenKind.keyword(keyword).isLexerClassifiedKeyword
31-
}
32-
return false
33-
}
34-
3536
public func swiftMemberEscaped() -> String {
36-
self == "init" ? "`init`" : self
37+
self.isValidSwiftIdentifier(for: .memberAccess) ? self : "`\(self)`"
3738
}
3839

3940
public func swiftIdentifierEscaped() -> String {
40-
(self == "init" || self.isSwiftKeyword) ? "`\(self)`" : self
41+
self.isValidSwiftIdentifier(for: .variableName) ? self : "`\(self)`"
4142
}
4243
}
4344

@@ -68,7 +69,7 @@ extension TecoCodeGenerator {
6869

6970
extension SourceFileSyntax {
7071
public func withCopyrightHeader() -> SourceFileSyntax {
71-
let header = """
72+
let headerTrivia = Trivia("""
7273
//===----------------------------------------------------------------------===//
7374
//
7475
// This source file is part of the Teco open source project
@@ -84,10 +85,8 @@ extension SourceFileSyntax {
8485
8586
// THIS FILE IS AUTOMATICALLY GENERATED by \(GeneratorContext.generator).
8687
// DO NOT EDIT.
87-
88-
89-
"""
90-
return self.with(\.leadingTrivia, .lineComment(header) + self.leadingTrivia)
88+
""")
89+
return self.with(\.leadingTrivia, headerTrivia + .newlines(2) + self.leadingTrivia)
9190
}
9291

9392
public func save(to url: URL) throws {
@@ -105,19 +104,8 @@ extension SourceFileSyntax {
105104
// Skip formatting and writing to disk in dry-run mode
106105
if GeneratorContext.dryRun { return }
107106

108-
// Work around styling issues regarding blank lines.
109-
var sourceCode = source.description.trimmingCharacters(in: .whitespacesAndNewlines).appending("\n")
110-
111-
// Work around styling issues regarding trailing whitespaces.
112-
sourceCode = sourceCode.split(omittingEmptySubsequences: false, whereSeparator: \.isNewline).map {
113-
var line = $0
114-
while line.last?.isWhitespace == true {
115-
line.removeLast()
116-
}
117-
return line
118-
}.joined(separator: "\n")
119-
120107
// Save to file.
108+
let sourceCode = source.description + "\n"
121109
try sourceCode.write(to: url, atomically: true, encoding: .utf8)
122110
}
123111
}

Sources/TecoCommonErrorGenerator/builder.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
#if compiler(>=6.0)
2+
internal import SwiftSyntax
3+
private import SwiftSyntaxBuilder
4+
private import TecoCodeGeneratorCommons
5+
#else
16
import SwiftSyntax
27
import SwiftSyntaxBuilder
38
import TecoCodeGeneratorCommons
9+
#endif
410

5-
func buildCommonErrorStructDecl(from errors: [CommonError]) throws -> DeclSyntax {
11+
func buildCommonErrorStructDecl(from errors: [CommonError]) throws -> some DeclSyntaxProtocol {
612
try StructDeclSyntax("""
713
/// Common error type returned by Tencent Cloud.
814
public struct TCCommonError: TCServiceErrorType
@@ -52,5 +58,5 @@ func buildCommonErrorStructDecl(from errors: [CommonError]) throws -> DeclSyntax
5258
}
5359
""")
5460
}
55-
}.as(DeclSyntax.self)!
61+
}
5662
}

Sources/TecoCommonErrorGenerator/data.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
#if compiler(>=6.0)
2+
private import OrderedCollections
3+
#else
14
@_implementationOnly import OrderedCollections
5+
#endif
26

37
// https://cloud.tencent.com/document/product/213/30435#.E5.85.AC.E5.85.B1.E9.94.99.E8.AF.AF.E7.A0.81
48
private let tcCommonErrors: [String : String] = [

Sources/TecoCommonErrorGenerator/generator.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1+
#if compiler(>=6.0)
2+
private import ArgumentParser
3+
private import struct Foundation.URL
4+
private import SwiftSyntax
5+
private import SwiftSyntaxBuilder
6+
private import TecoCodeGeneratorCommons
7+
#else
18
import ArgumentParser
9+
import struct Foundation.URL
210
import SwiftSyntax
311
import SwiftSyntaxBuilder
412
import TecoCodeGeneratorCommons
13+
#endif
514

615
@main
7-
struct TecoCommonErrorGenerator: TecoCodeGenerator {
16+
private struct TecoCommonErrorGenerator: TecoCodeGenerator {
817
static let startingYear = 2022
918

1019
@Option(name: .shortAndLong, completion: .file(extensions: ["swift"]), transform: URL.init(fileURLWithPath:))

Sources/TecoCommonErrorGenerator/helpers.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
#if compiler(>=6.0)
2+
private import ArgumentParser
3+
private import TecoCodeGeneratorCommons
4+
internal import class Foundation.JSONDecoder
5+
internal import struct Foundation.URL
6+
#else
17
import ArgumentParser
28
import TecoCodeGeneratorCommons
39
import class Foundation.JSONDecoder
10+
import struct Foundation.URL
11+
#endif
412

513
struct CommonError {
614
let code: String

Sources/TecoDateWrapperGenerator/builder.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
#if compiler(>=6.0)
2+
internal import SwiftSyntax
3+
private import SwiftSyntaxBuilder
4+
#else
15
import SwiftSyntax
26
import SwiftSyntaxBuilder
7+
#endif
38

49
@CodeBlockItemListBuilder
510
func buildImportDecls(for encoding: DateEncoding) -> CodeBlockItemListSyntax {
@@ -16,7 +21,7 @@ func buildImportDecls(for encoding: DateEncoding) -> CodeBlockItemListSyntax {
1621
DeclSyntax("@_implementationOnly import struct NIOConcurrencyHelpers.NIOLockedValueBox")
1722
}
1823

19-
func buildDateFormatterDecl(for encoding: DateEncoding) -> DeclSyntax {
24+
func buildDateFormatterDecl(for encoding: DateEncoding) -> some DeclSyntaxProtocol {
2025
let dateFormat: String
2126
switch encoding {
2227
case .date:

0 commit comments

Comments
 (0)