@@ -25,7 +25,50 @@ extension SyntaxProtocol {
2525
2626@_spi ( Experimental) extension SourceFileSyntax : ScopeSyntax {
2727 public var introducedNames : [ LookupName ] {
28- [ ]
28+ introducedNames ( using: . memberBlockUpToLastDecl)
29+ }
30+
31+ public func introducedNames( using nameIntroductionStrategy: FileScopeNameIntroductionStrategy ) -> [ LookupName ] {
32+ switch nameIntroductionStrategy {
33+ case . memberBlockUpToLastDecl:
34+ var encounteredNonDeclaration = false
35+
36+ return statements. flatMap { codeBlockItem in
37+ let item = codeBlockItem. item
38+
39+ if encounteredNonDeclaration {
40+ return LookupName . getNames ( from: item, accessibleAfter: codeBlockItem. endPosition)
41+ } else {
42+ if item. is ( DeclSyntax . self) || item. is ( VariableDeclSyntax . self) {
43+ return LookupName . getNames ( from: item)
44+ } else {
45+ encounteredNonDeclaration = true
46+ return LookupName . getNames ( from: item, accessibleAfter: codeBlockItem. endPosition)
47+ }
48+ }
49+ }
50+ case . codeBlock:
51+ return statements. flatMap { codeBlockItem in
52+ LookupName . getNames ( from: codeBlockItem. item, accessibleAfter: codeBlockItem. endPosition)
53+ }
54+ case . memberBlock:
55+ return statements. flatMap { codeBlockItem in
56+ LookupName . getNames ( from: codeBlockItem. item)
57+ }
58+ }
59+ }
60+
61+ public func lookup( for name: String ? , at syntax: SyntaxProtocol , with config: [ LookupConfig ] ) -> [ LookupResult ] {
62+ let nameIntroductionStrategy = config. first {
63+ $0 is FileScopeNameIntroductionStrategy
64+ } as? FileScopeNameIntroductionStrategy ?? . memberBlockUpToLastDecl
65+
66+ let names = introducedNames ( using: nameIntroductionStrategy)
67+ . filter { introducedName in
68+ introducedName. isAccessible ( at: syntax) && ( name == nil || introducedName. refersTo ( name!) )
69+ }
70+
71+ return [ . fromFileScope( self , withNames: names, nameIntroductionStrategy: nameIntroductionStrategy) ]
2972 }
3073}
3174
@@ -45,7 +88,17 @@ extension SyntaxProtocol {
4588
4689@_spi ( Experimental) extension ClosureExprSyntax : ScopeSyntax {
4790 public var introducedNames : [ LookupName ] {
48- signature? . parameterClause? . children ( viewMode: . sourceAccurate) . flatMap { parameter in
91+ let captureNames = signature? . capture? . children ( viewMode: . sourceAccurate) . flatMap { child in
92+ if let captureList = child. as ( ClosureCaptureListSyntax . self) {
93+ captureList. children ( viewMode: . sourceAccurate) . flatMap { capture in
94+ LookupName . getNames ( from: capture)
95+ }
96+ } else {
97+ LookupName . getNames ( from: child)
98+ }
99+ } ?? [ ]
100+
101+ let parameterNames = signature? . parameterClause? . children ( viewMode: . sourceAccurate) . flatMap { parameter in
49102 if let parameterList = parameter. as ( ClosureParameterListSyntax . self) {
50103 parameterList. children ( viewMode: . sourceAccurate) . flatMap { parameter in
51104 LookupName . getNames ( from: parameter)
@@ -54,6 +107,8 @@ extension SyntaxProtocol {
54107 LookupName . getNames ( from: parameter)
55108 }
56109 } ?? [ ]
110+
111+ return captureNames + parameterNames
57112 }
58113}
59114
@@ -91,11 +146,11 @@ extension SyntaxProtocol {
91146 }
92147 }
93148
94- public func lookup( for name: String ? , at syntax: SyntaxProtocol ) -> [ LookupResult ] {
149+ public func lookup( for name: String ? , at syntax: SyntaxProtocol , with config : [ LookupConfig ] ) -> [ LookupResult ] {
95150 if let elseBody, elseBody. position <= syntax. position, elseBody. endPosition >= syntax. position {
96- parentScope ? . lookup ( for: name, at: syntax) ?? [ ]
151+ lookupInParent ( for: name, at: syntax, with : config )
97152 } else {
98- defaultLookupImplementation ( for: name, at: syntax)
153+ defaultLookupImplementation ( for: name, at: syntax, with : config )
99154 }
100155 }
101156}
@@ -107,3 +162,17 @@ extension SyntaxProtocol {
107162 }
108163 }
109164}
165+
166+ @_spi ( Experimental) extension GuardStmtSyntax : ScopeSyntax {
167+ public var introducedNames : [ LookupName ] {
168+ [ ]
169+ }
170+
171+ public func lookup( for name: String ? , at syntax: SyntaxProtocol , with config: [ LookupConfig ] ) -> [ LookupResult ] {
172+ if body. position <= syntax. position && body. endPosition >= syntax. position {
173+ lookupInParent ( for: name, at: self , with: config)
174+ } else {
175+ defaultLookupImplementation ( for: name, at: syntax, with: config)
176+ }
177+ }
178+ }
0 commit comments