Skip to content

Commit e4cc532

Browse files
authored
Make bodiless declarations an error in JS (#54439)
1 parent a437de6 commit e4cc532

14 files changed

+106
-0
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6556,6 +6556,10 @@
65566556
"category": "Error",
65576557
"code": 8016
65586558
},
6559+
"Signature declarations can only be used in TypeScript files.": {
6560+
"category": "Error",
6561+
"code": 8017
6562+
},
65596563
"Report errors in .js files.": {
65606564
"category": "Message",
65616565
"code": 8019

src/compiler/program.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3022,6 +3022,14 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
30223022
case SyntaxKind.TypeAliasDeclaration:
30233023
diagnostics.push(createDiagnosticForNode(node, Diagnostics.Type_aliases_can_only_be_used_in_TypeScript_files));
30243024
return "skip";
3025+
case SyntaxKind.Constructor:
3026+
case SyntaxKind.MethodDeclaration:
3027+
case SyntaxKind.FunctionDeclaration:
3028+
if (!(node as FunctionLikeDeclaration).body) {
3029+
diagnostics.push(createDiagnosticForNode(node, Diagnostics.Signature_declarations_can_only_be_used_in_TypeScript_files));
3030+
return "skip";
3031+
}
3032+
return;
30253033
case SyntaxKind.EnumDeclaration:
30263034
const enumKeyword = Debug.checkDefined(tokenToString(SyntaxKind.EnumKeyword));
30273035
diagnostics.push(createDiagnosticForNode(node, Diagnostics._0_declarations_can_only_be_used_in_TypeScript_files, enumKeyword));
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
a.js(2,3): error TS8017: Signature declarations can only be used in TypeScript files.
2+
3+
4+
==== a.js (1 errors) ====
5+
class A {
6+
constructor();
7+
~~~~~~~~~~~~~~
8+
!!! error TS8017: Signature declarations can only be used in TypeScript files.
9+
}
10+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [tests/cases/compiler/jsFileCompilationConstructorOverloadSyntax.ts] ////
2+
3+
=== a.js ===
4+
class A {
5+
>A : Symbol(A, Decl(a.js, 0, 0))
6+
7+
constructor();
8+
}
9+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [tests/cases/compiler/jsFileCompilationConstructorOverloadSyntax.ts] ////
2+
3+
=== a.js ===
4+
class A {
5+
>A : A
6+
7+
constructor();
8+
}
9+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
a.js(1,10): error TS8017: Signature declarations can only be used in TypeScript files.
2+
3+
4+
==== a.js (1 errors) ====
5+
function foo();
6+
~~~
7+
!!! error TS8017: Signature declarations can only be used in TypeScript files.
8+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//// [tests/cases/compiler/jsFileCompilationFunctionOverloadSyntax.ts] ////
2+
3+
=== a.js ===
4+
function foo();
5+
>foo : Symbol(foo, Decl(a.js, 0, 0))
6+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//// [tests/cases/compiler/jsFileCompilationFunctionOverloadSyntax.ts] ////
2+
3+
=== a.js ===
4+
function foo();
5+
>foo : () => any
6+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
a.js(2,3): error TS8017: Signature declarations can only be used in TypeScript files.
2+
3+
4+
==== a.js (1 errors) ====
5+
class A {
6+
foo();
7+
~~~
8+
!!! error TS8017: Signature declarations can only be used in TypeScript files.
9+
}
10+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [tests/cases/compiler/jsFileCompilationMethodOverloadSyntax.ts] ////
2+
3+
=== a.js ===
4+
class A {
5+
>A : Symbol(A, Decl(a.js, 0, 0))
6+
7+
foo();
8+
>foo : Symbol(A.foo, Decl(a.js, 0, 9))
9+
}
10+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [tests/cases/compiler/jsFileCompilationMethodOverloadSyntax.ts] ////
2+
3+
=== a.js ===
4+
class A {
5+
>A : A
6+
7+
foo();
8+
>foo : () => any
9+
}
10+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @allowJs: true
2+
// @noEmit: true
3+
// @filename: a.js
4+
class A {
5+
constructor();
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @allowJs: true
2+
// @noEmit: true
3+
// @filename: a.js
4+
function foo();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @allowJs: true
2+
// @noEmit: true
3+
// @filename: a.js
4+
class A {
5+
foo();
6+
}

0 commit comments

Comments
 (0)