Skip to content

Commit

Permalink
Handle abstract and const modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
sheetalkamat committed Nov 9, 2016
1 parent 06331b5 commit 0208345
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -870,11 +870,12 @@ namespace ts {
diagnostics.push(createDiagnosticForNodeArray(nodes, Diagnostics.type_parameter_declarations_can_only_be_used_in_a_ts_file));
return;
}
// pass through
// pass through
let isConstValid = false;
case SyntaxKind.VariableStatement:
// Check modifiers
if (nodes === (<ClassDeclaration | FunctionLikeDeclaration | VariableStatement>parent).modifiers) {
return checkModifiers(<NodeArray<Modifier>>nodes);
return checkModifiers(<NodeArray<Modifier>>nodes, isConstValid);
}
break;
case SyntaxKind.PropertyDeclaration:
Expand Down Expand Up @@ -911,23 +912,27 @@ namespace ts {
}
}

function checkModifiers(modifiers: NodeArray<Modifier>) {
function checkModifiers(modifiers: NodeArray<Modifier>, isConstValid: boolean) {
for (const modifier of modifiers) {
switch (modifier.kind) {
case SyntaxKind.ConstKeyword:
if (isConstValid) {
continue;
}
//Fallthrough to report error
case SyntaxKind.PublicKeyword:
case SyntaxKind.PrivateKeyword:
case SyntaxKind.ProtectedKeyword:
case SyntaxKind.ReadonlyKeyword:
case SyntaxKind.DeclareKeyword:
case SyntaxKind.AbstractKeyword:
diagnostics.push(createDiagnosticForNode(modifier, Diagnostics._0_can_only_be_used_in_a_ts_file, tokenToString(modifier.kind)));
break;

// These are all legal modifiers.
case SyntaxKind.StaticKeyword:
case SyntaxKind.ExportKeyword:
case SyntaxKind.ConstKeyword:
case SyntaxKind.DefaultKeyword:
case SyntaxKind.AbstractKeyword:
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
tests/cases/compiler/a.js(1,1): error TS8009: 'abstract' can only be used in a .ts file.
tests/cases/compiler/a.js(2,5): error TS8009: 'abstract' can only be used in a .ts file.


!!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
==== tests/cases/compiler/a.js (2 errors) ====
abstract class c {
~~~~~~~~
!!! error TS8009: 'abstract' can only be used in a .ts file.
abstract x;
~~~~~~~~
!!! error TS8009: 'abstract' can only be used in a .ts file.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
=== tests/cases/compiler/a.js ===
const c = 10;
>c : Symbol(c, Decl(a.js, 0, 5))

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
=== tests/cases/compiler/a.js ===
const c = 10;
>c : 10
>10 : 10

5 changes: 5 additions & 0 deletions tests/cases/compiler/jsFileCompilationAbstractModifier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @allowJs: true
// @filename: a.js
abstract class c {
abstract x;
}
4 changes: 4 additions & 0 deletions tests/cases/compiler/jsFileCompilationConstModifier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @allowJs: true
// @filename: a.js
// @noEmit: true
const c = 10;

0 comments on commit 0208345

Please sign in to comment.