Skip to content

Commit 5759413

Browse files
committed
feat: also move the nil checks to callers for IsFunctionLike
1 parent bec9f9b commit 5759413

File tree

5 files changed

+13
-13
lines changed

5 files changed

+13
-13
lines changed

internal/ast/utilities.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,9 @@ func isFunctionLikeKind(kind Kind) bool {
514514
}
515515

516516
// Determines if a node is function- or signature-like.
517+
// Ensure node != nil before calling this
517518
func IsFunctionLike(node *Node) bool {
518-
// TODO(rbuckton): Move `node != nil` test to call sites
519-
return node != nil && isFunctionLikeKind(node.Kind)
519+
return isFunctionLikeKind(node.Kind)
520520
}
521521

522522
func IsFunctionLikeOrClassStaticBlockDeclaration(node *Node) bool {
@@ -1064,7 +1064,7 @@ func CanHaveDecorators(node *Node) bool {
10641064
}
10651065

10661066
func IsFunctionOrModuleBlock(node *Node) bool {
1067-
return IsSourceFile(node) || IsModuleBlock(node) || IsBlock(node) && IsFunctionLike(node.Parent)
1067+
return IsSourceFile(node) || IsModuleBlock(node) || IsBlock(node) && node.Parent != nil && IsFunctionLike(node.Parent)
10681068
}
10691069

10701070
func IsFunctionExpressionOrArrowFunction(node *Node) bool {
@@ -2369,12 +2369,12 @@ func GetImpliedNodeFormatForEmitWorker(sourceFile *SourceFile, options *core.Com
23692369
}
23702370
if sourceFile.ImpliedNodeFormat == core.ModuleKindCommonJS &&
23712371
( /*sourceFile.packageJsonScope.contents.packageJsonContent.type == "commonjs" ||*/ // !!!
2372-
tspath.FileExtensionIsOneOf(sourceFile.FileName(), []string{tspath.ExtensionCjs, tspath.ExtensionCts})) {
2372+
tspath.FileExtensionIsOneOf(sourceFile.FileName(), []string{tspath.ExtensionCjs, tspath.ExtensionCts})) {
23732373
return core.ModuleKindCommonJS
23742374
}
23752375
if sourceFile.ImpliedNodeFormat == core.ModuleKindESNext &&
23762376
( /*sourceFile.packageJsonScope?.contents.packageJsonContent.type === "module" ||*/ // !!!
2377-
tspath.FileExtensionIsOneOf(sourceFile.fileName, []string{tspath.ExtensionMjs, tspath.ExtensionMts})) {
2377+
tspath.FileExtensionIsOneOf(sourceFile.fileName, []string{tspath.ExtensionMjs, tspath.ExtensionMts})) {
23782378
return core.ModuleKindESNext
23792379
}
23802380
return core.ModuleKindNone

internal/binder/binder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2513,7 +2513,7 @@ func GetContainerFlags(node *ast.Node) ContainerFlags {
25132513
case ast.KindCatchClause, ast.KindForStatement, ast.KindForInStatement, ast.KindForOfStatement, ast.KindCaseBlock:
25142514
return ContainerFlagsIsBlockScopedContainer | ContainerFlagsHasLocals
25152515
case ast.KindBlock:
2516-
if ast.IsFunctionLike(node.Parent) || ast.IsClassStaticBlockDeclaration(node.Parent) {
2516+
if node.Parent != nil && (ast.IsFunctionLike(node.Parent) || ast.IsClassStaticBlockDeclaration(node.Parent)) {
25172517
return ContainerFlagsNone
25182518
} else {
25192519
return ContainerFlagsIsBlockScopedContainer | ContainerFlagsHasLocals

internal/checker/checker.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5534,7 +5534,7 @@ func (c *Checker) checkVarDeclaredNamesNotShadowed(node *ast.Node) {
55345534
}
55355535
// names of block-scoped and function scoped variables can collide only
55365536
// if block scoped variable is defined in the function\module\source file scope (because of variable hoisting)
5537-
namesShareScope := container != nil && (ast.IsBlock(container) && ast.IsFunctionLike(container.Parent) ||
5537+
namesShareScope := container != nil && (ast.IsBlock(container) && container.Parent != nil && ast.IsFunctionLike(container.Parent) ||
55385538
ast.IsModuleBlock(container) || ast.IsModuleDeclaration(container) || ast.IsSourceFile(container))
55395539
// here we know that function scoped variable is "shadowed" by block scoped one
55405540
// a var declaration can't hoist past a lexical declaration and it results in a SyntaxError at runtime
@@ -10862,7 +10862,7 @@ func (c *Checker) isUncalledFunctionReference(node *ast.Node, symbol *ast.Symbol
1086210862
return isCallOrNewExpression(parent) && ast.IsIdentifier(node) && c.hasMatchingArgument(parent, node)
1086310863
}
1086410864
return core.Every(symbol.Declarations, func(d *ast.Node) bool {
10865-
return !ast.IsFunctionLike(d) || c.isDeprecatedDeclaration(d)
10865+
return d == nil || !ast.IsFunctionLike(d) || c.isDeprecatedDeclaration(d)
1086610866
})
1086710867
}
1086810868
return true
@@ -11305,7 +11305,7 @@ func (c *Checker) tryGetThisTypeAt(node *ast.Node) *Type {
1130511305
}
1130611306

1130711307
func (c *Checker) tryGetThisTypeAtEx(node *ast.Node, includeGlobalThis bool, container *ast.Node) *Type {
11308-
if ast.IsFunctionLike(container) && (!c.isInParameterInitializerBeforeContainingFunction(node) || getThisParameter(container) != nil) {
11308+
if container != nil && ast.IsFunctionLike(container) && (!c.isInParameterInitializerBeforeContainingFunction(node) || getThisParameter(container) != nil) {
1130911309
thisType := c.getThisTypeOfDeclaration(container)
1131011310
// Note: a parameter initializer should refer to class-this unless function-this is explicitly annotated.
1131111311
// If this is a function in a JS file, it might be a class method.
@@ -17983,7 +17983,7 @@ func (c *Checker) getSignaturesOfSymbol(symbol *ast.Symbol) []*Signature {
1798317983
}
1798417984
var result []*Signature
1798517985
for i, decl := range symbol.Declarations {
17986-
if !ast.IsFunctionLike(decl) {
17986+
if decl == nil || !ast.IsFunctionLike(decl) {
1798717987
continue
1798817988
}
1798917989
// Don't include signature if node is the implementation of an overloaded function. A node is considered
@@ -28988,7 +28988,7 @@ func (c *Checker) getSymbolAtLocation(node *ast.Node, ignoreErrors bool) *ast.Sy
2898828988
fallthrough
2898928989
case ast.KindThisKeyword:
2899028990
container := c.getThisContainer(node, false /*includeArrowFunctions*/, false /*includeClassComputedPropertyName*/)
28991-
if ast.IsFunctionLike(container) {
28991+
if container != nil && ast.IsFunctionLike(container) {
2899228992
sig := c.getSignatureFromDeclaration(container)
2899328993
if sig.thisParameter != nil {
2899428994
return sig.thisParameter

internal/checker/flow.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2166,7 +2166,7 @@ func (c *Checker) hasTypePredicateOrNeverReturnType(sig *Signature) bool {
21662166

21672167
func (c *Checker) getExplicitThisType(node *ast.Node) *Type {
21682168
container := ast.GetThisContainer(node, false /*includeArrowFunctions*/, false /*includeClassComputedPropertyName*/)
2169-
if ast.IsFunctionLike(container) {
2169+
if container != nil && ast.IsFunctionLike(container) {
21702170
signature := c.getSignatureFromDeclaration(container)
21712171
if signature.thisParameter != nil {
21722172
return c.getExplicitTypeOfSymbol(signature.thisParameter, nil)

internal/checker/grammarchecks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2061,7 +2061,7 @@ func (c *Checker) checkGrammarStatementInAmbientContext(node *ast.Node) bool {
20612061
if node.Flags&ast.NodeFlagsAmbient != 0 {
20622062
// Find containing block which is either Block, ModuleBlock, SourceFile
20632063
links := c.nodeLinks.Get(node)
2064-
if !links.hasReportedStatementInAmbientContext && (ast.IsFunctionLike(node.Parent) || ast.IsAccessor(node.Parent)) {
2064+
if !links.hasReportedStatementInAmbientContext && (node.Parent != nil && ast.IsFunctionLike(node.Parent) || ast.IsAccessor(node.Parent)) {
20652065
links.hasReportedStatementInAmbientContext = c.grammarErrorOnFirstToken(node, diagnostics.An_implementation_cannot_be_declared_in_ambient_contexts)
20662066
return links.hasReportedStatementInAmbientContext
20672067
}

0 commit comments

Comments
 (0)