Skip to content

Commit 4130a45

Browse files
committed
Make examples runnable
1 parent 59d7f42 commit 4130a45

File tree

5 files changed

+96
-39
lines changed

5 files changed

+96
-39
lines changed

Examples/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
/.build

Examples/AddOneToIntegerLiterals.swift

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import SwiftSyntax
2-
import SwiftSyntaxParser
2+
import SwiftParser
33
import Foundation
44

55
/// AddOneToIntegerLiterals will visit each token in the Syntax tree, and
66
/// (if it is an integer literal token) add 1 to the integer and return the
77
/// new integer literal token.
88
///
9-
/// For example will it turn:
9+
/// For example, it will turn
1010
/// ```
1111
/// let x = 2
1212
/// let y = 3_000
1313
/// ```
14-
/// into:
14+
/// into
1515
/// ```
1616
/// let x = 3
1717
/// let y = 3001
1818
/// ```
19-
class AddOneToIntegerLiterals: SyntaxRewriter {
19+
///
20+
private class AddOneToIntegerLiterals: SyntaxRewriter {
2021
override func visit(_ token: TokenSyntax) -> Syntax {
2122
// Only transform integer literals.
2223
guard case .integerLiteral(let text) = token.tokenKind else {
@@ -37,8 +38,14 @@ class AddOneToIntegerLiterals: SyntaxRewriter {
3738
}
3839
}
3940

40-
let file = CommandLine.arguments[1]
41-
let url = URL(fileURLWithPath: file)
42-
let sourceFile = try SyntaxParser.parse(url)
43-
let incremented = AddOneToIntegerLiterals().visit(sourceFile)
44-
print(incremented)
41+
@main
42+
struct Main {
43+
static func main() throws {
44+
let file = CommandLine.arguments[1]
45+
let url = URL(fileURLWithPath: file)
46+
let source = try String(contentsOf: url, encoding: .utf8)
47+
let sourceFile = try Parser.parse(source: source)
48+
let incremented = AddOneToIntegerLiterals().visit(sourceFile)
49+
print(incremented)
50+
}
51+
}
Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
import SwiftSyntaxBuilder
22

3-
/// This example will print the following example:
3+
/// This example will print the following code:
44
///
5-
///```
5+
/// ```
66
/// import Foundation
77
/// import UIKit
88
/// class SomeViewController{
99
/// let tableView: UITableView
1010
/// }
11-
///```
12-
13-
let source = SourceFile {
14-
ImportDecl(path: "Foundation")
15-
ImportDecl(path: "UIKit")
16-
ClassDecl(identifier: "SomeViewController") {
17-
VariableDecl(.let, name: "tableView", type: "UITableView")
18-
}
19-
}
11+
/// ```
12+
///
13+
@main
14+
struct Main {
15+
static func main() {
16+
let source = SourceFile {
17+
ImportDecl(path: "Foundation")
18+
ImportDecl(path: "UIKit")
19+
ClassDecl(identifier: "SomeViewController") {
20+
VariableDecl(.let, name: "tableView", type: "UITableView")
21+
}
22+
}
2023

21-
let syntax = source.buildSyntax(format: Format())
24+
let syntax = source.build()
2225

23-
var text = ""
24-
syntax.write(to: &text)
26+
var text = ""
27+
syntax.write(to: &text)
2528

26-
print(text)
29+
print(text)
30+
}
31+
}

Examples/MigrateToNewIfLetSyntax.swift

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class MigrateToNewIfLetSyntax: SyntaxRewriter {
2929
let initializer = binding.initializer,
3030
// ... and both sides of the assignment are the same identifiers.
3131
binding.pattern.withoutTrivia().description == initializer.value.withoutTrivia().description {
32-
// Remove the initializer ...
32+
// Remove the initializer ...
3333
binding.initializer = nil
3434
// ... and remove whitespace before the comma (in `if` statements with multiple conditions).
3535
if index != node.conditions.count - 1 {
@@ -41,19 +41,16 @@ class MigrateToNewIfLetSyntax: SyntaxRewriter {
4141
}
4242
return StmtSyntax(node.withConditions(ConditionElementListSyntax(newConditions)))
4343
}
44-
45-
/// Utility function to migrate all swift files in a folder and its subfolders
46-
static func migrate(folderPath: String) {
47-
for case let fileURL as String in FileManager.default.enumerator(atPath: folderPath)! {
48-
if fileURL.hasSuffix("swift") {
49-
print("Rewriting", fileURL)
50-
let fullPath = folderPath + "/" + fileURL
51-
let tree = try! Parser.parse(source: String(data: FileManager.default.contents(atPath: fullPath)!, encoding: .utf8 )!)
52-
let newTree = MigrateToNewIfLetSyntax().visit(tree)
53-
try! newTree.description.write(toFile: fullPath, atomically: true, encoding: .utf8)
54-
}
55-
}
56-
}
5744
}
5845

59-
MigrateToNewIfLetSyntax.migrate(folderPath: "/path/to/folder")
46+
@main
47+
struct Main {
48+
static func main() throws {
49+
let file = CommandLine.arguments[1]
50+
let url = URL(fileURLWithPath: file)
51+
let source = try String(contentsOf: url, encoding: .utf8)
52+
let sourceFile = try Parser.parse(source: source)
53+
let rewritten = MigrateToNewIfLetSyntax().visit(sourceFile)
54+
print(rewritten)
55+
}
56+
}

Examples/Package.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// swift-tools-version: 5.7
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "Examples",
7+
platforms: [
8+
.macOS(.v10_15),
9+
],
10+
products: [
11+
.executable(name: "AddOneToIntegerLiterals", targets: ["AddOneToIntegerLiterals"]),
12+
.executable(name: "CodeGenerationUsingSwiftSyntaxBuilder", targets: ["CodeGenerationUsingSwiftSyntaxBuilder"]),
13+
.executable(name: "MigrateToNewIfLetSyntax", targets: ["MigrateToNewIfLetSyntax"]),
14+
],
15+
dependencies: [
16+
.package(path: "../"),
17+
],
18+
targets: [
19+
.executableTarget(
20+
name: "AddOneToIntegerLiterals",
21+
dependencies: [
22+
.product(name: "SwiftParser", package: "swift-syntax"),
23+
.product(name: "SwiftSyntax", package: "swift-syntax"),
24+
],
25+
path: ".",
26+
exclude: ["README.md", "CodeGenerationUsingSwiftSyntaxBuilder.swift", "MigrateToNewIfLetSyntax.swift"]
27+
),
28+
.executableTarget(
29+
name: "CodeGenerationUsingSwiftSyntaxBuilder",
30+
dependencies: [
31+
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
32+
],
33+
path: ".",
34+
exclude: ["README.md", "AddOneToIntegerLiterals.swift", "MigrateToNewIfLetSyntax.swift"]
35+
),
36+
.executableTarget(
37+
name: "MigrateToNewIfLetSyntax",
38+
dependencies: [
39+
.product(name: "SwiftParser", package: "swift-syntax"),
40+
.product(name: "SwiftSyntax", package: "swift-syntax"),
41+
],
42+
path: ".",
43+
exclude: ["README.md", "CodeGenerationUsingSwiftSyntaxBuilder.swift", "AddOneToIntegerLiterals.swift"]
44+
),
45+
]
46+
)

0 commit comments

Comments
 (0)