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

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Oct 7, 2016
1 parent 9e1c647 commit 5293361
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
19 changes: 11 additions & 8 deletions src/language/languageServiceHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,17 @@ export function wrapProgram(program: ts.Program): ts.LanguageService {
}

export function checkEdit(ls: ts.LanguageService, sf: ts.SourceFile, newText: string) {
(ls as any).editFile(sf.fileName, newText);
const newProgram = ls.getProgram();
const newSf = newProgram.getSourceFile(sf.fileName);
const newDiags = ts.getPreEmitDiagnostics(newProgram, newSf);
// revert
(ls as any).editFile(sf.fileName, sf.getFullText());
return newDiags;

if ("editFile" in Object.keys(ls)) {
const host = ls as any as LanguageServiceEditableHost;
host.editFile(sf.fileName, newText);
const newProgram = ls.getProgram();
const newSf = newProgram.getSourceFile(sf.fileName);
const newDiags = ts.getPreEmitDiagnostics(newProgram, newSf);
// revert
host.editFile(sf.fileName, sf.getFullText());
return newDiags;
}
return [];
}

export function createLanguageServiceHost(fileName: string, source: string): ts.LanguageServiceHost {
Expand Down
14 changes: 3 additions & 11 deletions src/rules/noUnusedVariableRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const REACT_NAMESPACE_IMPORT_NAME = "React";

const MODULE_SPECIFIER_MATCH = /^["'](.+)['"]$/;

export class Rule extends Lint.Rules.TypedRule {
export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "no-unused-variable",
Expand Down Expand Up @@ -75,12 +75,7 @@ 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, languageService: ts.LanguageService): Lint.RuleFailure[] {
return this.applyWithProgram(sourceFile, languageService);
}

public applyWithProgram(sourceFile: ts.SourceFile, languageService: ts.LanguageService): Lint.RuleFailure[] {
return this.applyWithWalker(new NoUnusedVariablesWalker(sourceFile, this.getOptions(), languageService));
}
}
Expand All @@ -94,7 +89,6 @@ class NoUnusedVariablesWalker extends Lint.RuleWalker {
private ignorePattern: RegExp;
private isReactUsed: boolean;
private reactImport: ts.NamespaceImport;
private dummyLanguageService: boolean;
private possibleFailures: Lint.RuleFailure[] = [];

constructor(sourceFile: ts.SourceFile, options: Lint.IOptions,
Expand Down Expand Up @@ -148,7 +142,7 @@ 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()) && !this.dummyLanguageService) {
if (this.possibleFailures.some(f => f.hasFix())) {
let newText = Lint.Fix.applyAll(this.getSourceFile().getFullText(),
this.possibleFailures.map(f => f.getFix()).filter(f => !!f));

Expand All @@ -164,9 +158,7 @@ class NoUnusedVariablesWalker extends Lint.RuleWalker {
this.addFailure(f);
} else {
let newText = f.getFix().apply(this.getSourceFile().getFullText());
if (Lint.checkEdit(this.languageService, this.getSourceFile(), newText).length > 0) {
console.error(`Found one of the broken fixes in ${this.getSourceFile().fileName}`);
} else {
if (Lint.checkEdit(this.languageService, this.getSourceFile(), newText).length === 0) {
this.addFailure(f);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"declaration": true,
"sourceMap": false,
"target": "es5",
"outDir": "../build/src"
"outDir": "../lib"
},
"atom": {
"rewriteTsconfig": false
Expand Down

0 comments on commit 5293361

Please sign in to comment.