Skip to content

Commit fd3f9d8

Browse files
committed
feat(1534): add final keyword
1 parent d514a69 commit fd3f9d8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1091
-514
lines changed

src/compiler/checker.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11850,6 +11850,7 @@ namespace ts {
1185011850
checkFlags |= (!(modifiers & ModifierFlags.NonPublicAccessibilityModifier) ? CheckFlags.ContainsPublic : 0) |
1185111851
(modifiers & ModifierFlags.Protected ? CheckFlags.ContainsProtected : 0) |
1185211852
(modifiers & ModifierFlags.Private ? CheckFlags.ContainsPrivate : 0) |
11853+
(modifiers & ModifierFlags.Final ? CheckFlags.ContainsFinal : 0) |
1185311854
(modifiers & ModifierFlags.Static ? CheckFlags.ContainsStatic : 0);
1185411855
if (!isPrototypeProperty(prop)) {
1185511856
syntheticFlag = CheckFlags.SyntheticProperty;
@@ -19083,6 +19084,13 @@ namespace ts {
1908319084
return Ternary.False;
1908419085
}
1908519086
}
19087+
else if (targetPropFlags & ModifierFlags.Final) {
19088+
if (reportErrors) {
19089+
reportError(Diagnostics.Property_0_is_final_in_type_1_that_cannot_be_overridden_in_type_2, symbolToString(targetProp),
19090+
typeToString(target), typeToString(source));
19091+
}
19092+
return Ternary.False;
19093+
}
1908619094
else if (targetPropFlags & ModifierFlags.Protected) {
1908719095
if (!isValidOverrideOf(sourceProp, targetProp)) {
1908819096
if (reportErrors) {
@@ -41342,7 +41350,7 @@ namespace ts {
4134241350
return quickResult;
4134341351
}
4134441352

41345-
let lastStatic: Node | undefined, lastDeclare: Node | undefined, lastAsync: Node | undefined, lastReadonly: Node | undefined, lastOverride: Node | undefined;
41353+
let lastStatic: Node | undefined, lastDeclare: Node | undefined, lastAsync: Node | undefined, lastReadonly: Node | undefined, lastOverride: Node | undefined, lastFinal: Node | undefined;
4134641354
let flags = ModifierFlags.None;
4134741355
for (const modifier of node.modifiers!) {
4134841356
if (modifier.kind !== SyntaxKind.ReadonlyKeyword) {
@@ -41484,6 +41492,19 @@ namespace ts {
4148441492

4148541493
flags |= ModifierFlags.Default;
4148641494
break;
41495+
case SyntaxKind.FinalKeyword:
41496+
if (flags & ModifierFlags.Final) {
41497+
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_already_seen, "final");
41498+
}
41499+
if (flags & ModifierFlags.Private) {
41500+
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "final", "private");
41501+
}
41502+
if (flags & ModifierFlags.Abstract) {
41503+
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "final", "abstract");
41504+
}
41505+
flags |= ModifierFlags.Final;
41506+
lastFinal = modifier;
41507+
break;
4148741508
case SyntaxKind.DeclareKeyword:
4148841509
if (flags & ModifierFlags.Ambient) {
4148941510
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_already_seen, "declare");
@@ -41580,6 +41601,9 @@ namespace ts {
4158041601
else if (flags & ModifierFlags.Readonly) {
4158141602
return grammarErrorOnNode(lastReadonly!, Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "readonly");
4158241603
}
41604+
if (flags & ModifierFlags.Final) {
41605+
return grammarErrorOnNode(lastFinal!, Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "final");
41606+
}
4158341607
return false;
4158441608
}
4158541609
else if ((node.kind === SyntaxKind.ImportDeclaration || node.kind === SyntaxKind.ImportEqualsDeclaration) && flags & ModifierFlags.Ambient) {

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3284,6 +3284,10 @@
32843284
"category": "Error",
32853285
"code": 2819
32863286
},
3287+
"Property '{0}' is final in type '{1}' that cannot be overridden in type '{2}'.": {
3288+
"category": "Error",
3289+
"code": 2820
3290+
},
32873291

32883292
"Import declaration '{0}' is using private name '{1}'.": {
32893293
"category": "Error",

src/compiler/factory/nodeFactory.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,7 @@ namespace ts {
983983
case SyntaxKind.PublicKeyword:
984984
case SyntaxKind.PrivateKeyword:
985985
case SyntaxKind.ProtectedKeyword:
986+
case SyntaxKind.FinalKeyword:
986987
case SyntaxKind.ReadonlyKeyword:
987988
case SyntaxKind.AbstractKeyword:
988989
case SyntaxKind.DeclareKeyword:
@@ -1066,6 +1067,7 @@ namespace ts {
10661067
if (flags & ModifierFlags.Public) result.push(createModifier(SyntaxKind.PublicKeyword));
10671068
if (flags & ModifierFlags.Private) result.push(createModifier(SyntaxKind.PrivateKeyword));
10681069
if (flags & ModifierFlags.Protected) result.push(createModifier(SyntaxKind.ProtectedKeyword));
1070+
if (flags & ModifierFlags.Final) result.push(createModifier(SyntaxKind.FinalKeyword));
10691071
if (flags & ModifierFlags.Abstract) result.push(createModifier(SyntaxKind.AbstractKeyword));
10701072
if (flags & ModifierFlags.Static) result.push(createModifier(SyntaxKind.StaticKeyword));
10711073
if (flags & ModifierFlags.Override) result.push(createModifier(SyntaxKind.OverrideKeyword));

src/compiler/program.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,6 +2161,7 @@ namespace ts {
21612161
case SyntaxKind.PublicKeyword:
21622162
case SyntaxKind.PrivateKeyword:
21632163
case SyntaxKind.ProtectedKeyword:
2164+
case SyntaxKind.FinalKeyword:
21642165
case SyntaxKind.ReadonlyKeyword:
21652166
case SyntaxKind.DeclareKeyword:
21662167
case SyntaxKind.AbstractKeyword:

src/compiler/scanner.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ namespace ts {
129129
private: SyntaxKind.PrivateKeyword,
130130
protected: SyntaxKind.ProtectedKeyword,
131131
public: SyntaxKind.PublicKeyword,
132+
final: SyntaxKind.FinalKeyword,
132133
override: SyntaxKind.OverrideKeyword,
133134
readonly: SyntaxKind.ReadonlyKeyword,
134135
require: SyntaxKind.RequireKeyword,

src/compiler/transformers/ts.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ namespace ts {
368368
case SyntaxKind.PublicKeyword:
369369
case SyntaxKind.PrivateKeyword:
370370
case SyntaxKind.ProtectedKeyword:
371+
case SyntaxKind.FinalKeyword:
371372
case SyntaxKind.AbstractKeyword:
372373
case SyntaxKind.OverrideKeyword:
373374
case SyntaxKind.ConstKeyword:

src/compiler/types.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ namespace ts {
156156
ProtectedKeyword,
157157
PublicKeyword,
158158
StaticKeyword,
159+
FinalKeyword,
159160
YieldKeyword,
160161
// Contextual keywords
161162
AbstractKeyword,
@@ -594,6 +595,7 @@ namespace ts {
594595
| SyntaxKind.PrivateKeyword
595596
| SyntaxKind.ProtectedKeyword
596597
| SyntaxKind.PublicKeyword
598+
| SyntaxKind.FinalKeyword
597599
| SyntaxKind.ReadonlyKeyword
598600
| SyntaxKind.OverrideKeyword
599601
| SyntaxKind.RequireKeyword
@@ -630,6 +632,7 @@ namespace ts {
630632
| SyntaxKind.PrivateKeyword
631633
| SyntaxKind.ProtectedKeyword
632634
| SyntaxKind.PublicKeyword
635+
| SyntaxKind.FinalKeyword
633636
| SyntaxKind.ReadonlyKeyword
634637
| SyntaxKind.OverrideKeyword
635638
| SyntaxKind.StaticKeyword
@@ -802,9 +805,10 @@ namespace ts {
802805
Protected = 1 << 4, // Property/Method
803806
Static = 1 << 5, // Property/Method
804807
Readonly = 1 << 6, // Property/Method
805-
Abstract = 1 << 7, // Class/Method/ConstructSignature
806-
Async = 1 << 8, // Property/Method/Function
807-
Default = 1 << 9, // Function/Class (export default declaration)
808+
Final = 1 << 7, // Property/Method
809+
Abstract = 1 << 8, // Class/Method/ConstructSignature
810+
Async = 1 << 9, // Property/Method/Function
811+
Default = 1 << 10, // Function/Class (export default declaration)
808812
Const = 1 << 11, // Const enum
809813
HasComputedJSDocModifiers = 1 << 12, // Indicates the computed modifier flags include modifiers from JSDoc.
810814

@@ -817,9 +821,9 @@ namespace ts {
817821
ParameterPropertyModifier = AccessibilityModifier | Readonly | Override,
818822
NonPublicAccessibilityModifier = Private | Protected,
819823

820-
TypeScriptModifier = Ambient | Public | Private | Protected | Readonly | Abstract | Const | Override,
824+
TypeScriptModifier = Ambient | Public | Private | Protected | Final | Readonly | Abstract | Const | Override,
821825
ExportDefault = Export | Default,
822-
All = Export | Ambient | Public | Private | Protected | Static | Readonly | Abstract | Async | Default | Const | Deprecated | Override
826+
All = Export | Ambient | Public | Private | Protected | Final | Static | Readonly | Abstract | Async | Default | Const | Deprecated | Override
823827
}
824828

825829
export const enum JsxFlags {
@@ -1059,6 +1063,7 @@ namespace ts {
10591063
export type PrivateKeyword = ModifierToken<SyntaxKind.PrivateKeyword>;
10601064
export type ProtectedKeyword = ModifierToken<SyntaxKind.ProtectedKeyword>;
10611065
export type PublicKeyword = ModifierToken<SyntaxKind.PublicKeyword>;
1066+
export type FinalKeyword = ModifierToken<SyntaxKind.FinalKeyword>;
10621067
export type ReadonlyKeyword = ModifierToken<SyntaxKind.ReadonlyKeyword>;
10631068
export type OverrideKeyword = ModifierToken<SyntaxKind.OverrideKeyword>;
10641069
export type StaticKeyword = ModifierToken<SyntaxKind.StaticKeyword>;
@@ -1076,6 +1081,7 @@ namespace ts {
10761081
| PrivateKeyword
10771082
| ProtectedKeyword
10781083
| PublicKeyword
1084+
| FinalKeyword
10791085
| OverrideKeyword
10801086
| ReadonlyKeyword
10811087
| StaticKeyword
@@ -4917,15 +4923,16 @@ namespace ts {
49174923
ContainsPublic = 1 << 8, // Synthetic property with public constituent(s)
49184924
ContainsProtected = 1 << 9, // Synthetic property with protected constituent(s)
49194925
ContainsPrivate = 1 << 10, // Synthetic property with private constituent(s)
4920-
ContainsStatic = 1 << 11, // Synthetic property with static constituent(s)
4921-
Late = 1 << 12, // Late-bound symbol for a computed property with a dynamic name
4922-
ReverseMapped = 1 << 13, // Property of reverse-inferred homomorphic mapped type
4923-
OptionalParameter = 1 << 14, // Optional parameter
4924-
RestParameter = 1 << 15, // Rest parameter
4925-
DeferredType = 1 << 16, // Calculation of the type of this symbol is deferred due to processing costs, should be fetched with `getTypeOfSymbolWithDeferredType`
4926-
HasNeverType = 1 << 17, // Synthetic property with at least one never type in constituents
4927-
Mapped = 1 << 18, // Property of mapped type
4928-
StripOptional = 1 << 19, // Strip optionality in mapped property
4926+
ContainsFinal = 1 << 11,
4927+
ContainsStatic = 1 << 12, // Synthetic property with static constituent(s)
4928+
Late = 1 << 13, // Late-bound symbol for a computed property with a dynamic name
4929+
ReverseMapped = 1 << 14, // Property of reverse-inferred homomorphic mapped type
4930+
OptionalParameter = 1 << 15, // Optional parameter
4931+
RestParameter = 1 << 16, // Rest parameter
4932+
DeferredType = 1 << 17, // Calculation of the type of this symbol is deferred due to processing costs, should be fetched with `getTypeOfSymbolWithDeferredType`
4933+
HasNeverType = 1 << 18, // Synthetic property with at least one never type in constituents
4934+
Mapped = 1 << 19, // Property of mapped type
4935+
StripOptional = 1 << 20, // Strip optionality in mapped property
49294936
Synthetic = SyntheticProperty | SyntheticMethod,
49304937
Discriminant = HasNonUniformType | HasLiteralType,
49314938
Partial = ReadPartial | WritePartial

src/compiler/utilities.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4886,6 +4886,7 @@ namespace ts {
48864886
case SyntaxKind.PublicKeyword: return ModifierFlags.Public;
48874887
case SyntaxKind.ProtectedKeyword: return ModifierFlags.Protected;
48884888
case SyntaxKind.PrivateKeyword: return ModifierFlags.Private;
4889+
case SyntaxKind.FinalKeyword: return ModifierFlags.Final;
48894890
case SyntaxKind.AbstractKeyword: return ModifierFlags.Abstract;
48904891
case SyntaxKind.ExportKeyword: return ModifierFlags.Export;
48914892
case SyntaxKind.DeclareKeyword: return ModifierFlags.Ambient;
@@ -5424,6 +5425,7 @@ namespace ts {
54245425
const checkFlags = (s as TransientSymbol).checkFlags;
54255426
const accessModifier = checkFlags & CheckFlags.ContainsPrivate ? ModifierFlags.Private :
54265427
checkFlags & CheckFlags.ContainsPublic ? ModifierFlags.Public :
5428+
checkFlags & CheckFlags.ContainsFinal ? ModifierFlags.Final :
54275429
ModifierFlags.Protected;
54285430
const staticModifier = checkFlags & CheckFlags.ContainsStatic ? ModifierFlags.Static : 0;
54295431
return accessModifier | staticModifier;

src/compiler/utilitiesPublic.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,7 @@ namespace ts {
11771177
case SyntaxKind.PublicKeyword:
11781178
case SyntaxKind.PrivateKeyword:
11791179
case SyntaxKind.ProtectedKeyword:
1180+
case SyntaxKind.FinalKeyword:
11801181
case SyntaxKind.ReadonlyKeyword:
11811182
case SyntaxKind.StaticKeyword:
11821183
case SyntaxKind.OverrideKeyword:
@@ -1192,7 +1193,7 @@ namespace ts {
11921193

11931194
/* @internal */
11941195
export function isClassMemberModifier(idToken: SyntaxKind): boolean {
1195-
return isParameterPropertyModifier(idToken) || idToken === SyntaxKind.StaticKeyword || idToken === SyntaxKind.OverrideKeyword;
1196+
return isParameterPropertyModifier(idToken) || idToken === SyntaxKind.FinalKeyword || idToken === SyntaxKind.StaticKeyword || idToken === SyntaxKind.OverrideKeyword;
11961197
}
11971198

11981199
export function isModifier(node: Node): node is Modifier {

src/harness/fourslashInterfaceImpl.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,7 @@ namespace FourSlashInterface {
11691169
case "private":
11701170
case "protected":
11711171
case "public":
1172+
case "final":
11721173
case "abstract":
11731174
case "any":
11741175
case "boolean":
@@ -1198,7 +1199,7 @@ namespace FourSlashInterface {
11981199
}
11991200

12001201
export const classElementKeywords: readonly ExpectedCompletionEntryObject[] =
1201-
["private", "protected", "public", "static", "abstract", "async", "constructor", "declare", "get", "readonly", "set", "override"].map(keywordEntry);
1202+
["private", "protected", "public", "static", "final", "abstract", "async", "constructor", "declare", "get", "readonly", "set", "override"].map(keywordEntry);
12021203

12031204
export const classElementInJsKeywords = getInJsKeywords(classElementKeywords);
12041205

src/services/completions.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2109,6 +2109,7 @@ namespace ts.Completions {
21092109
case SyntaxKind.TemplateMiddle:
21102110
return containingNodeKind === SyntaxKind.TemplateSpan; // `aa ${10} dd ${|
21112111

2112+
case SyntaxKind.FinalKeyword:
21122113
case SyntaxKind.PublicKeyword:
21132114
case SyntaxKind.PrivateKeyword:
21142115
case SyntaxKind.ProtectedKeyword:
@@ -2327,6 +2328,9 @@ namespace ts.Completions {
23272328
case "private":
23282329
classElementModifierFlags = classElementModifierFlags | ModifierFlags.Private;
23292330
break;
2331+
case "final":
2332+
classElementModifierFlags = classElementModifierFlags | ModifierFlags.Final;
2333+
break;
23302334
case "static":
23312335
classElementModifierFlags = classElementModifierFlags | ModifierFlags.Static;
23322336
break;
@@ -2340,7 +2344,7 @@ namespace ts.Completions {
23402344
}
23412345

23422346
// No member list for private methods
2343-
if (!(classElementModifierFlags & ModifierFlags.Private)) {
2347+
if (!(classElementModifierFlags & (ModifierFlags.Private | ModifierFlags.Final))) {
23442348
// List of property symbols of base type that are not private and already implemented
23452349
const baseTypeNodes = isClassLike(decl) && classElementModifierFlags & ModifierFlags.Override ? singleElementArray(getEffectiveBaseTypeNode(decl)) : getAllSuperTypeNodes(decl);
23462350
const baseSymbols = flatMap(baseTypeNodes, baseTypeNode => {
@@ -2602,6 +2606,7 @@ namespace ts.Completions {
26022606
case SyntaxKind.InterfaceKeyword:
26032607
case SyntaxKind.LetKeyword:
26042608
case SyntaxKind.PrivateKeyword:
2609+
case SyntaxKind.FinalKeyword:
26052610
case SyntaxKind.ProtectedKeyword:
26062611
case SyntaxKind.PublicKeyword:
26072612
case SyntaxKind.StaticKeyword:
@@ -3002,6 +3007,7 @@ namespace ts.Completions {
30023007
case SyntaxKind.PrivateKeyword:
30033008
case SyntaxKind.ProtectedKeyword:
30043009
case SyntaxKind.PublicKeyword:
3010+
case SyntaxKind.FinalKeyword:
30053011
case SyntaxKind.ReadonlyKeyword:
30063012
case SyntaxKind.StringKeyword:
30073013
case SyntaxKind.SymbolKeyword:

0 commit comments

Comments
 (0)