Skip to content

Commit 7db1c21

Browse files
authored
fix: eslint configuration should be enabled if files passed (#460)
If we pass the `files` option to the `eslint` configuration, it should be enabled by default. This fix also updates documentation of default eslint configuration to prevent common issues. ✅ Closes: #458
1 parent 1c2ab8c commit 7db1c21

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ module.exports = {
101101
plugins: [
102102
new ForkTsCheckerWebpackPlugin({
103103
eslint: {
104-
files: './src/**/*' // required - same as command `eslint ./src/**/* --ext .ts,.tsx,.js,.jsx`
104+
files: './src/**/*.{ts,tsx,js,jsx}' // required - same as command `eslint ./src/**/*.{ts,tsx,js,jsx} --ext .ts,.tsx,.js,.jsx`
105105
}
106106
})
107107
]
@@ -182,7 +182,7 @@ Options for the ESLint linter (`eslint` option object).
182182

183183
| Name | Type | Default value | Description |
184184
| -------------------- | ---------------------- | ------------------------- | ----------- |
185-
| `enabled` | `boolean` | `false` | If `true`, it enables ESLint linter. |
185+
| `enabled` | `boolean` | `false` | If `true`, it enables ESLint linter. If you set the `files` option, it will be `true` by default. |
186186
| `files` | `string` or `string[]` | This value is required | One or more [glob patterns](https://en.wikipedia.org/wiki/Glob_(programming)) to the files that should be linted. Works the same as the `eslint` command. |
187187
| `memoryLimit` | `number` | `2048` | Memory limit for the linter process in MB. If the process exits with the allocation failed error, try to increase this number. |
188188
| `options` | `object` | `{}` | [Options](https://eslint.org/docs/developer-guide/nodejs-api#cliengine) that can be used to initialize ESLint. |

src/eslint-reporter/EsLintReporterConfiguration.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,23 @@ function createEsLintReporterConfiguration(
2424
compiler: webpack.Compiler,
2525
options: EsLintReporterOptions | undefined
2626
): EsLintReporterConfiguration {
27-
return {
28-
enabled: !!(options && options.enabled === true),
29-
memoryLimit: 2048,
30-
...(typeof options === 'object' ? options : {}),
31-
files: (typeof options === 'object' ? castToArray(options.files) : []).map((filesPattern) =>
27+
const filesPatterns = (typeof options === 'object' ? castToArray(options.files) : []).map(
28+
(filesPattern) =>
3229
// ensure that `filesPattern` is an absolute path
3330
isAbsolute(filesPattern)
3431
? filesPattern
3532
: join(compiler.options.context || process.cwd(), filesPattern)
36-
),
33+
);
34+
35+
return {
36+
enabled:
37+
!!options &&
38+
typeof options !== 'boolean' &&
39+
filesPatterns.length > 0 && // enable by default if files are provided
40+
options.enabled !== false,
41+
memoryLimit: 2048,
42+
...(typeof options === 'object' ? options : {}),
43+
files: filesPatterns,
3744
options: {
3845
cwd: compiler.options.context || process.cwd(),
3946
extensions: ['.ts', '.tsx', '.js', '.jsx'],
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import webpack from 'webpack';
2+
import { join } from 'path';
3+
import { EsLintReporterConfiguration } from '../../../lib/eslint-reporter/EsLintReporterConfiguration';
4+
import { EsLintReporterOptions } from '../../../lib/eslint-reporter/EsLintReporterOptions';
5+
6+
describe('eslint-reporter/EsLintReporterConfiguration', () => {
7+
let compiler: webpack.Compiler;
8+
const context = '/webpack/context';
9+
10+
const configuration: EsLintReporterConfiguration = {
11+
enabled: false,
12+
memoryLimit: 2048,
13+
options: {
14+
cwd: context,
15+
extensions: ['.ts', '.tsx', '.js', '.jsx'],
16+
},
17+
files: [],
18+
};
19+
20+
beforeEach(() => {
21+
compiler = {
22+
options: {
23+
context,
24+
},
25+
} as webpack.Compiler;
26+
});
27+
28+
it.each([
29+
[undefined, configuration],
30+
[{}, configuration],
31+
[
32+
{ files: 'src/**/*.{js,ts,tsx,jsx}' },
33+
{
34+
...configuration,
35+
enabled: true,
36+
files: [join(context, 'src/**/*.{js,ts,tsx,jsx}')],
37+
},
38+
],
39+
[{ files: [] }, configuration],
40+
[{ enabled: true }, { ...configuration, enabled: true }],
41+
[{ memoryLimit: 512 }, { ...configuration, memoryLimit: 512 }],
42+
])('creates configuration from options %p', async (options, expectedConfiguration) => {
43+
const { createEsLintReporterConfiguration } = await import(
44+
'lib/eslint-reporter/EsLintReporterConfiguration'
45+
);
46+
const configuration = createEsLintReporterConfiguration(
47+
compiler,
48+
options as EsLintReporterOptions
49+
);
50+
51+
expect(configuration).toEqual(expectedConfiguration);
52+
});
53+
});

0 commit comments

Comments
 (0)