Skip to content

Commit daf3d4d

Browse files
colingmlukastaegert
authored andcommitted
fix(eslint): Prepare eslint for rollup 3 and upgrade eslint to newest version (#1309)
Prior to these changes, the eslint plugin was using an older version of eslint that relied on a class called CLIEngine. In newer versions this has now been deprecated and so upgrading versions required also updating to using ESLint internally instead. Ultimately this doesn't change the usage of the plugin but some of the option names have changed for ESLint (notably configFile needs to change to overrideConfigFile). The main driving factor for this change is to prevent conflicts that happen when using @rollup/plugin-typescript and this plugin together. Without this update, eslint would run on the transpiled code and so the error messages weren't as helpful. This update will allow the two plugins to work well together and provide helpful information about errors and warnings. This change also includes some necessary package updates to prepare eslint for Rollup 3. BREAKING CHANGES: - options structure has changed due to moving from CLIEngine t ESlint. Review the new supported options below: https://eslint.org/docs/latest/developer-guide/nodejs-api#-new-eslintoptions - requires Node 14
1 parent 69146cd commit daf3d4d

File tree

10 files changed

+2200
-1990
lines changed

10 files changed

+2200
-1990
lines changed

packages/eslint/README.md

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
[npm]: https://img.shields.io/npm/v/@rollup/plugin-alias
2-
[npm-url]: https://www.npmjs.com/package/@rollup/plugin-alias
3-
[size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-alias
4-
[size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-alias
1+
[npm]: https://img.shields.io/npm/v/@rollup/plugin-eslint
2+
[npm-url]: https://www.npmjs.com/package/@rollup/plugin-eslint
3+
[size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-eslint
4+
[size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-eslint
55

66
[![npm][npm]][npm-url]
77
[![size][size]][size-url]
@@ -11,6 +11,10 @@
1111

1212
🍣 A Rollup plugin to lint entry points and all imported files with ESLint.
1313

14+
## Requirements
15+
16+
This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v14.0.0+) and Rollup v1.20.0+.
17+
1418
## Install
1519

1620
Using npm:
@@ -38,51 +42,51 @@ export default {
3842

3943
## Options
4044

41-
See more options here [eslint-config](http://eslint.org/docs/developer-guide/nodejs-api#cliengine).
45+
This plugin takes a configuration object intended for the [ESLint constructor](https://eslint.org/docs/developer-guide/nodejs-api#-new-eslintoptions) with the addition of a `throwOnWarning`, `throwOnError`, `formatter`, `include` and `exclude` prop.
4246

4347
You can also use eslint configuration in the form of a `.eslintrc.*` file in your project's root. It will be loaded automatically.
4448

45-
### fix
49+
### exclude
4650

47-
Type: `Boolean`<br>
48-
Default: `false`
51+
Type: `String | String[]`<br>
52+
Default: `node_modules/**`
4953

50-
If true, will auto fix source code.
54+
A single [`picomatch`](https://github.com/micromatch/picomatch) pattern or an array of patterns controlling which files this plugin should explicitly include. Gets forwarded to the [`createFilter`](https://github.com/rollup/plugins/tree/master/packages/pluginutils#createfilter) method of `@rollup/pluginutils`.
5155

52-
### throwOnError
56+
### fix
5357

5458
Type: `Boolean`<br>
5559
Default: `false`
5660

57-
If true, will throw an error if any errors were found.
61+
If true, will auto fix source code.
5862

59-
### throwOnWarning
63+
### formatter
6064

61-
Type: `Boolean`<br>
62-
Default: `false`
65+
Type: `Function | String`<br>
66+
Default: `stylish`
6367

64-
If true, will throw an error if any warnings were found.
68+
Custom error formatter, the name of a built-in formatter, or the path to a custom formatter.
6569

6670
### include
6771

68-
Type: `Array | String`<br>
72+
Type: `String | String[]`<br>
6973
Default: `[]`
7074

71-
A single file, or array of files, to include when linting.
75+
A single [`picomatch`](https://github.com/micromatch/picomatch) pattern or an array of patterns controlling which files this plugin should explicitly include. Gets forwarded to the [`createFilter`](https://github.com/rollup/plugins/tree/master/packages/pluginutils#createfilter) method of `@rollup/pluginutils`.
7276

73-
### exclude
77+
### throwOnError
7478

75-
Type: `Array | String`<br>
76-
Default: `node_modules/**`
79+
Type: `Boolean`<br>
80+
Default: `false`
7781

78-
A single file, or array of files, to exclude when linting.
82+
If true, will throw an error and exit the process when ESLint reports any errors.
7983

80-
### formatter
84+
### throwOnWarning
8185

82-
Type: `Function | String`<br>
83-
Default: `stylish`
86+
Type: `Boolean`<br>
87+
Default: `false`
8488

85-
Custom error formatter or the name of a built-in formatter.
89+
If true, will throw an error and exit the process when ESLint reports any warnings.
8690

8791
## Meta
8892

packages/eslint/package.json

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rollup/plugin-eslint",
3-
"version": "8.0.2",
3+
"version": "8.0.4",
44
"publishConfig": {
55
"access": "public"
66
},
@@ -13,10 +13,15 @@
1313
"author": "Bogdan Chadkin <[email protected]>",
1414
"homepage": "https://github.com/rollup/plugins/tree/master/packages/eslint#readme",
1515
"bugs": "https://github.com/rollup/plugins/issues",
16-
"main": "dist/index.js",
17-
"module": "dist/index.es.js",
16+
"main": "./dist/cjs/index.js",
17+
"module": "./dist/es/index.js",
18+
"exports": {
19+
"import": "./dist/es/index.js",
20+
"types": "./types/index.d.ts",
21+
"default": "./dist/cjs/index.js"
22+
},
1823
"engines": {
19-
"node": ">= 10.0.0"
24+
"node": ">=14.0.0"
2025
},
2126
"scripts": {
2227
"build": "rollup -c",
@@ -34,6 +39,7 @@
3439
},
3540
"files": [
3641
"dist",
42+
"!dist/**/*.map",
3743
"types",
3844
"README.md",
3945
"LICENSE"
@@ -48,30 +54,26 @@
4854
"lint"
4955
],
5056
"peerDependencies": {
51-
"rollup": "^1.20.0||^2.0.0"
57+
"rollup": "^1.20.0||^2.0.0||^3.0.0"
58+
},
59+
"peerDependenciesMeta": {
60+
"rollup": {
61+
"optional": true
62+
}
5263
},
5364
"dependencies": {
54-
"@rollup/pluginutils": "^4.0.0",
55-
"eslint": "^7.12.0"
65+
"@rollup/pluginutils": "^4.2.1",
66+
"eslint": "^8.24.0"
5667
},
5768
"devDependencies": {
58-
"@rollup/plugin-node-resolve": "^9.0.0",
59-
"@rollup/plugin-typescript": "^6.0.0",
60-
"@types/eslint": "^7.2.2",
61-
"rollup": "^2.67.3",
62-
"typescript": "^4.1.2"
69+
"@rollup/plugin-node-resolve": "^14.1.0",
70+
"@rollup/plugin-typescript": "^8.5.0",
71+
"@types/eslint": "^8.4.6",
72+
"rollup": "^3.0.0-7",
73+
"typescript": "^4.8.3"
6374
},
64-
"types": "types/index.d.ts",
75+
"types": "./types/index.d.ts",
6576
"ava": {
66-
"babel": {
67-
"compileEnhancements": false
68-
},
69-
"extensions": [
70-
"ts"
71-
],
72-
"require": [
73-
"ts-node/register"
74-
],
7577
"files": [
7678
"!**/fixtures/**",
7779
"!**/helpers/**",

packages/eslint/rollup.config.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/eslint/rollup.config.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { readFileSync } from 'fs';
2+
3+
import { createConfig } from '../../shared/rollup.config.mjs';
4+
5+
export default createConfig({
6+
pkg: JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'))
7+
});

packages/eslint/src/index.ts

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,83 @@
1-
import * as path from 'path';
1+
import { relative, resolve, sep } from 'path';
22

33
import { Plugin } from 'rollup';
44
import { createFilter } from '@rollup/pluginutils';
5-
import { CLIEngine } from 'eslint';
5+
import { ESLint } from 'eslint';
66

7-
import { RollupEslintOptions } from '../types';
7+
import type { RollupEslintOptions } from '../types';
88

99
function normalizePath(id: string) {
10-
return path.relative(process.cwd(), id).split(path.sep).join('/');
10+
return relative(process.cwd(), id).split(sep).join('/');
1111
}
1212

1313
export default function eslint(options = {} as RollupEslintOptions): Plugin {
1414
if (typeof options === 'string') {
15-
const configFile = path.resolve(process.cwd(), options);
15+
const configFile = resolve(process.cwd(), options);
1616
// eslint-disable-next-line global-require, import/no-dynamic-require, no-param-reassign
1717
options = require(configFile);
1818
// Tell eslint not to look for configuration files.
1919
// eslint-disable-next-line no-param-reassign
2020
options.useEslintrc = false;
2121
}
2222

23-
const cli = new CLIEngine(options);
24-
let formatter: CLIEngine.Formatter;
25-
26-
switch (typeof options.formatter) {
27-
case 'string':
28-
formatter = cli.getFormatter(options.formatter);
29-
break;
30-
case 'function':
31-
({ formatter } = options);
32-
break;
33-
default:
34-
formatter = cli.getFormatter('stylish');
35-
}
23+
const {
24+
include,
25+
exclude = /node_modules/,
26+
throwOnWarning = false,
27+
throwOnError = false,
28+
formatter = 'stylish',
29+
...eslintOptions
30+
} = options;
3631

37-
const filter = createFilter(options.include, options.exclude || /node_modules/);
32+
const eslintInstance = new ESLint(eslintOptions);
33+
const filter = createFilter(include, exclude);
3834

3935
return {
4036
name: 'eslint',
41-
42-
// eslint-disable-next-line consistent-return
43-
transform(code, id) {
37+
async transform(_, id: string) {
4438
const file = normalizePath(id);
45-
if (!filter(id) || cli.isPathIgnored(file)) {
39+
if (!filter(id) || (await eslintInstance.isPathIgnored(file))) {
4640
return null;
4741
}
4842

49-
const report = cli.executeOnText(code, file);
50-
const hasWarnings = options.throwOnWarning && report.warningCount !== 0;
51-
const hasErrors = options.throwOnError && report.errorCount !== 0;
43+
const results = await eslintInstance.lintFiles(file);
44+
const [result] = results;
5245

53-
if (options.fix && report) {
54-
CLIEngine.outputFixes(report);
46+
if (eslintOptions.fix) {
47+
await ESLint.outputFixes(results);
5548
}
5649

57-
if (report.warningCount === 0 && report.errorCount === 0) {
50+
if (result.warningCount === 0 && result.errorCount === 0) {
5851
return null;
5952
}
6053

61-
const result = formatter(report.results);
54+
const eslintFormatter: ESLint.Formatter =
55+
typeof formatter === 'string'
56+
? await eslintInstance.loadFormatter(formatter)
57+
: { format: formatter };
58+
const output = eslintFormatter.format(results);
6259

63-
if (result) {
60+
if (output) {
6461
// eslint-disable-next-line no-console
65-
console.log(result);
62+
console.log(output);
6663
}
6764

68-
if (hasWarnings && hasErrors) {
69-
throw Error('Warnings or errors were found');
65+
const errorMessages = [];
66+
if (result.warningCount > 0 && throwOnWarning) {
67+
errorMessages.push(`${result.warningCount} warning${result.warningCount > 1 ? 's' : ''}`);
7068
}
7169

72-
if (hasWarnings) {
73-
throw Error('Warnings were found');
70+
if (result.errorCount > 0 && throwOnError) {
71+
errorMessages.push(`${result.errorCount} error${result.errorCount > 1 ? 's' : ''}`);
7472
}
7573

76-
if (hasErrors) {
77-
throw Error('Errors were found');
74+
if (errorMessages.length > 0) {
75+
throw new Error(
76+
`Found ${errorMessages.join(' and ')} in ${relative('.', result.filePath)}`
77+
);
7878
}
79+
80+
return null;
7981
}
8082
};
8183
}

packages/eslint/test/node_modules/current-package

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)