Skip to content

Commit

Permalink
fix(rules): check for proper modifier in base class
Browse files Browse the repository at this point in the history
  • Loading branch information
mgechev committed Feb 19, 2017
1 parent 2901718 commit 2e285e2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/templatesUsePublicRule.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Lint from 'tslint';
import * as ts from 'typescript';
import {stringDistance} from './util/utils';
import {getDeclaredProperties, getDeclaredMethods} from './util/classDeclarationUtils';
import {getClassMembers} from './util/classDeclarationUtils';
import {Ng2Walker} from './angular/ng2Walker';
import {RecursiveAngularExpressionVisitor} from './angular/templates/recursiveAngularExpressionVisitor';
import * as e from '@angular/compiler/src/expression_parser/ast';
Expand Down Expand Up @@ -34,7 +34,8 @@ class SymbolAccessValidator extends RecursiveAngularExpressionVisitor {
}
ast = <e.PropertyRead>receiver;
}
const allMembers = getDeclaredMethods(this.context.controller).concat(getDeclaredProperties(this.context.controller));
const allMembers = getClassMembers(this.context.controller, this.languageService.getProgram().getTypeChecker())
.map(s => s.declarations[0]);
const member = allMembers.filter((m: any) => m.name && m.name.text === ast.name).pop();
if (member) {
let isPublic = !member.modifiers || !member.modifiers
Expand Down Expand Up @@ -73,14 +74,16 @@ class SymbolAccessValidator extends RecursiveAngularExpressionVisitor {
}
}

export class Rule extends Lint.Rules.AbstractRule {
export class Rule extends Lint.Rules.TypedRule {
static FAILURE: string = 'The %s "%s" that you\'re trying to access does not exist in the class declaration.';

public apply(sourceFile:ts.SourceFile): Lint.RuleFailure[] {
public applyWithProgram(sourceFile: ts.SourceFile, languageService: ts.LanguageService): Lint.RuleFailure[] {
const sf = languageService.getProgram().getSourceFiles().filter(sf => sf.fileName === sourceFile.fileName).pop();
return this.applyWithWalker(
new Ng2Walker(sourceFile,
new Ng2Walker(sf,
this.getOptions(), {
expressionVisitorCtrl: SymbolAccessValidator
expressionVisitorCtrl: SymbolAccessValidator,
languageService
}));
}
}
Expand Down
40 changes: 40 additions & 0 deletions test/templatesUsePublicRule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,44 @@ describe('templates-use-public', () => {
assertSuccess('templates-use-public', source);
});
});

describe('inheritance', () => {
it('should support inheritance chain', () => {
let source = `
class Base {
readonly foo: any;
}
@Component({
selector: 'foobar',
template: '<div>{{ foo }}</div>
})
class Test extends Base {}`;
assertSuccess('templates-use-public', source);
});

it('should fail in case of protected inherited property', () => {
let source = `
class Base {
readonly protected foo: any;
}
@Component({
selector: 'foobar',
template: '<div>{{ foo }}</div>
})
class Test extends Base {}`;
assertFailure('templates-use-public', source, {
message: 'You can bind only to public class members.',
startPosition: {
line: 7,
character: 29
},
endPosition: {
line: 7,
character: 32
}
});
});
});
});

0 comments on commit 2e285e2

Please sign in to comment.