Skip to content

Commit b85dced

Browse files
committed
Remove name lookup method placeholder and test harness. Add missing cases for stopping walkParentTreeUpToFunctionBoundary. Format code.
1 parent 74ddcc4 commit b85dced

File tree

5 files changed

+71
-76
lines changed

5 files changed

+71
-76
lines changed

Sources/SwiftLexicalScopes/LexicalScopes.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ extension SyntaxProtocol {
2222

2323
/// Given syntax node position, returns the current switch case and it's fallthrough destination.
2424
@_spi(Compiler) @_spi(Testing) public func lookupFallthroughSourceAndDest()
25-
-> (source: SwitchCaseSyntax?, destination: SwitchCaseSyntax?) {
25+
-> (source: SwitchCaseSyntax?, destination: SwitchCaseSyntax?)
26+
{
2627
guard let scope else { return (nil, nil) }
2728
return scope.lookupFallthroughSourceAndDestination(at: self)
2829
}
@@ -32,10 +33,4 @@ extension SyntaxProtocol {
3233
guard let scope else { return nil }
3334
return scope.lookupCatchNode(at: Syntax(self))
3435
}
35-
36-
/// Given name and syntax node position, return referenced declaration.
37-
@_spi(Compiler) @_spi(Testing) public func lookupDeclarationsFor(name: String) -> [Syntax] {
38-
guard let scope else { return [] }
39-
return scope.getDeclarationsFor(name: name, at: self)
40-
}
4136
}

Sources/SwiftLexicalScopes/Scope.swift

Lines changed: 67 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ protocol Scope {
3232

3333
/// Syntax node that introduces this protocol.
3434
var sourceSyntax: SyntaxProtocol { get }
35-
36-
/// Returns the declaration `name` refers to at a particular syntax node location.
37-
func getDeclarationsFor(name: String, at syntax: SyntaxProtocol) -> [Syntax]
3835
}
3936

4037
extension Scope {
@@ -46,33 +43,41 @@ extension Scope {
4643
return syntax?.scope
4744
}
4845
}
49-
46+
5047
// MARK: - lookupLabeledStmts
51-
48+
5249
/// Given syntax node position, returns all available labeled statements.
5350
func lookupLabeledStmts(at syntax: SyntaxProtocol) -> [LabeledStmtSyntax] {
54-
return walkParentTreeUpToFunctionBoundary(at: syntax.parent, collect: LabeledStmtSyntax.self)
51+
return walkParentTreeUpToFunctionBoundary(
52+
at: syntax.parent,
53+
collect: LabeledStmtSyntax.self
54+
)
5555
}
56-
56+
5757
// MARK: - lookupFallthroughSourceAndDest
58-
58+
5959
/// Given syntax node position, returns the current switch case and it's fallthrough destination.
6060
func lookupFallthroughSourceAndDestination(at syntax: SyntaxProtocol) -> (SwitchCaseSyntax?, SwitchCaseSyntax?) {
61-
guard let originalSwitchCase = walkParentTreeUpToFunctionBoundary(at: Syntax(syntax), collect: SwitchCaseSyntax.self) else {
61+
guard
62+
let originalSwitchCase = walkParentTreeUpToFunctionBoundary(
63+
at: Syntax(syntax),
64+
collect: SwitchCaseSyntax.self
65+
)
66+
else {
6267
return (nil, nil)
6368
}
64-
69+
6570
let nextSwitchCase = lookupNextSwitchCase(at: originalSwitchCase)
66-
71+
6772
return (originalSwitchCase, nextSwitchCase)
6873
}
69-
74+
7075
/// Given a switch case, returns the case that follows according to the parent.
7176
private func lookupNextSwitchCase(at switchCaseSyntax: SwitchCaseSyntax) -> SwitchCaseSyntax? {
7277
guard let switchCaseListSyntax = switchCaseSyntax.parent?.as(SwitchCaseListSyntax.self) else { return nil }
73-
78+
7479
var visitedOriginalCase = false
75-
80+
7681
for child in switchCaseListSyntax.children(viewMode: .sourceAccurate) {
7782
if let thisCase = child.as(SwitchCaseSyntax.self) {
7883
if thisCase.id == switchCaseSyntax.id {
@@ -82,21 +87,21 @@ extension Scope {
8287
}
8388
}
8489
}
85-
90+
8691
return nil
8792
}
88-
93+
8994
// MARK: - lookupCatchNode
90-
95+
9196
/// Given syntax node position, returns the closest ancestor catch node.
9297
func lookupCatchNode(at syntax: Syntax) -> Syntax? {
9398
return lookupCatchNodeHelper(at: syntax, traversedCatchClause: false)
9499
}
95-
100+
96101
/// Given syntax node location, finds where an error could be caught. If set to `true`, `traverseCatchClause`lookup will skip the next do statement.
97102
private func lookupCatchNodeHelper(at syntax: Syntax?, traversedCatchClause: Bool) -> Syntax? {
98103
guard let syntax else { return nil }
99-
104+
100105
switch syntax.as(SyntaxEnum.self) {
101106
case .doStmt:
102107
if traversedCatchClause {
@@ -118,41 +123,63 @@ extension Scope {
118123
return lookupCatchNodeHelper(at: syntax.parent, traversedCatchClause: traversedCatchClause)
119124
}
120125
}
121-
126+
122127
/// Callect the first syntax node matching the collection type up to a function boundary.
123-
func walkParentTreeUpToFunctionBoundary<T: SyntaxProtocol>(at syntax: Syntax?,
124-
collect: T.Type) -> T? {
128+
func walkParentTreeUpToFunctionBoundary<T: SyntaxProtocol>(
129+
at syntax: Syntax?,
130+
collect: T.Type
131+
) -> T? {
125132
walkParentTreeUpToFunctionBoundary(at: syntax, collect: collect, stopWithFirstMatch: true).first
126133
}
127-
134+
128135
/// Callect syntax nodes matching the collection type up to a function boundary.
129-
func walkParentTreeUpToFunctionBoundary<T: SyntaxProtocol>(at syntax: Syntax?,
130-
collect: T.Type,
131-
stopWithFirstMatch: Bool = false) -> [T] {
132-
walkParentTree(upTo: [MemberBlockSyntax.self,
133-
FunctionDeclSyntax.self,
134-
InitializerDeclSyntax.self,
135-
ClosureExprSyntax.self],
136-
at: syntax,
137-
collect: collect,
138-
stopWithFirstMatch: stopWithFirstMatch
136+
func walkParentTreeUpToFunctionBoundary<T: SyntaxProtocol>(
137+
at syntax: Syntax?,
138+
collect: T.Type,
139+
stopWithFirstMatch: Bool = false
140+
) -> [T] {
141+
walkParentTree(
142+
upTo: [
143+
MemberBlockSyntax.self,
144+
FunctionDeclSyntax.self,
145+
InitializerDeclSyntax.self,
146+
DeinitializerDeclSyntax.self,
147+
AccessorDeclSyntax.self,
148+
ClosureExprSyntax.self,
149+
],
150+
at: syntax,
151+
collect: collect,
152+
stopWithFirstMatch: stopWithFirstMatch
139153
)
140154
}
141-
155+
142156
/// Callect syntax nodes matching the collection type up until encountering one of the specified syntax nodes.
143-
func walkParentTree<T: SyntaxProtocol>(upTo stopAt: [SyntaxProtocol.Type],
144-
at syntax: Syntax?,
145-
collect: T.Type,
146-
stopWithFirstMatch: Bool = false) -> [T] {
157+
func walkParentTree<T: SyntaxProtocol>(
158+
upTo stopAt: [SyntaxProtocol.Type],
159+
at syntax: Syntax?,
160+
collect: T.Type,
161+
stopWithFirstMatch: Bool = false
162+
) -> [T] {
147163
guard let syntax, !stopAt.contains(where: { syntax.is($0) }) else { return [] }
148164
if let matchedSyntax = syntax.as(T.self) {
149165
if stopWithFirstMatch {
150166
return [matchedSyntax]
151167
} else {
152-
return [matchedSyntax] + walkParentTree(upTo: stopAt, at: syntax.parent, collect: collect, stopWithFirstMatch: stopWithFirstMatch)
168+
return [matchedSyntax]
169+
+ walkParentTree(
170+
upTo: stopAt,
171+
at: syntax.parent,
172+
collect: collect,
173+
stopWithFirstMatch: stopWithFirstMatch
174+
)
153175
}
154176
} else {
155-
return walkParentTree(upTo: stopAt, at: syntax.parent, collect: collect, stopWithFirstMatch: stopWithFirstMatch)
177+
return walkParentTree(
178+
upTo: stopAt,
179+
at: syntax.parent,
180+
collect: collect,
181+
stopWithFirstMatch: stopWithFirstMatch
182+
)
156183
}
157184
}
158185
}

Sources/SwiftLexicalScopes/ScopeConcreteImplementations.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,4 @@ class FileScope: Scope {
2121
init(syntax: SyntaxProtocol) {
2222
self.sourceSyntax = syntax
2323
}
24-
25-
func getDeclarationsFor(name: String, at syntax: SyntaxProtocol) -> [Syntax] {
26-
// TODO: Implement the method
27-
return []
28-
}
2924
}

Tests/SwiftLexicalScopesTest/Assertions.swift

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -81,25 +81,3 @@ func assertLexicalScopeQuery(
8181
}
8282
}
8383
}
84-
85-
/// Parse `source` and check if the lexical name lookup matches results passed as `expected`.
86-
///
87-
/// - Parameters:
88-
/// - expected: A dictionary of markers with reference location as keys and expected declaration as values.
89-
func assertLexicalNameLookup(
90-
source: String,
91-
references: [String: [String]]
92-
) {
93-
assertLexicalScopeQuery(
94-
source: source,
95-
methodUnderTest: { argument in
96-
// Extract reference name and use it for lookup
97-
guard let name = argument.firstToken(viewMode: .sourceAccurate)?.text else {
98-
XCTFail("Couldn't find a token at \(argument)")
99-
return []
100-
}
101-
return argument.lookupDeclarationsFor(name: name)
102-
},
103-
expected: references
104-
)
105-
}

Tests/SwiftLexicalScopesTest/SimpleQueryTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ final class testSimpleQueries: XCTestCase {
6868
expected: ["3️⃣": ["2️⃣"], "4️⃣": ["1️⃣"]]
6969
)
7070
}
71-
71+
7272
func testLabeledStmtLookupClosureNestedWithinLoop() {
7373
assertLexicalScopeQuery(
7474
source: """
@@ -87,7 +87,7 @@ final class testSimpleQueries: XCTestCase {
8787
expected: ["3️⃣": ["2️⃣"], "4️⃣": ["1️⃣"]]
8888
)
8989
}
90-
90+
9191
func testLabeledStmtLookupFunctionNestedWithinLoop() {
9292
assertLexicalScopeQuery(
9393
source: """

0 commit comments

Comments
 (0)