Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sindresorhus/grunt-eslint
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 61e98a9dfe4a2dfbf7a0ad00e91dee4cfd77b6c8
Choose a base ref
...
head repository: sindresorhus/grunt-eslint
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 82c984f0b7a6857f81ba3d230c59dcafe63036c7
Choose a head ref
  • 16 commits
  • 11 files changed
  • 5 contributors

Commits on Dec 31, 2020

  1. Copy the full SHA
    3de5e21 View commit details

Commits on Oct 15, 2021

  1. Copy the full SHA
    dbaf9d5 View commit details
  2. Cleanup

    sindresorhus committed Oct 15, 2021
    Copy the full SHA
    7bb705b View commit details
  3. 24.0.0

    sindresorhus committed Oct 15, 2021
    Copy the full SHA
    5e086f7 View commit details

Commits on Jul 8, 2022

  1. Meta tweaks

    sindresorhus committed Jul 8, 2022
    Copy the full SHA
    259ce5c View commit details

Commits on Dec 4, 2022

  1. Copy the full SHA
    1322e30 View commit details
  2. 24.0.1

    sindresorhus committed Dec 4, 2022
    Copy the full SHA
    63a9fe7 View commit details

Commits on May 9, 2023

  1. Copy the full SHA
    89eb056 View commit details
  2. 24.1.0

    sindresorhus committed May 9, 2023
    Copy the full SHA
    88cbcac View commit details

Commits on Jun 23, 2023

  1. Update dependencies

    sindresorhus committed Jun 23, 2023
    Copy the full SHA
    826ca5f View commit details
  2. 24.2.0

    sindresorhus committed Jun 23, 2023
    Copy the full SHA
    fac0aff View commit details

Commits on Jul 14, 2023

  1. Update dependencies

    sindresorhus committed Jul 14, 2023
    Copy the full SHA
    495118b View commit details
  2. v24.3.0

    sindresorhus committed Jul 14, 2023
    Copy the full SHA
    0704be0 View commit details

Commits on Apr 11, 2024

  1. Update to ESLint 9, support flat config, and require Node.js 18 (#175)

    Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
    prantlf and sindresorhus authored Apr 11, 2024
    Copy the full SHA
    81834b4 View commit details
  2. Meta tweaks

    sindresorhus committed Apr 11, 2024
    Copy the full SHA
    5d282f8 View commit details
  3. 25.0.0

    sindresorhus committed Apr 11, 2024
    Copy the full SHA
    82c984f View commit details
Showing with 131 additions and 86 deletions.
  1. +0 −4 .github/funding.yml
  2. +3 −0 .github/security.md
  3. +21 −0 .github/workflows/main.yml
  4. +0 −5 .travis.yml
  5. +8 −0 conf/eslint.js
  6. +0 −6 conf/eslint.json
  7. +9 −7 conf/rules/no-alert.js
  8. +11 −6 gruntfile.js
  9. +8 −7 package.json
  10. +13 −9 readme.md
  11. +58 −42 tasks/eslint.js
4 changes: 0 additions & 4 deletions .github/funding.yml

This file was deleted.

3 changes: 3 additions & 0 deletions .github/security.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Security Policy

To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.
21 changes: 21 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: CI
on:
- push
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 20
- 18
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

8 changes: 8 additions & 0 deletions conf/eslint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = [
{
rules: {
'no-alert': 1,
'camelcase': 2
}
}
];
6 changes: 0 additions & 6 deletions conf/eslint.json

This file was deleted.

16 changes: 9 additions & 7 deletions conf/rules/no-alert.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use strict';
module.exports = function (context) {
return {
CallExpression(node) {
if (node.callee.name === 'alert') {
context.report(node, 'testing custom rules.');
module.exports = {
create(context) {
return {
CallExpression(node) {
if (node.callee.name === 'alert') {
context.report(node, 'testing custom rules.');
}
}
}
};
};
}
};
17 changes: 11 additions & 6 deletions gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
'use strict';

const noAlertRule = require('./conf/rules/no-alert');

module.exports = grunt => {
grunt.initConfig({
eslint: {
options: {
configFile: 'conf/eslint.json',
rulePaths: ['conf/rules'],
overrideConfigFile: 'conf/eslint.js',
plugins: {
noAlertRule
},
quiet: true
},
validate: ['test/fixture/{1,2}.js']
@@ -13,15 +18,15 @@ module.exports = grunt => {
eslint: {
command: 'grunt eslint',
options: {
callback: (err, stdout, stderr, cb) => {
callback: (error, stdout, stderr, callback) => {
if (/test\/fixture\/2\.js/.test(stdout)) {
if (/camelcase/.test(stdout) && !/\swarning\s/.test(stdout)) {
cb();
callback();
} else {
cb(false);
callback(false);
}
} else {
cb(false);
callback(false);
}
}
}
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "grunt-eslint",
"version": "23.0.0",
"version": "25.0.0",
"description": "Validate files with ESLint",
"license": "MIT",
"repository": "sindresorhus/grunt-eslint",
@@ -10,8 +10,9 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"sideEffects": false,
"engines": {
"node": ">=10"
"node": "^18.18.0 || ^20.9.0 || >=21.1.0"
},
"scripts": {
"test": "grunt"
@@ -27,13 +28,13 @@
"report"
],
"dependencies": {
"chalk": "^4.0.0",
"eslint": "^7.0.0"
"chalk": "^4.1.2",
"eslint": "^9.0.0"
},
"devDependencies": {
"grunt": "^1.0.1",
"grunt-cli": "^1.2.0",
"grunt-shell": "^3.0.1"
"grunt": "^1.6.1",
"grunt-cli": "^1.4.3",
"grunt-shell": "^4.0.0"
},
"peerDependencies": {
"grunt": ">=1"
22 changes: 13 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# grunt-eslint [![Build Status](https://travis-ci.com/sindresorhus/grunt-eslint.svg?branch=master)](https://travis-ci.com/github/sindresorhus/grunt-eslint)
# grunt-eslint

> Validate files with [ESLint](https://eslint.org)
![](screenshot.png)

## Install

```
$ npm install --save-dev grunt-eslint
```sh
npm install --save-dev grunt-eslint
```

## Usage
@@ -29,11 +29,15 @@ grunt.registerTask('default', ['eslint']);
### Custom config and rules

```js
const noAlertRule = require('./conf/rules/no-alert');

grunt.initConfig({
eslint: {
options: {
configFile: 'conf/eslint.json',
rulePaths: ['conf/rules']
overrideConfigFile: 'conf/eslint.js',
plugins: {
noAlertRule
}
},
target: ['file.js']
}
@@ -46,7 +50,7 @@ grunt.initConfig({
grunt.initConfig({
eslint: {
options: {
format: require('eslint-tap')
format: './node_modules/eslint-tap/index.js'
},
target: ['file.js']
}
@@ -55,7 +59,7 @@ grunt.initConfig({

## Options

See the [ESLint options](https://eslint.org/docs/developer-guide/nodejs-api#cliengine).
See the [ESLint options](https://eslint.org/docs/developer-guide/nodejs-api#-new-eslintoptions).

In addition the following options are supported:

@@ -64,7 +68,7 @@ In addition the following options are supported:
Type: `string`\
Default: `'stylish'`

Name of a [built-in formatter](https://github.com/eslint/eslint/tree/master/lib/cli-engine/formatters) or path to a custom one.
The name of a [built-in formatter](https://github.com/eslint/eslint/tree/master/lib/cli-engine/formatters) or path to a custom one.

Some formatters you might find useful: [eslint-json](https://github.com/sindresorhus/eslint-json), [eslint-tap](https://github.com/sindresorhus/eslint-tap).

@@ -87,7 +91,7 @@ Report errors only.
Type: `number`\
Default: `-1` *(Means no limit)*

Number of warnings to trigger non-zero exit code.
The nmber of warnings to trigger non-zero exit code.

### failOnError

100 changes: 58 additions & 42 deletions tasks/eslint.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,78 @@
'use strict';
const chalk = require('chalk');
const eslint = require('eslint');
const {ESLint} = require('eslint');

module.exports = grunt => {
grunt.registerMultiTask('eslint', 'Validate files with ESLint', function () {
const options = this.options({
outputFile: false,
quiet: false,
maxWarnings: -1,
failOnError: true,
});
const done = this.async();

if (this.filesSrc.length === 0) {
grunt.log.writeln(chalk.magenta('Could not find any files to validate'));
return true;
}
(async () => {
try {
const {
format,
quiet,
maxWarnings,
failOnError,
outputFile,
...options
} = this.options({
outputFile: false,
quiet: false,
maxWarnings: -1,
failOnError: true,
format: 'stylish'
});

const formatter = eslint.CLIEngine.getFormatter(options.format);
if (this.filesSrc.length === 0) {
grunt.log.writeln(chalk.magenta('Could not find any files to validate'));
done(true);
return;
}

if (!formatter) {
grunt.warn(`Could not find formatter ${options.format}`);
return false;
}
const engine = new ESLint(options);

const engine = new eslint.CLIEngine(options);
const formatter = await engine.loadFormatter(format);

let report;
try {
report = engine.executeOnFiles(this.filesSrc);
} catch (error) {
grunt.warn(error);
return false;
}
if (!formatter) {
grunt.warn(`Could not find formatter ${format}`);
done(false);
return;
}

if (options.fix) {
eslint.CLIEngine.outputFixes(report);
}
let results = await engine.lintFiles(this.filesSrc);

let results = report.results;
if (options.fix) {
await ESLint.outputFixes(results);
}

if (options.quiet) {
results = eslint.CLIEngine.getErrorResults(results);
}
if (quiet) {
results = ESLint.getErrorResults(results);
}

const output = formatter(results);
const output = await formatter.format(results);

if (options.outputFile) {
grunt.file.write(options.outputFile, output);
} else if (output) {
console.log(output);
}
if (outputFile) {
grunt.file.write(outputFile, output);
} else if (output) {
console.log(output);
}

const tooManyWarnings = options.maxWarnings >= 0 && report.warningCount > options.maxWarnings;
const {warningCount, errorCount} = results.reduce((count, {warningCount, errorCount}) => {
count.warningCount += warningCount;
count.errorCount += errorCount;
return count;
}, {warningCount: 0, errorCount: 0});

if (report.errorCount === 0 && tooManyWarnings) {
grunt.warn(`ESLint found too many warnings (maximum: ${options.maxWarnings})`);
}
const tooManyWarnings = maxWarnings >= 0 && warningCount > maxWarnings;

return options.failOnError ? report.errorCount === 0 : 0;
if (errorCount === 0 && tooManyWarnings) {
grunt.warn(`ESLint found too many warnings (maximum: ${maxWarnings})`);
}

done(failOnError ? errorCount === 0 : 0);
} catch (error) {
done(error);
}
})();
});
};