@@ -4,19 +4,24 @@ import { SourceFile } from 'typescript/lib/typescript';
44import { CodeWithSourceMap , ComponentMetadata } from './angular/metadata' ;
55import { NgWalker } from './angular/ngWalker' ;
66
7+ const DEFAULT_ANIMATIONS_LIMIT : number = 15 ;
78const DEFAULT_STYLES_LIMIT : number = 3 ;
89const DEFAULT_TEMPLATE_LIMIT : number = 3 ;
10+ const OPTION_ANIMATIONS = 'animations' ;
911const OPTION_STYLES = 'styles' ;
1012const OPTION_TEMPLATE = 'template' ;
1113
1214export class Rule extends Rules . AbstractRule {
1315 static readonly metadata : IRuleMetadata = {
1416 description : 'Disallows having too many lines in inline template and styles. Forces separate template or styles file creation.' ,
1517 descriptionDetails : 'See more at https://angular.io/guide/styleguide#style-05-04.' ,
16- optionExamples : [ true , [ true , { [ OPTION_STYLES ] : 8 , [ OPTION_TEMPLATE ] : 5 } ] ] ,
18+ optionExamples : [ true , [ true , { [ OPTION_ANIMATIONS ] : 20 , [ OPTION_STYLES ] : 8 , [ OPTION_TEMPLATE ] : 5 } ] ] ,
1719 options : {
1820 items : {
1921 properties : {
22+ [ OPTION_ANIMATIONS ] : {
23+ type : 'number'
24+ } ,
2025 [ OPTION_STYLES ] : {
2126 type : 'number'
2227 } ,
@@ -31,7 +36,8 @@ export class Rule extends Rules.AbstractRule {
3136 type : 'array'
3237 } ,
3338 optionsDescription : Utils . dedent `
34- It can take an optional object with the properties '${ OPTION_STYLES } ' and '${ OPTION_TEMPLATE } ':
39+ It can take an optional object with the properties '${ OPTION_ANIMATIONS } ', '${ OPTION_STYLES } ' and '${ OPTION_TEMPLATE } ':
40+ * \`${ OPTION_ANIMATIONS } \` - number > 0 defining the maximum allowed inline lines for animations. Defaults to ${ DEFAULT_ANIMATIONS_LIMIT } .
3541 * \`${ OPTION_STYLES } \` - number > 0 defining the maximum allowed inline lines for styles. Defaults to ${ DEFAULT_STYLES_LIMIT } .
3642 * \`${ OPTION_TEMPLATE } \` - number > 0 defining the maximum allowed inline lines for template. Defaults to ${ DEFAULT_TEMPLATE_LIMIT } .
3743 ` ,
@@ -60,14 +66,17 @@ export class Rule extends Rules.AbstractRule {
6066 }
6167}
6268
63- type PropertyType = 'styles' | 'template' ;
64-
69+ type PropertyType = 'animations' | 'styles' | 'template' ;
6570export type PropertyPair = { [ key in PropertyType ] ?: number } ;
6671
6772const generateFailure = ( type : PropertyType , limit : number , value : number ) : string => {
6873 return sprintf ( Rule . FAILURE_STRING , type , limit , value ) ;
6974} ;
7075
76+ export const getAnimationsFailure = ( value : number , limit = DEFAULT_ANIMATIONS_LIMIT ) : string => {
77+ return generateFailure ( OPTION_ANIMATIONS , limit , value ) ;
78+ } ;
79+
7180export const getStylesFailure = ( value : number , limit = DEFAULT_STYLES_LIMIT ) : string => {
7281 return generateFailure ( OPTION_STYLES , limit , value ) ;
7382} ;
@@ -77,29 +86,54 @@ export const getTemplateFailure = (value: number, limit = DEFAULT_TEMPLATE_LIMIT
7786} ;
7887
7988export class MaxInlineDeclarationsWalker extends NgWalker {
89+ private readonly animationsLinesLimit = DEFAULT_ANIMATIONS_LIMIT ;
8090 private readonly stylesLinesLimit = DEFAULT_STYLES_LIMIT ;
8191 private readonly templateLinesLimit = DEFAULT_TEMPLATE_LIMIT ;
8292 private readonly newLineRegExp = / \r \n | \r | \n / ;
8393
8494 constructor ( sourceFile : SourceFile , options : IOptions ) {
8595 super ( sourceFile , options ) ;
8696
87- const { styles = - 1 , template = - 1 } = ( options . ruleArguments [ 0 ] || [ ] ) as PropertyPair ;
97+ const { animations = - 1 , styles = - 1 , template = - 1 } = ( options . ruleArguments [ 0 ] || [ ] ) as PropertyPair ;
8898
99+ this . animationsLinesLimit = animations > - 1 ? animations : this . animationsLinesLimit ;
89100 this . stylesLinesLimit = styles > - 1 ? styles : this . stylesLinesLimit ;
90101 this . templateLinesLimit = template > - 1 ? template : this . templateLinesLimit ;
91102 }
92103
93104 protected visitNgComponent ( metadata : ComponentMetadata ) : void {
94- this . validateInlineTemplate ( metadata ) ;
105+ this . validateInlineAnimations ( metadata ) ;
95106 this . validateInlineStyles ( metadata ) ;
107+ this . validateInlineTemplate ( metadata ) ;
96108 super . visitNgComponent ( metadata ) ;
97109 }
98110
99111 private getLinesCount ( source : CodeWithSourceMap [ 'source' ] ) : number {
100112 return source ! . trim ( ) . split ( this . newLineRegExp ) . length ;
101113 }
102114
115+ private getInlineAnimationsLinesCount ( metadata : ComponentMetadata ) : number {
116+ return ( metadata . animations || [ ] ) . reduce ( ( previousValue , currentValue ) => {
117+ previousValue += this . getLinesCount ( currentValue . animation . source ) ;
118+
119+ return previousValue ;
120+ } , 0 ) ;
121+ }
122+
123+ private validateInlineAnimations ( metadata : ComponentMetadata ) : void {
124+ const linesCount = this . getInlineAnimationsLinesCount ( metadata ) ;
125+
126+ if ( linesCount <= this . animationsLinesLimit ) {
127+ return ;
128+ }
129+
130+ const failureMessage = getAnimationsFailure ( linesCount , this . animationsLinesLimit ) ;
131+
132+ for ( const animation of metadata . animations ) {
133+ this . addFailureAtNode ( animation . node ! , failureMessage ) ;
134+ }
135+ }
136+
103137 private getInlineStylesLinesCount ( metadata : ComponentMetadata ) : number {
104138 return ( metadata . styles || [ ] ) . reduce ( ( previousValue , currentValue ) => {
105139 if ( ! currentValue . url ) {
@@ -123,6 +157,7 @@ export class MaxInlineDeclarationsWalker extends NgWalker {
123157 this . addFailureAtNode ( style . node ! , failureMessage ) ;
124158 }
125159 }
160+
126161 private getTemplateLinesCount ( metadata : ComponentMetadata ) : number {
127162 return this . hasInlineTemplate ( metadata ) ? this . getLinesCount ( metadata . template . template . source ) : 0 ;
128163 }
0 commit comments