Skip to content

Commit 4a5c3e2

Browse files
authored
clean up/fix incorrect switch statements (#1667)
1 parent 98d2daf commit 4a5c3e2

File tree

8 files changed

+40
-56
lines changed

8 files changed

+40
-56
lines changed

internal/ast/utilities.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,21 +1764,15 @@ func GetThisContainer(node *Node, includeArrowFunctions bool, includeClassComput
17641764
}
17651765

17661766
func GetSuperContainer(node *Node, stopOnFunctions bool) *Node {
1767-
for {
1768-
node = node.Parent
1769-
if node == nil {
1770-
return nil
1771-
}
1767+
for node = node.Parent; node != nil; node = node.Parent {
17721768
switch node.Kind {
17731769
case KindComputedPropertyName:
17741770
node = node.Parent
1775-
break
17761771
case KindFunctionDeclaration, KindFunctionExpression, KindArrowFunction:
17771772
if !stopOnFunctions {
17781773
continue
17791774
}
1780-
// falls through
1781-
1775+
return node
17821776
case KindPropertyDeclaration, KindPropertySignature, KindMethodDeclaration, KindMethodSignature, KindConstructor, KindGetAccessor, KindSetAccessor, KindClassStaticBlockDeclaration:
17831777
return node
17841778
case KindDecorator:
@@ -1792,9 +1786,9 @@ func GetSuperContainer(node *Node, stopOnFunctions bool) *Node {
17921786
// from the parent class declaration.
17931787
node = node.Parent
17941788
}
1795-
break
17961789
}
17971790
}
1791+
return nil
17981792
}
17991793

18001794
func GetImmediatelyInvokedFunctionExpression(fn *Node) *Node {

internal/checker/checker.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14476,13 +14476,11 @@ func (c *Checker) resolveExternalModule(location *ast.Node, moduleReference stri
1447614476
} else if ast.IsVariableDeclaration(location) && location.AsVariableDeclaration().Initializer != nil && ast.IsRequireCall(location.AsVariableDeclaration().Initializer, true /*requireStringLiteralLikeArgument*/) {
1447714477
contextSpecifier = location.AsVariableDeclaration().Initializer.AsCallExpression().Arguments.Nodes[0]
1447814478
} else {
14479-
var ancestor *ast.Node
14480-
if ancestor == nil {
14481-
ancestor = ast.FindAncestor(location, ast.IsImportCall)
14482-
if ancestor != nil {
14483-
contextSpecifier = ancestor.AsCallExpression().Arguments.Nodes[0]
14484-
}
14479+
ancestor := ast.FindAncestor(location, ast.IsImportCall)
14480+
if ancestor != nil {
14481+
contextSpecifier = ancestor.AsCallExpression().Arguments.Nodes[0]
1448514482
}
14483+
1448614484
if ancestor == nil {
1448714485
ancestor = ast.FindAncestor(location, ast.IsImportDeclarationOrJSImportDeclaration)
1448814486
if ancestor != nil {

internal/checker/emitresolver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ func (r *emitResolver) requiresAddingImplicitUndefined(declaration *ast.Node, sy
519519
}
520520
t := r.checker.getTypeOfSymbol(symbol)
521521
r.checker.mappedSymbolLinks.Has(symbol)
522-
return !!((symbol.Flags&ast.SymbolFlagsProperty != 0) && (symbol.Flags&ast.SymbolFlagsOptional != 0) && isOptionalDeclaration(declaration) && r.checker.ReverseMappedSymbolLinks.Has(symbol) && r.checker.ReverseMappedSymbolLinks.Get(symbol).mappedType != nil && containsNonMissingUndefinedType(r.checker, t))
522+
return (symbol.Flags&ast.SymbolFlagsProperty != 0) && (symbol.Flags&ast.SymbolFlagsOptional != 0) && isOptionalDeclaration(declaration) && r.checker.ReverseMappedSymbolLinks.Has(symbol) && r.checker.ReverseMappedSymbolLinks.Get(symbol).mappedType != nil && containsNonMissingUndefinedType(r.checker, t)
523523
case ast.KindParameter, ast.KindJSDocParameterTag:
524524
return r.requiresAddingImplicitUndefinedWorker(declaration, enclosingDeclaration)
525525
default:

internal/checker/jsx.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -517,14 +517,13 @@ func (c *Checker) getJSXFragmentType(node *ast.Node) *Type {
517517
if jsxFactorySymbol.Flags&ast.SymbolFlagsAlias != 0 {
518518
resolvedAlias = c.resolveAlias(jsxFactorySymbol)
519519
}
520-
if jsxFactorySymbol != nil {
521-
reactExports := c.getExportsOfSymbol(resolvedAlias)
522-
typeSymbol := c.getSymbol(reactExports, ReactNames.Fragment, ast.SymbolFlagsBlockScopedVariable)
523-
if typeSymbol != nil {
524-
links.jsxFragmentType = c.getTypeOfSymbol(typeSymbol)
525-
} else {
526-
links.jsxFragmentType = c.errorType
527-
}
520+
521+
reactExports := c.getExportsOfSymbol(resolvedAlias)
522+
typeSymbol := c.getSymbol(reactExports, ReactNames.Fragment, ast.SymbolFlagsBlockScopedVariable)
523+
if typeSymbol != nil {
524+
links.jsxFragmentType = c.getTypeOfSymbol(typeSymbol)
525+
} else {
526+
links.jsxFragmentType = c.errorType
528527
}
529528
return links.jsxFragmentType
530529
}

internal/checker/nodebuilderimpl.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ func (b *nodeBuilderImpl) getSymbolChain(symbol *ast.Symbol, meaning ast.SymbolF
10241024
// If a parent symbol is an anonymous type, don't write it.
10251025
(symbol.Flags&(ast.SymbolFlagsTypeLiteral|ast.SymbolFlagsObjectLiteral) == 0) {
10261026
// If a parent symbol is an external module, don't write it. (We prefer just `x` vs `"foo/bar".x`.)
1027-
if !endOfChain && !yieldModuleSymbol && !!core.Some(symbol.Declarations, hasNonGlobalAugmentationExternalModuleSymbol) {
1027+
if !endOfChain && !yieldModuleSymbol && core.Some(symbol.Declarations, hasNonGlobalAugmentationExternalModuleSymbol) {
10281028
return nil
10291029
}
10301030
return []*ast.Symbol{symbol}
@@ -1740,7 +1740,7 @@ func (b *nodeBuilderImpl) signatureToSignatureDeclarationHelper(signature *Signa
17401740
typeParamList = b.f.NewNodeList(typeParameters)
17411741
}
17421742
var modifierList *ast.ModifierList
1743-
if modifiers != nil && len(modifiers) > 0 {
1743+
if len(modifiers) > 0 {
17441744
modifierList = b.f.NewModifierList(modifiers)
17451745
}
17461746
var name *ast.Node

internal/ls/completions.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,9 +1376,10 @@ func (l *LanguageService) getCompletionData(
13761376
}
13771377

13781378
var importAttributes *ast.ImportAttributesNode
1379-
if contextToken.Kind == ast.KindOpenBraceToken || contextToken.Kind == ast.KindCommaToken {
1379+
switch contextToken.Kind {
1380+
case ast.KindOpenBraceToken, ast.KindCommaToken:
13801381
importAttributes = core.IfElse(ast.IsImportAttributes(contextToken.Parent), contextToken.Parent, nil)
1381-
} else if contextToken.Kind == ast.KindColonToken {
1382+
case ast.KindColonToken:
13821383
importAttributes = core.IfElse(ast.IsImportAttributes(contextToken.Parent.Parent), contextToken.Parent.Parent, nil)
13831384
}
13841385

internal/ls/findallreferences.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,12 @@ func getContextNodeForNodeEntry(node *ast.Node) *ast.Node {
171171
}
172172

173173
// Jsx Tags
174-
if node.Parent.Kind == ast.KindJsxOpeningElement || node.Parent.Kind == ast.KindJsxClosingElement {
174+
switch node.Parent.Kind {
175+
case ast.KindJsxOpeningElement, ast.KindJsxClosingElement:
175176
return node.Parent.Parent
176-
} else if node.Parent.Kind == ast.KindJsxSelfClosingElement ||
177-
node.Parent.Kind == ast.KindLabeledStatement ||
178-
node.Parent.Kind == ast.KindBreakStatement ||
179-
node.Parent.Kind == ast.KindContinueStatement {
177+
case ast.KindJsxSelfClosingElement, ast.KindLabeledStatement, ast.KindBreakStatement, ast.KindContinueStatement:
180178
return node.Parent
181-
} else if node.Parent.Kind == ast.KindStringLiteral || node.Parent.Kind == ast.KindNoSubstitutionTemplateLiteral {
179+
case ast.KindStringLiteral, ast.KindNoSubstitutionTemplateLiteral:
182180
if validImport := tryGetImportFromModuleSpecifier(node); validImport != nil {
183181
declOrStatement := ast.FindAncestor(validImport, func(*ast.Node) bool {
184182
return ast.IsDeclaration(node) || ast.IsStatement(node) || ast.IsJSDocTag(node)
@@ -865,7 +863,6 @@ func getReferencesForSuperKeyword(superKeyword *ast.Node) []*SymbolAndEntries {
865863
case ast.KindPropertyDeclaration, ast.KindPropertySignature, ast.KindMethodDeclaration, ast.KindMethodSignature, ast.KindConstructor, ast.KindGetAccessor, ast.KindSetAccessor:
866864
staticFlag &= searchSpaceNode.ModifierFlags()
867865
searchSpaceNode = searchSpaceNode.Parent // re-assign to be the owning class
868-
break
869866
default:
870867
return nil
871868
}

internal/ls/utilities.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -287,55 +287,45 @@ func getPossibleTypeArgumentsInfo(tokenIn *ast.Node, sourceFile *ast.SourceFile)
287287
}
288288
}
289289
remainingLessThanTokens--
290-
break
291290
case ast.KindGreaterThanGreaterThanGreaterThanToken:
292291
remainingLessThanTokens = +3
293-
break
294292
case ast.KindGreaterThanGreaterThanToken:
295293
remainingLessThanTokens = +2
296-
break
297294
case ast.KindGreaterThanToken:
298295
remainingLessThanTokens++
299-
break
300296
case ast.KindCloseBraceToken:
301297
// This can be object type, skip until we find the matching open brace token
302298
// Skip until the matching open brace token
303299
token = findPrecedingMatchingToken(token, ast.KindOpenBraceToken, sourceFile)
304300
if token == nil {
305301
return nil
306302
}
307-
break
308303
case ast.KindCloseParenToken:
309304
// This can be object type, skip until we find the matching open brace token
310305
// Skip until the matching open brace token
311306
token = findPrecedingMatchingToken(token, ast.KindOpenParenToken, sourceFile)
312307
if token == nil {
313308
return nil
314309
}
315-
break
316310
case ast.KindCloseBracketToken:
317311
// This can be object type, skip until we find the matching open brace token
318312
// Skip until the matching open brace token
319313
token = findPrecedingMatchingToken(token, ast.KindOpenBracketToken, sourceFile)
320314
if token == nil {
321315
return nil
322316
}
323-
break
324-
325-
// Valid tokens in a type name. Skip.
326317
case ast.KindCommaToken:
318+
// Valid tokens in a type name. Skip.
327319
nTypeArguments++
328-
break
329320
case ast.KindEqualsGreaterThanToken, ast.KindIdentifier, ast.KindStringLiteral, ast.KindNumericLiteral,
330321
ast.KindBigIntLiteral, ast.KindTrueKeyword, ast.KindFalseKeyword, ast.KindTypeOfKeyword, ast.KindExtendsKeyword,
331322
ast.KindKeyOfKeyword, ast.KindDotToken, ast.KindBarToken, ast.KindQuestionToken, ast.KindColonToken:
332-
break
323+
// do nothing
333324
default:
334-
if ast.IsTypeNode(token) {
335-
break
325+
if !ast.IsTypeNode(token) {
326+
// Invalid token in type
327+
return nil
336328
}
337-
// Invalid token in type
338-
return nil
339329
}
340330
token = astnav.FindPrecedingToken(sourceFile, token.Pos())
341331
}
@@ -1215,15 +1205,18 @@ func getAdjustedLocationForImportDeclaration(node *ast.ImportDeclaration, forRen
12151205
// import /**/type { propertyName as [|name|] } from ...;
12161206
// import /**/type * as [|name|] from ...;
12171207
if namedBindings := node.ImportClause.AsImportClause().NamedBindings; namedBindings != nil {
1218-
if namedBindings.Kind == ast.KindNamedImports {
1208+
switch namedBindings.Kind {
1209+
case ast.KindNamedImports:
12191210
// do nothing if there is more than one binding
12201211
elements := namedBindings.AsNamedImports().Elements
12211212
if len(elements.Nodes) != 1 {
12221213
return nil
12231214
}
12241215
return elements.Nodes[0].Name()
1225-
} else if namedBindings.Kind == ast.KindNamespaceImport {
1216+
1217+
case ast.KindNamespaceImport:
12261218
return namedBindings.Name()
1219+
12271220
}
12281221
}
12291222
}
@@ -1244,14 +1237,15 @@ func getAdjustedLocationForExportDeclaration(node *ast.ExportDeclaration, forRen
12441237
// export /**/type { [|name|] } from ...
12451238
// export /**/type { propertyName as [|name|] } from ...
12461239
// export /**/type * as [|name|] ...
1247-
if node.ExportClause.Kind == ast.KindNamedExports {
1240+
switch node.ExportClause.Kind {
1241+
case ast.KindNamedExports:
12481242
// do nothing if there is more than one binding
12491243
elements := node.ExportClause.AsNamedExports().Elements
12501244
if len(elements.Nodes) != 1 {
12511245
return nil
12521246
}
12531247
return elements.Nodes[0].Name()
1254-
} else if node.ExportClause.Kind == ast.KindNamespaceExport {
1248+
case ast.KindNamespaceExport:
12551249
return node.ExportClause.Name()
12561250
}
12571251
}
@@ -1641,12 +1635,13 @@ func findPrecedingMatchingToken(token *ast.Node, matchingTokenKind ast.Kind, sou
16411635
return nil
16421636
}
16431637
token = preceding
1644-
if token.Kind == matchingTokenKind {
1638+
switch token.Kind {
1639+
case matchingTokenKind:
16451640
if remainingMatchingTokens == 0 {
16461641
return token
16471642
}
16481643
remainingMatchingTokens--
1649-
} else if token.Kind == tokenKind {
1644+
case tokenKind:
16501645
remainingMatchingTokens++
16511646
}
16521647
}

0 commit comments

Comments
 (0)