Skip to content

Commit 34d6b82

Browse files
Copilotjakebailey
andcommitted
Extract erasableSyntaxOnly check into helper method
Created shouldCheckErasableSyntax helper method to avoid repeating the pattern `c.compilerOptions.ErasableSyntaxOnly.IsTrue() && !ast.IsInJSFile(node)`. This improves code maintainability and readability. Co-authored-by: jakebailey <[email protected]>
1 parent 8873bcb commit 34d6b82

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

internal/checker/checker.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2475,6 +2475,10 @@ func (c *Checker) checkTypeParameterDeferred(node *ast.Node) {
24752475
}
24762476
}
24772477

2478+
func (c *Checker) shouldCheckErasableSyntax(node *ast.Node) bool {
2479+
return c.compilerOptions.ErasableSyntaxOnly.IsTrue() && !ast.IsInJSFile(node)
2480+
}
2481+
24782482
func (c *Checker) checkParameter(node *ast.Node) {
24792483
// Grammar checking
24802484
// It is a SyntaxError if the Identifier "eval" or the Identifier "arguments" occurs as the
@@ -2488,7 +2492,7 @@ func (c *Checker) checkParameter(node *ast.Node) {
24882492
paramName = node.Name().Text()
24892493
}
24902494
if ast.HasSyntacticModifier(node, ast.ModifierFlagsParameterPropertyModifier) {
2491-
if c.compilerOptions.ErasableSyntaxOnly.IsTrue() && !ast.IsInJSFile(node) {
2495+
if c.shouldCheckErasableSyntax(node) {
24922496
c.error(node, diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled)
24932497
}
24942498
if !(ast.IsConstructorDeclaration(fn) && ast.NodeIsPresent(fn.Body())) {
@@ -4843,7 +4847,7 @@ func (c *Checker) checkEnumDeclaration(node *ast.Node) {
48434847
c.checkExportsOnMergedDeclarations(node)
48444848
c.checkSourceElements(node.Members())
48454849

4846-
if c.compilerOptions.ErasableSyntaxOnly.IsTrue() && !ast.IsInJSFile(node) && node.Flags&ast.NodeFlagsAmbient == 0 {
4850+
if c.shouldCheckErasableSyntax(node) && node.Flags&ast.NodeFlagsAmbient == 0 {
48474851
c.error(node, diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled)
48484852
}
48494853

@@ -4933,7 +4937,7 @@ func (c *Checker) checkModuleDeclaration(node *ast.Node) {
49334937
symbol := c.getSymbolOfDeclaration(node)
49344938
// The following checks only apply on a non-ambient instantiated module declaration.
49354939
if symbol.Flags&ast.SymbolFlagsValueModule != 0 && !inAmbientContext && isInstantiatedModule(node, c.compilerOptions.ShouldPreserveConstEnums()) {
4936-
if c.compilerOptions.ErasableSyntaxOnly.IsTrue() && !ast.IsInJSFile(node) {
4940+
if c.shouldCheckErasableSyntax(node) {
49374941
c.error(node, diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled)
49384942
}
49394943
if c.compilerOptions.GetIsolatedModules() && ast.GetSourceFileOfNode(node).ExternalModuleIndicator == nil {
@@ -5232,7 +5236,7 @@ func (c *Checker) checkImportEqualsDeclaration(node *ast.Node) {
52325236
return // If we hit an import declaration in an illegal context, just bail out to avoid cascading errors.
52335237
}
52345238
c.checkGrammarModifiers(node)
5235-
if c.compilerOptions.ErasableSyntaxOnly.IsTrue() && !ast.IsInJSFile(node) && node.Flags&ast.NodeFlagsAmbient == 0 {
5239+
if c.shouldCheckErasableSyntax(node) && node.Flags&ast.NodeFlagsAmbient == 0 {
52365240
c.error(node, diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled)
52375241
}
52385242
if ast.IsInternalModuleImportEqualsDeclaration(node) || c.checkExternalImportOrExportDeclaration(node) {
@@ -5334,7 +5338,7 @@ func (c *Checker) checkExportAssignment(node *ast.Node) {
53345338
if c.checkGrammarModuleElementContext(node, illegalContextMessage) {
53355339
return // If we hit an export assignment in an illegal context, just bail out to avoid cascading errors.
53365340
}
5337-
if c.compilerOptions.ErasableSyntaxOnly.IsTrue() && !ast.IsInJSFile(node) && node.AsExportAssignment().IsExportEquals && node.Flags&ast.NodeFlagsAmbient == 0 {
5341+
if c.shouldCheckErasableSyntax(node) && node.AsExportAssignment().IsExportEquals && node.Flags&ast.NodeFlagsAmbient == 0 {
53385342
c.error(node, diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled)
53395343
}
53405344
container := node.Parent
@@ -11832,7 +11836,7 @@ func (c *Checker) classDeclarationExtendsNull(classDecl *ast.Node) bool {
1183211836

1183311837
func (c *Checker) checkAssertion(node *ast.Node, checkMode CheckMode) *Type {
1183411838
if node.Kind == ast.KindTypeAssertionExpression {
11835-
if c.compilerOptions.ErasableSyntaxOnly.IsTrue() && !ast.IsInJSFile(node) {
11839+
if c.shouldCheckErasableSyntax(node) {
1183611840
c.diagnostics.Add(ast.NewDiagnostic(ast.GetSourceFileOfNode(node), core.NewTextRange(scanner.SkipTrivia(ast.GetSourceFileOfNode(node).Text(), node.Pos()), node.Expression().Pos()), diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled))
1183711841
}
1183811842
}

0 commit comments

Comments
 (0)