mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-12-12 11:50:54 -06:00
Handle abstract and const modifiers
This commit is contained in:
parent
06331b57de
commit
6456325973
@ -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 isConstInvalid = true;
|
||||
case SyntaxKind.VariableStatement:
|
||||
// Check modifiers
|
||||
if (nodes === (<ClassDeclaration | FunctionLikeDeclaration | VariableStatement>parent).modifiers) {
|
||||
return checkModifiers(<NodeArray<Modifier>>nodes);
|
||||
return checkModifiers(<NodeArray<Modifier>>nodes, !isConstInvalid);
|
||||
}
|
||||
break;
|
||||
case SyntaxKind.PropertyDeclaration:
|
||||
@ -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:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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.
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
=== tests/cases/compiler/a.js ===
|
||||
const c = 10;
|
||||
>c : Symbol(c, Decl(a.js, 0, 5))
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
=== tests/cases/compiler/a.js ===
|
||||
const c = 10;
|
||||
>c : 10
|
||||
>10 : 10
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
// @allowJs: true
|
||||
// @filename: a.js
|
||||
abstract class c {
|
||||
abstract x;
|
||||
}
|
||||
4
tests/cases/compiler/jsFileCompilationConstModifier.ts
Normal file
4
tests/cases/compiler/jsFileCompilationConstModifier.ts
Normal file
@ -0,0 +1,4 @@
|
||||
// @allowJs: true
|
||||
// @filename: a.js
|
||||
// @noEmit: true
|
||||
const c = 10;
|
||||
Loading…
x
Reference in New Issue
Block a user