Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Really fix type parameter of applyWithFunction #2660

Merged
merged 5 commits into from
Aug 14, 2017
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
14 changes: 4 additions & 10 deletions src/language/rule/abstractRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> = T & {[K in keyof T]: T[K]};

export abstract class AbstractRule implements IRule {
public static metadata: IRuleMetadata;
protected readonly ruleArguments: any[];
Expand Down Expand Up @@ -48,16 +50,8 @@ export abstract class AbstractRule implements IRule {
}

protected applyWithFunction(sourceFile: ts.SourceFile, walkFn: (ctx: WalkContext<void>) => void): RuleFailure[];
protected applyWithFunction<T, U extends T>(
sourceFile: ts.SourceFile,
walkFn: (ctx: WalkContext<T>) => void,
options: U,
): RuleFailure[];
protected applyWithFunction<T, U extends T>(
sourceFile: ts.SourceFile,
walkFn: (ctx: WalkContext<T | void>) => void,
options?: U,
): RuleFailure[] {
protected applyWithFunction<T>(sourceFile: ts.SourceFile, walkFn: (ctx: WalkContext<T>) => void, options: NoInfer<T>): RuleFailure[];
protected applyWithFunction<T>(sourceFile: ts.SourceFile, walkFn: (ctx: WalkContext<T | void>) => void, options?: T): RuleFailure[] {
const ctx = new WalkContext(sourceFile, this.ruleName, options);
walkFn(ctx);
return ctx.failures;
Expand Down
2 changes: 1 addition & 1 deletion src/rules/arrayTypeRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>' 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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/deprecationRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>) => walk(ctx, program.getTypeChecker()));
return this.applyWithFunction(sourceFile, (ctx) => walk(ctx, program.getTypeChecker()));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/maxLineLengthRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/noFloatingPromisesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]>) => walk(ctx, program.getTypeChecker()),
(ctx) => walk(ctx, program.getTypeChecker()),
["Promise", ...this.ruleArguments as string[]],
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/noUnboundMethodRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Options>) => walk(ctx, program.getTypeChecker()), {
return this.applyWithFunction(sourceFile, (ctx) => walk(ctx, program.getTypeChecker()), {
ignoreStatic: this.ruleArguments.indexOf(OPTION_IGNORE_STATIC) !== -1,
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/noVoidExpressionRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Options, Options>(
return this.applyWithFunction(
sourceFile, (ctx) => walk(ctx, program.getTypeChecker()), { ignoreArrowFunctionShorthand });
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/objectLiteralSortKeysRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class Rule extends Lint.Rules.OptionallyTypedRule {
}

public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {
return this.applyWithFunction<Options, Options>(
return this.applyWithFunction(
sourceFile,
(ctx) => walk(ctx, program.getTypeChecker()),
parseOptions(this.ruleArguments),
Expand Down
2 changes: 1 addition & 1 deletion src/rules/promiseFunctionAsyncRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>) => walk(ctx, program.getTypeChecker()));
return this.applyWithFunction(sourceFile, (ctx) => walk(ctx, program.getTypeChecker()));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/strictBooleanExpressionsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class Rule extends Lint.Rules.TypedRule {

public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {
const options = parseOptions(this.ruleArguments, Lint.isStrictNullChecksEnabled(program.getCompilerOptions()));
return this.applyWithFunction(sourceFile, (ctx: Lint.WalkContext<Options>) => walk(ctx, program.getTypeChecker()), options);
return this.applyWithFunction(sourceFile, (ctx) => walk(ctx, program.getTypeChecker()), options);
}
}

Expand Down