Skip to content

Commit 4836ea7

Browse files
committed
feat(1534): add sealed keyword
1 parent 52e785f commit 4836ea7

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12158,6 +12158,7 @@ namespace ts {
1215812158
checkFlags |= (!(modifiers & ModifierFlags.NonPublicAccessibilityModifier) ? CheckFlags.ContainsPublic : 0) |
1215912159
(modifiers & ModifierFlags.Protected ? CheckFlags.ContainsProtected : 0) |
1216012160
(modifiers & ModifierFlags.Private ? CheckFlags.ContainsPrivate : 0) |
12161+
(modifiers & ModifierFlags.Sealed ? CheckFlags.ContainsSealed : 0) |
1216112162
(modifiers & ModifierFlags.Static ? CheckFlags.ContainsStatic : 0);
1216212163
if (!isPrototypeProperty(prop)) {
1216312164
syntheticFlag = CheckFlags.SyntheticProperty;
@@ -19648,6 +19649,13 @@ namespace ts {
1964819649
return Ternary.False;
1964919650
}
1965019651
}
19652+
else if (targetPropFlags & ModifierFlags.Sealed) {
19653+
if (reportErrors) {
19654+
reportError(Diagnostics.Property_0_is_sealed_in_type_1_that_cannot_be_overridden_in_type_2, symbolToString(targetProp),
19655+
typeToString(target), typeToString(source));
19656+
}
19657+
return Ternary.False;
19658+
}
1965119659
else if (targetPropFlags & ModifierFlags.Protected) {
1965219660
if (!isValidOverrideOf(sourceProp, targetProp)) {
1965319661
if (reportErrors) {
@@ -42589,7 +42597,7 @@ namespace ts {
4258942597
return quickResult;
4259042598
}
4259142599

42592-
let lastStatic: Node | undefined, lastDeclare: Node | undefined, lastAsync: Node | undefined, lastReadonly: Node | undefined, lastOverride: Node | undefined;
42600+
let lastStatic: Node | undefined, lastDeclare: Node | undefined, lastAsync: Node | undefined, lastReadonly: Node | undefined, lastOverride: Node | undefined, lastSealed: Node | undefined;
4259342601
let flags = ModifierFlags.None;
4259442602
for (const modifier of node.modifiers!) {
4259542603
if (modifier.kind !== SyntaxKind.ReadonlyKeyword) {
@@ -42731,6 +42739,19 @@ namespace ts {
4273142739

4273242740
flags |= ModifierFlags.Default;
4273342741
break;
42742+
case SyntaxKind.SealedKeyword:
42743+
if (flags & ModifierFlags.Sealed) {
42744+
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_already_seen, "sealed");
42745+
}
42746+
if (flags & ModifierFlags.Private) {
42747+
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "sealed", "private");
42748+
}
42749+
if (flags & ModifierFlags.Abstract) {
42750+
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "sealed", "abstract");
42751+
}
42752+
flags |= ModifierFlags.Sealed;
42753+
lastSealed = modifier;
42754+
break;
4273442755
case SyntaxKind.DeclareKeyword:
4273542756
if (flags & ModifierFlags.Ambient) {
4273642757
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_already_seen, "declare");
@@ -42827,6 +42848,9 @@ namespace ts {
4282742848
else if (flags & ModifierFlags.Readonly) {
4282842849
return grammarErrorOnNode(lastReadonly!, Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "readonly");
4282942850
}
42851+
if (flags & ModifierFlags.Sealed) {
42852+
return grammarErrorOnNode(lastSealed!, Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "sealed");
42853+
}
4283042854
return false;
4283142855
}
4283242856
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
@@ -3373,6 +3373,10 @@
33733373
"category": "Error",
33743374
"code": 2837
33753375
},
3376+
"Property '{0}' is sealed in type '{1}' that cannot be overridden in type '{2}'.": {
3377+
"category": "Error",
3378+
"code": 2838
3379+
},
33763380

33773381
"Import declaration '{0}' is using private name '{1}'.": {
33783382
"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
@@ -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

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 {
@@ -1065,6 +1069,7 @@ namespace ts {
10651069
export type PrivateKeyword = ModifierToken<SyntaxKind.PrivateKeyword>;
10661070
export type ProtectedKeyword = ModifierToken<SyntaxKind.ProtectedKeyword>;
10671071
export type PublicKeyword = ModifierToken<SyntaxKind.PublicKeyword>;
1072+
export type SealedKeyword = ModifierToken<SyntaxKind.SealedKeyword>;
10681073
export type ReadonlyKeyword = ModifierToken<SyntaxKind.ReadonlyKeyword>;
10691074
export type OverrideKeyword = ModifierToken<SyntaxKind.OverrideKeyword>;
10701075
export type StaticKeyword = ModifierToken<SyntaxKind.StaticKeyword>;
@@ -1082,6 +1087,7 @@ namespace ts {
10821087
| PrivateKeyword
10831088
| ProtectedKeyword
10841089
| PublicKeyword
1090+
| SealedKeyword
10851091
| OverrideKeyword
10861092
| ReadonlyKeyword
10871093
| StaticKeyword
@@ -4980,15 +4986,17 @@ namespace ts {
49804986
ContainsProtected = 1 << 9, // Synthetic property with protected constituent(s)
49814987
ContainsPrivate = 1 << 10, // Synthetic property with private constituent(s)
49824988
ContainsStatic = 1 << 11, // Synthetic property with static constituent(s)
4983-
Late = 1 << 12, // Late-bound symbol for a computed property with a dynamic name
4984-
ReverseMapped = 1 << 13, // Property of reverse-inferred homomorphic mapped type
4985-
OptionalParameter = 1 << 14, // Optional parameter
4986-
RestParameter = 1 << 15, // Rest parameter
4987-
DeferredType = 1 << 16, // Calculation of the type of this symbol is deferred due to processing costs, should be fetched with `getTypeOfSymbolWithDeferredType`
4988-
HasNeverType = 1 << 17, // Synthetic property with at least one never type in constituents
4989-
Mapped = 1 << 18, // Property of mapped type
4990-
StripOptional = 1 << 19, // Strip optionality in mapped property
4991-
Unresolved = 1 << 20, // Unresolved type alias symbol
4989+
ContainsSealed = 1 << 12,
4990+
4991+
Late = 1 << 13, // Late-bound symbol for a computed property with a dynamic name
4992+
ReverseMapped = 1 << 14, // Property of reverse-inferred homomorphic mapped type
4993+
OptionalParameter = 1 << 15, // Optional parameter
4994+
RestParameter = 1 << 16, // Rest parameter
4995+
DeferredType = 1 << 17, // Calculation of the type of this symbol is deferred due to processing costs, should be fetched with `getTypeOfSymbolWithDeferredType`
4996+
HasNeverType = 1 << 18, // Synthetic property with at least one never type in constituents
4997+
Mapped = 1 << 19, // Property of mapped type
4998+
StripOptional = 1 << 20, // Strip optionality in mapped property
4999+
Unresolved = 1 << 21, // Unresolved type alias symbol
49925000
Synthetic = SyntheticProperty | SyntheticMethod,
49935001
Discriminant = HasNonUniformType | HasLiteralType,
49945002
Partial = ReadPartial | WritePartial

src/compiler/utilities.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4968,6 +4968,7 @@ namespace ts {
49684968
case SyntaxKind.PublicKeyword: return ModifierFlags.Public;
49694969
case SyntaxKind.ProtectedKeyword: return ModifierFlags.Protected;
49704970
case SyntaxKind.PrivateKeyword: return ModifierFlags.Private;
4971+
case SyntaxKind.SealedKeyword: return ModifierFlags.Sealed;
49714972
case SyntaxKind.AbstractKeyword: return ModifierFlags.Abstract;
49724973
case SyntaxKind.ExportKeyword: return ModifierFlags.Export;
49734974
case SyntaxKind.DeclareKeyword: return ModifierFlags.Ambient;
@@ -5506,6 +5507,7 @@ namespace ts {
55065507
const checkFlags = (s as TransientSymbol).checkFlags;
55075508
const accessModifier = checkFlags & CheckFlags.ContainsPrivate ? ModifierFlags.Private :
55085509
checkFlags & CheckFlags.ContainsPublic ? ModifierFlags.Public :
5510+
checkFlags & CheckFlags.ContainsSealed ? ModifierFlags.Sealed :
55095511
ModifierFlags.Protected;
55105512
const staticModifier = checkFlags & CheckFlags.ContainsStatic ? ModifierFlags.Static : 0;
55115513
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 & 0 deletions
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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2908,6 +2908,9 @@ namespace ts.Completions {
29082908
case "private":
29092909
classElementModifierFlags = classElementModifierFlags | ModifierFlags.Private;
29102910
break;
2911+
case "sealed":
2912+
classElementModifierFlags = classElementModifierFlags | ModifierFlags.Sealed;
2913+
break;
29112914
case "static":
29122915
classElementModifierFlags = classElementModifierFlags | ModifierFlags.Static;
29132916
break;
@@ -2921,7 +2924,7 @@ namespace ts.Completions {
29212924
}
29222925

29232926
// No member list for private methods
2924-
if (!(classElementModifierFlags & ModifierFlags.Private)) {
2927+
if (!(classElementModifierFlags & (ModifierFlags.Private | ModifierFlags.Sealed))) {
29252928
// List of property symbols of base type that are not private and already implemented
29262929
const baseTypeNodes = isClassLike(decl) && classElementModifierFlags & ModifierFlags.Override ? singleElementArray(getEffectiveBaseTypeNode(decl)) : getAllSuperTypeNodes(decl);
29272930
const baseSymbols = flatMap(baseTypeNodes, baseTypeNode => {
@@ -3196,6 +3199,7 @@ namespace ts.Completions {
31963199
case SyntaxKind.InterfaceKeyword:
31973200
case SyntaxKind.LetKeyword:
31983201
case SyntaxKind.PrivateKeyword:
3202+
case SyntaxKind.SealedKeyword:
31993203
case SyntaxKind.ProtectedKeyword:
32003204
case SyntaxKind.PublicKeyword:
32013205
case SyntaxKind.StaticKeyword:
@@ -3588,6 +3592,7 @@ namespace ts.Completions {
35883592
case SyntaxKind.PrivateKeyword:
35893593
case SyntaxKind.ProtectedKeyword:
35903594
case SyntaxKind.PublicKeyword:
3595+
case SyntaxKind.SealedKeyword:
35913596
case SyntaxKind.ReadonlyKeyword:
35923597
case SyntaxKind.StringKeyword:
35933598
case SyntaxKind.SymbolKeyword:

0 commit comments

Comments
 (0)