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

feat(eslint): consistent-type-assertions #43556

Merged
merged 4 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
],

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

"no-duplicate-imports": "off",
"@typescript-eslint/no-duplicate-imports": "error",
Expand Down
10 changes: 5 additions & 5 deletions scripts/buildProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class DeclarationsWalker {
}
if (s.name === "Array" || s.name === "ReadOnlyArray") {
// we should process type argument instead
return this.processType((<any>type).typeArguments[0]);
return this.processType((type as any).typeArguments[0]);
}
else {
const declarations = s.getDeclarations();
Expand Down Expand Up @@ -84,12 +84,12 @@ class DeclarationsWalker {
case ts.SyntaxKind.PropertySignature:
case ts.SyntaxKind.Parameter:
case ts.SyntaxKind.IndexSignature:
if (((<ts.VariableDeclaration | ts.MethodDeclaration | ts.PropertyDeclaration | ts.ParameterDeclaration | ts.PropertySignature | ts.MethodSignature | ts.IndexSignatureDeclaration>node.parent).type) === node) {
if (((node.parent as ts.VariableDeclaration | ts.MethodDeclaration | ts.PropertyDeclaration | ts.ParameterDeclaration | ts.PropertySignature | ts.MethodSignature | ts.IndexSignatureDeclaration).type) === node) {
this.processTypeOfNode(node);
}
break;
case ts.SyntaxKind.InterfaceDeclaration:
const heritageClauses = (<ts.InterfaceDeclaration>node.parent).heritageClauses;
const heritageClauses = (node.parent as ts.InterfaceDeclaration).heritageClauses;
if (heritageClauses) {
if (heritageClauses[0].token !== ts.SyntaxKind.ExtendsKeyword) {
throw new Error(`Unexpected kind of heritage clause: ${ts.SyntaxKind[heritageClauses[0].kind]}`);
Expand All @@ -106,7 +106,7 @@ class DeclarationsWalker {

private processTypeOfNode(node: ts.Node): void {
if (node.kind === ts.SyntaxKind.UnionType) {
for (const t of (<ts.UnionTypeNode>node).types) {
for (const t of (node as ts.UnionTypeNode).types) {
this.processTypeOfNode(t);
}
}
Expand All @@ -120,7 +120,7 @@ class DeclarationsWalker {
}

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

/**
* 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
Expand Down
214 changes: 107 additions & 107 deletions src/compiler/binder.ts

Large diffs are not rendered by default.

2,094 changes: 1,047 additions & 1,047 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

38 changes: 19 additions & 19 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1287,7 +1287,7 @@ namespace ts {
case "string":
return mapDefined(values, v => validateJsonOptionValue(opt.element, v || "", errors));
default:
return mapDefined(values, v => parseCustomTypeOption(<CommandLineOptionOfCustomType>opt.element, v, errors));
return mapDefined(values, v => parseCustomTypeOption(opt.element as CommandLineOptionOfCustomType, v, errors));
}
}

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

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

/*@internal*/
Expand Down Expand Up @@ -1961,9 +1961,9 @@ namespace ts {
errors.push(createDiagnosticForNodeInSourceFile(sourceFile, valueExpression, Diagnostics.String_literal_with_double_quotes_expected));
}
reportInvalidOptionValue(option && (isString(option.type) && option.type !== "string"));
const text = (<StringLiteral>valueExpression).text;
const text = (valueExpression as StringLiteral).text;
if (option && !isString(option.type)) {
const customOption = <CommandLineOptionOfCustomType>option;
const customOption = option as CommandLineOptionOfCustomType;
// Validate custom option type
if (!customOption.type.has(text.toLowerCase())) {
errors.push(
Expand All @@ -1979,18 +1979,18 @@ namespace ts {

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

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

case SyntaxKind.ObjectLiteralExpression:
reportInvalidOptionValue(option && option.type !== "object");
const objectLiteralExpression = <ObjectLiteralExpression>valueExpression;
const objectLiteralExpression = valueExpression as ObjectLiteralExpression;

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

// Not in expected format
Expand Down Expand Up @@ -2172,7 +2172,7 @@ namespace ts {
return getCustomTypeMapOfCommandLineOption(optionDefinition.element);
}
else {
return (<CommandLineOptionOfCustomType>optionDefinition).type;
return (optionDefinition as CommandLineOptionOfCustomType).type;
}
}

Expand Down Expand Up @@ -2211,7 +2211,7 @@ namespace ts {
if (optionsNameMap.has(name) && optionsNameMap.get(name)!.category === Diagnostics.Command_line_Options) {
continue;
}
const value = <CompilerOptionsValue>options[name];
const value = options[name] as CompilerOptionsValue;
const optionDefinition = optionsNameMap.get(name.toLowerCase());
if (optionDefinition) {
const customTypeMap = getCustomTypeMapOfCommandLineOption(optionDefinition);
Expand Down Expand Up @@ -2782,7 +2782,7 @@ namespace ts {
case "extends":
const newBase = configFileName ? directoryOfCombinedPath(configFileName, basePath) : basePath;
extendedConfigPath = getExtendsConfigPath(
<string>value,
value as string,
host,
newBase,
errors,
Expand Down Expand Up @@ -2972,10 +2972,10 @@ namespace ts {
if (isCompilerOptionsValue(opt, value)) {
const optType = opt.type;
if (optType === "list" && isArray(value)) {
return convertJsonOptionOfListType(<CommandLineOptionOfListType>opt, value, basePath, errors);
return convertJsonOptionOfListType(opt as CommandLineOptionOfListType, value, basePath, errors);
}
else if (!isString(optType)) {
return convertJsonOptionOfCustomType(<CommandLineOptionOfCustomType>opt, <string>value, errors);
return convertJsonOptionOfCustomType(opt as CommandLineOptionOfCustomType, value as string, errors);
}
const validatedValue = validateJsonOptionValue(opt, value, errors);
return isNullOrUndefined(validatedValue) ? validatedValue : normalizeNonListOptionValue(opt, basePath, validatedValue);
Expand All @@ -2990,7 +2990,7 @@ namespace ts {
if (option.type === "list") {
const listOption = option;
if (listOption.element.isFilePath || !isString(listOption.element.type)) {
return <CompilerOptionsValue>filter(map(value, v => normalizeOptionValue(listOption.element, basePath, v)), v => !!v);
return filter(map(value, v => normalizeOptionValue(listOption.element, basePath, v)), v => !!v) as CompilerOptionsValue;
}
return value;
}
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1429,7 +1429,7 @@ namespace ts {
const result: any = {};
for (const id in object) {
if (hasOwnProperty.call(object, id)) {
result[id] = (<any>object)[id];
result[id] = (object as any)[id];
}
}
return result;
Expand All @@ -1441,7 +1441,7 @@ namespace ts {
* NOTE: This means that if a property exists in both `first` and `second`, the property in `first` will be chosen.
*/
export function extend<T1, T2>(first: T1, second: T2): T1 & T2 {
const result: T1 & T2 = <any>{};
const result: T1 & T2 = {} as any;
for (const id in second) {
if (hasOwnProperty.call(second, id)) {
(result as any)[id] = (second as any)[id];
Expand Down
28 changes: 14 additions & 14 deletions src/compiler/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ namespace ts {
export function fail(message?: string, stackCrawlMark?: AnyFunction): never {
debugger;
const e = new Error(message ? `Debug Failure. ${message}` : "Debug Failure.");
if ((<any>Error).captureStackTrace) {
(<any>Error).captureStackTrace(e, stackCrawlMark || fail);
if ((Error as any).captureStackTrace) {
(Error as any).captureStackTrace(e, stackCrawlMark || fail);
}
throw e;
}
Expand Down Expand Up @@ -288,7 +288,7 @@ namespace ts {
return "";
}
else if (func.hasOwnProperty("name")) {
return (<any>func).name;
return (func as any).name;
}
else {
const text = Function.prototype.toString.call(func);
Expand Down Expand Up @@ -348,43 +348,43 @@ namespace ts {
}

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

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

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

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

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

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

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

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

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

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

let isDebugInfoEnabled = false;
Expand Down Expand Up @@ -570,7 +570,7 @@ namespace ts {
}
},
__debugFlags: { get(this: Type) { return formatTypeFlags(this.flags); } },
__debugObjectFlags: { get(this: Type) { return this.flags & TypeFlags.Object ? formatObjectFlags((<ObjectType>this).objectFlags) : ""; } },
__debugObjectFlags: { get(this: Type) { return this.flags & TypeFlags.Object ? formatObjectFlags((this as ObjectType).objectFlags) : ""; } },
__debugTypeToString: {
value(this: Type) {
// avoid recomputing
Expand Down
Loading