Skip to content

Commit

Permalink
fix: report errors when bound to private in ngFor
Browse files Browse the repository at this point in the history
Fix #306
  • Loading branch information
mgechev committed May 8, 2017
1 parent ac3f651 commit 36705fc
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/angular/config.ts
Expand Up @@ -73,6 +73,7 @@ export const Config: Config = {
{ selector: '[md-tooltip], [mat-tooltip], [mdTooltip]', exportAs: 'mdTooltip' },
{ selector: 'md-select, mat-select', exportAs: 'mdSelect' },
{ selector: '[ngIf]', exportAs: 'ngIf', inputs: ['ngIf'] },
{ selector: '[ngFor][ngForOf]', exportAs: 'ngFor', inputs: ['ngForTemplate', 'ngForOf'] },
{ selector: '[ngSwitch]', exportAs: 'ngSwitch', inputs: ['ngSwitch'] }
],

Expand Down
4 changes: 2 additions & 2 deletions src/angular/templates/basicTemplateAstVisitor.ts
Expand Up @@ -51,7 +51,7 @@ const getExpressionDisplacement = (binding: any) => {
totalLength = binding.sourceSpan.end.offset - binding.sourceSpan.start.offset;
// The whitespace are possible only in:
// `[foo.bar] = "...."`,
// and they are verything except the attrLen and the valLen (-1 because of the close quote).
// and they are everything except the attrLen and the valLen (-1 because of the close quote).
let whitespace = totalLength - (attrLen + valLen) - 1;
// The resulted displacement is the length of the attribute + the whitespaces which
// can be located ONLY before the value (the binding).
Expand Down Expand Up @@ -99,12 +99,12 @@ export class BasicTemplateAstVisitor extends SourceMappingVisitor implements ast
visitNgContent(ast: ast.NgContentAst, context: any): any {}

visitEmbeddedTemplate(ast: ast.EmbeddedTemplateAst, context: any): any {
ast.directives.forEach(d => this.visit(d, context));
ast.variables.forEach(v => this.visit(v, context));
ast.children.forEach(e => this.visit(e, context));
ast.outputs.forEach(o => this.visit(o, context));
ast.attrs.forEach(a => this.visit(a, context));
ast.references.forEach(r => this.visit(r, context));
ast.directives.forEach(d => this.visit(d, context));
}

visitElement(element: ast.ElementAst, context: any): any {
Expand Down
2 changes: 1 addition & 1 deletion src/templatesUsePublicRule.ts
Expand Up @@ -83,7 +83,7 @@ export class Rule extends Lint.Rules.AbstractRule {
ruleName: 'templates-use-public-rule',
type: 'functionality',
description: `Ensure that properties and methods accessed from the template are public.`,
rationale: `When Angular compiles the templates, it has to access these propertes from outside the class.`,
rationale: `When Angular compiles the templates, it has to access these properties from outside the class.`,
options: null,
optionsDescription: `Not configurable.`,
typescriptOnly: true,
Expand Down
46 changes: 45 additions & 1 deletion test/templatesUsePublicRule.spec.ts
Expand Up @@ -192,7 +192,7 @@ describe('templates-use-public', () => {
assertSuccess('templates-use-public', source);
});

it.only('should succeed shadowed variable', () => {
it('should succeed shadowed variable', () => {
let source = `
@Component({
selector: 'foobar',
Expand All @@ -207,5 +207,49 @@ describe('templates-use-public', () => {
}`;
assertSuccess('templates-use-public', source);
});

// Angular doesn't provide the correct source span of the property.
it('should fail when private property used in *ngFor', () => {
let source = `
@Component({
selector: 'foobar',
template: \`
<div *ngFor="let smile of smile2">
~~~~~~
<smile-cmp [smile]="smile"></smile-cmp>
</div>
\`
})
class Test {
private smile2: any;
}`;
assertAnnotated({
ruleName: 'templates-use-public',
message: 'You can bind only to public class members. "smile2" is not a public class member.',
source
});
});

// Angular doesn't provide the correct source span of the property.
it('should fail when private property used in *ngFor', () => {
let source = `
@Component({
selector: 'foobar',
template: \`
<div *ngFor="let smile of smile">
~~~~~
<smile-cmp [smile]="smile"></smile-cmp>
</div>
\`
})
class Test {
private smile: any;
}`;
assertAnnotated({
ruleName: 'templates-use-public',
message: 'You can bind only to public class members. "smile" is not a public class member.',
source
});
});
});
});

0 comments on commit 36705fc

Please sign in to comment.