Skip to content

Commit 8dddaa3

Browse files
committed
Remove Scope related code. Rename SwiftLexicalScopes to SwiftLeixicalLookup. Fix bug with expression list after try keyword.
1 parent b85dced commit 8dddaa3

File tree

6 files changed

+52
-104
lines changed

6 files changed

+52
-104
lines changed

Package.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ let package = Package(
2828
.library(name: "SwiftSyntaxMacros", targets: ["SwiftSyntaxMacros"]),
2929
.library(name: "SwiftSyntaxMacroExpansion", targets: ["SwiftSyntaxMacroExpansion"]),
3030
.library(name: "SwiftSyntaxMacrosTestSupport", targets: ["SwiftSyntaxMacrosTestSupport"]),
31-
.library(name: "SwiftLexicalScopes", targets: ["SwiftLexicalScopes"]),
31+
.library(name: "SwiftLexicalLookup", targets: ["SwiftLexicalLookup"]),
3232
.library(
3333
name: "SwiftSyntaxMacrosGenericTestSupport",
3434
targets: ["SwiftSyntaxMacrosGenericTestSupport"]
@@ -244,16 +244,16 @@ let package = Package(
244244
]
245245
),
246246

247-
// MARK: SwiftLexicalScopes
247+
// MARK: SwiftLexicalLookup
248248

249249
.target(
250-
name: "SwiftLexicalScopes",
250+
name: "SwiftLexicalLookup",
251251
dependencies: ["SwiftSyntax"]
252252
),
253253

254254
.testTarget(
255-
name: "SwiftLexicalScopesTest",
256-
dependencies: ["_SwiftSyntaxTestSupport", "SwiftLexicalScopes"]
255+
name: "SwiftLexicalLookupTest",
256+
dependencies: ["_SwiftSyntaxTestSupport", "SwiftLexicalLookup"]
257257
),
258258

259259
// MARK: SwiftSyntaxMacrosGenericTestSupport

Sources/SwiftLexicalScopes/Scope.swift renamed to Sources/SwiftLexicalLookup/SimpleLookupQueries.swift

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,27 @@ import Foundation
1414
import SwiftSyntax
1515

1616
extension SyntaxProtocol {
17-
/// Scope at the syntax node. Could be inherited from parent or introduced at the node.
18-
var scope: Scope? {
19-
switch self.syntaxNodeType {
20-
case is SourceFileSyntax.Type:
21-
FileScope(syntax: self)
22-
default:
23-
parent?.scope
24-
}
17+
/// Given syntax node position, returns all available labeled statements.
18+
@_spi(Compiler) @_spi(Testing) public func lookupLabeledStmts() -> [LabeledStmtSyntax] {
19+
return lookupLabeledStmts(at: self)
2520
}
26-
}
27-
28-
/// Provide common functionality for specialized scope implementatations.
29-
protocol Scope {
30-
/// The parent of this scope.
31-
var parent: Scope? { get }
3221

33-
/// Syntax node that introduces this protocol.
34-
var sourceSyntax: SyntaxProtocol { get }
35-
}
22+
/// Given syntax node position, returns the current switch case and it's fallthrough destination.
23+
@_spi(Compiler) @_spi(Testing) public func lookupFallthroughSourceAndDest()
24+
-> (source: SwitchCaseSyntax?, destination: SwitchCaseSyntax?)
25+
{
26+
return lookupFallthroughSourceAndDestination(at: self)
27+
}
3628

37-
extension Scope {
38-
/// Recursively walks up syntax tree and finds the closest scope other than this scope.
39-
func getParentScope(forSyntax syntax: SyntaxProtocol?) -> Scope? {
40-
if let lookedUpScope = syntax?.scope, lookedUpScope.sourceSyntax.id == syntax?.id {
41-
return getParentScope(forSyntax: sourceSyntax.parent)
42-
} else {
43-
return syntax?.scope
44-
}
29+
/// Given syntax node position, returns the closest ancestor catch node.
30+
@_spi(Compiler) @_spi(Testing) public func lookupCatchNode() -> Syntax? {
31+
return lookupCatchNodeHelper(at: Syntax(self), traversedCatchClause: false)
4532
}
4633

4734
// MARK: - lookupLabeledStmts
4835

4936
/// Given syntax node position, returns all available labeled statements.
50-
func lookupLabeledStmts(at syntax: SyntaxProtocol) -> [LabeledStmtSyntax] {
37+
private func lookupLabeledStmts(at syntax: SyntaxProtocol) -> [LabeledStmtSyntax] {
5138
return walkParentTreeUpToFunctionBoundary(
5239
at: syntax.parent,
5340
collect: LabeledStmtSyntax.self
@@ -57,7 +44,9 @@ extension Scope {
5744
// MARK: - lookupFallthroughSourceAndDest
5845

5946
/// Given syntax node position, returns the current switch case and it's fallthrough destination.
60-
func lookupFallthroughSourceAndDestination(at syntax: SyntaxProtocol) -> (SwitchCaseSyntax?, SwitchCaseSyntax?) {
47+
private func lookupFallthroughSourceAndDestination(at syntax: SyntaxProtocol)
48+
-> (SwitchCaseSyntax?, SwitchCaseSyntax?)
49+
{
6150
guard
6251
let originalSwitchCase = walkParentTreeUpToFunctionBoundary(
6352
at: Syntax(syntax),
@@ -93,11 +82,6 @@ extension Scope {
9382

9483
// MARK: - lookupCatchNode
9584

96-
/// Given syntax node position, returns the closest ancestor catch node.
97-
func lookupCatchNode(at syntax: Syntax) -> Syntax? {
98-
return lookupCatchNodeHelper(at: syntax, traversedCatchClause: false)
99-
}
100-
10185
/// Given syntax node location, finds where an error could be caught. If set to `true`, `traverseCatchClause`lookup will skip the next do statement.
10286
private func lookupCatchNodeHelper(at syntax: Syntax?, traversedCatchClause: Bool) -> Syntax? {
10387
guard let syntax else { return nil }
@@ -117,23 +101,30 @@ extension Scope {
117101
} else {
118102
return lookupCatchNodeHelper(at: syntax.parent, traversedCatchClause: traversedCatchClause)
119103
}
120-
case .functionDecl, .accessorDecl, .initializerDecl:
104+
case .functionDecl, .accessorDecl, .initializerDecl, .deinitializerDecl, .closureExpr:
121105
return syntax
106+
case .exprList(let exprList):
107+
if let tryExpr = exprList.first?.as(TryExprSyntax.self), tryExpr.questionOrExclamationMark != nil {
108+
return Syntax(tryExpr)
109+
}
110+
return lookupCatchNodeHelper(at: syntax.parent, traversedCatchClause: traversedCatchClause)
122111
default:
123112
return lookupCatchNodeHelper(at: syntax.parent, traversedCatchClause: traversedCatchClause)
124113
}
125114
}
126115

116+
// MARK: - walkParentTree helper methods
117+
127118
/// Callect the first syntax node matching the collection type up to a function boundary.
128-
func walkParentTreeUpToFunctionBoundary<T: SyntaxProtocol>(
119+
private func walkParentTreeUpToFunctionBoundary<T: SyntaxProtocol>(
129120
at syntax: Syntax?,
130121
collect: T.Type
131122
) -> T? {
132123
walkParentTreeUpToFunctionBoundary(at: syntax, collect: collect, stopWithFirstMatch: true).first
133124
}
134125

135126
/// Callect syntax nodes matching the collection type up to a function boundary.
136-
func walkParentTreeUpToFunctionBoundary<T: SyntaxProtocol>(
127+
private func walkParentTreeUpToFunctionBoundary<T: SyntaxProtocol>(
137128
at syntax: Syntax?,
138129
collect: T.Type,
139130
stopWithFirstMatch: Bool = false
@@ -154,7 +145,7 @@ extension Scope {
154145
}
155146

156147
/// Callect syntax nodes matching the collection type up until encountering one of the specified syntax nodes.
157-
func walkParentTree<T: SyntaxProtocol>(
148+
private func walkParentTree<T: SyntaxProtocol>(
158149
upTo stopAt: [SyntaxProtocol.Type],
159150
at syntax: Syntax?,
160151
collect: T.Type,

Sources/SwiftLexicalScopes/LexicalScopes.swift

Lines changed: 0 additions & 36 deletions
This file was deleted.

Sources/SwiftLexicalScopes/ScopeConcreteImplementations.swift

Lines changed: 0 additions & 24 deletions
This file was deleted.

Tests/SwiftLexicalScopesTest/Assertions.swift renamed to Tests/SwiftLexicalLookupTest/Assertions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import Foundation
14-
@_spi(Testing) import SwiftLexicalScopes
14+
@_spi(Testing) import SwiftLexicalLookup
1515
import SwiftParser
1616
import SwiftSyntax
1717
import XCTest

Tests/SwiftLexicalScopesTest/SimpleQueryTests.swift renamed to Tests/SwiftLexicalLookupTest/SimpleQueryTests.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import Foundation
14-
@_spi(Testing) import SwiftLexicalScopes
14+
@_spi(Testing) import SwiftLexicalLookup
1515
import XCTest
1616

1717
final class testSimpleQueries: XCTestCase {
18-
func testLabeledStmtLookupThreeNestedScopes() {
18+
func testLabeledStmtLookupThreeNested() {
1919
assertLexicalScopeQuery(
2020
source: """
2121
1️⃣one: for i in 1..<10 {
@@ -176,4 +176,21 @@ final class testSimpleQueries: XCTestCase {
176176
expected: ["4️⃣": ["3️⃣"], "5️⃣": ["2️⃣"], "7️⃣": ["6️⃣"], "8️⃣": ["1️⃣"]]
177177
)
178178
}
179+
180+
func testCatchBlockLookupFromWithinExpressionList() {
181+
assertLexicalScopeQuery(
182+
source: """
183+
1️⃣do {
184+
try 2️⃣x + 3️⃣y + 4️⃣z
185+
5️⃣try! 6️⃣x + 7️⃣y + 8️⃣z
186+
} catch {
187+
print(error)
188+
}
189+
""",
190+
methodUnderTest: { argument in
191+
[argument.lookupCatchNode()]
192+
},
193+
expected: ["2️⃣": ["1️⃣"], "3️⃣": ["1️⃣"], "4️⃣": ["1️⃣"], "6️⃣": ["5️⃣"], "7️⃣": ["5️⃣"], "8️⃣": ["5️⃣"]]
194+
)
195+
}
179196
}

0 commit comments

Comments
 (0)