Skip to content

Commit db01e84

Browse files
authored
feat(eslint): consistent-type-assertions (microsoft#43556)
1 parent fc07ee2 commit db01e84

File tree

115 files changed

+2680
-2679
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+2680
-2679
lines changed

.eslintrc.json

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
],
3535

3636
"@typescript-eslint/consistent-type-definitions": ["error", "interface"],
37+
"@typescript-eslint/consistent-type-assertions": ["error", { "assertionStyle": "as" }],
3738

3839
"no-duplicate-imports": "off",
3940
"@typescript-eslint/no-duplicate-imports": "error",

scripts/buildProtocol.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class DeclarationsWalker {
4747
}
4848
if (s.name === "Array" || s.name === "ReadOnlyArray") {
4949
// we should process type argument instead
50-
return this.processType((<any>type).typeArguments[0]);
50+
return this.processType((type as any).typeArguments[0]);
5151
}
5252
else {
5353
const declarations = s.getDeclarations();
@@ -84,12 +84,12 @@ class DeclarationsWalker {
8484
case ts.SyntaxKind.PropertySignature:
8585
case ts.SyntaxKind.Parameter:
8686
case ts.SyntaxKind.IndexSignature:
87-
if (((<ts.VariableDeclaration | ts.MethodDeclaration | ts.PropertyDeclaration | ts.ParameterDeclaration | ts.PropertySignature | ts.MethodSignature | ts.IndexSignatureDeclaration>node.parent).type) === node) {
87+
if (((node.parent as ts.VariableDeclaration | ts.MethodDeclaration | ts.PropertyDeclaration | ts.ParameterDeclaration | ts.PropertySignature | ts.MethodSignature | ts.IndexSignatureDeclaration).type) === node) {
8888
this.processTypeOfNode(node);
8989
}
9090
break;
9191
case ts.SyntaxKind.InterfaceDeclaration:
92-
const heritageClauses = (<ts.InterfaceDeclaration>node.parent).heritageClauses;
92+
const heritageClauses = (node.parent as ts.InterfaceDeclaration).heritageClauses;
9393
if (heritageClauses) {
9494
if (heritageClauses[0].token !== ts.SyntaxKind.ExtendsKeyword) {
9595
throw new Error(`Unexpected kind of heritage clause: ${ts.SyntaxKind[heritageClauses[0].kind]}`);
@@ -106,7 +106,7 @@ class DeclarationsWalker {
106106

107107
private processTypeOfNode(node: ts.Node): void {
108108
if (node.kind === ts.SyntaxKind.UnionType) {
109-
for (const t of (<ts.UnionTypeNode>node).types) {
109+
for (const t of (node as ts.UnionTypeNode).types) {
110110
this.processTypeOfNode(t);
111111
}
112112
}
@@ -120,7 +120,7 @@ class DeclarationsWalker {
120120
}
121121

122122
function writeProtocolFile(outputFile: string, protocolTs: string, typeScriptServicesDts: string) {
123-
const options = { target: ts.ScriptTarget.ES5, declaration: true, noResolve: false, types: <string[]>[], stripInternal: true };
123+
const options = { target: ts.ScriptTarget.ES5, declaration: true, noResolve: false, types: [] as string[], stripInternal: true };
124124

125125
/**
126126
* 1st pass - generate a program from protocol.ts and typescriptservices.d.ts and emit core version of protocol.d.ts with all internal members stripped

src/compiler/binder.ts

+107-107
Large diffs are not rendered by default.

src/compiler/checker.ts

+1,047-1,047
Large diffs are not rendered by default.

src/compiler/commandLineParser.ts

+19-19
Original file line numberDiff line numberDiff line change
@@ -1287,7 +1287,7 @@ namespace ts {
12871287
case "string":
12881288
return mapDefined(values, v => validateJsonOptionValue(opt.element, v || "", errors));
12891289
default:
1290-
return mapDefined(values, v => parseCustomTypeOption(<CommandLineOptionOfCustomType>opt.element, v, errors));
1290+
return mapDefined(values, v => parseCustomTypeOption(opt.element as CommandLineOptionOfCustomType, v, errors));
12911291
}
12921292
}
12931293

@@ -1466,7 +1466,7 @@ namespace ts {
14661466
break;
14671467
// If not a primitive, the possible types are specified in what is effectively a map of options.
14681468
default:
1469-
options[opt.name] = parseCustomTypeOption(<CommandLineOptionOfCustomType>opt, args[i], errors);
1469+
options[opt.name] = parseCustomTypeOption(opt as CommandLineOptionOfCustomType, args[i], errors);
14701470
i++;
14711471
break;
14721472
}
@@ -1570,7 +1570,7 @@ namespace ts {
15701570
/* @internal */
15711571
export function getDiagnosticText(_message: DiagnosticMessage, ..._args: any[]): string {
15721572
const diagnostic = createCompilerDiagnostic.apply(undefined, arguments);
1573-
return <string>diagnostic.messageText;
1573+
return diagnostic.messageText as string;
15741574
}
15751575

15761576
export type DiagnosticReporter = (diagnostic: Diagnostic) => void;
@@ -1654,7 +1654,7 @@ namespace ts {
16541654
*/
16551655
export function readJsonConfigFile(fileName: string, readFile: (path: string) => string | undefined): TsConfigSourceFile {
16561656
const textOrDiagnostic = tryReadFile(fileName, readFile);
1657-
return isString(textOrDiagnostic) ? parseJsonText(fileName, textOrDiagnostic) : <TsConfigSourceFile>{ fileName, parseDiagnostics: [textOrDiagnostic] };
1657+
return isString(textOrDiagnostic) ? parseJsonText(fileName, textOrDiagnostic) : { fileName, parseDiagnostics: [textOrDiagnostic] } as TsConfigSourceFile;
16581658
}
16591659

16601660
/*@internal*/
@@ -1961,9 +1961,9 @@ namespace ts {
19611961
errors.push(createDiagnosticForNodeInSourceFile(sourceFile, valueExpression, Diagnostics.String_literal_with_double_quotes_expected));
19621962
}
19631963
reportInvalidOptionValue(option && (isString(option.type) && option.type !== "string"));
1964-
const text = (<StringLiteral>valueExpression).text;
1964+
const text = (valueExpression as StringLiteral).text;
19651965
if (option && !isString(option.type)) {
1966-
const customOption = <CommandLineOptionOfCustomType>option;
1966+
const customOption = option as CommandLineOptionOfCustomType;
19671967
// Validate custom option type
19681968
if (!customOption.type.has(text.toLowerCase())) {
19691969
errors.push(
@@ -1979,18 +1979,18 @@ namespace ts {
19791979

19801980
case SyntaxKind.NumericLiteral:
19811981
reportInvalidOptionValue(option && option.type !== "number");
1982-
return validateValue(Number((<NumericLiteral>valueExpression).text));
1982+
return validateValue(Number((valueExpression as NumericLiteral).text));
19831983

19841984
case SyntaxKind.PrefixUnaryExpression:
1985-
if ((<PrefixUnaryExpression>valueExpression).operator !== SyntaxKind.MinusToken || (<PrefixUnaryExpression>valueExpression).operand.kind !== SyntaxKind.NumericLiteral) {
1985+
if ((valueExpression as PrefixUnaryExpression).operator !== SyntaxKind.MinusToken || (valueExpression as PrefixUnaryExpression).operand.kind !== SyntaxKind.NumericLiteral) {
19861986
break; // not valid JSON syntax
19871987
}
19881988
reportInvalidOptionValue(option && option.type !== "number");
1989-
return validateValue(-Number((<NumericLiteral>(<PrefixUnaryExpression>valueExpression).operand).text));
1989+
return validateValue(-Number(((valueExpression as PrefixUnaryExpression).operand as NumericLiteral).text));
19901990

19911991
case SyntaxKind.ObjectLiteralExpression:
19921992
reportInvalidOptionValue(option && option.type !== "object");
1993-
const objectLiteralExpression = <ObjectLiteralExpression>valueExpression;
1993+
const objectLiteralExpression = valueExpression as ObjectLiteralExpression;
19941994

19951995
// Currently having element option declaration in the tsconfig with type "object"
19961996
// determines if it needs onSetValidOptionKeyValueInParent callback or not
@@ -1999,7 +1999,7 @@ namespace ts {
19991999
// vs what we set in the json
20002000
// If need arises, we can modify this interface and callbacks as needed
20012001
if (option) {
2002-
const { elementOptions, extraKeyDiagnostics, name: optionName } = <TsConfigOnlyOption>option;
2002+
const { elementOptions, extraKeyDiagnostics, name: optionName } = option as TsConfigOnlyOption;
20032003
return validateValue(convertObjectLiteralExpressionToJson(objectLiteralExpression,
20042004
elementOptions, extraKeyDiagnostics, optionName));
20052005
}
@@ -2012,8 +2012,8 @@ namespace ts {
20122012
case SyntaxKind.ArrayLiteralExpression:
20132013
reportInvalidOptionValue(option && option.type !== "list");
20142014
return validateValue(convertArrayLiteralExpressionToJson(
2015-
(<ArrayLiteralExpression>valueExpression).elements,
2016-
option && (<CommandLineOptionOfListType>option).element));
2015+
(valueExpression as ArrayLiteralExpression).elements,
2016+
option && (option as CommandLineOptionOfListType).element));
20172017
}
20182018

20192019
// Not in expected format
@@ -2172,7 +2172,7 @@ namespace ts {
21722172
return getCustomTypeMapOfCommandLineOption(optionDefinition.element);
21732173
}
21742174
else {
2175-
return (<CommandLineOptionOfCustomType>optionDefinition).type;
2175+
return (optionDefinition as CommandLineOptionOfCustomType).type;
21762176
}
21772177
}
21782178

@@ -2211,7 +2211,7 @@ namespace ts {
22112211
if (optionsNameMap.has(name) && optionsNameMap.get(name)!.category === Diagnostics.Command_line_Options) {
22122212
continue;
22132213
}
2214-
const value = <CompilerOptionsValue>options[name];
2214+
const value = options[name] as CompilerOptionsValue;
22152215
const optionDefinition = optionsNameMap.get(name.toLowerCase());
22162216
if (optionDefinition) {
22172217
const customTypeMap = getCustomTypeMapOfCommandLineOption(optionDefinition);
@@ -2782,7 +2782,7 @@ namespace ts {
27822782
case "extends":
27832783
const newBase = configFileName ? directoryOfCombinedPath(configFileName, basePath) : basePath;
27842784
extendedConfigPath = getExtendsConfigPath(
2785-
<string>value,
2785+
value as string,
27862786
host,
27872787
newBase,
27882788
errors,
@@ -2972,10 +2972,10 @@ namespace ts {
29722972
if (isCompilerOptionsValue(opt, value)) {
29732973
const optType = opt.type;
29742974
if (optType === "list" && isArray(value)) {
2975-
return convertJsonOptionOfListType(<CommandLineOptionOfListType>opt, value, basePath, errors);
2975+
return convertJsonOptionOfListType(opt as CommandLineOptionOfListType, value, basePath, errors);
29762976
}
29772977
else if (!isString(optType)) {
2978-
return convertJsonOptionOfCustomType(<CommandLineOptionOfCustomType>opt, <string>value, errors);
2978+
return convertJsonOptionOfCustomType(opt as CommandLineOptionOfCustomType, value as string, errors);
29792979
}
29802980
const validatedValue = validateJsonOptionValue(opt, value, errors);
29812981
return isNullOrUndefined(validatedValue) ? validatedValue : normalizeNonListOptionValue(opt, basePath, validatedValue);
@@ -2990,7 +2990,7 @@ namespace ts {
29902990
if (option.type === "list") {
29912991
const listOption = option;
29922992
if (listOption.element.isFilePath || !isString(listOption.element.type)) {
2993-
return <CompilerOptionsValue>filter(map(value, v => normalizeOptionValue(listOption.element, basePath, v)), v => !!v);
2993+
return filter(map(value, v => normalizeOptionValue(listOption.element, basePath, v)), v => !!v) as CompilerOptionsValue;
29942994
}
29952995
return value;
29962996
}

src/compiler/core.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,7 @@ namespace ts {
14291429
const result: any = {};
14301430
for (const id in object) {
14311431
if (hasOwnProperty.call(object, id)) {
1432-
result[id] = (<any>object)[id];
1432+
result[id] = (object as any)[id];
14331433
}
14341434
}
14351435
return result;
@@ -1441,7 +1441,7 @@ namespace ts {
14411441
* NOTE: This means that if a property exists in both `first` and `second`, the property in `first` will be chosen.
14421442
*/
14431443
export function extend<T1, T2>(first: T1, second: T2): T1 & T2 {
1444-
const result: T1 & T2 = <any>{};
1444+
const result: T1 & T2 = {} as any;
14451445
for (const id in second) {
14461446
if (hasOwnProperty.call(second, id)) {
14471447
(result as any)[id] = (second as any)[id];

src/compiler/debug.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ namespace ts {
112112
export function fail(message?: string, stackCrawlMark?: AnyFunction): never {
113113
debugger;
114114
const e = new Error(message ? `Debug Failure. ${message}` : "Debug Failure.");
115-
if ((<any>Error).captureStackTrace) {
116-
(<any>Error).captureStackTrace(e, stackCrawlMark || fail);
115+
if ((Error as any).captureStackTrace) {
116+
(Error as any).captureStackTrace(e, stackCrawlMark || fail);
117117
}
118118
throw e;
119119
}
@@ -288,7 +288,7 @@ namespace ts {
288288
return "";
289289
}
290290
else if (func.hasOwnProperty("name")) {
291-
return (<any>func).name;
291+
return (func as any).name;
292292
}
293293
else {
294294
const text = Function.prototype.toString.call(func);
@@ -348,43 +348,43 @@ namespace ts {
348348
}
349349

350350
export function formatSyntaxKind(kind: SyntaxKind | undefined): string {
351-
return formatEnum(kind, (<any>ts).SyntaxKind, /*isFlags*/ false);
351+
return formatEnum(kind, (ts as any).SyntaxKind, /*isFlags*/ false);
352352
}
353353

354354
export function formatNodeFlags(flags: NodeFlags | undefined): string {
355-
return formatEnum(flags, (<any>ts).NodeFlags, /*isFlags*/ true);
355+
return formatEnum(flags, (ts as any).NodeFlags, /*isFlags*/ true);
356356
}
357357

358358
export function formatModifierFlags(flags: ModifierFlags | undefined): string {
359-
return formatEnum(flags, (<any>ts).ModifierFlags, /*isFlags*/ true);
359+
return formatEnum(flags, (ts as any).ModifierFlags, /*isFlags*/ true);
360360
}
361361

362362
export function formatTransformFlags(flags: TransformFlags | undefined): string {
363-
return formatEnum(flags, (<any>ts).TransformFlags, /*isFlags*/ true);
363+
return formatEnum(flags, (ts as any).TransformFlags, /*isFlags*/ true);
364364
}
365365

366366
export function formatEmitFlags(flags: EmitFlags | undefined): string {
367-
return formatEnum(flags, (<any>ts).EmitFlags, /*isFlags*/ true);
367+
return formatEnum(flags, (ts as any).EmitFlags, /*isFlags*/ true);
368368
}
369369

370370
export function formatSymbolFlags(flags: SymbolFlags | undefined): string {
371-
return formatEnum(flags, (<any>ts).SymbolFlags, /*isFlags*/ true);
371+
return formatEnum(flags, (ts as any).SymbolFlags, /*isFlags*/ true);
372372
}
373373

374374
export function formatTypeFlags(flags: TypeFlags | undefined): string {
375-
return formatEnum(flags, (<any>ts).TypeFlags, /*isFlags*/ true);
375+
return formatEnum(flags, (ts as any).TypeFlags, /*isFlags*/ true);
376376
}
377377

378378
export function formatSignatureFlags(flags: SignatureFlags | undefined): string {
379-
return formatEnum(flags, (<any>ts).SignatureFlags, /*isFlags*/ true);
379+
return formatEnum(flags, (ts as any).SignatureFlags, /*isFlags*/ true);
380380
}
381381

382382
export function formatObjectFlags(flags: ObjectFlags | undefined): string {
383-
return formatEnum(flags, (<any>ts).ObjectFlags, /*isFlags*/ true);
383+
return formatEnum(flags, (ts as any).ObjectFlags, /*isFlags*/ true);
384384
}
385385

386386
export function formatFlowFlags(flags: FlowFlags | undefined): string {
387-
return formatEnum(flags, (<any>ts).FlowFlags, /*isFlags*/ true);
387+
return formatEnum(flags, (ts as any).FlowFlags, /*isFlags*/ true);
388388
}
389389

390390
let isDebugInfoEnabled = false;
@@ -570,7 +570,7 @@ namespace ts {
570570
}
571571
},
572572
__debugFlags: { get(this: Type) { return formatTypeFlags(this.flags); } },
573-
__debugObjectFlags: { get(this: Type) { return this.flags & TypeFlags.Object ? formatObjectFlags((<ObjectType>this).objectFlags) : ""; } },
573+
__debugObjectFlags: { get(this: Type) { return this.flags & TypeFlags.Object ? formatObjectFlags((this as ObjectType).objectFlags) : ""; } },
574574
__debugTypeToString: {
575575
value(this: Type) {
576576
// avoid recomputing

0 commit comments

Comments
 (0)