Skip to content

Commit

Permalink
feat(1534): add sealed keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk committed Dec 1, 2021
1 parent 670ad45 commit a1102c9
Show file tree
Hide file tree
Showing 57 changed files with 1,097 additions and 520 deletions.
26 changes: 25 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12107,6 +12107,7 @@ namespace ts {
checkFlags |= (!(modifiers & ModifierFlags.NonPublicAccessibilityModifier) ? CheckFlags.ContainsPublic : 0) |
(modifiers & ModifierFlags.Protected ? CheckFlags.ContainsProtected : 0) |
(modifiers & ModifierFlags.Private ? CheckFlags.ContainsPrivate : 0) |
(modifiers & ModifierFlags.Sealed ? CheckFlags.ContainsSealed : 0) |
(modifiers & ModifierFlags.Static ? CheckFlags.ContainsStatic : 0);
if (!isPrototypeProperty(prop)) {
syntheticFlag = CheckFlags.SyntheticProperty;
Expand Down Expand Up @@ -19587,6 +19588,13 @@ namespace ts {
return Ternary.False;
}
}
else if (targetPropFlags & ModifierFlags.Sealed) {
if (reportErrors) {
reportError(Diagnostics.Property_0_is_sealed_in_type_1_that_cannot_be_overridden_in_type_2, symbolToString(targetProp),
typeToString(target), typeToString(source));
}
return Ternary.False;
}
else if (targetPropFlags & ModifierFlags.Protected) {
if (!isValidOverrideOf(sourceProp, targetProp)) {
if (reportErrors) {
Expand Down Expand Up @@ -42417,7 +42425,7 @@ namespace ts {
return quickResult;
}

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

flags |= ModifierFlags.Default;
break;
case SyntaxKind.SealedKeyword:
if (flags & ModifierFlags.Sealed) {
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_already_seen, "sealed");
}
if (flags & ModifierFlags.Private) {
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "sealed", "private");
}
if (flags & ModifierFlags.Abstract) {
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "sealed", "abstract");
}
flags |= ModifierFlags.Sealed;
lastSealed = modifier;
break;
case SyntaxKind.DeclareKeyword:
if (flags & ModifierFlags.Ambient) {
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_already_seen, "declare");
Expand Down Expand Up @@ -42655,6 +42676,9 @@ namespace ts {
else if (flags & ModifierFlags.Readonly) {
return grammarErrorOnNode(lastReadonly!, Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "readonly");
}
if (flags & ModifierFlags.Sealed) {
return grammarErrorOnNode(lastSealed!, Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "sealed");
}
return false;
}
else if ((node.kind === SyntaxKind.ImportDeclaration || node.kind === SyntaxKind.ImportEqualsDeclaration) && flags & ModifierFlags.Ambient) {
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3357,6 +3357,10 @@
"category": "Error",
"code": 2836
},
"Property '{0}' is sealed in type '{1}' that cannot be overridden in type '{2}'.": {
"category": "Error",
"code": 2837
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/factory/nodeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,7 @@ namespace ts {
case SyntaxKind.PublicKeyword:
case SyntaxKind.PrivateKeyword:
case SyntaxKind.ProtectedKeyword:
case SyntaxKind.SealedKeyword:
case SyntaxKind.ReadonlyKeyword:
case SyntaxKind.AbstractKeyword:
case SyntaxKind.DeclareKeyword:
Expand Down Expand Up @@ -1070,6 +1071,7 @@ namespace ts {
if (flags & ModifierFlags.Public) result.push(createModifier(SyntaxKind.PublicKeyword));
if (flags & ModifierFlags.Private) result.push(createModifier(SyntaxKind.PrivateKeyword));
if (flags & ModifierFlags.Protected) result.push(createModifier(SyntaxKind.ProtectedKeyword));
if (flags & ModifierFlags.Sealed) result.push(createModifier(SyntaxKind.SealedKeyword));
if (flags & ModifierFlags.Abstract) result.push(createModifier(SyntaxKind.AbstractKeyword));
if (flags & ModifierFlags.Static) result.push(createModifier(SyntaxKind.StaticKeyword));
if (flags & ModifierFlags.Override) result.push(createModifier(SyntaxKind.OverrideKeyword));
Expand Down
1 change: 1 addition & 0 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2276,6 +2276,7 @@ namespace ts {
case SyntaxKind.PublicKeyword:
case SyntaxKind.PrivateKeyword:
case SyntaxKind.ProtectedKeyword:
case SyntaxKind.SealedKeyword:
case SyntaxKind.ReadonlyKeyword:
case SyntaxKind.DeclareKeyword:
case SyntaxKind.AbstractKeyword:
Expand Down
1 change: 1 addition & 0 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ namespace ts {
private: SyntaxKind.PrivateKeyword,
protected: SyntaxKind.ProtectedKeyword,
public: SyntaxKind.PublicKeyword,
sealed: SyntaxKind.SealedKeyword,
override: SyntaxKind.OverrideKeyword,
readonly: SyntaxKind.ReadonlyKeyword,
require: SyntaxKind.RequireKeyword,
Expand Down
1 change: 1 addition & 0 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ namespace ts {
case SyntaxKind.PublicKeyword:
case SyntaxKind.PrivateKeyword:
case SyntaxKind.ProtectedKeyword:
case SyntaxKind.SealedKeyword:
case SyntaxKind.AbstractKeyword:
case SyntaxKind.OverrideKeyword:
case SyntaxKind.ConstKeyword:
Expand Down
36 changes: 22 additions & 14 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ namespace ts {
ProtectedKeyword,
PublicKeyword,
StaticKeyword,
SealedKeyword,
YieldKeyword,
// Contextual keywords
AbstractKeyword,
Expand Down Expand Up @@ -598,6 +599,7 @@ namespace ts {
| SyntaxKind.PrivateKeyword
| SyntaxKind.ProtectedKeyword
| SyntaxKind.PublicKeyword
| SyntaxKind.SealedKeyword
| SyntaxKind.ReadonlyKeyword
| SyntaxKind.OverrideKeyword
| SyntaxKind.RequireKeyword
Expand Down Expand Up @@ -634,6 +636,7 @@ namespace ts {
| SyntaxKind.PrivateKeyword
| SyntaxKind.ProtectedKeyword
| SyntaxKind.PublicKeyword
| SyntaxKind.SealedKeyword
| SyntaxKind.ReadonlyKeyword
| SyntaxKind.OverrideKeyword
| SyntaxKind.StaticKeyword
Expand Down Expand Up @@ -806,9 +809,10 @@ namespace ts {
Protected = 1 << 4, // Property/Method
Static = 1 << 5, // Property/Method
Readonly = 1 << 6, // Property/Method
Abstract = 1 << 7, // Class/Method/ConstructSignature
Async = 1 << 8, // Property/Method/Function
Default = 1 << 9, // Function/Class (export default declaration)
Sealed = 1 << 7, // Property/Method
Abstract = 1 << 8, // Class/Method/ConstructSignature
Async = 1 << 9, // Property/Method/Function
Default = 1 << 10, // Function/Class (export default declaration)
Const = 1 << 11, // Const enum
HasComputedJSDocModifiers = 1 << 12, // Indicates the computed modifier flags include modifiers from JSDoc.

Expand All @@ -821,9 +825,9 @@ namespace ts {
ParameterPropertyModifier = AccessibilityModifier | Readonly | Override,
NonPublicAccessibilityModifier = Private | Protected,

TypeScriptModifier = Ambient | Public | Private | Protected | Readonly | Abstract | Const | Override,
TypeScriptModifier = Ambient | Public | Private | Protected | Sealed | Readonly | Abstract | Const | Override,
ExportDefault = Export | Default,
All = Export | Ambient | Public | Private | Protected | Static | Readonly | Abstract | Async | Default | Const | Deprecated | Override
All = Export | Ambient | Public | Private | Protected | Sealed | Static | Readonly | Abstract | Async | Default | Const | Deprecated | Override
}

export const enum JsxFlags {
Expand Down Expand Up @@ -1064,6 +1068,7 @@ namespace ts {
export type PrivateKeyword = ModifierToken<SyntaxKind.PrivateKeyword>;
export type ProtectedKeyword = ModifierToken<SyntaxKind.ProtectedKeyword>;
export type PublicKeyword = ModifierToken<SyntaxKind.PublicKeyword>;
export type SealedKeyword = ModifierToken<SyntaxKind.SealedKeyword>;
export type ReadonlyKeyword = ModifierToken<SyntaxKind.ReadonlyKeyword>;
export type OverrideKeyword = ModifierToken<SyntaxKind.OverrideKeyword>;
export type StaticKeyword = ModifierToken<SyntaxKind.StaticKeyword>;
Expand All @@ -1081,6 +1086,7 @@ namespace ts {
| PrivateKeyword
| ProtectedKeyword
| PublicKeyword
| SealedKeyword
| OverrideKeyword
| ReadonlyKeyword
| StaticKeyword
Expand Down Expand Up @@ -4977,15 +4983,17 @@ namespace ts {
ContainsProtected = 1 << 9, // Synthetic property with protected constituent(s)
ContainsPrivate = 1 << 10, // Synthetic property with private constituent(s)
ContainsStatic = 1 << 11, // Synthetic property with static constituent(s)
Late = 1 << 12, // Late-bound symbol for a computed property with a dynamic name
ReverseMapped = 1 << 13, // Property of reverse-inferred homomorphic mapped type
OptionalParameter = 1 << 14, // Optional parameter
RestParameter = 1 << 15, // Rest parameter
DeferredType = 1 << 16, // Calculation of the type of this symbol is deferred due to processing costs, should be fetched with `getTypeOfSymbolWithDeferredType`
HasNeverType = 1 << 17, // Synthetic property with at least one never type in constituents
Mapped = 1 << 18, // Property of mapped type
StripOptional = 1 << 19, // Strip optionality in mapped property
Unresolved = 1 << 20, // Unresolved type alias symbol
ContainsSealed = 1 << 12,

Late = 1 << 13, // Late-bound symbol for a computed property with a dynamic name
ReverseMapped = 1 << 14, // Property of reverse-inferred homomorphic mapped type
OptionalParameter = 1 << 15, // Optional parameter
RestParameter = 1 << 16, // Rest parameter
DeferredType = 1 << 17, // Calculation of the type of this symbol is deferred due to processing costs, should be fetched with `getTypeOfSymbolWithDeferredType`
HasNeverType = 1 << 18, // Synthetic property with at least one never type in constituents
Mapped = 1 << 19, // Property of mapped type
StripOptional = 1 << 20, // Strip optionality in mapped property
Unresolved = 1 << 21, // Unresolved type alias symbol
Synthetic = SyntheticProperty | SyntheticMethod,
Discriminant = HasNonUniformType | HasLiteralType,
Partial = ReadPartial | WritePartial
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4913,6 +4913,7 @@ namespace ts {
case SyntaxKind.PublicKeyword: return ModifierFlags.Public;
case SyntaxKind.ProtectedKeyword: return ModifierFlags.Protected;
case SyntaxKind.PrivateKeyword: return ModifierFlags.Private;
case SyntaxKind.SealedKeyword: return ModifierFlags.Sealed;
case SyntaxKind.AbstractKeyword: return ModifierFlags.Abstract;
case SyntaxKind.ExportKeyword: return ModifierFlags.Export;
case SyntaxKind.DeclareKeyword: return ModifierFlags.Ambient;
Expand Down Expand Up @@ -5451,6 +5452,7 @@ namespace ts {
const checkFlags = (s as TransientSymbol).checkFlags;
const accessModifier = checkFlags & CheckFlags.ContainsPrivate ? ModifierFlags.Private :
checkFlags & CheckFlags.ContainsPublic ? ModifierFlags.Public :
checkFlags & CheckFlags.ContainsSealed ? ModifierFlags.Sealed :
ModifierFlags.Protected;
const staticModifier = checkFlags & CheckFlags.ContainsStatic ? ModifierFlags.Static : 0;
return accessModifier | staticModifier;
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/utilitiesPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,7 @@ namespace ts {
case SyntaxKind.PublicKeyword:
case SyntaxKind.PrivateKeyword:
case SyntaxKind.ProtectedKeyword:
case SyntaxKind.SealedKeyword:
case SyntaxKind.ReadonlyKeyword:
case SyntaxKind.StaticKeyword:
case SyntaxKind.OverrideKeyword:
Expand All @@ -1198,7 +1199,7 @@ namespace ts {

/* @internal */
export function isClassMemberModifier(idToken: SyntaxKind): boolean {
return isParameterPropertyModifier(idToken) || idToken === SyntaxKind.StaticKeyword || idToken === SyntaxKind.OverrideKeyword;
return isParameterPropertyModifier(idToken) || idToken === SyntaxKind.SealedKeyword || idToken === SyntaxKind.StaticKeyword || idToken === SyntaxKind.OverrideKeyword;
}

export function isModifier(node: Node): node is Modifier {
Expand Down
3 changes: 2 additions & 1 deletion src/harness/fourslashInterfaceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,7 @@ namespace FourSlashInterface {
case "private":
case "protected":
case "public":
case "sealed":
case "abstract":
case "any":
case "boolean":
Expand Down Expand Up @@ -1189,7 +1190,7 @@ namespace FourSlashInterface {
}

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

export const classElementInJsKeywords = getInJsKeywords(classElementKeywords);

Expand Down
7 changes: 6 additions & 1 deletion src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2781,6 +2781,9 @@ namespace ts.Completions {
case "private":
classElementModifierFlags = classElementModifierFlags | ModifierFlags.Private;
break;
case "sealed":
classElementModifierFlags = classElementModifierFlags | ModifierFlags.Sealed;
break;
case "static":
classElementModifierFlags = classElementModifierFlags | ModifierFlags.Static;
break;
Expand All @@ -2794,7 +2797,7 @@ namespace ts.Completions {
}

// No member list for private methods
if (!(classElementModifierFlags & ModifierFlags.Private)) {
if (!(classElementModifierFlags & (ModifierFlags.Private | ModifierFlags.Sealed))) {
// List of property symbols of base type that are not private and already implemented
const baseTypeNodes = isClassLike(decl) && classElementModifierFlags & ModifierFlags.Override ? singleElementArray(getEffectiveBaseTypeNode(decl)) : getAllSuperTypeNodes(decl);
const baseSymbols = flatMap(baseTypeNodes, baseTypeNode => {
Expand Down Expand Up @@ -3069,6 +3072,7 @@ namespace ts.Completions {
case SyntaxKind.InterfaceKeyword:
case SyntaxKind.LetKeyword:
case SyntaxKind.PrivateKeyword:
case SyntaxKind.SealedKeyword:
case SyntaxKind.ProtectedKeyword:
case SyntaxKind.PublicKeyword:
case SyntaxKind.StaticKeyword:
Expand Down Expand Up @@ -3461,6 +3465,7 @@ namespace ts.Completions {
case SyntaxKind.PrivateKeyword:
case SyntaxKind.ProtectedKeyword:
case SyntaxKind.PublicKeyword:
case SyntaxKind.SealedKeyword:
case SyntaxKind.ReadonlyKeyword:
case SyntaxKind.StringKeyword:
case SyntaxKind.SymbolKeyword:
Expand Down
Loading

0 comments on commit a1102c9

Please sign in to comment.