Skip to content

Commit fef4d68

Browse files
Copilotjakebailey
andauthored
Skip erasableSyntaxOnly checks for JavaScript files (#1956)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: jakebailey <[email protected]>
1 parent 128ea7d commit fef4d68

File tree

5 files changed

+121
-6
lines changed

5 files changed

+121
-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() {
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())) {
@@ -4840,7 +4844,7 @@ func (c *Checker) checkEnumDeclaration(node *ast.Node) {
48404844
c.checkExportsOnMergedDeclarations(node)
48414845
c.checkSourceElements(node.Members())
48424846

4843-
if c.compilerOptions.ErasableSyntaxOnly.IsTrue() && node.Flags&ast.NodeFlagsAmbient == 0 {
4847+
if c.shouldCheckErasableSyntax(node) && node.Flags&ast.NodeFlagsAmbient == 0 {
48444848
c.error(node, diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled)
48454849
}
48464850

@@ -4930,7 +4934,7 @@ func (c *Checker) checkModuleDeclaration(node *ast.Node) {
49304934
symbol := c.getSymbolOfDeclaration(node)
49314935
// The following checks only apply on a non-ambient instantiated module declaration.
49324936
if symbol.Flags&ast.SymbolFlagsValueModule != 0 && !inAmbientContext && isInstantiatedModule(node, c.compilerOptions.ShouldPreserveConstEnums()) {
4933-
if c.compilerOptions.ErasableSyntaxOnly.IsTrue() {
4937+
if c.shouldCheckErasableSyntax(node) {
49344938
c.error(node, diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled)
49354939
}
49364940
if c.compilerOptions.GetIsolatedModules() && ast.GetSourceFileOfNode(node).ExternalModuleIndicator == nil {
@@ -5229,7 +5233,7 @@ func (c *Checker) checkImportEqualsDeclaration(node *ast.Node) {
52295233
return // If we hit an import declaration in an illegal context, just bail out to avoid cascading errors.
52305234
}
52315235
c.checkGrammarModifiers(node)
5232-
if c.compilerOptions.ErasableSyntaxOnly.IsTrue() && node.Flags&ast.NodeFlagsAmbient == 0 {
5236+
if c.shouldCheckErasableSyntax(node) && node.Flags&ast.NodeFlagsAmbient == 0 {
52335237
c.error(node, diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled)
52345238
}
52355239
if ast.IsInternalModuleImportEqualsDeclaration(node) || c.checkExternalImportOrExportDeclaration(node) {
@@ -5331,7 +5335,7 @@ func (c *Checker) checkExportAssignment(node *ast.Node) {
53315335
if c.checkGrammarModuleElementContext(node, illegalContextMessage) {
53325336
return // If we hit an export assignment in an illegal context, just bail out to avoid cascading errors.
53335337
}
5334-
if c.compilerOptions.ErasableSyntaxOnly.IsTrue() && node.AsExportAssignment().IsExportEquals && node.Flags&ast.NodeFlagsAmbient == 0 {
5338+
if c.shouldCheckErasableSyntax(node) && node.AsExportAssignment().IsExportEquals && node.Flags&ast.NodeFlagsAmbient == 0 {
53355339
c.error(node, diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled)
53365340
}
53375341
container := node.Parent
@@ -11810,7 +11814,7 @@ func (c *Checker) classDeclarationExtendsNull(classDecl *ast.Node) bool {
1181011814

1181111815
func (c *Checker) checkAssertion(node *ast.Node, checkMode CheckMode) *Type {
1181211816
if node.Kind == ast.KindTypeAssertionExpression {
11813-
if c.compilerOptions.ErasableSyntaxOnly.IsTrue() {
11817+
if c.shouldCheckErasableSyntax(node) {
1181411818
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))
1181511819
}
1181611820
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
index.ts(2,1): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
2+
index.ts(3,1): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
3+
4+
5+
==== index.ts (2 errors) ====
6+
// These should still error because they are in a TypeScript file
7+
import bar = require("./bar.cjs");
8+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9+
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
10+
import foo = require("./foo.js");
11+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12+
!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
13+
14+
==== bar.cjs (0 errors) ====
15+
module.exports = {
16+
a: 1,
17+
}
18+
19+
==== foo.js (0 errors) ====
20+
module.exports = {
21+
b: 2,
22+
}
23+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//// [tests/cases/compiler/erasableSyntaxOnlyJS.ts] ////
2+
3+
=== index.ts ===
4+
// These should still error because they are in a TypeScript file
5+
import bar = require("./bar.cjs");
6+
>bar : Symbol(bar, Decl(index.ts, 0, 0))
7+
8+
import foo = require("./foo.js");
9+
>foo : Symbol(foo, Decl(index.ts, 1, 34))
10+
11+
=== bar.cjs ===
12+
module.exports = {
13+
>module.exports : Symbol(export=, Decl(bar.cjs, 0, 0))
14+
>module : Symbol(module.exports)
15+
>exports : Symbol(export=, Decl(bar.cjs, 0, 0))
16+
17+
a: 1,
18+
>a : Symbol(a, Decl(bar.cjs, 0, 18))
19+
}
20+
21+
=== foo.js ===
22+
module.exports = {
23+
>module.exports : Symbol(export=, Decl(foo.js, 0, 0))
24+
>module : Symbol(module.exports)
25+
>exports : Symbol(export=, Decl(foo.js, 0, 0))
26+
27+
b: 2,
28+
>b : Symbol(b, Decl(foo.js, 0, 18))
29+
}
30+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//// [tests/cases/compiler/erasableSyntaxOnlyJS.ts] ////
2+
3+
=== index.ts ===
4+
// These should still error because they are in a TypeScript file
5+
import bar = require("./bar.cjs");
6+
>bar : { a: number; }
7+
8+
import foo = require("./foo.js");
9+
>foo : { b: number; }
10+
11+
=== bar.cjs ===
12+
module.exports = {
13+
>module.exports = { a: 1,} : { a: number; }
14+
>module.exports : { a: number; }
15+
>module : { "export=": { a: number; }; }
16+
>exports : { a: number; }
17+
>{ a: 1,} : { a: number; }
18+
19+
a: 1,
20+
>a : number
21+
>1 : 1
22+
}
23+
24+
=== foo.js ===
25+
module.exports = {
26+
>module.exports = { b: 2,} : { b: number; }
27+
>module.exports : { b: number; }
28+
>module : { "export=": { b: number; }; }
29+
>exports : { b: number; }
30+
>{ b: 2,} : { b: number; }
31+
32+
b: 2,
33+
>b : number
34+
>2 : 2
35+
}
36+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// @erasableSyntaxOnly: true
2+
// @allowJs: true
3+
// @checkJs: true
4+
// @noEmit: true
5+
6+
// JavaScript files should not have erasableSyntaxOnly checks
7+
// because they are already "erased" by definition.
8+
9+
// @Filename: bar.cjs
10+
module.exports = {
11+
a: 1,
12+
}
13+
14+
// @Filename: foo.js
15+
module.exports = {
16+
b: 2,
17+
}
18+
19+
// @Filename: index.ts
20+
// These should still error because they are in a TypeScript file
21+
import bar = require("./bar.cjs");
22+
import foo = require("./foo.js");

0 commit comments

Comments
 (0)