Skip to content

Commit a1102c9

Browse files
committed
feat(1534): add sealed keyword
1 parent 670ad45 commit a1102c9

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

+1097
-520
lines changed

src/compiler/checker.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12107,6 +12107,7 @@ namespace ts {
1210712107
checkFlags |= (!(modifiers & ModifierFlags.NonPublicAccessibilityModifier) ? CheckFlags.ContainsPublic : 0) |
1210812108
(modifiers & ModifierFlags.Protected ? CheckFlags.ContainsProtected : 0) |
1210912109
(modifiers & ModifierFlags.Private ? CheckFlags.ContainsPrivate : 0) |
12110+
(modifiers & ModifierFlags.Sealed ? CheckFlags.ContainsSealed : 0) |
1211012111
(modifiers & ModifierFlags.Static ? CheckFlags.ContainsStatic : 0);
1211112112
if (!isPrototypeProperty(prop)) {
1211212113
syntheticFlag = CheckFlags.SyntheticProperty;
@@ -19587,6 +19588,13 @@ namespace ts {
1958719588
return Ternary.False;
1958819589
}
1958919590
}
19591+
else if (targetPropFlags & ModifierFlags.Sealed) {
19592+
if (reportErrors) {
19593+
reportError(Diagnostics.Property_0_is_sealed_in_type_1_that_cannot_be_overridden_in_type_2, symbolToString(targetProp),
19594+
typeToString(target), typeToString(source));
19595+
}
19596+
return Ternary.False;
19597+
}
1959019598
else if (targetPropFlags & ModifierFlags.Protected) {
1959119599
if (!isValidOverrideOf(sourceProp, targetProp)) {
1959219600
if (reportErrors) {
@@ -42417,7 +42425,7 @@ namespace ts {
4241742425
return quickResult;
4241842426
}
4241942427

42420-
let lastStatic: Node | undefined, lastDeclare: Node | undefined, lastAsync: Node | undefined, lastReadonly: Node | undefined, lastOverride: Node | undefined;
42428+
let lastStatic: Node | undefined, lastDeclare: Node | undefined, lastAsync: Node | undefined, lastReadonly: Node | undefined, lastOverride: Node | undefined, lastSealed: Node | undefined;
4242142429
let flags = ModifierFlags.None;
4242242430
for (const modifier of node.modifiers!) {
4242342431
if (modifier.kind !== SyntaxKind.ReadonlyKeyword) {
@@ -42559,6 +42567,19 @@ namespace ts {
4255942567

4256042568
flags |= ModifierFlags.Default;
4256142569
break;
42570+
case SyntaxKind.SealedKeyword:
42571+
if (flags & ModifierFlags.Sealed) {
42572+
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_already_seen, "sealed");
42573+
}
42574+
if (flags & ModifierFlags.Private) {
42575+
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "sealed", "private");
42576+
}
42577+
if (flags & ModifierFlags.Abstract) {
42578+
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "sealed", "abstract");
42579+
}
42580+
flags |= ModifierFlags.Sealed;
42581+
lastSealed = modifier;
42582+
break;
4256242583
case SyntaxKind.DeclareKeyword:
4256342584
if (flags & ModifierFlags.Ambient) {
4256442585
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_already_seen, "declare");
@@ -42655,6 +42676,9 @@ namespace ts {
4265542676
else if (flags & ModifierFlags.Readonly) {
4265642677
return grammarErrorOnNode(lastReadonly!, Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "readonly");
4265742678
}
42679+
if (flags & ModifierFlags.Sealed) {
42680+
return grammarErrorOnNode(lastSealed!, Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "sealed");
42681+
}
4265842682
return false;
4265942683
}
4266042684
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
@@ -3357,6 +3357,10 @@
33573357
"category": "Error",
33583358
"code": 2836
33593359
},
3360+
"Property '{0}' is sealed in type '{1}' that cannot be overridden in type '{2}'.": {
3361+
"category": "Error",
3362+
"code": 2837
3363+
},
33603364

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

src/compiler/factory/nodeFactory.ts

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2276,6 +2276,7 @@ namespace ts {
22762276
case SyntaxKind.PublicKeyword:
22772277
case SyntaxKind.PrivateKeyword:
22782278
case SyntaxKind.ProtectedKeyword:
2279+
case SyntaxKind.SealedKeyword:
22792280
case SyntaxKind.ReadonlyKeyword:
22802281
case SyntaxKind.DeclareKeyword:
22812282
case SyntaxKind.AbstractKeyword:

src/compiler/scanner.ts

Lines changed: 1 addition & 0 deletions
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

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

src/compiler/types.ts

Lines changed: 22 additions & 14 deletions
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
@@ -4977,15 +4983,17 @@ namespace ts {
49774983
ContainsProtected = 1 << 9, // Synthetic property with protected constituent(s)
49784984
ContainsPrivate = 1 << 10, // Synthetic property with private constituent(s)
49794985
ContainsStatic = 1 << 11, // Synthetic property with static constituent(s)
4980-
Late = 1 << 12, // Late-bound symbol for a computed property with a dynamic name
4981-
ReverseMapped = 1 << 13, // Property of reverse-inferred homomorphic mapped type
4982-
OptionalParameter = 1 << 14, // Optional parameter
4983-
RestParameter = 1 << 15, // Rest parameter
4984-
DeferredType = 1 << 16, // Calculation of the type of this symbol is deferred due to processing costs, should be fetched with `getTypeOfSymbolWithDeferredType`
4985-
HasNeverType = 1 << 17, // Synthetic property with at least one never type in constituents
4986-
Mapped = 1 << 18, // Property of mapped type
4987-
StripOptional = 1 << 19, // Strip optionality in mapped property
4988-
Unresolved = 1 << 20, // Unresolved type alias symbol
4986+
ContainsSealed = 1 << 12,
4987+
4988+
Late = 1 << 13, // Late-bound symbol for a computed property with a dynamic name
4989+
ReverseMapped = 1 << 14, // Property of reverse-inferred homomorphic mapped type
4990+
OptionalParameter = 1 << 15, // Optional parameter
4991+
RestParameter = 1 << 16, // Rest parameter
4992+
DeferredType = 1 << 17, // Calculation of the type of this symbol is deferred due to processing costs, should be fetched with `getTypeOfSymbolWithDeferredType`
4993+
HasNeverType = 1 << 18, // Synthetic property with at least one never type in constituents
4994+
Mapped = 1 << 19, // Property of mapped type
4995+
StripOptional = 1 << 20, // Strip optionality in mapped property
4996+
Unresolved = 1 << 21, // Unresolved type alias symbol
49894997
Synthetic = SyntheticProperty | SyntheticMethod,
49904998
Discriminant = HasNonUniformType | HasLiteralType,
49914999
Partial = ReadPartial | WritePartial

src/compiler/utilities.ts

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,7 @@ namespace FourSlashInterface {
11601160
case "private":
11611161
case "protected":
11621162
case "public":
1163+
case "sealed":
11631164
case "abstract":
11641165
case "any":
11651166
case "boolean":
@@ -1189,7 +1190,7 @@ namespace FourSlashInterface {
11891190
}
11901191

11911192
export const classElementKeywords: readonly ExpectedCompletionEntryObject[] =
1192-
["private", "protected", "public", "static", "abstract", "async", "constructor", "declare", "get", "readonly", "set", "override"].map(keywordEntry);
1193+
["private", "protected", "public", "static", "sealed", "abstract", "async", "constructor", "declare", "get", "readonly", "set", "override"].map(keywordEntry);
11931194

11941195
export const classElementInJsKeywords = getInJsKeywords(classElementKeywords);
11951196

src/services/completions.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2781,6 +2781,9 @@ namespace ts.Completions {
27812781
case "private":
27822782
classElementModifierFlags = classElementModifierFlags | ModifierFlags.Private;
27832783
break;
2784+
case "sealed":
2785+
classElementModifierFlags = classElementModifierFlags | ModifierFlags.Sealed;
2786+
break;
27842787
case "static":
27852788
classElementModifierFlags = classElementModifierFlags | ModifierFlags.Static;
27862789
break;
@@ -2794,7 +2797,7 @@ namespace ts.Completions {
27942797
}
27952798

27962799
// No member list for private methods
2797-
if (!(classElementModifierFlags & ModifierFlags.Private)) {
2800+
if (!(classElementModifierFlags & (ModifierFlags.Private | ModifierFlags.Sealed))) {
27982801
// List of property symbols of base type that are not private and already implemented
27992802
const baseTypeNodes = isClassLike(decl) && classElementModifierFlags & ModifierFlags.Override ? singleElementArray(getEffectiveBaseTypeNode(decl)) : getAllSuperTypeNodes(decl);
28002803
const baseSymbols = flatMap(baseTypeNodes, baseTypeNode => {
@@ -3069,6 +3072,7 @@ namespace ts.Completions {
30693072
case SyntaxKind.InterfaceKeyword:
30703073
case SyntaxKind.LetKeyword:
30713074
case SyntaxKind.PrivateKeyword:
3075+
case SyntaxKind.SealedKeyword:
30723076
case SyntaxKind.ProtectedKeyword:
30733077
case SyntaxKind.PublicKeyword:
30743078
case SyntaxKind.StaticKeyword:
@@ -3461,6 +3465,7 @@ namespace ts.Completions {
34613465
case SyntaxKind.PrivateKeyword:
34623466
case SyntaxKind.ProtectedKeyword:
34633467
case SyntaxKind.PublicKeyword:
3468+
case SyntaxKind.SealedKeyword:
34643469
case SyntaxKind.ReadonlyKeyword:
34653470
case SyntaxKind.StringKeyword:
34663471
case SyntaxKind.SymbolKeyword:

0 commit comments

Comments
 (0)