Skip to content

Commit a53300a

Browse files
Copilotjakebailey
andcommitted
Skip erasableSyntaxOnly checks for JavaScript files
JavaScript files are already "erased" by definition, so they should not have erasableSyntaxOnly checks applied. This fix adds !ast.IsInJSFile(node) checks to all six places where erasableSyntaxOnly errors are reported: - Parameter properties - Enums - Namespace/module declarations - Import equals declarations - Export equals assignments - Type assertions Fixes the issue where module.exports = { ... } in .cjs/.js files was incorrectly triggering TS1294 error when erasableSyntaxOnly was enabled. Co-authored-by: jakebailey <[email protected]>
1 parent de0d812 commit a53300a

File tree

7 files changed

+196
-6
lines changed

7 files changed

+196
-6
lines changed

internal/checker/checker.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2488,7 +2488,7 @@ func (c *Checker) checkParameter(node *ast.Node) {
24882488
paramName = node.Name().Text()
24892489
}
24902490
if ast.HasSyntacticModifier(node, ast.ModifierFlagsParameterPropertyModifier) {
2491-
if c.compilerOptions.ErasableSyntaxOnly.IsTrue() {
2491+
if c.compilerOptions.ErasableSyntaxOnly.IsTrue() && !ast.IsInJSFile(node) {
24922492
c.error(node, diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled)
24932493
}
24942494
if !(ast.IsConstructorDeclaration(fn) && ast.NodeIsPresent(fn.Body())) {
@@ -4843,7 +4843,7 @@ func (c *Checker) checkEnumDeclaration(node *ast.Node) {
48434843
c.checkExportsOnMergedDeclarations(node)
48444844
c.checkSourceElements(node.Members())
48454845

4846-
if c.compilerOptions.ErasableSyntaxOnly.IsTrue() && node.Flags&ast.NodeFlagsAmbient == 0 {
4846+
if c.compilerOptions.ErasableSyntaxOnly.IsTrue() && !ast.IsInJSFile(node) && node.Flags&ast.NodeFlagsAmbient == 0 {
48474847
c.error(node, diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled)
48484848
}
48494849

@@ -4933,7 +4933,7 @@ func (c *Checker) checkModuleDeclaration(node *ast.Node) {
49334933
symbol := c.getSymbolOfDeclaration(node)
49344934
// The following checks only apply on a non-ambient instantiated module declaration.
49354935
if symbol.Flags&ast.SymbolFlagsValueModule != 0 && !inAmbientContext && isInstantiatedModule(node, c.compilerOptions.ShouldPreserveConstEnums()) {
4936-
if c.compilerOptions.ErasableSyntaxOnly.IsTrue() {
4936+
if c.compilerOptions.ErasableSyntaxOnly.IsTrue() && !ast.IsInJSFile(node) {
49374937
c.error(node, diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled)
49384938
}
49394939
if c.compilerOptions.GetIsolatedModules() && ast.GetSourceFileOfNode(node).ExternalModuleIndicator == nil {
@@ -5232,7 +5232,7 @@ func (c *Checker) checkImportEqualsDeclaration(node *ast.Node) {
52325232
return // If we hit an import declaration in an illegal context, just bail out to avoid cascading errors.
52335233
}
52345234
c.checkGrammarModifiers(node)
5235-
if c.compilerOptions.ErasableSyntaxOnly.IsTrue() && node.Flags&ast.NodeFlagsAmbient == 0 {
5235+
if c.compilerOptions.ErasableSyntaxOnly.IsTrue() && !ast.IsInJSFile(node) && node.Flags&ast.NodeFlagsAmbient == 0 {
52365236
c.error(node, diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled)
52375237
}
52385238
if ast.IsInternalModuleImportEqualsDeclaration(node) || c.checkExternalImportOrExportDeclaration(node) {
@@ -5334,7 +5334,7 @@ func (c *Checker) checkExportAssignment(node *ast.Node) {
53345334
if c.checkGrammarModuleElementContext(node, illegalContextMessage) {
53355335
return // If we hit an export assignment in an illegal context, just bail out to avoid cascading errors.
53365336
}
5337-
if c.compilerOptions.ErasableSyntaxOnly.IsTrue() && node.AsExportAssignment().IsExportEquals && node.Flags&ast.NodeFlagsAmbient == 0 {
5337+
if c.compilerOptions.ErasableSyntaxOnly.IsTrue() && !ast.IsInJSFile(node) && node.AsExportAssignment().IsExportEquals && node.Flags&ast.NodeFlagsAmbient == 0 {
53385338
c.error(node, diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled)
53395339
}
53405340
container := node.Parent
@@ -11832,7 +11832,7 @@ func (c *Checker) classDeclarationExtendsNull(classDecl *ast.Node) bool {
1183211832

1183311833
func (c *Checker) checkAssertion(node *ast.Node, checkMode CheckMode) *Type {
1183411834
if node.Kind == ast.KindTypeAssertionExpression {
11835-
if c.compilerOptions.ErasableSyntaxOnly.IsTrue() {
11835+
if c.compilerOptions.ErasableSyntaxOnly.IsTrue() && !ast.IsInJSFile(node) {
1183611836
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))
1183711837
}
1183811838
}
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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--- old.erasableSyntaxOnlyJS.symbols
2+
+++ new.erasableSyntaxOnlyJS.symbols
3+
@@= skipped -0, +0 lines =@@
4+
-<no content>
5+
+//// [tests/cases/compiler/erasableSyntaxOnlyJS.ts] ////
6+
+
7+
+=== index.ts ===
8+
+// These should still error because they are in a TypeScript file
9+
+import bar = require("./bar.cjs");
10+
+>bar : Symbol(bar, Decl(index.ts, 0, 0))
11+
+
12+
+import foo = require("./foo.js");
13+
+>foo : Symbol(foo, Decl(index.ts, 1, 34))
14+
+
15+
+=== bar.cjs ===
16+
+module.exports = {
17+
+>module.exports : Symbol(export=, Decl(bar.cjs, 0, 0))
18+
+>module : Symbol(module.exports)
19+
+>exports : Symbol(export=, Decl(bar.cjs, 0, 0))
20+
+
21+
+ a: 1,
22+
+>a : Symbol(a, Decl(bar.cjs, 0, 18))
23+
+}
24+
+
25+
+=== foo.js ===
26+
+module.exports = {
27+
+>module.exports : Symbol(export=, Decl(foo.js, 0, 0))
28+
+>module : Symbol(module.exports)
29+
+>exports : Symbol(export=, Decl(foo.js, 0, 0))
30+
+
31+
+ b: 2,
32+
+>b : Symbol(b, Decl(foo.js, 0, 18))
33+
+}
34+
+
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--- old.erasableSyntaxOnlyJS.errors.txt
2+
+++ new.erasableSyntaxOnlyJS.errors.txt
3+
@@= skipped -0, +0 lines =@@
4+
-<no content>
5+
+index.ts(2,1): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
6+
+index.ts(3,1): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
7+
+
8+
+
9+
+==== index.ts (2 errors) ====
10+
+ // These should still error because they are in a TypeScript file
11+
+ import bar = require("./bar.cjs");
12+
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13+
+!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
14+
+ import foo = require("./foo.js");
15+
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16+
+!!! error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.
17+
+
18+
+==== bar.cjs (0 errors) ====
19+
+ module.exports = {
20+
+ a: 1,
21+
+ }
22+
+
23+
+==== foo.js (0 errors) ====
24+
+ module.exports = {
25+
+ b: 2,
26+
+ }
27+
+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--- old.erasableSyntaxOnlyJS.types
2+
+++ new.erasableSyntaxOnlyJS.types
3+
@@= skipped -0, +0 lines =@@
4+
-<no content>
5+
+//// [tests/cases/compiler/erasableSyntaxOnlyJS.ts] ////
6+
+
7+
+=== index.ts ===
8+
+// These should still error because they are in a TypeScript file
9+
+import bar = require("./bar.cjs");
10+
+>bar : { a: number; }
11+
+
12+
+import foo = require("./foo.js");
13+
+>foo : { b: number; }
14+
+
15+
+=== bar.cjs ===
16+
+module.exports = {
17+
+>module.exports = { a: 1,} : { a: number; }
18+
+>module.exports : { a: number; }
19+
+>module : { "export=": { a: number; }; }
20+
+>exports : { a: number; }
21+
+>{ a: 1,} : { a: number; }
22+
+
23+
+ a: 1,
24+
+>a : number
25+
+>1 : 1
26+
+}
27+
+
28+
+=== foo.js ===
29+
+module.exports = {
30+
+>module.exports = { b: 2,} : { b: number; }
31+
+>module.exports : { b: number; }
32+
+>module : { "export=": { b: number; }; }
33+
+>exports : { b: number; }
34+
+>{ b: 2,} : { b: number; }
35+
+
36+
+ b: 2,
37+
+>b : number
38+
+>2 : 2
39+
+}
40+
+

0 commit comments

Comments
 (0)