@@ -8,7 +8,7 @@ function cli(api){
8
8
//-------------------------------------------------------------------------
9
9
// Helper functions
10
10
//-------------------------------------------------------------------------
11
-
11
+
12
12
/**
13
13
* Returns an array of messages for a particular type.
14
14
* @param messages {Array} Array of CSS Lint messages.
@@ -18,7 +18,7 @@ function cli(api){
18
18
function pluckByType ( messages , type ) {
19
19
return messages . filter ( function ( message ) {
20
20
return message . type === type ;
21
- } ) ;
21
+ } ) ;
22
22
}
23
23
24
24
/**
@@ -29,24 +29,24 @@ function cli(api){
29
29
function gatherRules ( options , ruleset ) {
30
30
var warnings = options . rules || options . warnings ,
31
31
errors = options . errors ;
32
-
32
+
33
33
if ( warnings ) {
34
34
ruleset = ruleset || { } ;
35
35
warnings . split ( "," ) . forEach ( function ( value ) {
36
36
ruleset [ value ] = 1 ;
37
37
} ) ;
38
38
}
39
-
39
+
40
40
if ( errors ) {
41
41
ruleset = ruleset || { } ;
42
42
errors . split ( "," ) . forEach ( function ( value ) {
43
43
ruleset [ value ] = 2 ;
44
44
} ) ;
45
45
}
46
-
46
+
47
47
return ruleset ;
48
48
}
49
-
49
+
50
50
/**
51
51
* Filters out rules using the ignore command line option.
52
52
* @param options {Object} the CLI options
@@ -55,17 +55,51 @@ function cli(api){
55
55
function filterRules ( options ) {
56
56
var ignore = options . ignore ,
57
57
ruleset = null ;
58
-
58
+
59
59
if ( ignore ) {
60
60
ruleset = CSSLint . getRuleset ( ) ;
61
61
ignore . split ( "," ) . forEach ( function ( value ) {
62
62
delete ruleset [ value ] ;
63
- } ) ;
63
+ } ) ;
64
64
}
65
-
65
+
66
66
return ruleset ;
67
67
}
68
68
69
+
70
+ /**
71
+ * Filters out files using the exclude-list command line option.
72
+ * @param files {Array} the list of files to check for exclusions
73
+ * @param options {Object} the CLI options
74
+ * @return {Array } A list of files
75
+ */
76
+ function filterFiles ( files , options ) {
77
+ var excludeList = options [ "exclude-list" ] ,
78
+ excludeFiles = [ ] ,
79
+ filesToLint = files ;
80
+
81
+
82
+ if ( excludeList ) {
83
+ // Build up the exclude list, expanding any directory exclusions that were passed in
84
+ excludeList . split ( "," ) . forEach ( function ( value ) {
85
+ if ( api . isDirectory ( value ) ) {
86
+ excludeFiles = excludeFiles . concat ( api . getFiles ( value ) ) ;
87
+ } else {
88
+ excludeFiles . push ( value ) ;
89
+ }
90
+ } ) ;
91
+
92
+ // Remove the excluded files from the list of files to lint
93
+ excludeFiles . forEach ( function ( value ) {
94
+ if ( filesToLint . indexOf ( value ) > - 1 ) {
95
+ filesToLint . splice ( filesToLint . indexOf ( value ) , 1 ) ;
96
+ }
97
+ } ) ;
98
+ }
99
+
100
+ return filesToLint ;
101
+ }
102
+
69
103
/**
70
104
* Outputs all available rules to the CLI.
71
105
* @return {void }
@@ -107,12 +141,12 @@ function cli(api){
107
141
if ( output ) {
108
142
api . print ( output ) ;
109
143
}
110
-
144
+
111
145
if ( messages . length > 0 && pluckByType ( messages , "error" ) . length > 0 ) {
112
146
exitCode = 1 ;
113
147
}
114
148
}
115
-
149
+
116
150
return exitCode ;
117
151
}
118
152
@@ -126,14 +160,15 @@ function cli(api){
126
160
"\nUsage: csslint-rhino.js [options]* [file|dir]*" ,
127
161
" " ,
128
162
"Global Options" ,
129
- " --help Displays this information." ,
130
- " --format=<format> Indicate which format to use for output." ,
131
- " --list-rules Outputs all of the rules available." ,
132
- " --quiet Only output when errors are present." ,
133
- " --errors=<rule[,rule]+> Indicate which rules to include as errors." ,
134
- " --warnings=<rule[,rule]+> Indicate which rules to include as warnings." ,
135
- " --ignore=<rule,[,rule]+> Indicate which rules to ignore completely." ,
136
- " --version Outputs the current version number."
163
+ " --help Displays this information." ,
164
+ " --format=<format> Indicate which format to use for output." ,
165
+ " --list-rules Outputs all of the rules available." ,
166
+ " --quiet Only output when errors are present." ,
167
+ " --errors=<rule[,rule]+> Indicate which rules to include as errors." ,
168
+ " --warnings=<rule[,rule]+> Indicate which rules to include as warnings." ,
169
+ " --ignore=<rule[,rule]+> Indicate which rules to ignore completely." ,
170
+ " --exclude-list=<file|dir[,file|dir]+> Indicate which files/directories to exclude from being linted." ,
171
+ " --version Outputs the current version number."
137
172
] . join ( "\n" ) + "\n" ) ;
138
173
}
139
174
@@ -143,56 +178,58 @@ function cli(api){
143
178
* @param options {Object} options object
144
179
* @return {Number } exit code
145
180
*/
146
- function processFiles ( files , options ) {
181
+ function processFiles ( fileArray , options ) {
147
182
var exitCode = 0 ,
148
183
formatId = options . format || "text" ,
149
184
formatter ,
185
+ files = filterFiles ( fileArray , options ) ,
150
186
output ;
151
-
187
+
152
188
if ( ! files . length ) {
153
189
api . print ( "csslint: No files specified." ) ;
154
190
exitCode = 1 ;
155
191
} else {
156
192
if ( ! CSSLint . hasFormat ( formatId ) ) {
157
193
api . print ( "csslint: Unknown format '" + formatId + "'. Cannot proceed." ) ;
158
- exitCode = 1 ;
194
+ exitCode = 1 ;
159
195
} else {
160
196
formatter = CSSLint . getFormatter ( formatId ) ;
161
-
197
+
162
198
output = formatter . startFormat ( ) ;
163
199
if ( output ) {
164
200
api . print ( output ) ;
165
201
}
166
202
203
+
167
204
files . forEach ( function ( file ) {
168
205
if ( exitCode === 0 ) {
169
206
exitCode = processFile ( file , options ) ;
170
207
} else {
171
208
processFile ( file , options ) ;
172
209
}
173
210
} ) ;
174
-
211
+
175
212
output = formatter . endFormat ( ) ;
176
213
if ( output ) {
177
214
api . print ( output ) ;
178
215
}
179
216
}
180
217
}
181
218
return exitCode ;
182
- }
183
-
184
-
219
+ }
220
+
221
+
185
222
function processArguments ( args , options ) {
186
223
var arg = args . shift ( ) ,
187
224
argName ,
188
225
parts ,
189
226
files = [ ] ;
190
-
227
+
191
228
while ( arg ) {
192
229
if ( arg . indexOf ( "--" ) === 0 ) {
193
230
argName = arg . substring ( 2 ) ;
194
231
options [ argName ] = true ;
195
-
232
+
196
233
if ( argName . indexOf ( "=" ) > - 1 ) {
197
234
parts = argName . split ( "=" ) ;
198
235
options [ parts [ 0 ] ] = parts [ 1 ] ;
@@ -201,7 +238,7 @@ function cli(api){
201
238
}
202
239
203
240
} else {
204
-
241
+
205
242
//see if it's a directory or a file
206
243
if ( api . isDirectory ( arg ) ) {
207
244
files = files . concat ( api . getFiles ( arg ) ) ;
@@ -211,24 +248,24 @@ function cli(api){
211
248
}
212
249
arg = args . shift ( ) ;
213
250
}
214
-
251
+
215
252
options . files = files ;
216
253
return options ;
217
254
}
218
-
255
+
219
256
function readConfigFile ( options ) {
220
257
var data = api . readFile ( api . getFullPath ( ".csslintrc" ) ) ;
221
- if ( data ) {
258
+ if ( data ) {
222
259
options = processArguments ( data . split ( / [ \s \n \r ] + / m) , options ) ;
223
260
api . print ( "ignore = " + options . ignore ) ;
224
261
api . print ( "errors = " + options . errors ) ;
225
262
api . print ( "warnings = " + options . warnings ) ;
226
263
}
227
-
264
+
228
265
return options ;
229
266
}
230
-
231
-
267
+
268
+
232
269
233
270
//-----------------------------------------------------------------------------
234
271
// Process command line
@@ -237,10 +274,10 @@ function cli(api){
237
274
var args = api . args ,
238
275
argCount = args . length ,
239
276
options = { } ;
240
-
277
+
241
278
// first look for config file .csslintrc
242
- options = readConfigFile ( options ) ;
243
-
279
+ options = readConfigFile ( options ) ;
280
+
244
281
// Command line arguments override config file
245
282
options = processArguments ( args , options ) ;
246
283
0 commit comments