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

Commit

Permalink
Always pass a language service to rule.apply*
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Oct 7, 2016
1 parent f836dc1 commit 9e1c647
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/language/rule/abstractRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export abstract class AbstractRule implements IRule {
return this.options;
}

public abstract apply(sourceFile: ts.SourceFile): RuleFailure[];
public abstract apply(sourceFile: ts.SourceFile, languageService: ts.LanguageService): RuleFailure[];

public applyWithWalker(walker: RuleWalker): RuleFailure[] {
walker.walk(walker.getSourceFile());
Expand Down
2 changes: 1 addition & 1 deletion src/language/rule/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export interface IDisabledInterval {
export interface IRule {
getOptions(): IOptions;
isEnabled(): boolean;
apply(sourceFile: ts.SourceFile): RuleFailure[];
apply(sourceFile: ts.SourceFile, languageService: ts.LanguageService): RuleFailure[];
applyWithWalker(walker: RuleWalker): RuleFailure[];
}

Expand Down
2 changes: 1 addition & 1 deletion src/language/rule/typedRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {AbstractRule} from "./abstractRule";
import {RuleFailure} from "./rule";

export abstract class TypedRule extends AbstractRule {
public apply(sourceFile: ts.SourceFile): RuleFailure[] {
public apply(sourceFile: ts.SourceFile, languageService: ts.LanguageService): RuleFailure[] {
// if no program is given to the linter, throw an error
throw new Error(`${this.getOptions().ruleName} requires type checking`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/noForInArrayRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class Rule extends Lint.Rules.TypedRule {

public static FAILURE_STRING = "for-in loops over arrays are forbidden. Use for-of or array.forEach instead.";

public applyWithProgram(sourceFile: ts.SourceFile, langSvc: Lint.TslintLanguageService): Lint.RuleFailure[] {
public applyWithProgram(sourceFile: ts.SourceFile, langSvc: ts.LanguageService): Lint.RuleFailure[] {
const noForInArrayWalker = new NoForInArrayWalker(sourceFile, this.getOptions(), langSvc.getProgram());
return this.applyWithWalker(noForInArrayWalker);
}
Expand Down
3 changes: 1 addition & 2 deletions src/rules/noMergeableNamespaceRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ export class Rule extends Lint.Rules.AbstractRule {
return `Mergeable namespace ${identifier} found. Merge its contents with the namespace on line ${locationToMerge.line}.`;
}

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const languageService = Lint.createLanguageService(sourceFile.fileName, sourceFile.getFullText());
public apply(sourceFile: ts.SourceFile, languageService: ts.LanguageService): Lint.RuleFailure[] {
const noMergeableNamespaceWalker = new NoMergeableNamespaceWalker(sourceFile, this.getOptions(), languageService);
return this.applyWithWalker(noMergeableNamespaceWalker);
}
Expand Down
14 changes: 5 additions & 9 deletions src/rules/noUnusedVariableRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ export class Rule extends Lint.Rules.TypedRule {
public static FAILURE_STRING_FACTORY = (type: string, name: string) => `Unused ${type}: '${name}'`;

// no-undefined-variable optionally allows type-checking
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithProgram(sourceFile, undefined);
public apply(sourceFile: ts.SourceFile, languageService: ts.LanguageService): Lint.RuleFailure[] {
return this.applyWithProgram(sourceFile, languageService);
}

public applyWithProgram(sourceFile: ts.SourceFile, langSvc?: ts.LanguageService): Lint.RuleFailure[] {
return this.applyWithWalker(new NoUnusedVariablesWalker(sourceFile, this.getOptions(), langSvc));
public applyWithProgram(sourceFile: ts.SourceFile, languageService: ts.LanguageService): Lint.RuleFailure[] {
return this.applyWithWalker(new NoUnusedVariablesWalker(sourceFile, this.getOptions(), languageService));
}
}

Expand All @@ -98,16 +98,12 @@ class NoUnusedVariablesWalker extends Lint.RuleWalker {
private possibleFailures: Lint.RuleFailure[] = [];

constructor(sourceFile: ts.SourceFile, options: Lint.IOptions,
private languageService?: ts.LanguageService) {
private languageService: ts.LanguageService) {
super(sourceFile, options);
this.skipVariableDeclaration = false;
this.skipParameterDeclaration = false;
this.hasSeenJsxElement = false;
this.isReactUsed = false;
if (!languageService) {
this.dummyLanguageService = true;
this.languageService = Lint.createLanguageService(sourceFile.fileName, sourceFile.getFullText());
}

const ignorePatternOption = this.getOptions().filter((option: any) => {
return typeof option === "object" && option["ignore-pattern"] != null;
Expand Down
3 changes: 1 addition & 2 deletions src/rules/noUseBeforeDeclareRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING_PREFIX = "variable '";
public static FAILURE_STRING_POSTFIX = "' used before declaration";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const languageService = Lint.createLanguageService(sourceFile.fileName, sourceFile.getFullText());
public apply(sourceFile: ts.SourceFile, languageService: ts.LanguageService): Lint.RuleFailure[] {
return this.applyWithWalker(new NoUseBeforeDeclareWalker(sourceFile, this.getOptions(), languageService));
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/rules/preferForOfRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ export class Rule extends Lint.Rules.AbstractRule {

public static FAILURE_STRING = "Expected a 'for-of' loop instead of a 'for' loop with this simple iteration";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const languageService = Lint.createLanguageService(sourceFile.fileName, sourceFile.getFullText());
public apply(sourceFile: ts.SourceFile, languageService: ts.LanguageService): Lint.RuleFailure[] {
return this.applyWithWalker(new PreferForOfWalker(sourceFile, this.getOptions(), languageService));
}
}
Expand Down Expand Up @@ -67,7 +66,7 @@ class PreferForOfWalker extends Lint.RuleWalker {
// so remove those from the count to get the count inside the loop block
const incrementorCount = highlights[0].highlightSpans.length - 3;

// Find `array[i]`-like usages by building up a regex
// Find `array[i]`-like usages by building up a regex
const arrayTokenForRegex = arrayToken.getText().replace(".", "\\.");
const incrementorForRegex = incrementorVariable.getText().replace(".", "\\.");
const regex = new RegExp(`${arrayTokenForRegex}\\[\\s*${incrementorForRegex}\\s*\\]`, "g");
Expand Down
2 changes: 1 addition & 1 deletion src/rules/restrictPlusOperandsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class Rule extends Lint.Rules.TypedRule {
public static MISMATCHED_TYPES_FAILURE = "Types of values used in '+' operation must match";
public static UNSUPPORTED_TYPE_FAILURE_FACTORY = (type: string) => `cannot add type ${type}`;

public applyWithProgram(sourceFile: ts.SourceFile, langSvc: Lint.TslintLanguageService): Lint.RuleFailure[] {
public applyWithProgram(sourceFile: ts.SourceFile, langSvc: ts.LanguageService): Lint.RuleFailure[] {
return this.applyWithWalker(new RestrictPlusOperandsWalker(sourceFile, this.getOptions(), langSvc.getProgram()));
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/tslintMulti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
} from "./configuration";
import { EnableDisableRulesWalker } from "./enableDisableRules";
import { findFormatter } from "./formatterLoader";
import { wrapProgram } from "./language/languageServiceHost";
import { createLanguageService, wrapProgram } from "./language/languageServiceHost";
import { IFormatter } from "./language/formatter/formatter";
import { RuleFailure } from "./language/rule/rule";
import { TypedRule } from "./language/rule/typedRule";
Expand Down Expand Up @@ -101,6 +101,7 @@ class MultiLinter {
}
} else {
sourceFile = getSourceFile(fileName, source);
this.languageService = createLanguageService(fileName, source);
}

if (sourceFile === undefined) {
Expand All @@ -125,7 +126,7 @@ class MultiLinter {
if (this.program && rule instanceof TypedRule) {
ruleFailures = rule.applyWithProgram(sourceFile, this.languageService);
} else {
ruleFailures = rule.apply(sourceFile);
ruleFailures = rule.apply(sourceFile, this.languageService);
}
for (let ruleFailure of ruleFailures) {
if (!this.containsRule(this.failures, ruleFailure)) {
Expand Down

0 comments on commit 9e1c647

Please sign in to comment.