From 95d28e07702478871bc2c644f01e066467fa44dc Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Mon, 1 May 2017 22:17:00 +0200 Subject: [PATCH 1/3] Really fix type parameter of applyWithFunction --- src/language/rule/abstractRule.ts | 12 ++---------- src/rules/arrayTypeRule.ts | 2 +- src/rules/maxLineLengthRule.ts | 2 +- src/rules/noFloatingPromisesRule.ts | 2 +- src/rules/noVoidExpressionRule.ts | 2 +- src/rules/strictBooleanExpressionsRule.ts | 2 +- 6 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/language/rule/abstractRule.ts b/src/language/rule/abstractRule.ts index 09e821ed46f..70ec63c09a6 100644 --- a/src/language/rule/abstractRule.ts +++ b/src/language/rule/abstractRule.ts @@ -49,16 +49,8 @@ export abstract class AbstractRule implements IRule { } protected applyWithFunction(sourceFile: ts.SourceFile, walkFn: (ctx: WalkContext) => void): RuleFailure[]; - protected applyWithFunction( - sourceFile: ts.SourceFile, - walkFn: (ctx: WalkContext) => void, - options: U, - ): RuleFailure[]; - protected applyWithFunction( - sourceFile: ts.SourceFile, - walkFn: (ctx: WalkContext) => void, - options?: U, - ): RuleFailure[] { + protected applyWithFunction(sourceFile: ts.SourceFile, walkFn: (ctx: WalkContext) => void, options: {} & T): RuleFailure[]; + protected applyWithFunction(sourceFile: ts.SourceFile, walkFn: (ctx: WalkContext) => void, options?: T): RuleFailure[] { const ctx = new WalkContext(sourceFile, this.ruleName, options); walkFn(ctx); return this.filterFailures(ctx.failures); diff --git a/src/rules/arrayTypeRule.ts b/src/rules/arrayTypeRule.ts index 3c19c1f071a..f1e26722386 100644 --- a/src/rules/arrayTypeRule.ts +++ b/src/rules/arrayTypeRule.ts @@ -52,7 +52,7 @@ export class Rule extends Lint.Rules.AbstractRule { public static FAILURE_STRING_GENERIC_SIMPLE = "Array type using 'T[]' is forbidden for non-simple types. Use 'Array' instead."; public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { - return this.applyWithFunction(sourceFile, walk, this.ruleArguments[0]); + return this.applyWithFunction(sourceFile, walk, this.ruleArguments[0] as Option); } } diff --git a/src/rules/maxLineLengthRule.ts b/src/rules/maxLineLengthRule.ts index c8e4deee2cf..606d9ff8acf 100644 --- a/src/rules/maxLineLengthRule.ts +++ b/src/rules/maxLineLengthRule.ts @@ -49,7 +49,7 @@ export class Rule extends Lint.Rules.AbstractRule { } public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { - return this.applyWithFunction(sourceFile, walk, this.ruleArguments[0]); + return this.applyWithFunction(sourceFile, walk, this.ruleArguments[0] as number); } } diff --git a/src/rules/noFloatingPromisesRule.ts b/src/rules/noFloatingPromisesRule.ts index 4601a11eb2e..98d1030e8eb 100644 --- a/src/rules/noFloatingPromisesRule.ts +++ b/src/rules/noFloatingPromisesRule.ts @@ -49,7 +49,7 @@ export class Rule extends Lint.Rules.TypedRule { public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] { return this.applyWithFunction( sourceFile, - (ctx: Lint.WalkContext) => walk(ctx, program.getTypeChecker()), + (ctx) => walk(ctx, program.getTypeChecker()), ["Promise", ...this.ruleArguments as string[]], ); } diff --git a/src/rules/noVoidExpressionRule.ts b/src/rules/noVoidExpressionRule.ts index 37def9cbeb0..598f3645c02 100644 --- a/src/rules/noVoidExpressionRule.ts +++ b/src/rules/noVoidExpressionRule.ts @@ -49,7 +49,7 @@ export class Rule extends Lint.Rules.TypedRule { public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] { const ignoreArrowFunctionShorthand = this.ruleArguments.indexOf(OPTION_IGNORE_ARROW_FUNCTION_SHORTHAND) !== -1; - return this.applyWithFunction( + return this.applyWithFunction( sourceFile, (ctx) => walk(ctx, program.getTypeChecker()), { ignoreArrowFunctionShorthand }); } } diff --git a/src/rules/strictBooleanExpressionsRule.ts b/src/rules/strictBooleanExpressionsRule.ts index e37987dab9d..d432ceb5603 100644 --- a/src/rules/strictBooleanExpressionsRule.ts +++ b/src/rules/strictBooleanExpressionsRule.ts @@ -76,7 +76,7 @@ export class Rule extends Lint.Rules.TypedRule { public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] { const strictNullChecks = !!program.getCompilerOptions().strictNullChecks; const options = parseOptions(this.ruleArguments, strictNullChecks); - return this.applyWithFunction(sourceFile, (ctx: Lint.WalkContext) => walk(ctx, program.getTypeChecker()), options); + return this.applyWithFunction(sourceFile, (ctx) => walk(ctx, program.getTypeChecker()), options); } } From 4ac711a2eee3a7998eb0a2d759914e8c67d6cd3c Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Mon, 14 Aug 2017 20:19:19 +0200 Subject: [PATCH 2/3] fix type error on undefined union --- src/language/rule/abstractRule.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/language/rule/abstractRule.ts b/src/language/rule/abstractRule.ts index 0ea4bde4f6a..079a831f0d6 100644 --- a/src/language/rule/abstractRule.ts +++ b/src/language/rule/abstractRule.ts @@ -20,6 +20,8 @@ import * as ts from "typescript"; import { IWalker, WalkContext } from "../walker"; import { IOptions, IRule, IRuleMetadata, RuleFailure, RuleSeverity } from "./rule"; +export type NoInfer = T & {[K in keyof T]: T[K]}; + export abstract class AbstractRule implements IRule { public static metadata: IRuleMetadata; protected readonly ruleArguments: any[]; @@ -48,7 +50,7 @@ export abstract class AbstractRule implements IRule { } protected applyWithFunction(sourceFile: ts.SourceFile, walkFn: (ctx: WalkContext) => void): RuleFailure[]; - protected applyWithFunction(sourceFile: ts.SourceFile, walkFn: (ctx: WalkContext) => void, options: {} & T): RuleFailure[]; + protected applyWithFunction(sourceFile: ts.SourceFile, walkFn: (ctx: WalkContext) => void, options: NoInfer): RuleFailure[]; protected applyWithFunction(sourceFile: ts.SourceFile, walkFn: (ctx: WalkContext) => void, options?: T): RuleFailure[] { const ctx = new WalkContext(sourceFile, this.ruleName, options); walkFn(ctx); From 56d4aae0e21d73417ee8e7d04b616f70d5e6858c Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Mon, 14 Aug 2017 20:25:41 +0200 Subject: [PATCH 3/3] Fix merge --- src/rules/deprecationRule.ts | 2 +- src/rules/noUnboundMethodRule.ts | 2 +- src/rules/objectLiteralSortKeysRule.ts | 2 +- src/rules/promiseFunctionAsyncRule.ts | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/rules/deprecationRule.ts b/src/rules/deprecationRule.ts index 4bc48d612c6..6ab3880cf05 100644 --- a/src/rules/deprecationRule.ts +++ b/src/rules/deprecationRule.ts @@ -44,7 +44,7 @@ export class Rule extends Lint.Rules.TypedRule { } public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] { - return this.applyWithFunction(sourceFile, (ctx: Lint.WalkContext) => walk(ctx, program.getTypeChecker())); + return this.applyWithFunction(sourceFile, (ctx) => walk(ctx, program.getTypeChecker())); } } diff --git a/src/rules/noUnboundMethodRule.ts b/src/rules/noUnboundMethodRule.ts index bb615de77d8..b7a5dacc070 100644 --- a/src/rules/noUnboundMethodRule.ts +++ b/src/rules/noUnboundMethodRule.ts @@ -45,7 +45,7 @@ export class Rule extends Lint.Rules.TypedRule { public static FAILURE_STRING = "Avoid referencing unbound methods which may cause unintentional scoping of 'this'."; public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] { - return this.applyWithFunction(sourceFile, (ctx: Lint.WalkContext) => walk(ctx, program.getTypeChecker()), { + return this.applyWithFunction(sourceFile, (ctx) => walk(ctx, program.getTypeChecker()), { ignoreStatic: this.ruleArguments.indexOf(OPTION_IGNORE_STATIC) !== -1, }); } diff --git a/src/rules/objectLiteralSortKeysRule.ts b/src/rules/objectLiteralSortKeysRule.ts index 732cb8a6d44..1cad382640c 100644 --- a/src/rules/objectLiteralSortKeysRule.ts +++ b/src/rules/objectLiteralSortKeysRule.ts @@ -77,7 +77,7 @@ export class Rule extends Lint.Rules.OptionallyTypedRule { } public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] { - return this.applyWithFunction( + return this.applyWithFunction( sourceFile, (ctx) => walk(ctx, program.getTypeChecker()), parseOptions(this.ruleArguments), diff --git a/src/rules/promiseFunctionAsyncRule.ts b/src/rules/promiseFunctionAsyncRule.ts index e0d6535e82d..e32e5226eab 100644 --- a/src/rules/promiseFunctionAsyncRule.ts +++ b/src/rules/promiseFunctionAsyncRule.ts @@ -42,7 +42,7 @@ export class Rule extends Lint.Rules.TypedRule { public static FAILURE_STRING = "functions that return promises must be async"; public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] { - return this.applyWithFunction(sourceFile, (ctx: Lint.WalkContext) => walk(ctx, program.getTypeChecker())); + return this.applyWithFunction(sourceFile, (ctx) => walk(ctx, program.getTypeChecker())); } }