Skip to content

Commit

Permalink
fix(rules): make sure we use defined in the template variables
Browse files Browse the repository at this point in the history
Fix #123
  • Loading branch information
mgechev committed Oct 19, 2016
1 parent 6060ce0 commit cbd86e1
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
2 changes: 0 additions & 2 deletions src/angular/ng2Walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ export class Ng2Walker extends Lint.RuleWalker {
new this._config.templateVisitorCtrl(
this.getSourceFile(), this._originalOptions, context, baseStart, this._config.expressionVisitorCtrl);
compiler.templateVisitAll(visitor, roots, context);
// roots.forEach(r => r.visit(visitor, null));
// roots.forEach((root: compiler.TemplateAst) => visitor.visit(root));
visitor.getFailures().forEach(f => this.addFailure(f));
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/angular/templates/basicTemplateAstVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export interface TemplateAstVisitorCtr {
}

export class BasicTemplateAstVisitor extends Lint.RuleWalker implements ast.TemplateAstVisitor {
private _variables = [];

constructor(sourceFile: ts.SourceFile,
private _originalOptions: Lint.IOptions,
Expand All @@ -84,6 +85,7 @@ export class BasicTemplateAstVisitor extends Lint.RuleWalker implements ast.Temp
protected visitNg2TemplateAST(ast: e.AST, templateStart: number) {
const templateVisitor =
new this.expressionVisitorCtrl(this.getSourceFile(), this._originalOptions, this.context, templateStart);
templateVisitor.preDefinedVariables = this._variables;
templateVisitor.visit(ast);
templateVisitor.getFailures().forEach(f => this.addFailure(f));
}
Expand Down Expand Up @@ -113,11 +115,11 @@ export class BasicTemplateAstVisitor extends Lint.RuleWalker implements ast.Temp
}

visitReference(ast: ast.ReferenceAst, context: any): any {
console.log(ast);
// console.log(ast);
}

visitVariable(ast: ast.VariableAst, context: any): any {
console.log(ast);
this._variables.push(ast.name);
}

visitEvent(ast: ast.BoundEventAst, context: any): any {
Expand Down
2 changes: 2 additions & 0 deletions src/angular/templates/recursiveAngularExpressionVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as ts from 'typescript';
import * as e from '@angular/compiler/src/expression_parser/ast';

export class RecursiveAngularExpressionVisitor extends Lint.RuleWalker implements e.AstVisitor {
public preDefinedVariables = [];

constructor(sourceFile: ts.SourceFile, options: Lint.IOptions,
protected context: ts.ClassDeclaration, protected basePosition: number) {
super(sourceFile, options);
Expand Down
3 changes: 2 additions & 1 deletion src/noAccessMissingMemberRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class SymbolAccessValidator extends RecursiveAngularExpressionVisitor {
symbolType = 'property';
}
available = getDeclaredMethodNames(this.context)
.concat(getDeclaredPropertyNames(this.context));
.concat(getDeclaredPropertyNames(this.context))
.concat(this.preDefinedVariables);
ast.receiver.visit(this);
// Do not support nested properties yet
if (ast.receiver && (<any>ast.receiver).name) {
Expand Down
19 changes: 17 additions & 2 deletions test/noAccessMissingMemberRule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ describe('no-access-missing-member', () => {
assertSuccess('no-access-missing-member', source);
});

it('should work with getters', () => {
it('should work with setters', () => {
let source = `
@Component({
selector: 'foobar',
Expand All @@ -434,7 +434,7 @@ describe('no-access-missing-member', () => {
// let source = `
// @Component({
// template: \`<form #loginForm="ngForm">
// <button type="submit" [disabled]="!loginForm.valid">Se connecter</button>
// <button type="submit" [disabled]="!loginForm.valid"></button>
// </form>\`
// })
// class Test {
Expand All @@ -444,6 +444,21 @@ describe('no-access-missing-member', () => {
// assertSuccess('no-access-missing-member', source);
// });

it('should work with getters', () => {
let source = `
@Component({
template: \`
<ul>
<li *ngFor="let bar of foo">{{ bar }}</li>
</ul>
\`
})
class Test {
foo = [];
}`;
assertSuccess('no-access-missing-member', source);
});

// TODO
// it('should work with getters', () => {
// let source = `
Expand Down

0 comments on commit cbd86e1

Please sign in to comment.