-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dbaf9d5
commit 7bb705b
Showing
6 changed files
with
68 additions
and
63 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,76 @@ | ||
'use strict'; | ||
const chalk = require('chalk'); | ||
const { ESLint } = require('eslint'); | ||
const {ESLint} = require('eslint'); | ||
|
||
module.exports = grunt => { | ||
grunt.registerMultiTask('eslint', 'Validate files with ESLint', async function () { | ||
grunt.registerMultiTask('eslint', 'Validate files with ESLint', function () { | ||
const done = this.async(); | ||
|
||
try { | ||
const { format, quiet, maxWarnings, failOnError, outputFile, ...options } = this.options({ | ||
outputFile: false, | ||
quiet: false, | ||
maxWarnings: -1, | ||
failOnError: true, | ||
format: "stylish" | ||
}); | ||
(async () => { | ||
try { | ||
const { | ||
format, | ||
quiet, | ||
maxWarnings, | ||
failOnError, | ||
outputFile, | ||
...options | ||
} = this.options({ | ||
outputFile: false, | ||
quiet: false, | ||
maxWarnings: -1, | ||
failOnError: true, | ||
format: 'stylish' | ||
}); | ||
|
||
if (this.filesSrc.length === 0) { | ||
grunt.log.writeln(chalk.magenta('Could not find any files to validate')); | ||
return true; | ||
} | ||
if (this.filesSrc.length === 0) { | ||
grunt.log.writeln(chalk.magenta('Could not find any files to validate')); | ||
return true; | ||
} | ||
|
||
const engine = new ESLint(options); | ||
const engine = new ESLint(options); | ||
|
||
const formatter = await engine.loadFormatter(format); | ||
const formatter = await engine.loadFormatter(format); | ||
|
||
if (!formatter) { | ||
grunt.warn(`Could not find formatter ${format}`); | ||
return false; | ||
} | ||
if (!formatter) { | ||
grunt.warn(`Could not find formatter ${format}`); | ||
return false; | ||
} | ||
|
||
let results = await engine.lintFiles(this.filesSrc); | ||
let results = await engine.lintFiles(this.filesSrc); | ||
|
||
if (options.fix) { | ||
await ESLint.outputFixes(results); | ||
} | ||
if (options.fix) { | ||
await ESLint.outputFixes(results); | ||
} | ||
|
||
if (quiet) { | ||
results = ESLint.getErrorResults(results); | ||
} | ||
if (quiet) { | ||
results = ESLint.getErrorResults(results); | ||
} | ||
|
||
const output = formatter.format(results); | ||
const output = formatter.format(results); | ||
|
||
if (outputFile) { | ||
grunt.file.write(outputFile, output); | ||
} else if (output) { | ||
console.log(output); | ||
} | ||
if (outputFile) { | ||
grunt.file.write(outputFile, output); | ||
} else if (output) { | ||
console.log(output); | ||
} | ||
|
||
const { warningCount, errorCount } = results.reduce((count, { warningCount, errorCount }) => { | ||
count.warningCount += warningCount; | ||
count.errorCount += errorCount; | ||
return count; | ||
}, { warningCount: 0, errorCount: 0 }); | ||
const {warningCount, errorCount} = results.reduce((count, {warningCount, errorCount}) => { | ||
count.warningCount += warningCount; | ||
count.errorCount += errorCount; | ||
return count; | ||
}, {warningCount: 0, errorCount: 0}); | ||
|
||
const tooManyWarnings = maxWarnings >= 0 && warningCount > maxWarnings; | ||
const tooManyWarnings = maxWarnings >= 0 && warningCount > maxWarnings; | ||
|
||
if (errorCount === 0 && tooManyWarnings) { | ||
grunt.warn(`ESLint found too many warnings (maximum: ${maxWarnings})`); | ||
} | ||
if (errorCount === 0 && tooManyWarnings) { | ||
grunt.warn(`ESLint found too many warnings (maximum: ${maxWarnings})`); | ||
} | ||
|
||
done(failOnError ? errorCount === 0 : 0); | ||
} catch (err) { | ||
done(err); | ||
} | ||
done(failOnError ? errorCount === 0 : 0); | ||
} catch (error) { | ||
done(error); | ||
} | ||
})(); | ||
}); | ||
}; | ||
}; |