Skip to content

Commit

Permalink
Merge pull request #381 from wKoza/gh_379
Browse files Browse the repository at this point in the history
fix(rules) : fix banana-in-a-box rule
  • Loading branch information
mgechev authored Aug 8, 2017
2 parents 5ef90aa + 666bb92 commit 8719674
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/bananaInBoxRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const InvalidSyntaxBoxOpen = '([';
const InvalidSyntaxBoxClose = '])';
const ValidSyntaxOpen = '[(';
const ValidSyntaxClose = ')]';
const InvalidSyntaxBoxRe = new RegExp('\\(\\[(.*?)\\]\\)(.*?)');
const InvalidSyntaxBoxRe = new RegExp('\\[(.*?)\\]');

const getReplacements = (text: ast.BoundEventAst, absolutePosition: number) => {
const expr: string = (text.sourceSpan as any).toString();
Expand All @@ -29,15 +29,14 @@ class BananaInBoxTemplateVisitor extends BasicTemplateAstVisitor {

visitEvent(prop: ast.BoundEventAst, context: any): any {

if (prop.sourceSpan) {
// Note that will not be reliable for different interpolation symbols
if (prop.name) {
let error = null;
const expr: any = (<any>prop.sourceSpan).toString();
if (InvalidSyntaxBoxRe.test(expr)) {
if (InvalidSyntaxBoxRe.test(prop.name)) {
error = 'Invalid binding syntax. Use [(expr)] instead';
}

if (error) {
const expr: any = (<any>prop.sourceSpan).toString();
const internalStart = expr.indexOf(InvalidSyntaxBoxOpen) + 1;
const start = prop.sourceSpan.start.offset + internalStart;
const absolutePosition = this.getSourcePosition(start - 1);
Expand Down
27 changes: 27 additions & 0 deletions test/bananaInBoxRule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@ describe('banana-in-box', () => {
assertSuccess('banana-in-box', source);
});

it('should work with proper style', () => {
let source = `
@Component({
template: \` <a (click)="navigate(['/resources'])"> \`
})
class Bar {}
`;
assertSuccess('banana-in-box', source);
});

});


describe('failure', () => {
it('should fail when the box is in the banana', () => {
let source = `
Expand All @@ -32,6 +43,22 @@ describe('banana-in-box', () => {
source
});
});

it('should fail when the box is in the banana', () => {
let source = `
@Component({
template: \` <comp ([bar])="foo" name="foo"></comp>
~~~~~~~~~~~~~
\`
})
class Bar {}
`;
assertAnnotated({
ruleName: 'banana-in-box',
message: 'Invalid binding syntax. Use [(expr)] instead',
source
});
});
});

describe('replacements', () => {
Expand Down

0 comments on commit 8719674

Please sign in to comment.