diff --git a/src/bananaInBoxRule.ts b/src/bananaInBoxRule.ts index a0f52e3b8..8639a3078 100644 --- a/src/bananaInBoxRule.ts +++ b/src/bananaInBoxRule.ts @@ -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(); @@ -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 = (prop.sourceSpan).toString(); - if (InvalidSyntaxBoxRe.test(expr)) { + if (InvalidSyntaxBoxRe.test(prop.name)) { error = 'Invalid binding syntax. Use [(expr)] instead'; } if (error) { + const expr: any = (prop.sourceSpan).toString(); const internalStart = expr.indexOf(InvalidSyntaxBoxOpen) + 1; const start = prop.sourceSpan.start.offset + internalStart; const absolutePosition = this.getSourcePosition(start - 1); diff --git a/test/bananaInBoxRule.spec.ts b/test/bananaInBoxRule.spec.ts index 149bbdb44..a72f40185 100644 --- a/test/bananaInBoxRule.spec.ts +++ b/test/bananaInBoxRule.spec.ts @@ -14,8 +14,19 @@ describe('banana-in-box', () => { assertSuccess('banana-in-box', source); }); + it('should work with proper style', () => { + let source = ` + @Component({ + template: \` \` + }) + class Bar {} + `; + assertSuccess('banana-in-box', source); + }); + }); + describe('failure', () => { it('should fail when the box is in the banana', () => { let source = ` @@ -32,6 +43,22 @@ describe('banana-in-box', () => { source }); }); + + it('should fail when the box is in the banana', () => { + let source = ` + @Component({ + template: \` + ~~~~~~~~~~~~~ + \` + }) + class Bar {} + `; + assertAnnotated({ + ruleName: 'banana-in-box', + message: 'Invalid binding syntax. Use [(expr)] instead', + source + }); + }); }); describe('replacements', () => {