Skip to content

Commit 0208345

Browse files
committed
Handle abstract and const modifiers
1 parent 06331b5 commit 0208345

6 files changed

+42
-5
lines changed

src/compiler/program.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -870,11 +870,12 @@ namespace ts {
870870
diagnostics.push(createDiagnosticForNodeArray(nodes, Diagnostics.type_parameter_declarations_can_only_be_used_in_a_ts_file));
871871
return;
872872
}
873-
// pass through
873+
// pass through
874+
let isConstValid = false;
874875
case SyntaxKind.VariableStatement:
875876
// Check modifiers
876877
if (nodes === (<ClassDeclaration | FunctionLikeDeclaration | VariableStatement>parent).modifiers) {
877-
return checkModifiers(<NodeArray<Modifier>>nodes);
878+
return checkModifiers(<NodeArray<Modifier>>nodes, isConstValid);
878879
}
879880
break;
880881
case SyntaxKind.PropertyDeclaration:
@@ -911,23 +912,27 @@ namespace ts {
911912
}
912913
}
913914

914-
function checkModifiers(modifiers: NodeArray<Modifier>) {
915+
function checkModifiers(modifiers: NodeArray<Modifier>, isConstValid: boolean) {
915916
for (const modifier of modifiers) {
916917
switch (modifier.kind) {
918+
case SyntaxKind.ConstKeyword:
919+
if (isConstValid) {
920+
continue;
921+
}
922+
//Fallthrough to report error
917923
case SyntaxKind.PublicKeyword:
918924
case SyntaxKind.PrivateKeyword:
919925
case SyntaxKind.ProtectedKeyword:
920926
case SyntaxKind.ReadonlyKeyword:
921927
case SyntaxKind.DeclareKeyword:
928+
case SyntaxKind.AbstractKeyword:
922929
diagnostics.push(createDiagnosticForNode(modifier, Diagnostics._0_can_only_be_used_in_a_ts_file, tokenToString(modifier.kind)));
923930
break;
924931

925932
// These are all legal modifiers.
926933
case SyntaxKind.StaticKeyword:
927934
case SyntaxKind.ExportKeyword:
928-
case SyntaxKind.ConstKeyword:
929935
case SyntaxKind.DefaultKeyword:
930-
case SyntaxKind.AbstractKeyword:
931936
}
932937
}
933938
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
2+
tests/cases/compiler/a.js(1,1): error TS8009: 'abstract' can only be used in a .ts file.
3+
tests/cases/compiler/a.js(2,5): error TS8009: 'abstract' can only be used in a .ts file.
4+
5+
6+
!!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
7+
==== tests/cases/compiler/a.js (2 errors) ====
8+
abstract class c {
9+
~~~~~~~~
10+
!!! error TS8009: 'abstract' can only be used in a .ts file.
11+
abstract x;
12+
~~~~~~~~
13+
!!! error TS8009: 'abstract' can only be used in a .ts file.
14+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/compiler/a.js ===
2+
const c = 10;
3+
>c : Symbol(c, Decl(a.js, 0, 5))
4+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/compiler/a.js ===
2+
const c = 10;
3+
>c : 10
4+
>10 : 10
5+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @allowJs: true
2+
// @filename: a.js
3+
abstract class c {
4+
abstract x;
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @allowJs: true
2+
// @filename: a.js
3+
// @noEmit: true
4+
const c = 10;

0 commit comments

Comments
 (0)