Skip to content

Commit d23982a

Browse files
committed
Display message if no ESLint configuration is found
1 parent c828b32 commit d23982a

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

lib/loaders/eslint.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ const WebpackConfig = require('../WebpackConfig'); //eslint-disable-line no-unus
1313
const loaderFeatures = require('../features');
1414
const applyOptionsCallback = require('../utils/apply-options-callback');
1515

16+
function isMissingConfigError(e) {
17+
if (!e.message || !e.message.includes('No ESLint configuration found')) {
18+
return false;
19+
}
20+
21+
return true;
22+
}
23+
1624
module.exports = {
1725
/**
1826
* @param {WebpackConfig} webpackConfig
@@ -21,6 +29,40 @@ module.exports = {
2129
getOptions(webpackConfig) {
2230
loaderFeatures.ensurePackagesExistAndAreCorrectVersion('eslint');
2331

32+
const eslint = require('eslint'); // eslint-disable-line node/no-unpublished-require
33+
const engine = new eslint.CLIEngine({
34+
cwd: webpackConfig.runtimeConfig.context,
35+
});
36+
37+
try {
38+
engine.config.getConfigHierarchy(webpackConfig.runtimeConfig.context);
39+
} catch (e) {
40+
if (isMissingConfigError(e)) {
41+
const chalk = require('chalk').default;
42+
const packageHelper = require('../package-helper');
43+
44+
const message = `No ESLint configration has been found.
45+
46+
${chalk.bgGreen.black('', 'FIX', '')} Run command ${chalk.yellow('./node_modules/.bin/eslint --init')} or create manually a ${chalk.yellow('.eslintrc.js')} file at the root of your project.
47+
48+
If you prefer to create a ${chalk.yellow('.eslintrc.js')} file by yourself, here is an example to get you started!
49+
50+
Install ${chalk.yellow('babel-eslint')} to prevent potential parsing issues with your code: ${packageHelper.getInstallCommand([[{ name: 'babel-eslint' }]])}
51+
52+
And then create the following file:
53+
54+
${chalk.yellow(`// .eslintrc.js
55+
module.exports = {
56+
parser: 'babel-eslint',
57+
extends: ['eslint:recommended'],
58+
}
59+
`)}`;
60+
throw new Error(message);
61+
}
62+
63+
throw e;
64+
}
65+
2466
const eslintLoaderOptions = {
2567
cache: true,
2668
emitWarning: true

lib/package-helper.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,5 @@ module.exports = {
195195
getMissingPackageRecommendations,
196196
getInvalidPackageVersionRecommendations,
197197
addPackagesVersionConstraint,
198+
getInstallCommand,
198199
};

test/functional.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
'use strict';
1111

12+
const os = require('os');
1213
const chai = require('chai');
1314
chai.use(require('chai-fs'));
1415
const expect = chai.expect;
@@ -1763,6 +1764,32 @@ module.exports = {
17631764
}, true);
17641765
});
17651766

1767+
it('When enabled and without any configuration, ESLint will throw an error and a nice message should be displayed', (done) => {
1768+
const cwd = process.cwd();
1769+
1770+
this.timeout(5000);
1771+
setTimeout(() => {
1772+
process.chdir(cwd);
1773+
done();
1774+
}, 4000);
1775+
1776+
const appDir = testSetup.createTestAppDir(os.tmpdir()); // to prevent issue with Encore's .eslintrc.js
1777+
const config = testSetup.createWebpackConfig(appDir, 'www/build', 'dev');
1778+
config.setPublicPath('/build');
1779+
config.addEntry('main', './js/eslint');
1780+
config.enableEslintLoader({
1781+
// Force eslint-loader to output errors instead of sometimes
1782+
// using warnings (see: https://github.com/MoOx/eslint-loader#errors-and-warning)
1783+
emitError: true,
1784+
});
1785+
1786+
process.chdir(appDir);
1787+
1788+
expect(() => {
1789+
testSetup.runWebpack(config, (webpackAssert, stats) => {});
1790+
}).to.throw('No ESLint configration has been found.');
1791+
});
1792+
17661793
it('Code splitting with dynamic import', (done) => {
17671794
const config = createWebpackConfig('www/build', 'dev');
17681795
config.setPublicPath('/build');

test/helpers/setup.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ const testFixturesDir = path.join(__dirname, '../', '../', 'fixtures');
2525

2626
let servers = [];
2727

28-
function createTestAppDir() {
29-
const testAppDir = path.join(tmpDir, Math.random().toString(36).substring(7));
28+
function createTestAppDir(rootDir = tmpDir) {
29+
const testAppDir = path.join(rootDir, Math.random().toString(36).substring(7));
3030

3131
// copy the fixtures into this new directory
3232
fs.copySync(testFixturesDir, testAppDir);

0 commit comments

Comments
 (0)