Skip to content

Commit

Permalink
fix(rule): inline template max lines rule #536 (#556)
Browse files Browse the repository at this point in the history
* Rule now only validates inline template and styles, before this fix it was looking also into template / style files
  • Loading branch information
NagRock authored and wKoza committed Apr 4, 2018
1 parent 671e954 commit 174ed46
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/maxInlineDeclarationsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export class Rule extends Lint.Rules.AbstractRule {
hasFix: false
};

private templateLinesLimit: number = 3;
private stylesLinesLimit: number = 3;
private readonly templateLinesLimit: number = 3;
private readonly stylesLinesLimit: number = 3;

constructor(options: IOptions) {
super(options);
Expand Down Expand Up @@ -66,16 +66,16 @@ export class MaxInlineDeclarationsValidator extends NgWalker {
}

private hasInlineTemplate(metadata: ComponentMetadata): boolean {
return !!metadata.template && !!metadata.template.template && !!metadata.template.template.source;
return !!metadata.template && !metadata.template.url && !!metadata.template.template && !!metadata.template.template.source;
}

private getTemplateLinesCount(metadata: ComponentMetadata): number {
return metadata.template.template.source.split(this.newLineRegExp).length;
}

private validateInlineStyles(metadata: ComponentMetadata): void {
if (this.hasInlineStyles(metadata) && this.getStylesLinesCount(metadata) > this.stylesLinesLimit) {
const stylesLinesCount = this.getStylesLinesCount(metadata);
if (this.hasInlineStyles(metadata) && this.getInlineStylesLinesCount(metadata) > this.stylesLinesLimit) {
const stylesLinesCount = this.getInlineStylesLinesCount(metadata);
const msg = `Inline styles lines limit exceeded. Defined limit: ${this.stylesLinesLimit} / styles lines: ${stylesLinesCount}`;
for (let i = 0; i < metadata.styles.length; i++) {
this.addFailureAtNode(metadata.styles[i].node, msg);
Expand All @@ -90,18 +90,20 @@ export class MaxInlineDeclarationsValidator extends NgWalker {

for (let i = 0; i < metadata.styles.length; i++) {
const style = metadata.styles[i];
if (style.style && style.style.source) {
if (!style.url && style.style && style.style.source) {
return true;
}
}

return false;
}

private getStylesLinesCount(metadata: ComponentMetadata) {
private getInlineStylesLinesCount(metadata: ComponentMetadata) {
let result = 0;
for (let i = 0; i < metadata.styles.length; i++) {
result += metadata.styles[i].style.source.split(this.newLineRegExp).length;
if (!metadata.styles[i].url) {
result += metadata.styles[i].style.source.split(this.newLineRegExp).length;
}
}
return result;
}
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/inlineTemplateMaxLines/large.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
div {
width: 300px;
height: 300px;
font-size: 30px;
display: block;
float: left;
}
7 changes: 7 additions & 0 deletions test/fixtures/inlineTemplateMaxLines/large.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div>
<p>This</p>
<p>is</p>
<p>sample</p>
<p>template</p>
<p>file</p>
</div>
28 changes: 28 additions & 0 deletions test/inlineTemplateMaxLinesRule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ describe('max-inline-declarations', () => {
assertSuccess('max-inline-declarations', ast);
});
});

describe('component template file exceeded inline template line limit', () => {
it('should not report any failure', () => {
const source = `
@Component({
selector: 'foobar',
templateUrl: './large.html'
})
class Test {}`;

const ast = getAst(source, __dirname + '/../../test/fixtures/inlineTemplateMaxLines/foo.ts');
assertSuccess('max-inline-declarations', ast);
});
});
});

describe('styles', () => {
Expand Down Expand Up @@ -203,5 +217,19 @@ describe('max-inline-declarations', () => {
assertSuccess('max-inline-declarations', ast);
});
});

describe('component styles file exceeded inline styles line limit', () => {
it('should not report any failure', () => {
const source = `
@Component({
selector: 'foobar',
styleUrls: ['./large.css']
})
class Test {}`;

const ast = getAst(source, __dirname + '/../../test/fixtures/inlineTemplateMaxLines/foo.ts');
assertSuccess('max-inline-declarations', ast);
});
});
});
});

0 comments on commit 174ed46

Please sign in to comment.