Skip to content

Commit 93cb3ea

Browse files
committed
Create a new enum representing all possible implicit name cases. Simplify LookupName.
1 parent 8b5fbd2 commit 93cb3ea

File tree

5 files changed

+98
-63
lines changed

5 files changed

+98
-63
lines changed

Sources/SwiftLexicalLookup/LookupName.swift

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,25 @@
1212

1313
import SwiftSyntax
1414

15-
@_spi(Experimental) public enum LookupName {
16-
/// Identifier associated with the name.
17-
/// Could be an identifier of a variable, function or closure parameter and more
18-
case identifier(IdentifiableSyntax, accessibleAfter: AbsolutePosition?)
19-
/// Declaration associated with the name.
20-
/// Could be class, struct, actor, protocol, function and more
21-
case declaration(NamedDeclSyntax, accessibleAfter: AbsolutePosition?)
15+
@_spi(Experimental) public enum LookupImplicitNameKind {
2216
/// `self` keyword representing object instance.
23-
case `self`(DeclSyntaxProtocol)
17+
case `self`(SyntaxProtocol)
2418
/// `Self` keyword representing object type.
2519
case `Self`(DeclSyntaxProtocol)
2620
/// `self` captured by a closure.
27-
case selfCaptured(ClosureCaptureSyntax)
28-
/// `error` available inside `catch` clause.
2921
case error(CatchClauseSyntax)
3022
/// `newValue` available by default inside `set` and `willSet`.
3123
case newValue(AccessorDeclSyntax)
3224
/// `oldValue` available by default inside `didSet`.
3325
case oldValue(AccessorDeclSyntax)
34-
26+
3527
/// Syntax associated with this name.
3628
@_spi(Experimental) public var syntax: SyntaxProtocol {
3729
switch self {
38-
case .identifier(let syntax, _):
39-
syntax
40-
case .declaration(let syntax, _):
41-
syntax
4230
case .self(let syntax):
4331
syntax
4432
case .Self(let syntax):
4533
syntax
46-
case .selfCaptured(let syntax):
47-
syntax
4834
case .error(let syntax):
4935
syntax
5036
case .newValue(let syntax):
@@ -53,6 +39,45 @@ import SwiftSyntax
5339
syntax
5440
}
5541
}
42+
43+
/// Used for name comparison.
44+
var name: String {
45+
switch self {
46+
case .self:
47+
"self"
48+
case .Self:
49+
"Self"
50+
case .error:
51+
"error"
52+
case .newValue:
53+
"newValue"
54+
case .oldValue:
55+
"oldValue"
56+
}
57+
}
58+
}
59+
60+
@_spi(Experimental) public enum LookupName {
61+
/// Identifier associated with the name.
62+
/// Could be an identifier of a variable, function or closure parameter and more.
63+
case identifier(IdentifiableSyntax, accessibleAfter: AbsolutePosition?)
64+
/// Declaration associated with the name.
65+
/// Could be class, struct, actor, protocol, function and more.
66+
case declaration(NamedDeclSyntax, accessibleAfter: AbsolutePosition?)
67+
/// Name introduced implicitly certain syntax nodes.
68+
case implicit(LookupImplicitNameKind)
69+
70+
/// Syntax associated with this name.
71+
@_spi(Experimental) public var syntax: SyntaxProtocol {
72+
switch self {
73+
case .identifier(let syntax, _):
74+
syntax
75+
case .declaration(let syntax, _):
76+
syntax
77+
case .implicit(let implicitName):
78+
implicitName.syntax
79+
}
80+
}
5681

5782
/// Introduced name.
5883
@_spi(Experimental) public var identifier: Identifier? {
@@ -82,16 +107,8 @@ import SwiftSyntax
82107
switch self {
83108
case .identifier, .declaration:
84109
identifier?.name
85-
case .self, .selfCaptured:
86-
"self"
87-
case .Self:
88-
"Self"
89-
case .error:
90-
"error"
91-
case .newValue:
92-
"newValue"
93-
case .oldValue:
94-
"oldValue"
110+
case .implicit(let implicitName):
111+
implicitName.name
95112
}
96113
}
97114

@@ -153,7 +170,7 @@ import SwiftSyntax
153170
if let closureCapture = identifiable as? ClosureCaptureSyntax,
154171
closureCapture.identifier.tokenKind == .keyword(.self)
155172
{
156-
return [.selfCaptured(closureCapture)] // Handle `self` closure capture.
173+
return [.implicit(.self(closureCapture))] // Handle `self` closure capture.
157174
} else if identifiable.identifier.text != "_" {
158175
return [.identifier(identifiable, accessibleAfter: accessibleAfter)]
159176
} else {

Sources/SwiftLexicalLookup/ScopeImplementations.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,9 +379,9 @@ extension SyntaxProtocol {
379379
} else {
380380
switch accessorSpecifier.tokenKind {
381381
case .keyword(.set), .keyword(.willSet):
382-
[.newValue(self)]
382+
[.implicit(.newValue(self))]
383383
case .keyword(.didSet):
384-
[.oldValue(self)]
384+
[.implicit(.oldValue(self))]
385385
default:
386386
[]
387387
}

Sources/SwiftLexicalLookup/TypeScopeSyntax.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import SwiftSyntax
1818

1919
@_spi(Experimental) extension TypeScopeSyntax {
2020
public var implicitInstanceAndTypeNames: [LookupName] {
21-
[.self(self), .Self(self)]
21+
[.implicit(.self(self)), .implicit(.Self(self))]
2222
}
2323

2424
public var introducedNames: [LookupName] {

Tests/SwiftLexicalLookupTest/ExpectedName.swift

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,60 @@ extension String: ExpectedName {
2525
}
2626
}
2727

28+
enum ImplicitNameExpectation {
29+
case `self`(String)
30+
case `Self`(String)
31+
case error(String)
32+
case newValue(String)
33+
case oldValue(String)
34+
35+
func assertExpectation(marker: String, for name: LookupImplicitNameKind) {
36+
switch (name, self) {
37+
case (.self, .self): break
38+
case (.Self, .Self): break
39+
case (.error, .error): break
40+
case (.newValue, .newValue): break
41+
case (.oldValue, .oldValue): break
42+
default:
43+
XCTFail("For marker \(marker), actual name kind \(name) doesn't match expected \(self)")
44+
}
45+
}
46+
47+
var marker: String {
48+
switch self {
49+
case .self(let marker),
50+
.Self(let marker),
51+
.error(let marker),
52+
.newValue(let marker),
53+
.oldValue(let marker):
54+
marker
55+
}
56+
}
57+
}
58+
2859
/// Can be used to optionally assert
2960
/// exact lookup name kind.
3061
enum NameExpectation: ExpectedName {
3162
case identifier(String)
3263
case declaration(String)
33-
case selfInstance(String)
34-
case selfType(String)
35-
case selfCaptured(String)
36-
case error(String)
37-
case newValue(String)
38-
case oldValue(String)
64+
case implicit(ImplicitNameExpectation)
3965

4066
var marker: String {
4167
switch self {
4268
case .identifier(let marker),
43-
.declaration(let marker),
44-
.selfInstance(let marker),
45-
.selfType(let marker),
46-
.selfCaptured(let marker),
47-
.error(let marker),
48-
.newValue(let marker),
49-
.oldValue(let marker):
69+
.declaration(let marker):
5070
marker
71+
case .implicit(let implicitName):
72+
implicitName.marker
5173
}
5274
}
5375

5476
private func assertExpectation(marker: String, for name: LookupName) {
5577
switch (name, self) {
5678
case (.identifier, .identifier): break
5779
case (.declaration, .declaration): break
58-
case (.self, .selfInstance): break
59-
case (.Self, .selfType): break
60-
case (.selfCaptured, .selfCaptured): break
61-
case (.error, .error): break
62-
case (.newValue, .newValue): break
63-
case (.oldValue, .oldValue): break
80+
case (.implicit(let implicitName), .implicit(let implicitNameExpectation)):
81+
implicitNameExpectation.assertExpectation(marker: marker, for: implicitName)
6482
default:
6583
XCTFail("For marker \(marker), actual name kind \(name) doesn't match expected \(self)")
6684
}

Tests/SwiftLexicalLookupTest/NameLookupTests.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ final class testNameLookup: XCTestCase {
150150
""",
151151
references: [
152152
"5️⃣": [
153-
.fromScope(ClosureExprSyntax.self, expectedNames: [NameExpectation.selfCaptured("2️⃣")]),
154-
.fromScope(ClassDeclSyntax.self, expectedNames: [NameExpectation.selfInstance("7️⃣")]),
153+
.fromScope(ClosureExprSyntax.self, expectedNames: [NameExpectation.implicit(.self("2️⃣"))]),
154+
.fromScope(ClassDeclSyntax.self, expectedNames: [NameExpectation.implicit(.self("7️⃣"))]),
155155
],
156156
"6️⃣": [
157157
.fromScope(ClosureExprSyntax.self, expectedNames: ["3️⃣"]),
@@ -412,7 +412,7 @@ final class testNameLookup: XCTestCase {
412412
.fromScope(MemberBlockSyntax.self, expectedNames: ["1️⃣", "2️⃣", "3️⃣"]),
413413
.fromScope(
414414
ClassDeclSyntax.self,
415-
expectedNames: [NameExpectation.selfInstance("🔟"), NameExpectation.selfType("🔟")]
415+
expectedNames: [NameExpectation.implicit(.self("🔟")), NameExpectation.implicit(.Self("🔟"))]
416416
),
417417
.fromFileScope(expectedNames: ["🔟"]),
418418
],
@@ -423,7 +423,7 @@ final class testNameLookup: XCTestCase {
423423
.fromScope(MemberBlockSyntax.self, expectedNames: ["1️⃣", "2️⃣", "3️⃣"]),
424424
.fromScope(
425425
ClassDeclSyntax.self,
426-
expectedNames: [NameExpectation.selfInstance("🔟"), NameExpectation.selfType("🔟")]
426+
expectedNames: [NameExpectation.implicit(.self("🔟")), NameExpectation.implicit(.Self("🔟"))]
427427
),
428428
.fromFileScope(expectedNames: ["🔟"]),
429429
],
@@ -651,15 +651,15 @@ final class testNameLookup: XCTestCase {
651651
""",
652652
references: [
653653
"3️⃣": [
654-
.fromScope(StructDeclSyntax.self, expectedNames: [NameExpectation.selfType("2️⃣")]),
655-
.fromScope(ExtensionDeclSyntax.self, expectedNames: [NameExpectation.selfType("1️⃣")]),
654+
.fromScope(StructDeclSyntax.self, expectedNames: [NameExpectation.implicit(.Self("2️⃣"))]),
655+
.fromScope(ExtensionDeclSyntax.self, expectedNames: [NameExpectation.implicit(.Self("1️⃣"))]),
656656
],
657657
"4️⃣": [
658-
.fromScope(StructDeclSyntax.self, expectedNames: [NameExpectation.selfInstance("2️⃣")]),
659-
.fromScope(ExtensionDeclSyntax.self, expectedNames: [NameExpectation.selfInstance("1️⃣")]),
658+
.fromScope(StructDeclSyntax.self, expectedNames: [NameExpectation.implicit(.self("2️⃣"))]),
659+
.fromScope(ExtensionDeclSyntax.self, expectedNames: [NameExpectation.implicit(.self("1️⃣"))]),
660660
],
661-
"5️⃣": [.fromScope(ExtensionDeclSyntax.self, expectedNames: [NameExpectation.selfType("1️⃣")])],
662-
"6️⃣": [.fromScope(ExtensionDeclSyntax.self, expectedNames: [NameExpectation.selfInstance("1️⃣")])],
661+
"5️⃣": [.fromScope(ExtensionDeclSyntax.self, expectedNames: [NameExpectation.implicit(.Self("1️⃣"))])],
662+
"6️⃣": [.fromScope(ExtensionDeclSyntax.self, expectedNames: [NameExpectation.implicit(.self("1️⃣"))])],
663663
]
664664
)
665665
}
@@ -691,10 +691,10 @@ final class testNameLookup: XCTestCase {
691691
}
692692
""",
693693
references: [
694-
"2️⃣": [.fromScope(AccessorDeclSyntax.self, expectedNames: [NameExpectation.newValue("1️⃣")])],
694+
"2️⃣": [.fromScope(AccessorDeclSyntax.self, expectedNames: [NameExpectation.implicit(.newValue("1️⃣"))])],
695695
"4️⃣": [.fromScope(AccessorDeclSyntax.self, expectedNames: [NameExpectation.identifier("3️⃣")])],
696-
"6️⃣": [.fromScope(AccessorDeclSyntax.self, expectedNames: [NameExpectation.newValue("5️⃣")])],
697-
"8️⃣": [.fromScope(AccessorDeclSyntax.self, expectedNames: [NameExpectation.oldValue("7️⃣")])],
696+
"6️⃣": [.fromScope(AccessorDeclSyntax.self, expectedNames: [NameExpectation.implicit(.newValue("5️⃣"))])],
697+
"8️⃣": [.fromScope(AccessorDeclSyntax.self, expectedNames: [NameExpectation.implicit(.oldValue("7️⃣"))])],
698698
]
699699
)
700700
}

0 commit comments

Comments
 (0)