7
7
import * as languageFacts from './languageFacts' ;
8
8
import { Rules , LintConfigurationSettings , Rule , Settings } from './lintRules' ;
9
9
import * as nodes from '../parser/cssNodes' ;
10
+ import calculateBoxModel , { Element } from './lintUtil' ;
11
+ import { union } from '../utils/arrays' ;
10
12
11
13
import * as nls from 'vscode-nls' ;
12
14
import { TextDocument } from 'vscode-languageserver-types' ;
13
15
const localize = nls . loadMessageBundle ( ) ;
14
16
15
- class Element {
16
-
17
- public name : string ;
18
- public node : nodes . Declaration ;
19
-
20
- constructor ( text : string , data : nodes . Declaration ) {
21
- this . name = text ;
22
- this . node = data ;
23
- }
24
- }
25
-
26
17
class NodesByRootMap {
27
18
public data : { [ name : string ] : { nodes : nodes . Node [ ] ; names : string [ ] } } = { } ;
28
19
@@ -58,7 +49,7 @@ export class LintVisitor implements nodes.IVisitor {
58
49
private keyframes : NodesByRootMap ;
59
50
private documentText : string ;
60
51
61
- private validProperties : { [ name : string ] : boolean } ;
52
+ private validProperties : { [ name : string ] : boolean } ;
62
53
63
54
private constructor ( document : TextDocument , settings : LintConfigurationSettings ) {
64
55
this . settings = settings ;
@@ -79,7 +70,7 @@ export class LintVisitor implements nodes.IVisitor {
79
70
}
80
71
}
81
72
82
- private isValidPropertyDeclaration ( decl : nodes . Declaration ) : boolean {
73
+ private isValidPropertyDeclaration ( decl : nodes . Declaration ) : boolean {
83
74
const propertyName = decl . getFullPropertyName ( ) . toLowerCase ( ) ;
84
75
return this . validProperties [ propertyName ] ;
85
76
}
@@ -274,7 +265,6 @@ export class LintVisitor implements nodes.IVisitor {
274
265
this . addEntry ( node . getSelectors ( ) , Rules . EmptyRuleSet ) ;
275
266
}
276
267
277
- let self = this ;
278
268
let propertyTable : Element [ ] = [ ] ;
279
269
for ( let element of declarations . getChildren ( ) ) {
280
270
if ( element instanceof nodes . Declaration ) {
@@ -290,46 +280,36 @@ export class LintVisitor implements nodes.IVisitor {
290
280
// No error when box-sizing property is specified, as it assumes the user knows what he's doing.
291
281
// see https://github.com/CSSLint/csslint/wiki/Beware-of-box-model-size
292
282
/////////////////////////////////////////////////////////////
293
- if ( this . fetch ( propertyTable , 'box-sizing' ) . length === 0 ) {
294
- let widthEntries = this . fetch ( propertyTable , 'width' ) ;
295
- if ( widthEntries . length > 0 ) {
296
- let problemDetected = false ;
297
- for ( let p of [ 'border' , 'border-left' , 'border-right' , 'padding' , 'padding-left' , 'padding-right' ] ) {
298
- let elements = this . fetch ( propertyTable , p ) ;
299
- for ( let element of elements ) {
300
- let value = element . node . getValue ( ) ;
301
- if ( value && ! value . matches ( 'none' ) ) {
302
- this . addEntry ( element . node , Rules . BewareOfBoxModelSize ) ;
303
- problemDetected = true ;
304
- }
305
- }
306
- }
307
- if ( problemDetected ) {
308
- for ( let widthEntry of widthEntries ) {
309
- this . addEntry ( widthEntry . node , Rules . BewareOfBoxModelSize ) ;
310
- }
311
- }
283
+ const boxModel = calculateBoxModel ( propertyTable ) ;
284
+ if ( boxModel . width ) {
285
+ let properties : Element [ ] = [ ] ;
286
+ if ( boxModel . right . value ) {
287
+ properties = union ( properties , boxModel . right . properties ) ;
312
288
}
313
- let heightEntries = this . fetch ( propertyTable , 'height' ) ;
314
- if ( heightEntries . length > 0 ) {
315
- let problemDetected = false ;
316
- for ( let p of [ 'border' , 'border-top' , 'border-bottom' , 'padding' , 'padding-top' , 'padding-bottom' ] ) {
317
- let elements = this . fetch ( propertyTable , p ) ;
318
- for ( let element of elements ) {
319
- let value = element . node . getValue ( ) ;
320
- if ( value && ! value . matches ( 'none' ) ) {
321
- this . addEntry ( element . node , Rules . BewareOfBoxModelSize ) ;
322
- problemDetected = true ;
323
- }
324
- }
289
+ if ( boxModel . left . value ) {
290
+ properties = union ( properties , boxModel . left . properties ) ;
291
+ }
292
+ if ( properties . length !== 0 ) {
293
+ for ( const item of properties ) {
294
+ this . addEntry ( item . node , Rules . BewareOfBoxModelSize ) ;
325
295
}
326
- if ( problemDetected ) {
327
- for ( let heightEntry of heightEntries ) {
328
- this . addEntry ( heightEntry . node , Rules . BewareOfBoxModelSize ) ;
329
- }
296
+ this . addEntry ( boxModel . width . node , Rules . BewareOfBoxModelSize ) ;
297
+ }
298
+ }
299
+ if ( boxModel . height ) {
300
+ let properties : Element [ ] = [ ] ;
301
+ if ( boxModel . top . value ) {
302
+ properties = union ( properties , boxModel . top . properties ) ;
303
+ }
304
+ if ( boxModel . bottom . value ) {
305
+ properties = union ( properties , boxModel . bottom . properties ) ;
306
+ }
307
+ if ( properties . length !== 0 ) {
308
+ for ( const item of properties ) {
309
+ this . addEntry ( item . node , Rules . BewareOfBoxModelSize ) ;
330
310
}
311
+ this . addEntry ( boxModel . height . node , Rules . BewareOfBoxModelSize ) ;
331
312
}
332
-
333
313
}
334
314
335
315
/////////////////////////////////////////////////////////////
@@ -340,14 +320,14 @@ export class LintVisitor implements nodes.IVisitor {
340
320
let displayElems = this . fetchWithValue ( propertyTable , 'display' , 'inline' ) ;
341
321
if ( displayElems . length > 0 ) {
342
322
for ( let prop of [ 'width' , 'height' , 'margin-top' , 'margin-bottom' , 'float' ] ) {
343
- let elem = self . fetch ( propertyTable , prop ) ;
323
+ let elem = this . fetch ( propertyTable , prop ) ;
344
324
for ( let index = 0 ; index < elem . length ; index ++ ) {
345
325
let node = elem [ index ] . node ;
346
326
let value = node . getValue ( ) ;
347
327
if ( prop === 'float' && ( ! value || value . matches ( 'none' ) ) ) {
348
328
continue ;
349
329
}
350
- self . addEntry ( node , Rules . PropertyIgnoredDueToDisplay , localize ( 'rule.propertyIgnoredDueToDisplayInline' , "Property is ignored due to the display. With 'display: inline', the width, height, margin-top, margin-bottom, and float properties have no effect." ) ) ;
330
+ this . addEntry ( node , Rules . PropertyIgnoredDueToDisplay , localize ( 'rule.propertyIgnoredDueToDisplayInline' , "Property is ignored due to the display. With 'display: inline', the width, height, margin-top, margin-bottom, and float properties have no effect." ) ) ;
351
331
}
352
332
}
353
333
}
@@ -502,7 +482,7 @@ export class LintVisitor implements nodes.IVisitor {
502
482
return true ;
503
483
}
504
484
505
- let decl = < nodes . Declaration > node . findParent ( nodes . NodeType . Declaration ) ;
485
+ let decl = < nodes . Declaration > node . findParent ( nodes . NodeType . Declaration ) ;
506
486
if ( decl ) {
507
487
let declValue = decl . getValue ( ) ;
508
488
if ( declValue ) {
@@ -559,7 +539,7 @@ export class LintVisitor implements nodes.IVisitor {
559
539
}
560
540
561
541
private visitHexColorValue ( node : nodes . HexColorValue ) : boolean {
562
- // Rule: #eeff0011 or #eeff00 or #ef01 or #ef0
542
+ // Rule: #eeff0011 or #eeff00 or #ef01 or #ef0
563
543
let length = node . length ;
564
544
if ( length !== 9 && length !== 7 && length !== 5 && length !== 4 ) {
565
545
this . addEntry ( node , Rules . HexColorLength ) ;
@@ -601,5 +581,3 @@ export class LintVisitor implements nodes.IVisitor {
601
581
return true ;
602
582
}
603
583
}
604
-
605
-
0 commit comments