Skip to content

Commit 143258c

Browse files
committed
feat(1534): add sealed keyword
1 parent ef9fd97 commit 143258c

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

+1098
-519
lines changed

src/compiler/checker.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -12144,6 +12144,7 @@ namespace ts {
1214412144
checkFlags |= (!(modifiers & ModifierFlags.NonPublicAccessibilityModifier) ? CheckFlags.ContainsPublic : 0) |
1214512145
(modifiers & ModifierFlags.Protected ? CheckFlags.ContainsProtected : 0) |
1214612146
(modifiers & ModifierFlags.Private ? CheckFlags.ContainsPrivate : 0) |
12147+
(modifiers & ModifierFlags.Sealed ? CheckFlags.ContainsSealed : 0) |
1214712148
(modifiers & ModifierFlags.Static ? CheckFlags.ContainsStatic : 0);
1214812149
if (!isPrototypeProperty(prop)) {
1214912150
syntheticFlag = CheckFlags.SyntheticProperty;
@@ -19623,6 +19624,13 @@ namespace ts {
1962319624
return Ternary.False;
1962419625
}
1962519626
}
19627+
else if (targetPropFlags & ModifierFlags.Sealed) {
19628+
if (reportErrors) {
19629+
reportError(Diagnostics.Property_0_is_sealed_in_type_1_that_cannot_be_overridden_in_type_2, symbolToString(targetProp),
19630+
typeToString(target), typeToString(source));
19631+
}
19632+
return Ternary.False;
19633+
}
1962619634
else if (targetPropFlags & ModifierFlags.Protected) {
1962719635
if (!isValidOverrideOf(sourceProp, targetProp)) {
1962819636
if (reportErrors) {
@@ -42458,7 +42466,7 @@ namespace ts {
4245842466
return quickResult;
4245942467
}
4246042468

42461-
let lastStatic: Node | undefined, lastDeclare: Node | undefined, lastAsync: Node | undefined, lastReadonly: Node | undefined, lastOverride: Node | undefined;
42469+
let lastStatic: Node | undefined, lastDeclare: Node | undefined, lastAsync: Node | undefined, lastReadonly: Node | undefined, lastOverride: Node | undefined, lastSealed: Node | undefined;
4246242470
let flags = ModifierFlags.None;
4246342471
for (const modifier of node.modifiers!) {
4246442472
if (modifier.kind !== SyntaxKind.ReadonlyKeyword) {
@@ -42600,6 +42608,19 @@ namespace ts {
4260042608

4260142609
flags |= ModifierFlags.Default;
4260242610
break;
42611+
case SyntaxKind.SealedKeyword:
42612+
if (flags & ModifierFlags.Sealed) {
42613+
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_already_seen, "sealed");
42614+
}
42615+
if (flags & ModifierFlags.Private) {
42616+
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "sealed", "private");
42617+
}
42618+
if (flags & ModifierFlags.Abstract) {
42619+
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "sealed", "abstract");
42620+
}
42621+
flags |= ModifierFlags.Sealed;
42622+
lastSealed = modifier;
42623+
break;
4260342624
case SyntaxKind.DeclareKeyword:
4260442625
if (flags & ModifierFlags.Ambient) {
4260542626
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_already_seen, "declare");
@@ -42696,6 +42717,9 @@ namespace ts {
4269642717
else if (flags & ModifierFlags.Readonly) {
4269742718
return grammarErrorOnNode(lastReadonly!, Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "readonly");
4269842719
}
42720+
if (flags & ModifierFlags.Sealed) {
42721+
return grammarErrorOnNode(lastSealed!, Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "sealed");
42722+
}
4269942723
return false;
4270042724
}
4270142725
else if ((node.kind === SyntaxKind.ImportDeclaration || node.kind === SyntaxKind.ImportEqualsDeclaration) && flags & ModifierFlags.Ambient) {

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -3361,6 +3361,10 @@
33613361
"category": "Error",
33623362
"code": 2836
33633363
},
3364+
"Property '{0}' is sealed in type '{1}' that cannot be overridden in type '{2}'.": {
3365+
"category": "Error",
3366+
"code": 2837
3367+
},
33643368

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

src/compiler/factory/nodeFactory.ts

+2
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,7 @@ namespace ts {
987987
case SyntaxKind.PublicKeyword:
988988
case SyntaxKind.PrivateKeyword:
989989
case SyntaxKind.ProtectedKeyword:
990+
case SyntaxKind.SealedKeyword:
990991
case SyntaxKind.ReadonlyKeyword:
991992
case SyntaxKind.AbstractKeyword:
992993
case SyntaxKind.DeclareKeyword:
@@ -1070,6 +1071,7 @@ namespace ts {
10701071
if (flags & ModifierFlags.Public) result.push(createModifier(SyntaxKind.PublicKeyword));
10711072
if (flags & ModifierFlags.Private) result.push(createModifier(SyntaxKind.PrivateKeyword));
10721073
if (flags & ModifierFlags.Protected) result.push(createModifier(SyntaxKind.ProtectedKeyword));
1074+
if (flags & ModifierFlags.Sealed) result.push(createModifier(SyntaxKind.SealedKeyword));
10731075
if (flags & ModifierFlags.Abstract) result.push(createModifier(SyntaxKind.AbstractKeyword));
10741076
if (flags & ModifierFlags.Static) result.push(createModifier(SyntaxKind.StaticKeyword));
10751077
if (flags & ModifierFlags.Override) result.push(createModifier(SyntaxKind.OverrideKeyword));

src/compiler/program.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2353,6 +2353,7 @@ namespace ts {
23532353
case SyntaxKind.PublicKeyword:
23542354
case SyntaxKind.PrivateKeyword:
23552355
case SyntaxKind.ProtectedKeyword:
2356+
case SyntaxKind.SealedKeyword:
23562357
case SyntaxKind.ReadonlyKeyword:
23572358
case SyntaxKind.DeclareKeyword:
23582359
case SyntaxKind.AbstractKeyword:

src/compiler/scanner.ts

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ namespace ts {
130130
private: SyntaxKind.PrivateKeyword,
131131
protected: SyntaxKind.ProtectedKeyword,
132132
public: SyntaxKind.PublicKeyword,
133+
sealed: SyntaxKind.SealedKeyword,
133134
override: SyntaxKind.OverrideKeyword,
134135
readonly: SyntaxKind.ReadonlyKeyword,
135136
require: SyntaxKind.RequireKeyword,

src/compiler/transformers/ts.ts

+1
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.SealedKeyword:
371372
case SyntaxKind.AbstractKeyword:
372373
case SyntaxKind.OverrideKeyword:
373374
case SyntaxKind.ConstKeyword:

src/compiler/types.ts

+22-14
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ namespace ts {
156156
ProtectedKeyword,
157157
PublicKeyword,
158158
StaticKeyword,
159+
SealedKeyword,
159160
YieldKeyword,
160161
// Contextual keywords
161162
AbstractKeyword,
@@ -598,6 +599,7 @@ namespace ts {
598599
| SyntaxKind.PrivateKeyword
599600
| SyntaxKind.ProtectedKeyword
600601
| SyntaxKind.PublicKeyword
602+
| SyntaxKind.SealedKeyword
601603
| SyntaxKind.ReadonlyKeyword
602604
| SyntaxKind.OverrideKeyword
603605
| SyntaxKind.RequireKeyword
@@ -634,6 +636,7 @@ namespace ts {
634636
| SyntaxKind.PrivateKeyword
635637
| SyntaxKind.ProtectedKeyword
636638
| SyntaxKind.PublicKeyword
639+
| SyntaxKind.SealedKeyword
637640
| SyntaxKind.ReadonlyKeyword
638641
| SyntaxKind.OverrideKeyword
639642
| SyntaxKind.StaticKeyword
@@ -806,9 +809,10 @@ namespace ts {
806809
Protected = 1 << 4, // Property/Method
807810
Static = 1 << 5, // Property/Method
808811
Readonly = 1 << 6, // Property/Method
809-
Abstract = 1 << 7, // Class/Method/ConstructSignature
810-
Async = 1 << 8, // Property/Method/Function
811-
Default = 1 << 9, // Function/Class (export default declaration)
812+
Sealed = 1 << 7, // Property/Method
813+
Abstract = 1 << 8, // Class/Method/ConstructSignature
814+
Async = 1 << 9, // Property/Method/Function
815+
Default = 1 << 10, // Function/Class (export default declaration)
812816
Const = 1 << 11, // Const enum
813817
HasComputedJSDocModifiers = 1 << 12, // Indicates the computed modifier flags include modifiers from JSDoc.
814818

@@ -821,9 +825,9 @@ namespace ts {
821825
ParameterPropertyModifier = AccessibilityModifier | Readonly | Override,
822826
NonPublicAccessibilityModifier = Private | Protected,
823827

824-
TypeScriptModifier = Ambient | Public | Private | Protected | Readonly | Abstract | Const | Override,
828+
TypeScriptModifier = Ambient | Public | Private | Protected | Sealed | Readonly | Abstract | Const | Override,
825829
ExportDefault = Export | Default,
826-
All = Export | Ambient | Public | Private | Protected | Static | Readonly | Abstract | Async | Default | Const | Deprecated | Override
830+
All = Export | Ambient | Public | Private | Protected | Sealed | Static | Readonly | Abstract | Async | Default | Const | Deprecated | Override
827831
}
828832

829833
export const enum JsxFlags {
@@ -1064,6 +1068,7 @@ namespace ts {
10641068
export type PrivateKeyword = ModifierToken<SyntaxKind.PrivateKeyword>;
10651069
export type ProtectedKeyword = ModifierToken<SyntaxKind.ProtectedKeyword>;
10661070
export type PublicKeyword = ModifierToken<SyntaxKind.PublicKeyword>;
1071+
export type SealedKeyword = ModifierToken<SyntaxKind.SealedKeyword>;
10671072
export type ReadonlyKeyword = ModifierToken<SyntaxKind.ReadonlyKeyword>;
10681073
export type OverrideKeyword = ModifierToken<SyntaxKind.OverrideKeyword>;
10691074
export type StaticKeyword = ModifierToken<SyntaxKind.StaticKeyword>;
@@ -1081,6 +1086,7 @@ namespace ts {
10811086
| PrivateKeyword
10821087
| ProtectedKeyword
10831088
| PublicKeyword
1089+
| SealedKeyword
10841090
| OverrideKeyword
10851091
| ReadonlyKeyword
10861092
| StaticKeyword
@@ -4978,15 +4984,17 @@ namespace ts {
49784984
ContainsProtected = 1 << 9, // Synthetic property with protected constituent(s)
49794985
ContainsPrivate = 1 << 10, // Synthetic property with private constituent(s)
49804986
ContainsStatic = 1 << 11, // Synthetic property with static constituent(s)
4981-
Late = 1 << 12, // Late-bound symbol for a computed property with a dynamic name
4982-
ReverseMapped = 1 << 13, // Property of reverse-inferred homomorphic mapped type
4983-
OptionalParameter = 1 << 14, // Optional parameter
4984-
RestParameter = 1 << 15, // Rest parameter
4985-
DeferredType = 1 << 16, // Calculation of the type of this symbol is deferred due to processing costs, should be fetched with `getTypeOfSymbolWithDeferredType`
4986-
HasNeverType = 1 << 17, // Synthetic property with at least one never type in constituents
4987-
Mapped = 1 << 18, // Property of mapped type
4988-
StripOptional = 1 << 19, // Strip optionality in mapped property
4989-
Unresolved = 1 << 20, // Unresolved type alias symbol
4987+
ContainsSealed = 1 << 12,
4988+
4989+
Late = 1 << 13, // Late-bound symbol for a computed property with a dynamic name
4990+
ReverseMapped = 1 << 14, // Property of reverse-inferred homomorphic mapped type
4991+
OptionalParameter = 1 << 15, // Optional parameter
4992+
RestParameter = 1 << 16, // Rest parameter
4993+
DeferredType = 1 << 17, // Calculation of the type of this symbol is deferred due to processing costs, should be fetched with `getTypeOfSymbolWithDeferredType`
4994+
HasNeverType = 1 << 18, // Synthetic property with at least one never type in constituents
4995+
Mapped = 1 << 19, // Property of mapped type
4996+
StripOptional = 1 << 20, // Strip optionality in mapped property
4997+
Unresolved = 1 << 21, // Unresolved type alias symbol
49904998
Synthetic = SyntheticProperty | SyntheticMethod,
49914999
Discriminant = HasNonUniformType | HasLiteralType,
49925000
Partial = ReadPartial | WritePartial

src/compiler/utilities.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4913,6 +4913,7 @@ namespace ts {
49134913
case SyntaxKind.PublicKeyword: return ModifierFlags.Public;
49144914
case SyntaxKind.ProtectedKeyword: return ModifierFlags.Protected;
49154915
case SyntaxKind.PrivateKeyword: return ModifierFlags.Private;
4916+
case SyntaxKind.SealedKeyword: return ModifierFlags.Sealed;
49164917
case SyntaxKind.AbstractKeyword: return ModifierFlags.Abstract;
49174918
case SyntaxKind.ExportKeyword: return ModifierFlags.Export;
49184919
case SyntaxKind.DeclareKeyword: return ModifierFlags.Ambient;
@@ -5451,6 +5452,7 @@ namespace ts {
54515452
const checkFlags = (s as TransientSymbol).checkFlags;
54525453
const accessModifier = checkFlags & CheckFlags.ContainsPrivate ? ModifierFlags.Private :
54535454
checkFlags & CheckFlags.ContainsPublic ? ModifierFlags.Public :
5455+
checkFlags & CheckFlags.ContainsSealed ? ModifierFlags.Sealed :
54545456
ModifierFlags.Protected;
54555457
const staticModifier = checkFlags & CheckFlags.ContainsStatic ? ModifierFlags.Static : 0;
54565458
return accessModifier | staticModifier;

src/compiler/utilitiesPublic.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,7 @@ namespace ts {
11831183
case SyntaxKind.PublicKeyword:
11841184
case SyntaxKind.PrivateKeyword:
11851185
case SyntaxKind.ProtectedKeyword:
1186+
case SyntaxKind.SealedKeyword:
11861187
case SyntaxKind.ReadonlyKeyword:
11871188
case SyntaxKind.StaticKeyword:
11881189
case SyntaxKind.OverrideKeyword:
@@ -1198,7 +1199,7 @@ namespace ts {
11981199

11991200
/* @internal */
12001201
export function isClassMemberModifier(idToken: SyntaxKind): boolean {
1201-
return isParameterPropertyModifier(idToken) || idToken === SyntaxKind.StaticKeyword || idToken === SyntaxKind.OverrideKeyword;
1202+
return isParameterPropertyModifier(idToken) || idToken === SyntaxKind.SealedKeyword || idToken === SyntaxKind.StaticKeyword || idToken === SyntaxKind.OverrideKeyword;
12021203
}
12031204

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

src/harness/fourslashInterfaceImpl.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,7 @@ namespace FourSlashInterface {
11991199
case "private":
12001200
case "protected":
12011201
case "public":
1202+
case "sealed":
12021203
case "abstract":
12031204
case "any":
12041205
case "boolean":
@@ -1238,6 +1239,7 @@ namespace FourSlashInterface {
12381239
"protected",
12391240
"public",
12401241
"readonly",
1242+
"sealed",
12411243
"set",
12421244
"static",
12431245
].map(keywordEntry);

src/services/completions.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -2807,6 +2807,9 @@ namespace ts.Completions {
28072807
case "private":
28082808
classElementModifierFlags = classElementModifierFlags | ModifierFlags.Private;
28092809
break;
2810+
case "sealed":
2811+
classElementModifierFlags = classElementModifierFlags | ModifierFlags.Sealed;
2812+
break;
28102813
case "static":
28112814
classElementModifierFlags = classElementModifierFlags | ModifierFlags.Static;
28122815
break;
@@ -2820,7 +2823,7 @@ namespace ts.Completions {
28202823
}
28212824

28222825
// No member list for private methods
2823-
if (!(classElementModifierFlags & ModifierFlags.Private)) {
2826+
if (!(classElementModifierFlags & (ModifierFlags.Private | ModifierFlags.Sealed))) {
28242827
// List of property symbols of base type that are not private and already implemented
28252828
const baseTypeNodes = isClassLike(decl) && classElementModifierFlags & ModifierFlags.Override ? singleElementArray(getEffectiveBaseTypeNode(decl)) : getAllSuperTypeNodes(decl);
28262829
const baseSymbols = flatMap(baseTypeNodes, baseTypeNode => {
@@ -3095,6 +3098,7 @@ namespace ts.Completions {
30953098
case SyntaxKind.InterfaceKeyword:
30963099
case SyntaxKind.LetKeyword:
30973100
case SyntaxKind.PrivateKeyword:
3101+
case SyntaxKind.SealedKeyword:
30983102
case SyntaxKind.ProtectedKeyword:
30993103
case SyntaxKind.PublicKeyword:
31003104
case SyntaxKind.StaticKeyword:
@@ -3487,6 +3491,7 @@ namespace ts.Completions {
34873491
case SyntaxKind.PrivateKeyword:
34883492
case SyntaxKind.ProtectedKeyword:
34893493
case SyntaxKind.PublicKeyword:
3494+
case SyntaxKind.SealedKeyword:
34903495
case SyntaxKind.ReadonlyKeyword:
34913496
case SyntaxKind.StringKeyword:
34923497
case SyntaxKind.SymbolKeyword:

0 commit comments

Comments
 (0)