Skip to content

Commit

Permalink
fix(rules): handle the desugered syntax of ngFor
Browse files Browse the repository at this point in the history
Fix #346
  • Loading branch information
mgechev committed Jul 1, 2017
1 parent 8a24aed commit 77965af
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
29 changes: 20 additions & 9 deletions src/angular/templates/basicTemplateAstVisitor.ts
Expand Up @@ -45,21 +45,32 @@ const getExpressionDisplacement = (binding: any) => {
if (!(binding instanceof ast.BoundDirectivePropertyAst)) {
attrLen = binding.name.length + 4 + subBindingLen;
}

// Total length of the attribute value
if (binding instanceof ast.BoundEventAst) {
valLen = binding.handler.span.end;
} else if (binding instanceof ast.BoundDirectivePropertyAst && binding.templateName === 'ngForOf') {
// Handling everything except [ngForOf] differently
// [ngForOf] requires different logic because it gets desugered from *ngFor
const content = binding.sourceSpan.end.file.content;
const ngForSubstr = content.substring(binding.sourceSpan.start.offset, binding.sourceSpan.end.offset);
result = ngForSubstr.lastIndexOf((binding.value as any).source) + '*ngFor'.length;
} else {
valLen = binding.value.span.end;
}
// Total length of the entire part of the template AST corresponding to this AST.
totalLength = binding.sourceSpan.end.offset - binding.sourceSpan.start.offset;
// The whitespace are possible only in:
// `[foo.bar] = "...."`,
// and they are everything except the attrLen and the valLen (-1 because of the close quote).
let whitespace = totalLength - (attrLen + valLen) - 1;
// The resulted displacement is the length of the attribute + the whitespaces which
// can be located ONLY before the value (the binding).
result = whitespace + attrLen + binding.sourceSpan.start.offset;

// Handling everything except [ngForOf]
if (!(binding instanceof ast.BoundDirectivePropertyAst) || binding.templateName !== 'ngForOf') {
// Total length of the entire part of the template AST corresponding to this AST.
totalLength = binding.sourceSpan.end.offset - binding.sourceSpan.start.offset;
// The whitespace are possible only in:
// `[foo.bar] = "...."`,
// and they are everything except the attrLen and the valLen (-1 because of the close quote).
let whitespace = totalLength - (attrLen + valLen) - 1;
// The resulted displacement is the length of the attribute + the whitespaces which
// can be located ONLY before the value (the binding).
result = whitespace + attrLen + binding.sourceSpan.start.offset;
}
} else if (binding instanceof ast.BoundTextAst) {
result = binding.sourceSpan.start.offset;
}
Expand Down
1 change: 0 additions & 1 deletion src/angularWhitespaceRule.ts
Expand Up @@ -159,7 +159,6 @@ class PipeWhitespaceVisitor extends RecursiveAngularExpressionVisitor implements
const exprEnd = context.getSourcePosition(ast.exp.span.end);
const sf = context.getSourceFile().getFullText();
const exprText = sf.substring(exprStart, exprEnd);

const replacements = [];
// Handling the right side of the pipe
let leftBeginning = exprEnd + 1; // exprEnd === '|'
Expand Down
4 changes: 2 additions & 2 deletions test/angularWhitespaceRule.spec.ts
Expand Up @@ -55,7 +55,7 @@ describe('angular-whitespace', () => {
assertSuccess('angular-whitespace', source, ['check-pipe']);
});

it.only('should succeed with in structural directives with proper style', () => {
it('should succeed with in structural directives with proper style', () => {
let source = `
@Component({
selector: 'foo',
Expand Down Expand Up @@ -128,7 +128,7 @@ describe('angular-whitespace', () => {
@Component({
selector: 'foo',
template: \`
<div *ngFor="let pony of ponies | slice:0:1">{{ pony }}</div>
<div *ngFor="let pony of ponies | slice:0:1 ">{{ pony }}</div>
\`
})
class Bar {
Expand Down
4 changes: 2 additions & 2 deletions test/noAccessMissingMemberRule.spec.ts
Expand Up @@ -657,7 +657,7 @@ describe('no-access-missing-member', () => {
let source = `
@Component({
template: \`<div *ngIf="context"></div>
~~~~~~~
~~~~~~~
\`
})
class Test {
Expand All @@ -674,7 +674,7 @@ describe('no-access-missing-member', () => {
let source = `
@Component({
template: \`<div *ngSwitch="context">
~~~~~~~
~~~~~~~
<span *ngSwitchCase="bar"></span>
</div>\`
})
Expand Down
4 changes: 2 additions & 2 deletions test/templatesUsePublicRule.spec.ts
Expand Up @@ -215,7 +215,7 @@ describe('templates-use-public', () => {
selector: 'foobar',
template: \`
<div *ngFor="let smile of smile2">
~~~~~~
~~~~~~
<smile-cmp [smile]="smile"></smile-cmp>
</div>
\`
Expand All @@ -237,7 +237,7 @@ describe('templates-use-public', () => {
selector: 'foobar',
template: \`
<div *ngFor="let smile of smile">
~~~~~
~~~~~
<smile-cmp [smile]="smile"></smile-cmp>
</div>
\`
Expand Down

0 comments on commit 77965af

Please sign in to comment.