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

Commit

Permalink
Merge pull request #610 from palantir/use-tsx-extensions
Browse files Browse the repository at this point in the history
Use .tsx extensions when interacting with language services when appropriate
  • Loading branch information
adidahiya committed Aug 28, 2015
2 parents 631656d + 5bec928 commit e40bb19
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
1 change: 1 addition & 0 deletions lib/tslint.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions src/language/languageServiceHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
12 changes: 5 additions & 7 deletions src/rules/noUnusedVariableRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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}'`));
}
Expand Down
8 changes: 3 additions & 5 deletions src/rules/noUseBeforeDeclareRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down Expand Up @@ -101,7 +98,8 @@ class NoUseBeforeDeclareWalker extends Lint.ScopeAwareRuleWalker<VisitedVariable
}

private validateUsageForVariable(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]);
for (let highlight of highlights) {
for (let highlightSpan of highlight.highlightSpans) {
const referencePosition = highlightSpan.textSpan.start;
Expand Down
4 changes: 1 addition & 3 deletions test/tsxTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ describe("TSX syntax", () => {
});

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);
});
});

Expand Down

0 comments on commit e40bb19

Please sign in to comment.