Skip to content

Commit

Permalink
Fix associating let-shorthand uses with the correct declaration, atte…
Browse files Browse the repository at this point in the history
…mpt #2
  • Loading branch information
ileitch committed Mar 4, 2023
1 parent 7655012 commit c92f3a3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
38 changes: 30 additions & 8 deletions Sources/PeripheryKit/Syntax/DeclarationSyntaxVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class DeclarationSyntaxVisitor: PeripherySyntaxVisitor {
private(set) var results: [Result] = []
private var letShorthandIdentifiers: Set<String> = []
private var didVisitCapitalSelfFunctionCall: Bool = false
private var codeBlockStackDepth = 0
private var declarationBodyStackDepth = 0

var resultsByLocation: [SourceLocation: Result] {
results.reduce(into: [SourceLocation: Result]()) { (dict, result) in
Expand Down Expand Up @@ -116,15 +116,13 @@ final class DeclarationSyntaxVisitor: PeripherySyntaxVisitor {
)
}

func visit(_: CodeBlockSyntax) {
codeBlockStackDepth += 1
}

func visitPost(_: CodeBlockSyntax) {
codeBlockStackDepth -= 1
func visit(_: FunctionDeclSyntax) {
declarationBodyStackDepth += 1
}

func visitPost(_ node: FunctionDeclSyntax) {
declarationBodyStackDepth -= 1

parse(
modifiers: node.modifiers,
attributes: node.attributes,
Expand All @@ -137,7 +135,13 @@ final class DeclarationSyntaxVisitor: PeripherySyntaxVisitor {
)
}

func visit(_: InitializerDeclSyntax) {
declarationBodyStackDepth += 1
}

func visitPost(_ node: InitializerDeclSyntax) {
declarationBodyStackDepth -= 1

parse(
modifiers: node.modifiers,
attributes: node.attributes,
Expand All @@ -149,7 +153,13 @@ final class DeclarationSyntaxVisitor: PeripherySyntaxVisitor {
)
}

func visit(_: DeinitializerDeclSyntax) {
declarationBodyStackDepth += 1
}

func visitPost(_ node: DeinitializerDeclSyntax) {
declarationBodyStackDepth -= 1

parse(
modifiers: node.modifiers,
attributes: node.attributes,
Expand All @@ -158,7 +168,13 @@ final class DeclarationSyntaxVisitor: PeripherySyntaxVisitor {
)
}

func visit(_: SubscriptDeclSyntax) {
declarationBodyStackDepth += 1
}

func visitPost(_ node: SubscriptDeclSyntax) {
declarationBodyStackDepth -= 1

parse(
modifiers: node.modifiers,
attributes: node.attributes,
Expand All @@ -171,7 +187,13 @@ final class DeclarationSyntaxVisitor: PeripherySyntaxVisitor {
)
}

func visit(_: VariableDeclSyntax) {
declarationBodyStackDepth += 1
}

func visitPost(_ node: VariableDeclSyntax) {
declarationBodyStackDepth -= 1

for binding in node.bindings {
if binding.pattern.is(IdentifierPatternSyntax.self) {
let closureSignature = binding.initializer?.value.as(ClosureExprSyntax.self)?.signature
Expand Down Expand Up @@ -305,7 +327,7 @@ final class DeclarationSyntaxVisitor: PeripherySyntaxVisitor {

// Only associate let shorthand identifiers in nested code blocks with the top-most
// code block.
if codeBlockStackDepth == 0 {
if declarationBodyStackDepth == 0 {
letShorthandIdentifiers = self.letShorthandIdentifiers
self.letShorthandIdentifiers.removeAll()
}
Expand Down
13 changes: 0 additions & 13 deletions Sources/PeripheryKit/Syntax/MultiplexingSyntaxVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ protocol PeripherySyntaxVisitor {
func visit(_ node: ImportDeclSyntax)
func visit(_ node: OptionalBindingConditionSyntax)
func visit(_ node: FunctionCallExprSyntax)
func visit(_: CodeBlockSyntax)

func visitPost(_ node: ClassDeclSyntax)
func visitPost(_ node: ProtocolDeclSyntax)
Expand All @@ -44,7 +43,6 @@ protocol PeripherySyntaxVisitor {
func visitPost(_ node: ImportDeclSyntax)
func visitPost(_ node: OptionalBindingConditionSyntax)
func visitPost(_ node: FunctionCallExprSyntax)
func visitPost(_ node: CodeBlockSyntax)
}

extension PeripherySyntaxVisitor {
Expand All @@ -66,7 +64,6 @@ extension PeripherySyntaxVisitor {
func visit(_ node: ImportDeclSyntax) { }
func visit(_ node: OptionalBindingConditionSyntax) {}
func visit(_ node: FunctionCallExprSyntax) {}
func visit(_: CodeBlockSyntax) {}

func visitPost(_ node: ClassDeclSyntax) {}
func visitPost(_ node: ProtocolDeclSyntax) {}
Expand All @@ -86,7 +83,6 @@ extension PeripherySyntaxVisitor {
func visitPost(_ node: ImportDeclSyntax) {}
func visitPost(_ node: OptionalBindingConditionSyntax) {}
func visitPost(_ node: FunctionCallExprSyntax) {}
func visitPost(_ node: CodeBlockSyntax) {}
}

final class MultiplexingSyntaxVisitor: SyntaxVisitor {
Expand Down Expand Up @@ -203,11 +199,6 @@ final class MultiplexingSyntaxVisitor: SyntaxVisitor {
return .visitChildren
}

override func visit(_ node: CodeBlockSyntax) -> SyntaxVisitorContinueKind {
visitors.forEach { $0.visit(node) }
return .visitChildren
}

override func visitPost(_ node: ClassDeclSyntax) {
visitors.forEach { $0.visitPost(node) }
}
Expand Down Expand Up @@ -279,8 +270,4 @@ final class MultiplexingSyntaxVisitor: SyntaxVisitor {
override func visitPost(_ node: FunctionCallExprSyntax) {
visitors.forEach { $0.visitPost(node) }
}

override func visitPost(_ node: CodeBlockSyntax) {
visitors.forEach { $0.visitPost(node) }
}
}

0 comments on commit c92f3a3

Please sign in to comment.