Skip to content

Commit 0086996

Browse files
author
Manuel Raimann
committed
Extracted logic, to deduplicate code
1 parent fa49635 commit 0086996

File tree

1 file changed

+27
-54
lines changed

1 file changed

+27
-54
lines changed

internal/checker/nodebuilderimpl.go

Lines changed: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,31 @@ func (b *NodeBuilderImpl) setCommentRange(node *ast.Node, range_ *ast.Node) {
340340
}
341341
}
342342

343+
func isEntityNameInTypePosition(node *ast.Node) (shouldCheckOrTrack bool, dontVisitChildren bool) {
344+
parent := node.Parent
345+
if parent != nil {
346+
switch parent.Kind {
347+
case ast.KindTypeReference:
348+
if parent.AsTypeReference().TypeName == node {
349+
return true, true
350+
}
351+
case ast.KindExpressionWithTypeArguments:
352+
if parent.AsExpressionWithTypeArguments().Expression == node {
353+
return true, true
354+
}
355+
case ast.KindTypeQuery:
356+
if parent.AsTypeQueryNode().ExprName == node {
357+
return true, true
358+
}
359+
case ast.KindImportType:
360+
if parent.AsImportTypeNode().Qualifier == node {
361+
return true, true
362+
}
363+
}
364+
}
365+
return false, false
366+
}
367+
343368
func (b *NodeBuilderImpl) tryReuseExistingTypeNodeHelper(existing *ast.TypeNode) *ast.TypeNode {
344369
enclosingDeclaration := b.ctx.enclosingDeclaration
345370
isValid := true
@@ -351,33 +376,7 @@ func (b *NodeBuilderImpl) tryReuseExistingTypeNodeHelper(existing *ast.TypeNode)
351376
}
352377

353378
if ast.IsEntityName(node) || ast.IsEntityNameExpression(node) {
354-
parent := node.Parent
355-
shouldCheck := false
356-
dontVisitChildren := false
357-
if parent != nil {
358-
switch parent.Kind {
359-
case ast.KindTypeReference:
360-
if parent.AsTypeReference().TypeName == node {
361-
shouldCheck = true
362-
dontVisitChildren = true
363-
}
364-
case ast.KindExpressionWithTypeArguments:
365-
if parent.AsExpressionWithTypeArguments().Expression == node {
366-
shouldCheck = true
367-
dontVisitChildren = true
368-
}
369-
case ast.KindTypeQuery:
370-
if parent.AsTypeQueryNode().ExprName == node {
371-
shouldCheck = true
372-
dontVisitChildren = true
373-
}
374-
case ast.KindImportType:
375-
if parent.AsImportTypeNode().Qualifier == node {
376-
shouldCheck = true
377-
dontVisitChildren = true
378-
}
379-
}
380-
}
379+
shouldCheck, dontVisitChildren := isEntityNameInTypePosition(node)
381380

382381
if shouldCheck {
383382
if !b.ctx.tracker.IsEntityNameVisible(node, enclosingDeclaration) {
@@ -401,33 +400,7 @@ func (b *NodeBuilderImpl) tryReuseExistingTypeNodeHelper(existing *ast.TypeNode)
401400
var track func(node *ast.Node) bool
402401
track = func(node *ast.Node) bool {
403402
if ast.IsEntityName(node) || ast.IsEntityNameExpression(node) {
404-
parent := node.Parent
405-
shouldTrack := false
406-
dontVisitChildren := false
407-
if parent != nil {
408-
switch parent.Kind {
409-
case ast.KindTypeReference:
410-
if parent.AsTypeReference().TypeName == node {
411-
shouldTrack = true
412-
dontVisitChildren = true
413-
}
414-
case ast.KindExpressionWithTypeArguments:
415-
if parent.AsExpressionWithTypeArguments().Expression == node {
416-
shouldTrack = true
417-
dontVisitChildren = true
418-
}
419-
case ast.KindTypeQuery:
420-
if parent.AsTypeQueryNode().ExprName == node {
421-
shouldTrack = true
422-
dontVisitChildren = true
423-
}
424-
case ast.KindImportType:
425-
if parent.AsImportTypeNode().Qualifier == node {
426-
shouldTrack = true
427-
dontVisitChildren = true
428-
}
429-
}
430-
}
403+
shouldTrack, dontVisitChildren := isEntityNameInTypePosition(node)
431404

432405
if shouldTrack {
433406
b.ctx.tracker.TrackEntityName(node, enclosingDeclaration)

0 commit comments

Comments
 (0)