@@ -11,7 +11,7 @@ import { dirname, resolve, sep } from 'path'
11
11
import * as request from 'request'
12
12
import * as stripJsonComments from 'strip-json-comments'
13
13
import type { HTMLHint as IHTMLHint } from '../core/core'
14
- import type { Hint , Ruleset } from '../core/types'
14
+ import type { Configuration , Hint } from '../core/types'
15
15
import { Formatter } from './formatter'
16
16
17
17
const HTMLHint : typeof IHTMLHint = require ( '../htmlhint.js' ) . HTMLHint
@@ -96,9 +96,11 @@ if (format) {
96
96
formatter . setFormat ( format )
97
97
}
98
98
99
+ // TODO: parse and validate `program.rules`
100
+
99
101
hintTargets ( arrTargets , {
100
102
rulesdir : program . rulesdir ,
101
- ruleset : program . rules ,
103
+ config : program . rules ? { rules : program . rules } : undefined ,
102
104
formatter : formatter ,
103
105
ignore : program . ignore ,
104
106
} )
@@ -121,7 +123,7 @@ function hintTargets(
121
123
arrTargets : string [ ] ,
122
124
options : {
123
125
formatter : Formatter
124
- ruleset ?: Ruleset
126
+ config ?: Configuration
125
127
rulesdir ?: string
126
128
ignore ?: string
127
129
}
@@ -213,7 +215,7 @@ function hintAllFiles(
213
215
options : {
214
216
ignore ?: string
215
217
formatter : Formatter
216
- ruleset ?: Ruleset
218
+ config ?: Configuration
217
219
} ,
218
220
onFinised : ( result : {
219
221
targetFileCount : number
@@ -241,22 +243,22 @@ function hintAllFiles(
241
243
time : number
242
244
} > = [ ]
243
245
244
- // init ruleset
245
- let ruleset = options . ruleset
246
- if ( ruleset === undefined ) {
247
- ruleset = getConfig ( program . config , globInfo . base , formatter )
246
+ // init config
247
+ let config = options . config
248
+ if ( config === undefined ) {
249
+ config = getConfig ( program . config , globInfo . base , formatter )
248
250
}
249
251
250
252
// hint queue
251
253
const hintQueue = asyncQueue < string > ( ( filepath , next ) => {
252
254
const startTime = new Date ( ) . getTime ( )
253
255
254
256
if ( filepath === 'stdin' ) {
255
- hintStdin ( ruleset , hintNext )
257
+ hintStdin ( config , hintNext )
256
258
} else if ( / ^ h t t p s ? : \/ \/ / . test ( filepath ) ) {
257
- hintUrl ( filepath , ruleset , hintNext )
259
+ hintUrl ( filepath , config , hintNext )
258
260
} else {
259
- const messages = hintFile ( filepath , ruleset )
261
+ const messages = hintFile ( filepath , config )
260
262
hintNext ( messages )
261
263
}
262
264
@@ -370,14 +372,15 @@ function getConfig(
370
372
configPath : string | undefined ,
371
373
base : string ,
372
374
formatter : Formatter
373
- ) {
375
+ ) : Configuration | undefined {
374
376
if ( configPath === undefined && existsSync ( base ) ) {
375
377
// find default config file in parent directory
376
378
if ( statSync ( base ) . isDirectory ( ) === false ) {
377
379
base = dirname ( base )
378
380
}
379
381
380
382
while ( base ) {
383
+ // TODO: load via cosmiconfig (https://github.com/htmlhint/HTMLHint/issues/126)
381
384
const tmpConfigFile = resolve ( base , '. htmlhintrc ')
382
385
383
386
if ( existsSync ( tmpConfigFile ) ) {
@@ -395,20 +398,22 @@ function getConfig(
395
398
396
399
// TODO: can configPath be undefined here?
397
400
if ( configPath !== undefined && existsSync ( configPath ) ) {
398
- const config = readFileSync ( configPath , 'utf - 8 ')
399
- let ruleset : Ruleset = { }
401
+ const configContent = readFileSync ( configPath , 'utf - 8 ')
402
+ let config : Configuration | undefined
400
403
401
404
try {
402
- ruleset = JSON . parse ( stripJsonComments ( config ) )
405
+ config = JSON . parse ( stripJsonComments ( configContent ) )
403
406
formatter . emit ( 'config ', {
404
- ruleset : ruleset ,
405
- configPath : configPath ,
407
+ configPath ,
408
+ config ,
406
409
} )
407
410
} catch ( e ) {
408
411
// ignore
409
412
}
410
413
411
- return ruleset
414
+ // TODO: validate config
415
+
416
+ return config
412
417
}
413
418
}
414
419
@@ -456,7 +461,7 @@ function walkPath(
456
461
}
457
462
458
463
// hint file
459
- function hintFile ( filepath : string , ruleset ?: Ruleset ) {
464
+ function hintFile ( filepath : string , config ?: Configuration ) {
460
465
let content = ''
461
466
462
467
try {
@@ -465,12 +470,12 @@ function hintFile(filepath: string, ruleset?: Ruleset) {
465
470
// ignore
466
471
}
467
472
468
- return HTMLHint . verify ( content , { rules : ruleset } )
473
+ return HTMLHint . verify ( content , config )
469
474
}
470
475
471
476
// hint stdin
472
477
function hintStdin (
473
- ruleset : Ruleset | undefined ,
478
+ config : Configuration | undefined ,
474
479
callback : ( messages : Hint [ ] ) = > void
475
480
) {
476
481
process . stdin . setEncoding ( 'utf8' )
@@ -483,20 +488,20 @@ function hintStdin(
483
488
484
489
process . stdin . on ( 'end' , ( ) => {
485
490
const content = buffers . join ( '' )
486
- const messages = HTMLHint . verify ( content , { rules : ruleset } )
491
+ const messages = HTMLHint . verify ( content , config )
487
492
callback ( messages )
488
493
} )
489
494
}
490
495
491
496
// hint url
492
497
function hintUrl (
493
498
url : string ,
494
- ruleset : Ruleset | undefined ,
499
+ config : Configuration | undefined ,
495
500
callback : ( messages : Hint [ ] ) = > void
496
501
) {
497
502
request . get ( url , ( error , response , body ) => {
498
503
if ( ! error && response . statusCode == 200 ) {
499
- const messages = HTMLHint . verify ( body , { rules : ruleset } )
504
+ const messages = HTMLHint . verify ( body , config )
500
505
callback ( messages )
501
506
} else {
502
507
callback ( [ ] )
0 commit comments