Skip to content

Commit a1e0504

Browse files
author
Andy
authored
Merge pull request #9189 from Microsoft/js_property_declaration
Allow property declarations in .js files
2 parents 97be083 + 9b6472a commit a1e0504

File tree

5 files changed

+40
-24
lines changed

5 files changed

+40
-24
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2921,10 +2921,6 @@
29212921
"category": "Error",
29222922
"code": 8012
29232923
},
2924-
"'property declarations' can only be used in a .ts file.": {
2925-
"category": "Error",
2926-
"code": 8014
2927-
},
29282924
"'enum declarations' can only be used in a .ts file.": {
29292925
"category": "Error",
29302926
"code": 8015

src/compiler/program.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,8 +1601,19 @@ namespace ts {
16011601
}
16021602
break;
16031603
case SyntaxKind.PropertyDeclaration:
1604-
diagnostics.push(createDiagnosticForNode(node, Diagnostics.property_declarations_can_only_be_used_in_a_ts_file));
1605-
return true;
1604+
const propertyDeclaration = <PropertyDeclaration>node;
1605+
if (propertyDeclaration.modifiers) {
1606+
for (const modifier of propertyDeclaration.modifiers) {
1607+
if (modifier.kind !== SyntaxKind.StaticKeyword) {
1608+
diagnostics.push(createDiagnosticForNode(modifier, Diagnostics._0_can_only_be_used_in_a_ts_file, tokenToString(modifier.kind)));
1609+
return true;
1610+
}
1611+
}
1612+
}
1613+
if (checkTypeAnnotation((<PropertyDeclaration>node).type)) {
1614+
return true;
1615+
}
1616+
break;
16061617
case SyntaxKind.EnumDeclaration:
16071618
diagnostics.push(createDiagnosticForNode(node, Diagnostics.enum_declarations_can_only_be_used_in_a_ts_file));
16081619
return true;

tests/baselines/reference/jsFileCompilationPropertySyntaxOfClass.errors.txt

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/cases/compiler/jsFileCompilationPropertySyntaxOfClass.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/cases/fourslash/getJavaScriptSemanticDiagnostics18.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,35 @@
22

33
// @allowJs: true
44
// @Filename: a.js
5-
//// class C { v }
5+
////class C {
6+
//// x; // Regular property declaration allowed
7+
//// static y; // static allowed
8+
//// public z; // public not allowed
9+
////}
610

11+
goTo.file("a.js");
712
verify.getSemanticDiagnostics(`[
813
{
9-
"message": "'property declarations' can only be used in a .ts file.",
10-
"start": 10,
11-
"length": 1,
14+
"message": "\'public\' can only be used in a .ts file.",
15+
"start": 93,
16+
"length": 6,
1217
"category": "error",
13-
"code": 8014
18+
"code": 8009
1419
}
15-
]`);
20+
]`);
21+
22+
// @Filename: b.js
23+
////class C {
24+
//// x: number; // Types not allowed
25+
////}
26+
27+
goTo.file("b.js");
28+
verify.getSemanticDiagnostics(`[
29+
{
30+
"message": "'types' can only be used in a .ts file.",
31+
"start": 17,
32+
"length": 6,
33+
"category": "error",
34+
"code": 8010
35+
}
36+
]`);

0 commit comments

Comments
 (0)