diff --git a/internal/ast/utilities.go b/internal/ast/utilities.go index 2cddf320ff..199b547521 100644 --- a/internal/ast/utilities.go +++ b/internal/ast/utilities.go @@ -1764,21 +1764,15 @@ func GetThisContainer(node *Node, includeArrowFunctions bool, includeClassComput } func GetSuperContainer(node *Node, stopOnFunctions bool) *Node { - for { - node = node.Parent - if node == nil { - return nil - } + for node = node.Parent; node != nil; node = node.Parent { switch node.Kind { case KindComputedPropertyName: node = node.Parent - break case KindFunctionDeclaration, KindFunctionExpression, KindArrowFunction: if !stopOnFunctions { continue } - // falls through - + return node case KindPropertyDeclaration, KindPropertySignature, KindMethodDeclaration, KindMethodSignature, KindConstructor, KindGetAccessor, KindSetAccessor, KindClassStaticBlockDeclaration: return node case KindDecorator: @@ -1792,9 +1786,9 @@ func GetSuperContainer(node *Node, stopOnFunctions bool) *Node { // from the parent class declaration. node = node.Parent } - break } } + return nil } func GetImmediatelyInvokedFunctionExpression(fn *Node) *Node { diff --git a/internal/checker/checker.go b/internal/checker/checker.go index 397ad9cbef..5db40b6f9f 100644 --- a/internal/checker/checker.go +++ b/internal/checker/checker.go @@ -14476,13 +14476,11 @@ func (c *Checker) resolveExternalModule(location *ast.Node, moduleReference stri } else if ast.IsVariableDeclaration(location) && location.AsVariableDeclaration().Initializer != nil && ast.IsRequireCall(location.AsVariableDeclaration().Initializer, true /*requireStringLiteralLikeArgument*/) { contextSpecifier = location.AsVariableDeclaration().Initializer.AsCallExpression().Arguments.Nodes[0] } else { - var ancestor *ast.Node - if ancestor == nil { - ancestor = ast.FindAncestor(location, ast.IsImportCall) - if ancestor != nil { - contextSpecifier = ancestor.AsCallExpression().Arguments.Nodes[0] - } + ancestor := ast.FindAncestor(location, ast.IsImportCall) + if ancestor != nil { + contextSpecifier = ancestor.AsCallExpression().Arguments.Nodes[0] } + if ancestor == nil { ancestor = ast.FindAncestor(location, ast.IsImportDeclarationOrJSImportDeclaration) if ancestor != nil { diff --git a/internal/checker/emitresolver.go b/internal/checker/emitresolver.go index 310334363d..ba40b72a08 100644 --- a/internal/checker/emitresolver.go +++ b/internal/checker/emitresolver.go @@ -519,7 +519,7 @@ func (r *emitResolver) requiresAddingImplicitUndefined(declaration *ast.Node, sy } t := r.checker.getTypeOfSymbol(symbol) r.checker.mappedSymbolLinks.Has(symbol) - 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)) + 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) case ast.KindParameter, ast.KindJSDocParameterTag: return r.requiresAddingImplicitUndefinedWorker(declaration, enclosingDeclaration) default: diff --git a/internal/checker/jsx.go b/internal/checker/jsx.go index 0a0cfa8f8d..7f0342d76b 100644 --- a/internal/checker/jsx.go +++ b/internal/checker/jsx.go @@ -517,14 +517,13 @@ func (c *Checker) getJSXFragmentType(node *ast.Node) *Type { if jsxFactorySymbol.Flags&ast.SymbolFlagsAlias != 0 { resolvedAlias = c.resolveAlias(jsxFactorySymbol) } - if jsxFactorySymbol != nil { - reactExports := c.getExportsOfSymbol(resolvedAlias) - typeSymbol := c.getSymbol(reactExports, ReactNames.Fragment, ast.SymbolFlagsBlockScopedVariable) - if typeSymbol != nil { - links.jsxFragmentType = c.getTypeOfSymbol(typeSymbol) - } else { - links.jsxFragmentType = c.errorType - } + + reactExports := c.getExportsOfSymbol(resolvedAlias) + typeSymbol := c.getSymbol(reactExports, ReactNames.Fragment, ast.SymbolFlagsBlockScopedVariable) + if typeSymbol != nil { + links.jsxFragmentType = c.getTypeOfSymbol(typeSymbol) + } else { + links.jsxFragmentType = c.errorType } return links.jsxFragmentType } diff --git a/internal/checker/nodebuilderimpl.go b/internal/checker/nodebuilderimpl.go index c9d84a294d..9f07545c05 100644 --- a/internal/checker/nodebuilderimpl.go +++ b/internal/checker/nodebuilderimpl.go @@ -1024,7 +1024,7 @@ func (b *nodeBuilderImpl) getSymbolChain(symbol *ast.Symbol, meaning ast.SymbolF // If a parent symbol is an anonymous type, don't write it. (symbol.Flags&(ast.SymbolFlagsTypeLiteral|ast.SymbolFlagsObjectLiteral) == 0) { // If a parent symbol is an external module, don't write it. (We prefer just `x` vs `"foo/bar".x`.) - if !endOfChain && !yieldModuleSymbol && !!core.Some(symbol.Declarations, hasNonGlobalAugmentationExternalModuleSymbol) { + if !endOfChain && !yieldModuleSymbol && core.Some(symbol.Declarations, hasNonGlobalAugmentationExternalModuleSymbol) { return nil } return []*ast.Symbol{symbol} @@ -1740,7 +1740,7 @@ func (b *nodeBuilderImpl) signatureToSignatureDeclarationHelper(signature *Signa typeParamList = b.f.NewNodeList(typeParameters) } var modifierList *ast.ModifierList - if modifiers != nil && len(modifiers) > 0 { + if len(modifiers) > 0 { modifierList = b.f.NewModifierList(modifiers) } var name *ast.Node diff --git a/internal/ls/completions.go b/internal/ls/completions.go index a7e249637d..0caaab5c01 100644 --- a/internal/ls/completions.go +++ b/internal/ls/completions.go @@ -1378,9 +1378,10 @@ func (l *LanguageService) getCompletionData( } var importAttributes *ast.ImportAttributesNode - if contextToken.Kind == ast.KindOpenBraceToken || contextToken.Kind == ast.KindCommaToken { + switch contextToken.Kind { + case ast.KindOpenBraceToken, ast.KindCommaToken: importAttributes = core.IfElse(ast.IsImportAttributes(contextToken.Parent), contextToken.Parent, nil) - } else if contextToken.Kind == ast.KindColonToken { + case ast.KindColonToken: importAttributes = core.IfElse(ast.IsImportAttributes(contextToken.Parent.Parent), contextToken.Parent.Parent, nil) } diff --git a/internal/ls/findallreferences.go b/internal/ls/findallreferences.go index 875be0442d..8f099d691f 100644 --- a/internal/ls/findallreferences.go +++ b/internal/ls/findallreferences.go @@ -171,14 +171,12 @@ func getContextNodeForNodeEntry(node *ast.Node) *ast.Node { } // Jsx Tags - if node.Parent.Kind == ast.KindJsxOpeningElement || node.Parent.Kind == ast.KindJsxClosingElement { + switch node.Parent.Kind { + case ast.KindJsxOpeningElement, ast.KindJsxClosingElement: return node.Parent.Parent - } else if node.Parent.Kind == ast.KindJsxSelfClosingElement || - node.Parent.Kind == ast.KindLabeledStatement || - node.Parent.Kind == ast.KindBreakStatement || - node.Parent.Kind == ast.KindContinueStatement { + case ast.KindJsxSelfClosingElement, ast.KindLabeledStatement, ast.KindBreakStatement, ast.KindContinueStatement: return node.Parent - } else if node.Parent.Kind == ast.KindStringLiteral || node.Parent.Kind == ast.KindNoSubstitutionTemplateLiteral { + case ast.KindStringLiteral, ast.KindNoSubstitutionTemplateLiteral: if validImport := tryGetImportFromModuleSpecifier(node); validImport != nil { declOrStatement := ast.FindAncestor(validImport, func(*ast.Node) bool { return ast.IsDeclaration(node) || ast.IsStatement(node) || ast.IsJSDocTag(node) @@ -865,7 +863,6 @@ func getReferencesForSuperKeyword(superKeyword *ast.Node) []*SymbolAndEntries { case ast.KindPropertyDeclaration, ast.KindPropertySignature, ast.KindMethodDeclaration, ast.KindMethodSignature, ast.KindConstructor, ast.KindGetAccessor, ast.KindSetAccessor: staticFlag &= searchSpaceNode.ModifierFlags() searchSpaceNode = searchSpaceNode.Parent // re-assign to be the owning class - break default: return nil } diff --git a/internal/ls/utilities.go b/internal/ls/utilities.go index b4f888fd12..566352b0bb 100644 --- a/internal/ls/utilities.go +++ b/internal/ls/utilities.go @@ -287,16 +287,12 @@ func getPossibleTypeArgumentsInfo(tokenIn *ast.Node, sourceFile *ast.SourceFile) } } remainingLessThanTokens-- - break case ast.KindGreaterThanGreaterThanGreaterThanToken: remainingLessThanTokens = +3 - break case ast.KindGreaterThanGreaterThanToken: remainingLessThanTokens = +2 - break case ast.KindGreaterThanToken: remainingLessThanTokens++ - break case ast.KindCloseBraceToken: // This can be object type, skip until we find the matching open brace token // Skip until the matching open brace token @@ -304,7 +300,6 @@ func getPossibleTypeArgumentsInfo(tokenIn *ast.Node, sourceFile *ast.SourceFile) if token == nil { return nil } - break case ast.KindCloseParenToken: // This can be object type, skip until we find the matching open brace token // Skip until the matching open brace token @@ -312,7 +307,6 @@ func getPossibleTypeArgumentsInfo(tokenIn *ast.Node, sourceFile *ast.SourceFile) if token == nil { return nil } - break case ast.KindCloseBracketToken: // This can be object type, skip until we find the matching open brace token // Skip until the matching open brace token @@ -320,22 +314,18 @@ func getPossibleTypeArgumentsInfo(tokenIn *ast.Node, sourceFile *ast.SourceFile) if token == nil { return nil } - break - - // Valid tokens in a type name. Skip. case ast.KindCommaToken: + // Valid tokens in a type name. Skip. nTypeArguments++ - break case ast.KindEqualsGreaterThanToken, ast.KindIdentifier, ast.KindStringLiteral, ast.KindNumericLiteral, ast.KindBigIntLiteral, ast.KindTrueKeyword, ast.KindFalseKeyword, ast.KindTypeOfKeyword, ast.KindExtendsKeyword, ast.KindKeyOfKeyword, ast.KindDotToken, ast.KindBarToken, ast.KindQuestionToken, ast.KindColonToken: - break + // do nothing default: - if ast.IsTypeNode(token) { - break + if !ast.IsTypeNode(token) { + // Invalid token in type + return nil } - // Invalid token in type - return nil } token = astnav.FindPrecedingToken(sourceFile, token.Pos()) } @@ -1215,15 +1205,18 @@ func getAdjustedLocationForImportDeclaration(node *ast.ImportDeclaration, forRen // import /**/type { propertyName as [|name|] } from ...; // import /**/type * as [|name|] from ...; if namedBindings := node.ImportClause.AsImportClause().NamedBindings; namedBindings != nil { - if namedBindings.Kind == ast.KindNamedImports { + switch namedBindings.Kind { + case ast.KindNamedImports: // do nothing if there is more than one binding elements := namedBindings.AsNamedImports().Elements if len(elements.Nodes) != 1 { return nil } return elements.Nodes[0].Name() - } else if namedBindings.Kind == ast.KindNamespaceImport { + + case ast.KindNamespaceImport: return namedBindings.Name() + } } } @@ -1244,14 +1237,15 @@ func getAdjustedLocationForExportDeclaration(node *ast.ExportDeclaration, forRen // export /**/type { [|name|] } from ... // export /**/type { propertyName as [|name|] } from ... // export /**/type * as [|name|] ... - if node.ExportClause.Kind == ast.KindNamedExports { + switch node.ExportClause.Kind { + case ast.KindNamedExports: // do nothing if there is more than one binding elements := node.ExportClause.AsNamedExports().Elements if len(elements.Nodes) != 1 { return nil } return elements.Nodes[0].Name() - } else if node.ExportClause.Kind == ast.KindNamespaceExport { + case ast.KindNamespaceExport: return node.ExportClause.Name() } } @@ -1641,12 +1635,13 @@ func findPrecedingMatchingToken(token *ast.Node, matchingTokenKind ast.Kind, sou return nil } token = preceding - if token.Kind == matchingTokenKind { + switch token.Kind { + case matchingTokenKind: if remainingMatchingTokens == 0 { return token } remainingMatchingTokens-- - } else if token.Kind == tokenKind { + case tokenKind: remainingMatchingTokens++ } }