Skip to content

Commit 8719674

Browse files
authored
Merge pull request #381 from wKoza/gh_379
fix(rules) : fix banana-in-a-box rule
2 parents 5ef90aa + 666bb92 commit 8719674

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/bananaInBoxRule.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const InvalidSyntaxBoxOpen = '([';
99
const InvalidSyntaxBoxClose = '])';
1010
const ValidSyntaxOpen = '[(';
1111
const ValidSyntaxClose = ')]';
12-
const InvalidSyntaxBoxRe = new RegExp('\\(\\[(.*?)\\]\\)(.*?)');
12+
const InvalidSyntaxBoxRe = new RegExp('\\[(.*?)\\]');
1313

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

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

32-
if (prop.sourceSpan) {
33-
// Note that will not be reliable for different interpolation symbols
32+
if (prop.name) {
3433
let error = null;
35-
const expr: any = (<any>prop.sourceSpan).toString();
36-
if (InvalidSyntaxBoxRe.test(expr)) {
34+
if (InvalidSyntaxBoxRe.test(prop.name)) {
3735
error = 'Invalid binding syntax. Use [(expr)] instead';
3836
}
3937

4038
if (error) {
39+
const expr: any = (<any>prop.sourceSpan).toString();
4140
const internalStart = expr.indexOf(InvalidSyntaxBoxOpen) + 1;
4241
const start = prop.sourceSpan.start.offset + internalStart;
4342
const absolutePosition = this.getSourcePosition(start - 1);

test/bananaInBoxRule.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,19 @@ describe('banana-in-box', () => {
1414
assertSuccess('banana-in-box', source);
1515
});
1616

17+
it('should work with proper style', () => {
18+
let source = `
19+
@Component({
20+
template: \` <a (click)="navigate(['/resources'])"> \`
21+
})
22+
class Bar {}
23+
`;
24+
assertSuccess('banana-in-box', source);
25+
});
26+
1727
});
1828

29+
1930
describe('failure', () => {
2031
it('should fail when the box is in the banana', () => {
2132
let source = `
@@ -32,6 +43,22 @@ describe('banana-in-box', () => {
3243
source
3344
});
3445
});
46+
47+
it('should fail when the box is in the banana', () => {
48+
let source = `
49+
@Component({
50+
template: \` <comp ([bar])="foo" name="foo"></comp>
51+
~~~~~~~~~~~~~
52+
\`
53+
})
54+
class Bar {}
55+
`;
56+
assertAnnotated({
57+
ruleName: 'banana-in-box',
58+
message: 'Invalid binding syntax. Use [(expr)] instead',
59+
source
60+
});
61+
});
3562
});
3663

3764
describe('replacements', () => {

0 commit comments

Comments
 (0)