1
1
'use strict' ;
2
2
const chalk = require ( 'chalk' ) ;
3
- const eslint = require ( 'eslint' ) ;
3
+ const { ESLint } = require ( 'eslint' ) ;
4
4
5
5
module . exports = grunt => {
6
- grunt . registerMultiTask ( 'eslint' , 'Validate files with ESLint' , function ( ) {
7
- const options = this . options ( {
8
- outputFile : false ,
9
- quiet : false ,
10
- maxWarnings : - 1 ,
11
- failOnError : true ,
12
- } ) ;
13
-
14
- if ( this . filesSrc . length === 0 ) {
15
- grunt . log . writeln ( chalk . magenta ( 'Could not find any files to validate' ) ) ;
16
- return true ;
17
- }
6
+ grunt . registerMultiTask ( 'eslint' , 'Validate files with ESLint' , async function ( ) {
7
+ const done = this . async ( ) ;
18
8
19
- const formatter = eslint . CLIEngine . getFormatter ( options . format ) ;
9
+ try {
10
+ const { format, quiet, maxWarnings, failOnError, outputFile, ...options } = this . options ( {
11
+ outputFile : false ,
12
+ quiet : false ,
13
+ maxWarnings : - 1 ,
14
+ failOnError : true ,
15
+ format : "stylish"
16
+ } ) ;
20
17
21
- if ( ! formatter ) {
22
- grunt . warn ( ` Could not find formatter ${ options . format } ` ) ;
23
- return false ;
24
- }
18
+ if ( this . filesSrc . length === 0 ) {
19
+ grunt . log . writeln ( chalk . magenta ( ' Could not find any files to validate' ) ) ;
20
+ return true ;
21
+ }
25
22
26
- const engine = new eslint . CLIEngine ( options ) ;
23
+ const engine = new ESLint ( options ) ;
27
24
28
- let report ;
29
- try {
30
- report = engine . executeOnFiles ( this . filesSrc ) ;
31
- } catch ( error ) {
32
- grunt . warn ( error ) ;
33
- return false ;
34
- }
25
+ const formatter = await engine . loadFormatter ( format ) ;
35
26
36
- if ( options . fix ) {
37
- eslint . CLIEngine . outputFixes ( report ) ;
38
- }
27
+ if ( ! formatter ) {
28
+ grunt . warn ( `Could not find formatter ${ format } ` ) ;
29
+ return false ;
30
+ }
39
31
40
- let results = report . results ;
32
+ let results = await engine . lintFiles ( this . filesSrc ) ;
41
33
42
- if ( options . quiet ) {
43
- results = eslint . CLIEngine . getErrorResults ( results ) ;
44
- }
34
+ if ( options . fix ) {
35
+ await ESLint . outputFixes ( results ) ;
36
+ }
45
37
46
- const output = formatter ( results ) ;
38
+ if ( quiet ) {
39
+ results = ESLint . getErrorResults ( results ) ;
40
+ }
47
41
48
- if ( options . outputFile ) {
49
- grunt . file . write ( options . outputFile , output ) ;
50
- } else if ( output ) {
51
- console . log ( output ) ;
52
- }
42
+ const output = formatter . format ( results ) ;
53
43
54
- const tooManyWarnings = options . maxWarnings >= 0 && report . warningCount > options . maxWarnings ;
44
+ if ( outputFile ) {
45
+ grunt . file . write ( outputFile , output ) ;
46
+ } else if ( output ) {
47
+ console . log ( output ) ;
48
+ }
55
49
56
- if ( report . errorCount === 0 && tooManyWarnings ) {
57
- grunt . warn ( `ESLint found too many warnings (maximum: ${ options . maxWarnings } )` ) ;
58
- }
50
+ const { warningCount, errorCount } = results . reduce ( ( count , { warningCount, errorCount } ) => {
51
+ count . warningCount += warningCount ;
52
+ count . errorCount += errorCount ;
53
+ return count ;
54
+ } , { warningCount : 0 , errorCount : 0 } ) ;
59
55
60
- return options . failOnError ? report . errorCount === 0 : 0 ;
56
+ const tooManyWarnings = maxWarnings >= 0 && warningCount > maxWarnings ;
57
+
58
+ if ( errorCount === 0 && tooManyWarnings ) {
59
+ grunt . warn ( `ESLint found too many warnings (maximum: ${ maxWarnings } )` ) ;
60
+ }
61
+
62
+ done ( failOnError ? errorCount === 0 : 0 ) ;
63
+ } catch ( err ) {
64
+ done ( err ) ;
65
+ }
61
66
} ) ;
62
- } ;
67
+ } ;
0 commit comments