Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add override keyword #13217

Closed
wants to merge 10 commits into from
Closed
1 change: 1 addition & 0 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3332,6 +3332,7 @@ namespace ts {
case SyntaxKind.TypeAssertionExpression:
case SyntaxKind.AsExpression:
case SyntaxKind.NonNullExpression:
case SyntaxKind.OverrideKeyword:
case SyntaxKind.ReadonlyKeyword:
// These nodes are TypeScript syntax.
transformFlags |= TransformFlags.AssertTypeScript;
Expand Down
184 changes: 176 additions & 8 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@ namespace ts {
category: Diagnostics.Strict_Type_Checking_Options,
description: Diagnostics.Raise_error_on_expressions_and_declarations_with_an_implied_any_type,
},
{
name: "noImplicitOverride",
type: "boolean",
showInSimplifiedHelpView: true,
category: Diagnostics.Strict_Type_Checking_Options,
description: Diagnostics.Raise_error_when_inherited_Slashoverridden_class_members_are_not_explicitly_marked_override,
},
{
name: "strictNullChecks",
type: "boolean",
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/declarationEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,9 @@ namespace ts {
if (flags & ModifierFlags.Static) {
write("static ");
}
if (flags & ModifierFlags.Override) {
write("override ");
}
if (flags & ModifierFlags.Readonly) {
write("readonly ");
}
Expand Down
31 changes: 29 additions & 2 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@
"category": "Error",
"code": 1241
},
"'abstract' modifier can only appear on a class, method, or property declaration.": {
"'{0}' modifier can only appear on a class, method, or property declaration.": {
"category": "Error",
"code": 1242
},
Expand Down Expand Up @@ -1964,6 +1964,34 @@
"category": "Error",
"code": 2565
},
"Overload signatures must all be override or non-override.": {
"category": "Error",
"code": 2566
},
"Accessors must both be override or non-override.": {
"category": "Error",
"code": 2567
},
"Raise error when inherited/overridden class members are not explicitly marked 'override'": {
"category": "Error",
"code": 2568
},
"Class member '{0}' was marked 'override', but no matching declaration was found in any supertype of '{1}'": {
"category": "Error",
"code": 2569
},
"Class member '{0}' must be marked 'override' when noImplicitOverride is enabled (inherited from {1})": {
"category": "Error",
"code": 2570
},
"Override modifier cannot be used with an optional property declaration": {
"category": "Error",
"code": 2572
},
"All declarations of an override method must be consecutive.": {
"category": "Error",
"code": 2573
},
"JSX element attributes type '{0}' may not be a union type.": {
"category": "Error",
"code": 2600
Expand Down Expand Up @@ -3786,7 +3814,6 @@
"category": "Message",
"code": 90017
},

"Disable checking for this file.": {
"category": "Message",
"code": 90018
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4934,6 +4934,7 @@ namespace ts {
case SyntaxKind.PrivateKeyword:
case SyntaxKind.ProtectedKeyword:
case SyntaxKind.PublicKeyword:
case SyntaxKind.OverrideKeyword:
case SyntaxKind.ReadonlyKeyword:
nextToken();
// ASI takes effect for this modifier.
Expand Down Expand Up @@ -5020,6 +5021,7 @@ namespace ts {
case SyntaxKind.PublicKeyword:
case SyntaxKind.PrivateKeyword:
case SyntaxKind.ProtectedKeyword:
case SyntaxKind.OverrideKeyword:
case SyntaxKind.StaticKeyword:
case SyntaxKind.ReadonlyKeyword:
// When these don't start a declaration, they may be the start of a class member if an identifier
Expand Down Expand Up @@ -5101,6 +5103,7 @@ namespace ts {
case SyntaxKind.PrivateKeyword:
case SyntaxKind.ProtectedKeyword:
case SyntaxKind.PublicKeyword:
case SyntaxKind.OverrideKeyword:
case SyntaxKind.AbstractKeyword:
case SyntaxKind.StaticKeyword:
case SyntaxKind.ReadonlyKeyword:
Expand Down Expand Up @@ -5403,6 +5406,7 @@ namespace ts {
case SyntaxKind.PublicKeyword:
case SyntaxKind.PrivateKeyword:
case SyntaxKind.ProtectedKeyword:
case SyntaxKind.OverrideKeyword:
case SyntaxKind.StaticKeyword:
case SyntaxKind.ReadonlyKeyword:
return true;
Expand Down
1 change: 1 addition & 0 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,7 @@ namespace ts {
case SyntaxKind.PublicKeyword:
case SyntaxKind.PrivateKeyword:
case SyntaxKind.ProtectedKeyword:
case SyntaxKind.OverrideKeyword:
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 @@ -104,6 +104,7 @@ namespace ts {
"null": SyntaxKind.NullKeyword,
"number": SyntaxKind.NumberKeyword,
"object": SyntaxKind.ObjectKeyword,
"override": SyntaxKind.OverrideKeyword,
"package": SyntaxKind.PackageKeyword,
"private": SyntaxKind.PrivateKeyword,
"protected": SyntaxKind.ProtectedKeyword,
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 @@ -365,6 +365,7 @@ namespace ts {
case SyntaxKind.ConstKeyword:
case SyntaxKind.DeclareKeyword:
case SyntaxKind.ReadonlyKeyword:
case SyntaxKind.OverrideKeyword:
// TypeScript accessibility and readonly modifiers are elided.

case SyntaxKind.ArrayType:
Expand Down
8 changes: 6 additions & 2 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ namespace ts {
UniqueKeyword,
FromKeyword,
GlobalKeyword,
OverrideKeyword,
OfKeyword, // LastKeyword and LastToken and LastContextualKeyword

// Parse tree nodes
Expand Down Expand Up @@ -497,15 +498,16 @@ namespace ts {
Abstract = 1 << 7, // Class/Method/ConstructSignature
Async = 1 << 8, // Property/Method/Function
Default = 1 << 9, // Function/Class (export default declaration)
Override = 1 << 10, // Property/Method
Const = 1 << 11, // Variable declaration
HasComputedFlags = 1 << 29, // Modifier flags have been computed

AccessibilityModifier = Public | Private | Protected,
// Accessibility modifiers and 'readonly' can be attached to a parameter in a constructor to make it a property.
ParameterPropertyModifier = AccessibilityModifier | Readonly,
ParameterPropertyModifier = AccessibilityModifier | Readonly | Override,
NonPublicAccessibilityModifier = Private | Protected,

TypeScriptModifier = Ambient | Public | Private | Protected | Readonly | Abstract | Const,
TypeScriptModifier = Ambient | Public | Private | Protected | Override | Readonly | Abstract | Const,
ExportDefault = Export | Default,
}

Expand Down Expand Up @@ -618,6 +620,7 @@ namespace ts {
| Token<SyntaxKind.PublicKeyword>
| Token<SyntaxKind.PrivateKeyword>
| Token<SyntaxKind.ProtectedKeyword>
| Token<SyntaxKind.OverrideKeyword>
| Token<SyntaxKind.ReadonlyKeyword>
| Token<SyntaxKind.StaticKeyword>
;
Expand Down Expand Up @@ -3836,6 +3839,7 @@ namespace ts {
noEmitOnError?: boolean;
noErrorTruncation?: boolean;
noFallthroughCasesInSwitch?: boolean;
noImplicitOverride?: boolean; // Always combine with strict property (?)
noImplicitAny?: boolean; // Always combine with strict property
noImplicitReturns?: boolean;
noImplicitThis?: boolean; // Always combine with strict property
Expand Down
4 changes: 3 additions & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ namespace ts {
// construct.
return getSpanOfTokenAtPosition(sourceFile, node.pos);
}

const pos = nodeIsMissing(errorNode)
? errorNode.pos
: skipTrivia(sourceFile.text, errorNode.pos);
Expand Down Expand Up @@ -3145,6 +3145,7 @@ namespace ts {
case SyntaxKind.DefaultKeyword: return ModifierFlags.Default;
case SyntaxKind.AsyncKeyword: return ModifierFlags.Async;
case SyntaxKind.ReadonlyKeyword: return ModifierFlags.Readonly;
case SyntaxKind.OverrideKeyword: return ModifierFlags.Override;
}
return ModifierFlags.None;
}
Expand Down Expand Up @@ -5113,6 +5114,7 @@ namespace ts {
case SyntaxKind.PrivateKeyword:
case SyntaxKind.ProtectedKeyword:
case SyntaxKind.ReadonlyKeyword:
case SyntaxKind.OverrideKeyword:
case SyntaxKind.StaticKeyword:
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions src/harness/unittests/transpile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,10 @@ var x = 0;`, {
options: { compilerOptions: { noImplicitAny: true }, fileName: "input.js", reportDiagnostics: true }
});

transpilesCorrectly("Supports setting 'noImplicitOverride'", "x;", {
options: { compilerOptions: { noImplicitOverride: true }, fileName: "input.js", reportDiagnostics: true }
});

transpilesCorrectly("Supports setting 'noImplicitReturns'", "x;", {
options: { compilerOptions: { noImplicitReturns: true }, fileName: "input.js", reportDiagnostics: true }
});
Expand Down
1 change: 1 addition & 0 deletions src/server/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2518,6 +2518,7 @@ namespace ts.server.protocol {
noErrorTruncation?: boolean;
noFallthroughCasesInSwitch?: boolean;
noImplicitAny?: boolean;
noImplicitOverride?: boolean;
noImplicitReturns?: boolean;
noImplicitThis?: boolean;
noUnusedLocals?: boolean;
Expand Down
Loading