Skip to content

Commit 89c8a5b

Browse files
committed
feat: also move the nil checks to callers for IsFunctionLike
1 parent 65eca1e commit 89c8a5b

File tree

5 files changed

+12
-12
lines changed

5 files changed

+12
-12
lines changed

internal/ast/utilities.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -534,9 +534,9 @@ func IsFunctionLikeKind(kind Kind) bool {
534534
}
535535

536536
// Determines if a node is function- or signature-like.
537+
// Ensure node != nil before calling this
537538
func IsFunctionLike(node *Node) bool {
538-
// TODO(rbuckton): Move `node != nil` test to call sites
539-
return node != nil && IsFunctionLikeKind(node.Kind)
539+
return isFunctionLikeKind(node.Kind)
540540
}
541541

542542
func IsFunctionLikeOrClassStaticBlockDeclaration(node *Node) bool {
@@ -1149,7 +1149,7 @@ func CanHaveDecorators(node *Node) bool {
11491149
}
11501150

11511151
func IsFunctionOrModuleBlock(node *Node) bool {
1152-
return IsSourceFile(node) || IsModuleBlock(node) || IsBlock(node) && IsFunctionLike(node.Parent)
1152+
return IsSourceFile(node) || IsModuleBlock(node) || IsBlock(node) && node.Parent != nil && IsFunctionLike(node.Parent)
11531153
}
11541154

11551155
func IsFunctionExpressionOrArrowFunction(node *Node) bool {
@@ -2561,12 +2561,12 @@ func GetImpliedNodeFormatForEmitWorker(fileName string, emitModuleKind core.Modu
25612561
}
25622562
if sourceFileMetaData.ImpliedNodeFormat == core.ModuleKindCommonJS &&
25632563
(sourceFileMetaData.PackageJsonType == "commonjs" ||
2564-
tspath.FileExtensionIsOneOf(fileName, []string{tspath.ExtensionCjs, tspath.ExtensionCts})) {
2564+
tspath.FileExtensionIsOneOf(fileName, []string{tspath.ExtensionCjs, tspath.ExtensionCts})) {
25652565
return core.ModuleKindCommonJS
25662566
}
25672567
if sourceFileMetaData.ImpliedNodeFormat == core.ModuleKindESNext &&
25682568
(sourceFileMetaData.PackageJsonType == "module" ||
2569-
tspath.FileExtensionIsOneOf(fileName, []string{tspath.ExtensionMjs, tspath.ExtensionMts})) {
2569+
tspath.FileExtensionIsOneOf(fileName, []string{tspath.ExtensionMjs, tspath.ExtensionMts})) {
25702570
return core.ModuleKindESNext
25712571
}
25722572
return core.ModuleKindNone

internal/binder/binder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2599,7 +2599,7 @@ func GetContainerFlags(node *ast.Node) ContainerFlags {
25992599
case ast.KindCatchClause, ast.KindForStatement, ast.KindForInStatement, ast.KindForOfStatement, ast.KindCaseBlock:
26002600
return ContainerFlagsIsBlockScopedContainer | ContainerFlagsHasLocals
26012601
case ast.KindBlock:
2602-
if ast.IsFunctionLike(node.Parent) || ast.IsClassStaticBlockDeclaration(node.Parent) {
2602+
if node.Parent != nil && (ast.IsFunctionLike(node.Parent) || ast.IsClassStaticBlockDeclaration(node.Parent)) {
26032603
return ContainerFlagsNone
26042604
} else {
26052605
return ContainerFlagsIsBlockScopedContainer | ContainerFlagsHasLocals

internal/checker/checker.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5706,7 +5706,7 @@ func (c *Checker) checkVarDeclaredNamesNotShadowed(node *ast.Node) {
57065706
}
57075707
// names of block-scoped and function scoped variables can collide only
57085708
// if block scoped variable is defined in the function\module\source file scope (because of variable hoisting)
5709-
namesShareScope := container != nil && (ast.IsBlock(container) && ast.IsFunctionLike(container.Parent) ||
5709+
namesShareScope := container != nil && (ast.IsBlock(container) && container.Parent != nil && ast.IsFunctionLike(container.Parent) ||
57105710
ast.IsModuleBlock(container) || ast.IsModuleDeclaration(container) || ast.IsSourceFile(container))
57115711
// here we know that function scoped variable is "shadowed" by block scoped one
57125712
// a var declaration can't hoist past a lexical declaration and it results in a SyntaxError at runtime
@@ -11149,7 +11149,7 @@ func (c *Checker) isUncalledFunctionReference(node *ast.Node, symbol *ast.Symbol
1114911149
return ast.IsCallOrNewExpression(parent) && ast.IsIdentifier(node) && c.hasMatchingArgument(parent, node)
1115011150
}
1115111151
return core.Every(symbol.Declarations, func(d *ast.Node) bool {
11152-
return !ast.IsFunctionLike(d) || c.IsDeprecatedDeclaration(d)
11152+
return d == nil || !ast.IsFunctionLike(d) || c.IsDeprecatedDeclaration(d)
1115311153
})
1115411154
}
1115511155
return true
@@ -18874,7 +18874,7 @@ func (c *Checker) getSignaturesOfSymbol(symbol *ast.Symbol) []*Signature {
1887418874
}
1887518875
var result []*Signature
1887618876
for i, decl := range symbol.Declarations {
18877-
if !ast.IsFunctionLike(decl) {
18877+
if decl == nil || !ast.IsFunctionLike(decl) {
1887818878
continue
1887918879
}
1888018880
// Don't include signature if node is the implementation of an overloaded function. A node is considered
@@ -30094,7 +30094,7 @@ func (c *Checker) getSymbolAtLocation(node *ast.Node, ignoreErrors bool) *ast.Sy
3009430094
fallthrough
3009530095
case ast.KindThisKeyword:
3009630096
container := c.getThisContainer(node, false /*includeArrowFunctions*/, false /*includeClassComputedPropertyName*/)
30097-
if ast.IsFunctionLike(container) {
30097+
if container != nil && ast.IsFunctionLike(container) {
3009830098
sig := c.getSignatureFromDeclaration(container)
3009930099
if sig.thisParameter != nil {
3010030100
return sig.thisParameter

internal/checker/flow.go

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

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

internal/checker/grammarchecks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2069,7 +2069,7 @@ func (c *Checker) checkGrammarStatementInAmbientContext(node *ast.Node) bool {
20692069
if node.Flags&ast.NodeFlagsAmbient != 0 {
20702070
// Find containing block which is either Block, ModuleBlock, SourceFile
20712071
links := c.nodeLinks.Get(node)
2072-
if !links.hasReportedStatementInAmbientContext && (ast.IsFunctionLike(node.Parent) || ast.IsAccessor(node.Parent)) {
2072+
if !links.hasReportedStatementInAmbientContext && (node.Parent != nil && ast.IsFunctionLike(node.Parent) || ast.IsAccessor(node.Parent)) {
20732073
links.hasReportedStatementInAmbientContext = c.grammarErrorOnFirstToken(node, diagnostics.An_implementation_cannot_be_declared_in_ambient_contexts)
20742074
return links.hasReportedStatementInAmbientContext
20752075
}

0 commit comments

Comments
 (0)