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

Commit

Permalink
Fix merge issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Noah Chen committed Nov 23, 2016
1 parent bcc86a2 commit dbbe1e4
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 181 deletions.
2 changes: 1 addition & 1 deletion src/language/languageServiceHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function wrapProgram(program: ts.Program): ts.LanguageService {
getCompilationSettings: () => program.getCompilerOptions(),
getCurrentDirectory: () => program.getCurrentDirectory(),
getDefaultLibFileName: () => null,
getScriptFileNames: () => program.getSourceFiles().map(sf => sf.fileName),
getScriptFileNames: () => program.getSourceFiles().map((sf) => sf.fileName),
getScriptSnapshot: (name: string) => {
if (files.hasOwnProperty(name)) {
return ts.ScriptSnapshot.fromString(files[name]);
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 @@ -26,7 +26,7 @@ export abstract class TypedRule extends AbstractRule {
return "applyWithProgram" in rule;
}

public apply(sourceFile: ts.SourceFile, languageService: ts.LanguageService): RuleFailure[] {
public apply(): RuleFailure[] {
// if no program is given to the linter, throw an error
throw new Error(`${this.getOptions().ruleName} requires type checking`);
}
Expand Down
11 changes: 9 additions & 2 deletions src/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { EnableDisableRulesWalker } from "./enableDisableRules";
import { findFormatter } from "./formatterLoader";
import { ILinterOptions, LintResult } from "./index";
import { IFormatter } from "./language/formatter/formatter";
import { createLanguageService, wrapProgram } from "./language/languageServiceHost";
import { Fix, IRule, RuleFailure } from "./language/rule/rule";
import { TypedRule } from "./language/rule/typedRule";
import * as utils from "./language/utils";
Expand All @@ -50,6 +51,7 @@ class Linter {

private failures: RuleFailure[] = [];
private fixes: RuleFailure[] = [];
private languageService: ts.LanguageService;

/**
* Creates a TypeScript program object from a tsconfig.json file path and optional project directory.
Expand Down Expand Up @@ -93,6 +95,10 @@ class Linter {
throw new Error("ILinterOptions does not contain the property `configuration` as of version 4. " +
"Did you mean to pass the `IConfigurationFile` object to lint() ? ");
}

if (program) {
this.languageService = wrapProgram(program);
}
}

public lint(fileName: string, source?: string, configuration: IConfigurationFile = DEFAULT_CONFIG): void {
Expand Down Expand Up @@ -158,9 +164,9 @@ class Linter {
private applyRule(rule: IRule, sourceFile: ts.SourceFile) {
let ruleFailures: RuleFailure[] = [];
if (this.program && TypedRule.isTypedRule(rule)) {
ruleFailures = rule.applyWithProgram(sourceFile, this.program);
ruleFailures = rule.applyWithProgram(sourceFile, this.languageService);
} else {
ruleFailures = rule.apply(sourceFile);
ruleFailures = rule.apply(sourceFile, this.languageService);
}
let fileFailures: RuleFailure[] = [];
for (let ruleFailure of ruleFailures) {
Expand Down Expand Up @@ -201,6 +207,7 @@ class Linter {
}
} else {
sourceFile = utils.getSourceFile(fileName, source);
this.languageService = createLanguageService(fileName, source);
}

if (sourceFile === undefined) {
Expand Down
4 changes: 2 additions & 2 deletions src/rules/completedDocsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ export class Rule extends Lint.Rules.TypedRule {
Rule.ARGUMENT_PROPERTIES,
];

public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {
public applyWithProgram(sourceFile: ts.SourceFile, langSvc: ts.LanguageService): Lint.RuleFailure[] {
const options = this.getOptions();
const completedDocsWalker = new CompletedDocsWalker(sourceFile, options, program);
const completedDocsWalker = new CompletedDocsWalker(sourceFile, options, langSvc.getProgram());

const nodesToCheck = this.getNodesToCheck(options.ruleArguments);
completedDocsWalker.setNodesToCheck(nodesToCheck);
Expand Down
6 changes: 3 additions & 3 deletions src/rules/noUnusedVariableRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ class NoUnusedVariablesWalker extends Lint.RuleWalker {

let someFixBrokeIt = false;
// Performance optimization: type-check the whole file before verifying individual fixes
if (this.possibleFailures.some(f => f.hasFix())) {
if (this.possibleFailures.some((f) => f.hasFix())) {
let newText = Lint.Fix.applyAll(this.getSourceFile().getFullText(),
this.possibleFailures.map(f => f.getFix()).filter(f => !!f));
this.possibleFailures.map((f) => f.getFix()).filter((f) => !!f));

// If we have the program, we can verify that the fix doesn't introduce failures
if (Lint.checkEdit(this.languageService, this.getSourceFile(), newText).length > 0) {
Expand All @@ -156,7 +156,7 @@ class NoUnusedVariablesWalker extends Lint.RuleWalker {
}
}

this.possibleFailures.forEach(f => {
this.possibleFailures.forEach((f) => {
if (!someFixBrokeIt || !f.hasFix()) {
this.addFailure(f);
} else {
Expand Down
1 change: 0 additions & 1 deletion src/rules/preferForOfRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ 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());
return this.applyWithWalker(new PreferForOfWalker(sourceFile, this.getOptions()));
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,19 @@ export function runTest(testDirectory: string, rulesDirectory?: string | string[
const filesToLint = glob.sync(path.join(testDirectory, `**/*${MARKUP_FILE_EXTENSION}`));
const tslintConfig = Linter.findConfiguration(path.join(testDirectory, "tslint.json"), null).results;
const tsConfig = path.join(testDirectory, "tsconfig.json");
let compilerOptions: ts.CompilerOptions = {};
let compilerOptions: ts.CompilerOptions = { allowJs: true };
if (fs.existsSync(tsConfig)) {
const {config, error} = ts.readConfigFile(tsConfig, ts.sys.readFile);
if (error) {
throw new Error(JSON.stringify(error));
}
compilerOptions = ts.parseJsonConfigFileContent(config, {readDirectory: ts.sys.readDirectory}, testDirectory).options;

const parseConfigHost = {
fileExists: fs.existsSync,
readDirectory: ts.sys.readDirectory,
useCaseSensitiveFileNames: true,
};
compilerOptions = ts.parseJsonConfigFileContent(config, parseConfigHost, testDirectory).options;
}
const results: TestResult = { directory: testDirectory, results: {} };

Expand Down
169 changes: 0 additions & 169 deletions src/tslintMulti.ts

This file was deleted.

1 comment on commit dbbe1e4

@alexeagle
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you!!!

Please sign in to comment.