Skip to content

Commit 3609b2e

Browse files
IllusionMHnchen63
authored andcommitted
Workaround for palantir#1400. Excludes imported properties from error (palantir#1620)
1 parent fa8f644 commit 3609b2e

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/rules/noUseBeforeDeclareRule.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ export class Rule extends Lint.Rules.AbstractRule {
4646
type VisitedVariables = {[varName: string]: boolean};
4747

4848
class NoUseBeforeDeclareWalker extends Lint.ScopeAwareRuleWalker<VisitedVariables> {
49+
private importedPropertiesPositions: number[] = [];
50+
4951
constructor(sourceFile: ts.SourceFile, options: Lint.IOptions, private languageService: ts.LanguageService) {
5052
super(sourceFile, options);
5153
}
@@ -89,6 +91,9 @@ class NoUseBeforeDeclareWalker extends Lint.ScopeAwareRuleWalker<VisitedVariable
8991

9092
public visitNamedImports(node: ts.NamedImports) {
9193
for (let namedImport of node.elements) {
94+
if (namedImport.propertyName != null) {
95+
this.saveImportedPropertiesPositions(namedImport.propertyName.getStart());
96+
}
9297
this.validateUsageForVariable(namedImport.name.text, namedImport.name.getStart());
9398
}
9499
super.visitNamedImports(node);
@@ -120,12 +125,20 @@ class NoUseBeforeDeclareWalker extends Lint.ScopeAwareRuleWalker<VisitedVariable
120125
for (let highlight of highlights) {
121126
for (let highlightSpan of highlight.highlightSpans) {
122127
const referencePosition = highlightSpan.textSpan.start;
123-
if (referencePosition < position) {
128+
if (referencePosition < position && !this.isImportedPropertyName(referencePosition)) {
124129
const failureString = Rule.FAILURE_STRING_PREFIX + name + Rule.FAILURE_STRING_POSTFIX;
125130
this.addFailure(this.createFailure(referencePosition, name.length, failureString));
126131
}
127132
}
128133
}
129134
}
130135
}
136+
137+
private saveImportedPropertiesPositions(position: number): void {
138+
this.importedPropertiesPositions.push(position);
139+
}
140+
141+
private isImportedPropertyName(position: number): boolean {
142+
return this.importedPropertiesPositions.indexOf(position) !== -1;
143+
}
131144
}

test/rules/no-use-before-declare/test.ts.lint

+9
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,12 @@ function a() {
7272
var b = ({c: dog}) => { dog++; };
7373
b({ c: 5 });
7474
}
75+
76+
import { ITest as ITest0 } from './ImportRegularAlias';
77+
import {/*ensure comments works*/ ITest as ITest1 } from './ImportAliasWithComment';
78+
import {
79+
ITest as ITest2
80+
} from './ImportWithLineBreaks';
81+
import {First, ITest as ITest3 } from './ImportAliasSecond';
82+
83+
import ITest from './InterfaceFile';

0 commit comments

Comments
 (0)