Skip to content

Commit 4f43ec6

Browse files
authored
Merge pull request #477 from pennig/swift-syntax-510.0.2
Update swift-syntax to 510.0.2
2 parents 69f7a88 + 9f1412b commit 4f43ec6

10 files changed

+43
-56
lines changed

Generator/Package.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ let package = Package(
1111
.library(name: "NeedleFramework", targets: ["NeedleFramework"])
1212
],
1313
dependencies: [
14-
.package(url: "https://github.com/apple/swift-tools-support-core", .exact("0.2.7")),
14+
.package(url: "https://github.com/apple/swift-tools-support-core", exact: "0.2.7"),
1515
.package(url: "https://github.com/uber/swift-concurrency.git", .upToNextMajor(from: "0.6.5")),
16-
.package(url: "https://github.com/uber/swift-common.git", .exact("0.5.0")),
17-
.package(url: "https://github.com/apple/swift-syntax.git", revision: "0e85cf7"),
16+
.package(url: "https://github.com/uber/swift-common.git", exact: "0.5.0"),
17+
.package(url: "https://github.com/apple/swift-syntax.git", .upToNextMajor(from: "510.0.0")),
1818
],
1919
targets: [
2020
.target(
@@ -24,7 +24,7 @@ let package = Package(
2424
.product(name: "Concurrency", package: "swift-concurrency"),
2525
.product(name: "SourceParsingFramework", package: "swift-common"),
2626
.product(name: "SwiftSyntax", package: "swift-syntax"),
27-
.product(name: "SwiftSyntaxParser", package: "swift-syntax"),
27+
.product(name: "SwiftParser", package: "swift-syntax"),
2828
]),
2929
.testTarget(
3030
name: "NeedleFrameworkTests",

Generator/Sources/NeedleFramework/Parsing/BaseVisitor.swift

+11-7
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ class BaseVisitor: SyntaxVisitor {
3939
var isParsingComponentDeclarationLine: Bool = false
4040

4141
override func visitPost(_ node: FunctionCallExprSyntax) {
42-
if let callexpr = node.calledExpression.firstToken?.text,
43-
let currentEntityName = currentEntityNode?.typeName {
42+
if
43+
let callexpr = node.calledExpression.firstToken(viewMode: .sourceAccurate)?.text,
44+
let currentEntityName = currentEntityNode?.typeName
45+
{
4446
componentToCallExprs[currentEntityName, default: []].insert(callexpr)
4547
}
4648
}
@@ -60,9 +62,11 @@ class BaseVisitor: SyntaxVisitor {
6062
let isInternal = !(isPublic || isPrivate || isFileprivate)
6163

6264
let memberProperties = node.bindings.compactMap { pattern -> Property? in
63-
guard let propertyType = pattern.typeAnnotation?.type.description.trimmed,
64-
let propertyName = pattern.firstToken?.text else {
65-
return nil
65+
guard
66+
let propertyType = pattern.typeAnnotation?.type.description.trimmed,
67+
let propertyName = pattern.firstToken(viewMode: .sourceAccurate)?.text
68+
else {
69+
return nil
6670
}
6771
if isPrivate || isFileprivate {
6872
info("\(currentEntityName) (\(propertyName): \(propertyType)) property is private/fileprivate, therefore inaccessible on DI graph.")
@@ -76,11 +80,11 @@ class BaseVisitor: SyntaxVisitor {
7680
}
7781

7882
override func visitPost(_ node: ImportDeclSyntax) {
79-
let importStatement = node.withoutTrivia().description.trimmed
83+
let importStatement = node.trimmed.description.trimmed
8084
imports.append(importStatement)
8185
}
8286

83-
override func visit(_ node: MemberDeclBlockSyntax) -> SyntaxVisitorContinueKind {
87+
override func visit(_ node: MemberBlockSyntax) -> SyntaxVisitorContinueKind {
8488
isParsingComponentDeclarationLine = false
8589
return .visitChildren
8690
}

Generator/Sources/NeedleFramework/Parsing/Pluginized/Tasks/PluginizedASTDeclarationParserTask.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private final class PluginizedVisitor: BaseVisitor {
138138
if currentEntityNode?.typeName == currentPluginizedComponentName {
139139

140140
for (i, genericArgument) in node.enumerated() {
141-
let argumentName = genericArgument.argumentType.description.trimmed.removingModulePrefix
141+
let argumentName = genericArgument.argument.description.trimmed.removingModulePrefix
142142

143143
switch i {
144144
case 0:
@@ -152,7 +152,7 @@ private final class PluginizedVisitor: BaseVisitor {
152152
}
153153
}
154154
} else if currentEntityNode?.typeName == currentNonCoreComponentName {
155-
currentDependencyProtocol = node.first?.argumentType.description.trimmed.removingModulePrefix
155+
currentDependencyProtocol = node.first?.argument.description.trimmed.removingModulePrefix
156156
}
157157
}
158158

Generator/Sources/NeedleFramework/Parsing/Tasks/ASTProducerTask.swift

+2-6
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717
import Concurrency
1818
import Foundation
1919
import SourceParsingFramework
20-
#if swift(>=5.6)
21-
import SwiftSyntaxParser
22-
#else
23-
import SwiftSyntax
24-
#endif
20+
import SwiftParser
2521

2622
/// A task that parses a Swift source content and produces Swift AST that
2723
/// can then be parsed into the dependency graph.
@@ -42,7 +38,7 @@ class ASTProducerTask: AbstractTask<AST> {
4238
/// - returns: The `AST` data model.
4339
/// - throws: Any error occurred during execution.
4440
override func execute() throws -> AST {
45-
let syntax = try SyntaxParser.parse(sourceUrl)
41+
let syntax = try Parser.parse(source: String(contentsOf: sourceUrl))
4642
return AST(sourceHash: MD5(string: sourceContent), sourceFileSyntax: syntax, filePath: sourceUrl.path)
4743
}
4844

Generator/Sources/NeedleFramework/Parsing/Tasks/ComponentExtensionsParserTask.swift

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import Concurrency
1818
import Foundation
1919
import SwiftSyntax
20-
import SourceParsingFramework
2120

2221
/// A task that parses Swift AST component extensions into data models.
2322
class ComponentExtensionsParserTask: AbstractTask<ComponentExtensionNode> {

Generator/Sources/NeedleFramework/Parsing/Tasks/DeclarationsParserTask.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private final class Visitor: BaseVisitor {
112112

113113
override func visitPost(_ node: GenericArgumentListSyntax) {
114114
guard isParsingComponentDeclarationLine else {return }
115-
currentDependencyProtocol = node.first?.argumentType.description.trimmed.removingModulePrefix
115+
currentDependencyProtocol = node.first?.argument.description.trimmed.removingModulePrefix
116116
}
117117

118118
override func visit(_ node: ExtensionDeclSyntax) -> SyntaxVisitorContinueKind {

Generator/Sources/NeedleFramework/Utilities/SwiftSyntaxExtensions.swift

+15-15
Original file line numberDiff line numberDiff line change
@@ -21,57 +21,57 @@ import SwiftSyntax
2121
/// An entity node is either a Protocol or Class syntax node
2222
protocol EntityNode: SyntaxNodeWithModifiers {
2323
var typeName: String { get }
24-
var inheritanceClause: TypeInheritanceClauseSyntax? { get }
24+
var inheritanceClause: InheritanceClauseSyntax? { get }
2525
}
2626

2727
extension EntityNode {
2828
/// Checks whether the entity inherits from a certain type with `typeName`
2929
func inherits(from typeName: String) -> Bool {
3030

31-
let inheritedTypeSyntax = inheritanceClause?.inheritedTypeCollection.first?.typeName
31+
let inheritedTypeSyntax = inheritanceClause?.inheritedTypes.first?.type
3232
// Usually, first token is the inherited type name. But sometimes it could also be the module prefix.
3333
// In that case, we need to look for the actual type name by checking for `MemberTypeIdentifierSyntax`
34-
if inheritedTypeSyntax?.firstToken?.nextToken?.tokenKind != TokenKind.period {
35-
return inheritedTypeSyntax?.firstToken?.text == typeName
34+
if inheritedTypeSyntax?.firstToken(viewMode: .sourceAccurate)?.nextToken(viewMode: .sourceAccurate)?.tokenKind != TokenKind.period {
35+
return inheritedTypeSyntax?.firstToken(viewMode: .sourceAccurate)?.text == typeName
3636
} else {
37-
return inheritedTypeSyntax?.as(MemberTypeIdentifierSyntax.self)?.name.text == typeName
37+
return inheritedTypeSyntax?.as(MemberTypeSyntax.self)?.name.text == typeName
3838
}
3939
}
4040

4141
var inheritanceHasGenericArgument: Bool {
42-
let inheritanceTypeToken = inheritanceClause?.inheritedTypeCollection.first?.typeName
43-
return inheritanceTypeToken?.as(SimpleTypeIdentifierSyntax.self)?.genericArgumentClause != nil ||
44-
inheritanceTypeToken?.as(MemberTypeIdentifierSyntax.self)?.genericArgumentClause != nil
42+
let inheritanceTypeToken = inheritanceClause?.inheritedTypes.first?.type
43+
return inheritanceTypeToken?.as(IdentifierTypeSyntax.self)?.genericArgumentClause != nil ||
44+
inheritanceTypeToken?.as(MemberTypeSyntax.self)?.genericArgumentClause != nil
4545
}
4646
}
4747

4848
protocol SyntaxNodeWithModifiers {
49-
var modifiers: ModifierListSyntax? { get }
49+
var modifiers: DeclModifierListSyntax { get }
5050
}
5151

5252
extension SyntaxNodeWithModifiers {
5353
var isInternal: Bool {
54-
modifiers?.first?.name.text == nil || modifiers?.first?.name.text == "internal"
54+
modifiers.first?.name.text == nil || modifiers.first?.name.text == "internal"
5555
}
5656

5757
var isPublic: Bool {
58-
modifiers?.first?.name.text == "public"
58+
modifiers.first?.name.text == "public"
5959
}
6060

6161
var isPrivate: Bool {
62-
modifiers?.first?.name.text == "private"
62+
modifiers.first?.name.text == "private"
6363
}
6464

6565
var isFileprivate: Bool {
66-
modifiers?.first?.name.text == "fileprivate"
66+
modifiers.first?.name.text == "fileprivate"
6767
}
6868
}
6969

7070
// MARK: - SwiftSyntax Protocol Extensions
7171

72-
extension IdentifiedDeclSyntax {
72+
extension NamedDeclSyntax {
7373
var typeName: String {
74-
return identifier.description.trimmed
74+
return name.description.trimmed
7575
}
7676
}
7777

Generator/Tests/NeedleFrameworkTests/Parsing/ASTProducerTaskTests.swift

+2-6
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,14 @@
1616

1717
import XCTest
1818
@testable import NeedleFramework
19-
#if swift(>=5.6)
20-
import SwiftSyntaxParser
21-
#else
22-
import SwiftSyntax
23-
#endif
19+
import SwiftParser
2420

2521
class ASTProducerTaskTests: AbstractParserTests {
2622

2723
func test_execute_verifyNextTask() {
2824
let sourceUrl = fixtureUrl(for: "ComponentSample.swift")
2925
let sourceContent = try! String(contentsOf: sourceUrl)
30-
let astContent = try! SyntaxParser.parse(sourceUrl)
26+
let astContent = Parser.parse(source: sourceContent)
3127

3228
let task = ASTProducerTask(sourceUrl: sourceUrl, sourceContent: sourceContent)
3329
let result = try! task.execute()

Generator/Tests/NeedleFrameworkTests/Parsing/DeclarationsParserTaskTests.swift

+4-8
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,15 @@
1717
import XCTest
1818
@testable import NeedleFramework
1919
@testable import SourceParsingFramework
20-
#if swift(>=5.6)
21-
import SwiftSyntaxParser
22-
#else
23-
import SwiftSyntax
24-
#endif
20+
import SwiftParser
2521

2622
class DeclarationsParserTaskTests: AbstractParserTests {
2723

2824
func test_execute_withPrivateProperties_verifyLog() {
2925
let sourceUrl = fixtureUrl(for: "PrivateSample.swift")
3026
let sourceContent = try! String(contentsOf: sourceUrl)
3127
let ast = AST(sourceHash: MD5(string: sourceContent),
32-
sourceFileSyntax: try! SyntaxParser.parse(sourceUrl), filePath: sourceUrl.path)
28+
sourceFileSyntax: Parser.parse(source: sourceContent), filePath: sourceUrl.path)
3329

3430
let task = DeclarationsParserTask(ast: ast)
3531
_ = try! task.execute()
@@ -46,8 +42,8 @@ class DeclarationsParserTaskTests: AbstractParserTests {
4642
let sourceContent = try! String(contentsOf: sourceUrl)
4743
let imports = ["import UIKit", "import RIBs", "import Foundation", "import protocol Audio.Recordable"]
4844
let ast = AST(sourceHash: MD5(string: sourceContent),
49-
sourceFileSyntax: try! SyntaxParser.parse(sourceUrl), filePath: sourceUrl.path)
50-
45+
sourceFileSyntax: Parser.parse(source: sourceContent), filePath: sourceUrl.path)
46+
5147
let task = DeclarationsParserTask(ast: ast)
5248
let node = try! task.execute()
5349

Generator/Tests/NeedleFrameworkTests/Parsing/Pluginized/PluginizedDeclarationsParserTaskTests.swift

+2-6
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@
1616

1717
import XCTest
1818
@testable import NeedleFramework
19-
#if swift(>=5.6)
20-
import SwiftSyntaxParser
21-
#else
22-
import SwiftSyntax
23-
#endif
19+
import SwiftParser
2420

2521
class PluginizedDeclarationsParserTaskTests: AbstractParserTests {
2622

@@ -29,7 +25,7 @@ class PluginizedDeclarationsParserTaskTests: AbstractParserTests {
2925
let sourceContent = try! String(contentsOf: sourceUrl)
3026
let imports = ["import UIKit", "import RIBs", "import Foundation"]
3127
let ast = AST(sourceHash: MD5(string: sourceContent),
32-
sourceFileSyntax: try! SyntaxParser.parse(sourceUrl), filePath: sourceUrl.path)
28+
sourceFileSyntax: Parser.parse(source: sourceContent), filePath: sourceUrl.path)
3329

3430
let task = PluginizedDeclarationsParserTask(ast: ast)
3531
let node = try! task.execute()

0 commit comments

Comments
 (0)