Skip to content

Commit

Permalink
Workaround for palantir#1400. Excludes imported properties from error
Browse files Browse the repository at this point in the history
  • Loading branch information
IllusionMH committed Oct 9, 2016
1 parent 01e9e3c commit b73ccfe
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/rules/noUseBeforeDeclareRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export class Rule extends Lint.Rules.AbstractRule {
type VisitedVariables = {[varName: string]: boolean};

class NoUseBeforeDeclareWalker extends Lint.ScopeAwareRuleWalker<VisitedVariables> {
private importedPropertiesPositions: number[] = [];

constructor(sourceFile: ts.SourceFile, options: Lint.IOptions, private languageService: ts.LanguageService) {
super(sourceFile, options);
}
Expand Down Expand Up @@ -89,6 +91,9 @@ class NoUseBeforeDeclareWalker extends Lint.ScopeAwareRuleWalker<VisitedVariable

public visitNamedImports(node: ts.NamedImports) {
for (let namedImport of node.elements) {
if (namedImport.propertyName != null) {
this.saveImportedPropertiesPositions(namedImport.propertyName.getStart());
}
this.validateUsageForVariable(namedImport.name.text, namedImport.name.getStart());
}
super.visitNamedImports(node);
Expand Down Expand Up @@ -120,12 +125,20 @@ class NoUseBeforeDeclareWalker extends Lint.ScopeAwareRuleWalker<VisitedVariable
for (let highlight of highlights) {
for (let highlightSpan of highlight.highlightSpans) {
const referencePosition = highlightSpan.textSpan.start;
if (referencePosition < position) {
if (referencePosition < position && !this.isImportedPropertyName(referencePosition)) {
const failureString = Rule.FAILURE_STRING_PREFIX + name + Rule.FAILURE_STRING_POSTFIX;
this.addFailure(this.createFailure(referencePosition, name.length, failureString));
}
}
}
}
}

private saveImportedPropertiesPositions(position: number): void {
this.importedPropertiesPositions.push(position);
}

private isImportedPropertyName(position: number): boolean {
return this.importedPropertiesPositions.indexOf(position) !== -1;
}
}
9 changes: 9 additions & 0 deletions test/rules/no-use-before-declare/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,12 @@ function a() {
var b = ({c: dog}) => { dog++; };
b({ c: 5 });
}

import { ITest as ITest0 } from './ImportRegularAlias';
import {/*ensure comments works*/ ITest as ITest1 } from './ImportAliasWithComment';
import {
ITest as ITest2
} from './ImportWithLineBreaks';
import {First, ITest as ITest3 } from './ImportAliasSecond';

import ITest from './InterfaceFile';

0 comments on commit b73ccfe

Please sign in to comment.