Skip to content

Commit

Permalink
feat: make angular whitespace configuragble
Browse files Browse the repository at this point in the history
  • Loading branch information
mgechev committed Jun 16, 2017
1 parent 3a06b48 commit 09c12d3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
16 changes: 14 additions & 2 deletions src/angularWhitespaceRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ const getReplacements = (text: ast.BoundTextAst, absolutePosition: number) => {
];
};

class InterpolationWhitespaceVisitor extends BasicTemplateAstVisitor {
type Option = 'check-interpolation' | 'check-pipe';

interface ConfigurableVisitor {
getOption(): Option;
}

class InterpolationWhitespaceVisitor extends BasicTemplateAstVisitor implements ConfigurableVisitor {
visitBoundText(text: ast.BoundTextAst, context: BasicTemplateAstVisitor): any {
if (ExpTypes.ASTWithSource(text.value)) {
// Note that will not be reliable for different interpolation symbols
Expand All @@ -49,15 +55,21 @@ class InterpolationWhitespaceVisitor extends BasicTemplateAstVisitor {
}
return null;
}

getOption(): Option {
return 'check-interpolation';
}
}

class WhitespaceTemplateVisitor extends BasicTemplateAstVisitor {
private visitors = [
private visitors: (BasicTemplateAstVisitor & ConfigurableVisitor)[] = [
new InterpolationWhitespaceVisitor(this.getSourceFile(), this.getOptions(), this.context, this.templateStart)
];

visitBoundText(text: ast.BoundTextAst, context: any): any {
const options = this.getOptions();
this.visitors
.filter(v => options.indexOf(v.getOption()) >= 0)
.map(v => v.visitBoundText(text, this))
.filter(f => !!f)
.forEach(f => this.addFailure(f));
Expand Down
15 changes: 9 additions & 6 deletions test/angularWhitespaceRule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe.only('angular-whitespace', () => {
})
class Bar {}
`;
assertSuccess('angular-whitespace', source);
assertSuccess('angular-whitespace', source, ['check-interpolation']);
});

it('should work with proper style and complex expressions', () => {
Expand All @@ -25,7 +25,7 @@ describe.only('angular-whitespace', () => {
})
class Bar {}
`;
assertSuccess('angular-whitespace', source);
assertSuccess('angular-whitespace', source, ['check-interpolation']);
});

it('should work with properties', () => {
Expand All @@ -37,7 +37,7 @@ describe.only('angular-whitespace', () => {
})
class Bar {}
`;
assertSuccess('angular-whitespace', source);
assertSuccess('angular-whitespace', source, ['check-interpolation']);
});
});

Expand All @@ -55,7 +55,8 @@ describe.only('angular-whitespace', () => {
assertAnnotated({
ruleName: 'angular-whitespace',
message: 'Missing whitespace in interpolation; expecting {{ expr }}',
source
source,
options: ['check-interpolation']
});
});
});
Expand All @@ -73,7 +74,8 @@ describe.only('angular-whitespace', () => {
const failures = assertAnnotated({
ruleName: 'angular-whitespace',
message: 'Missing whitespace in interpolation; expecting {{ expr }}',
source
source,
options: ['check-interpolation']
});

const res = Replacement.applyAll(source, failures[0].getFix());
Expand All @@ -99,7 +101,8 @@ describe.only('angular-whitespace', () => {
const failures = assertAnnotated({
ruleName: 'angular-whitespace',
message: 'Extra whitespace in interpolation; expecting {{ expr }}',
source
source,
options: ['check-interpolation']
});

const res = Replacement.applyAll(source, failures[0].getFix());
Expand Down

0 comments on commit 09c12d3

Please sign in to comment.