From 5bec92813d93760568eef1b23c3bfc738ceb80a6 Mon Sep 17 00:00:00 2001 From: Jason Killian Date: Wed, 26 Aug 2015 00:35:24 -0400 Subject: [PATCH] Use correct filename when using language services - this allows .tsx files to be handled properly --- lib/tslint.d.ts | 1 + src/language/languageServiceHost.ts | 6 ++++++ src/rules/noUnusedVariableRule.ts | 12 +++++------- src/rules/noUseBeforeDeclareRule.ts | 8 +++----- test/tsxTests.ts | 4 +--- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/tslint.d.ts b/lib/tslint.d.ts index 27341b18ec6..053822a825c 100644 --- a/lib/tslint.d.ts +++ b/lib/tslint.d.ts @@ -116,6 +116,7 @@ declare module Lint { } declare module Lint { function createLanguageServiceHost(fileName: string, source: string): ts.LanguageServiceHost; + function createLanguageService(fileName: string, source: string): ts.LanguageService; } declare module Lint.Rules { class AbstractRule implements Lint.IRule { diff --git a/src/language/languageServiceHost.ts b/src/language/languageServiceHost.ts index fe2dcbfb2d8..cb95f32734c 100644 --- a/src/language/languageServiceHost.ts +++ b/src/language/languageServiceHost.ts @@ -28,4 +28,10 @@ module Lint { return host; } + + export function createLanguageService(fileName: string, source: string) { + const documentRegistry = ts.createDocumentRegistry(); + const languageServiceHost = Lint.createLanguageServiceHost(fileName, source); + return ts.createLanguageService(languageServiceHost, documentRegistry); + } } diff --git a/src/rules/noUnusedVariableRule.ts b/src/rules/noUnusedVariableRule.ts index b38f44367d9..dd6361d3227 100644 --- a/src/rules/noUnusedVariableRule.ts +++ b/src/rules/noUnusedVariableRule.ts @@ -20,25 +20,22 @@ export class Rule extends Lint.Rules.AbstractRule { public static FAILURE_STRING = "unused variable: "; public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { - const documentRegistry = ts.createDocumentRegistry(); - const languageServiceHost = Lint.createLanguageServiceHost("file.ts", sourceFile.getFullText()); - const languageService = ts.createLanguageService(languageServiceHost, documentRegistry); - + const languageService = Lint.createLanguageService(sourceFile.fileName, sourceFile.getFullText()); return this.applyWithWalker(new NoUnusedVariablesWalker(sourceFile, this.getOptions(), languageService)); } } class NoUnusedVariablesWalker extends Lint.RuleWalker { + private languageService: ts.LanguageService; private skipBindingElement: boolean; private skipParameterDeclaration: boolean; private skipVariableDeclaration: boolean; - private languageService: ts.LanguageService; constructor(sourceFile: ts.SourceFile, options: Lint.IOptions, languageService: ts.LanguageService) { super(sourceFile, options); + this.languageService = languageService; this.skipVariableDeclaration = false; this.skipParameterDeclaration = false; - this.languageService = languageService; } public visitBindingElement(node: ts.BindingElement) { @@ -200,7 +197,8 @@ class NoUnusedVariablesWalker extends Lint.RuleWalker { } private validateReferencesForVariable(name: string, position: number) { - const highlights = this.languageService.getDocumentHighlights("file.ts", position, ["file.ts"]); + const fileName = this.getSourceFile().fileName; + const highlights = this.languageService.getDocumentHighlights(fileName, position, [fileName]); if (highlights == null || highlights[0].highlightSpans.length <= 1) { this.addFailure(this.createFailure(position, name.length, `${Rule.FAILURE_STRING}'${name}'`)); } diff --git a/src/rules/noUseBeforeDeclareRule.ts b/src/rules/noUseBeforeDeclareRule.ts index 3d47c12947a..2d4abbea1f1 100644 --- a/src/rules/noUseBeforeDeclareRule.ts +++ b/src/rules/noUseBeforeDeclareRule.ts @@ -19,10 +19,7 @@ export class Rule extends Lint.Rules.AbstractRule { public static FAILURE_STRING_POSTFIX = "' used before declaration"; public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { - const documentRegistry = ts.createDocumentRegistry(); - const languageServiceHost = Lint.createLanguageServiceHost("file.ts", sourceFile.getFullText()); - const languageService = ts.createLanguageService(languageServiceHost, documentRegistry); - + const languageService = Lint.createLanguageService(sourceFile.fileName, sourceFile.getFullText()); return this.applyWithWalker(new NoUseBeforeDeclareWalker(sourceFile, this.getOptions(), languageService)); } } @@ -101,7 +98,8 @@ class NoUseBeforeDeclareWalker extends Lint.ScopeAwareRuleWalker { }); it("with no false positives", () => { - // todo (#558): there should only be 3, but there is a false positive no-unused-variable failure due - // to a compiler bug - assert.lengthOf(actualFailures, 4); + assert.lengthOf(actualFailures, 3); }); });