Skip to content

Commit

Permalink
fix : change metadata rule
Browse files Browse the repository at this point in the history
  • Loading branch information
wKoza committed Jun 14, 2017
1 parent 7d0dec0 commit 9b16c2f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
34 changes: 17 additions & 17 deletions src/bananaInBoxRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ import {BasicTemplateAstVisitor} from './angular/templates/basicTemplateAstVisit
import {NgWalker} from './angular/ngWalker';


const BoxInABananaOpen = '([';
const BoxInABananaClose = '])';
const BananaInABoxOpen = '[(';
const BananaInABoxClose = ')]';
const BoxInABananaRe = new RegExp('\\(\\[(.*?)\\]\\)(.*?)');
const InvalidSyntaxBoxOpen = '([';
const InvalidSyntaxBoxClose = '])';
const ValidSyntaxOpen = '[(';
const ValidSyntaxClose = ')]';
const InvalidSyntaxBoxRe = new RegExp('\\(\\[(.*?)\\]\\)(.*?)');

const getReplacements = (text: ast.BoundEventAst, absolutePosition: number) => {
const expr: string = (text.sourceSpan as any).toString();
const internalStart = expr.indexOf('([');
const internalEnd = expr.lastIndexOf('])');
const len = internalEnd - internalStart - BoxInABananaClose.length;
const trimmed = expr.substr(internalStart + BoxInABananaOpen.length, len).trim();
const internalStart = expr.indexOf(InvalidSyntaxBoxOpen);
const internalEnd = expr.lastIndexOf(InvalidSyntaxBoxClose);
const len = internalEnd - internalStart - InvalidSyntaxBoxClose.length;
const trimmed = expr.substr(internalStart + InvalidSyntaxBoxOpen.length, len).trim();

return [
new Lint.Replacement(absolutePosition,
internalEnd - internalStart + BoxInABananaClose.length,
`${BananaInABoxOpen}${trimmed}${BananaInABoxClose}`)
internalEnd - internalStart + ValidSyntaxClose.length,
`${ValidSyntaxOpen}${trimmed}${ValidSyntaxClose}`)
];
};

Expand All @@ -33,12 +33,12 @@ class BananaInBoxTemplateVisitor extends BasicTemplateAstVisitor {
// Note that will not be reliable for different interpolation symbols
let error = null;
const expr: any = (<any>prop.sourceSpan).toString();
if (BoxInABananaRe.test(expr)) {
error = 'The box is in a banana! Expecting Banana in a box [(expr)]';
if (InvalidSyntaxBoxRe.test(expr)) {
error = 'Invalid binding syntax. Use [(expr)] instead';
}

if (error) {
const internalStart = expr.indexOf(BoxInABananaOpen);
const internalStart = expr.indexOf(InvalidSyntaxBoxOpen);
const start = prop.sourceSpan.start.offset + internalStart;
const absolutePosition = this.getSourcePosition(start);

Expand All @@ -56,8 +56,8 @@ export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: 'banana-in-box',
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 properties from outside the class.`,
description: `Ensure that the two-way data binding syntax is correct.`,
rationale: `The parens "()" should have been inside the brackets "[]".`,
options: null,
optionsDescription: `Not configurable.`,
typescriptOnly: true,
Expand All @@ -73,4 +73,4 @@ export class Rule extends Lint.Rules.AbstractRule {
templateVisitorCtrl: BananaInBoxTemplateVisitor,
}));
}
}
}
4 changes: 2 additions & 2 deletions test/bananaInBoxRule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('banana-in-box', () => {
`;
assertAnnotated({
ruleName: 'banana-in-box',
message: 'The box is in a banana! Expecting Banana in a box [(expr)]',
message: 'Invalid binding syntax. Use [(expr)] instead',
source
});
});
Expand All @@ -46,7 +46,7 @@ describe('banana-in-box', () => {
`;
const failures = assertAnnotated({
ruleName: 'banana-in-box',
message: 'The box is in a banana! Expecting Banana in a box [(expr)]',
message: 'Invalid binding syntax. Use [(expr)] instead',
source
});

Expand Down

0 comments on commit 9b16c2f

Please sign in to comment.