Skip to content

Commit e1831be

Browse files
committed
Fix class declaration availability in code block scopes.
1 parent ba0ca70 commit e1831be

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

Sources/SwiftLexicalLookup/LookupName.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ import SwiftSyntax
1818
case identifier(IdentifiableSyntax, accessibleAfter: AbsolutePosition?)
1919
/// Declaration associated with the name.
2020
/// Could be class, struct, actor, protocol, function and more.
21-
case declaration(NamedDeclSyntax, accessibleAfter: AbsolutePosition?)
21+
case declaration(NamedDeclSyntax)
2222

2323
/// Syntax associated with this name.
2424
@_spi(Experimental) public var syntax: SyntaxProtocol {
2525
switch self {
2626
case .identifier(let syntax, _):
2727
return syntax
28-
case .declaration(let syntax, _):
28+
case .declaration(let syntax):
2929
return syntax
3030
}
3131
}
@@ -35,7 +35,7 @@ import SwiftSyntax
3535
switch self {
3636
case .identifier(let syntax, _):
3737
return Identifier(syntax.identifier)
38-
case .declaration(let syntax, _):
38+
case .declaration(let syntax):
3939
return Identifier(syntax.name)
4040
}
4141
}
@@ -44,8 +44,10 @@ import SwiftSyntax
4444
/// If set to `nil`, the name is available at any point in scope.
4545
var accessibleAfter: AbsolutePosition? {
4646
switch self {
47-
case .identifier(_, let absolutePosition), .declaration(_, let absolutePosition):
47+
case .identifier(_, let absolutePosition):
4848
return absolutePosition
49+
default:
50+
return nil
4951
}
5052
}
5153

@@ -133,6 +135,6 @@ import SwiftSyntax
133135
namedDecl: NamedDeclSyntax,
134136
accessibleAfter: AbsolutePosition? = nil
135137
) -> [LookupName] {
136-
[.declaration(namedDecl, accessibleAfter: accessibleAfter)]
138+
[.declaration(namedDecl)]
137139
}
138140
}

Tests/SwiftLexicalLookupTest/NameLookupTests.swift

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -486,13 +486,13 @@ final class testNameLookup: XCTestCase {
486486
487487
9️⃣class d {}
488488
489-
let x = 0️⃣d
489+
let 🔟a = 0️⃣d
490490
""",
491491
references: [
492492
"3️⃣": [.fromFileScope(expectedNames: ["1️⃣", "8️⃣"])],
493493
"4️⃣": [.fromFileScope(expectedNames: ["2️⃣"])],
494494
"5️⃣": [.fromFileScope(expectedNames: ["7️⃣"])],
495-
"6️⃣": [],
495+
"6️⃣": [.fromFileScope(expectedNames: ["9️⃣"])],
496496
"0️⃣": [.fromFileScope(expectedNames: ["9️⃣"])],
497497
],
498498
expectedResultTypes: .all(ClassDeclSyntax.self, except: ["8️⃣": IdentifierPatternSyntax.self])
@@ -516,16 +516,19 @@ final class testNameLookup: XCTestCase {
516516
517517
9️⃣class d {}
518518
519-
let x = 0️⃣d
519+
let 🔟a = 0️⃣d
520520
""",
521521
references: [
522-
"3️⃣": [.fromFileScope(expectedNames: ["1️⃣", "8️⃣"])],
522+
"3️⃣": [.fromFileScope(expectedNames: ["1️⃣", "8️⃣", "🔟"])],
523523
"4️⃣": [.fromFileScope(expectedNames: ["2️⃣"])],
524524
"5️⃣": [.fromFileScope(expectedNames: ["7️⃣"])],
525525
"6️⃣": [.fromFileScope(expectedNames: ["9️⃣"])],
526526
"0️⃣": [.fromFileScope(expectedNames: ["9️⃣"])],
527527
],
528-
expectedResultTypes: .all(ClassDeclSyntax.self, except: ["8️⃣": IdentifierPatternSyntax.self]),
528+
expectedResultTypes: .all(ClassDeclSyntax.self, except: [
529+
"8️⃣": IdentifierPatternSyntax.self,
530+
"🔟": IdentifierPatternSyntax.self
531+
]),
529532
config: LookupConfig(fileScopeHandling: .memberBlock)
530533
)
531534
}
@@ -547,17 +550,35 @@ final class testNameLookup: XCTestCase {
547550
548551
9️⃣class d {}
549552
550-
let x = 0️⃣d
553+
let 🔟a = 0️⃣d
551554
""",
552555
references: [
553556
"3️⃣": [.fromFileScope(expectedNames: ["1️⃣"])],
554-
"4️⃣": [],
555-
"5️⃣": [],
556-
"6️⃣": [],
557+
"4️⃣": [.fromFileScope(expectedNames: ["2️⃣"])],
558+
"5️⃣": [.fromFileScope(expectedNames: ["7️⃣"])],
559+
"6️⃣": [.fromFileScope(expectedNames: ["9️⃣"])],
557560
"0️⃣": [.fromFileScope(expectedNames: ["9️⃣"])],
558561
],
559562
expectedResultTypes: .all(ClassDeclSyntax.self, except: ["8️⃣": IdentifierPatternSyntax.self]),
560563
config: LookupConfig(fileScopeHandling: .codeBlock)
561564
)
562565
}
566+
567+
func testDeclarationAvailabilityInCodeBlock() {
568+
assertLexicalNameLookup(
569+
source: """
570+
func x {
571+
1️⃣class A {}
572+
573+
let a = 2️⃣A()
574+
575+
3️⃣class A {}
576+
}
577+
""",
578+
references: [
579+
"2️⃣": [.fromScope(CodeBlockSyntax.self, expectedNames: ["1️⃣", "3️⃣"])]
580+
],
581+
expectedResultTypes: .all(ClassDeclSyntax.self)
582+
)
583+
}
563584
}

0 commit comments

Comments
 (0)