1 | const Future = require('fluture');
|
2 | const merge = require('deepmerge');
|
3 | const clone = require('lodash.clonedeep');
|
4 | const { CLIEngine } = require('eslint');
|
5 | const {
|
6 | assoc, curry, evolve, keys, omit, pipe, prop, reduce
|
7 | } = require('ramda');
|
8 | const { basename, join } = require('path');
|
9 |
|
10 | const MODULES = join(__dirname, 'node_modules');
|
11 | const getEslintOptions = config => config.module.rule('lint').use('eslint').get('options');
|
12 | const renameKeys = curry((definition, obj) =>
|
13 | reduce((acc, key) => assoc(definition[key] || key, obj[key], acc), {}, keys(obj)));
|
14 | const reduceToTrueKeys = reduce((acc, key) => assoc(key, true, acc), {});
|
15 | const getEslintRcConfig = pipe(
|
16 | getEslintOptions,
|
17 | clone,
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | omit(['failOnError', 'emitWarning', 'emitError', 'cwd', 'useEslintrc', 'fix', 'extensions']),
|
25 | renameKeys({ envs: 'env', baseConfig: 'extends' }),
|
26 | evolve({
|
27 | extends: prop('extends'),
|
28 | globals: reduceToTrueKeys,
|
29 | env: reduceToTrueKeys
|
30 | })
|
31 | );
|
32 |
|
33 | module.exports = (neutrino, opts = {}) => {
|
34 | const failBuild = process.env.NODE_ENV !== 'development' || neutrino.options.command !== 'start';
|
35 | const options = merge.all([
|
36 | opts,
|
37 | !opts.include ? { include: [neutrino.options.source] } : {}
|
38 | ]);
|
39 |
|
40 | neutrino.config
|
41 | .resolve
|
42 | .modules
|
43 | .add(MODULES)
|
44 | .end()
|
45 | .end()
|
46 | .resolveLoader
|
47 | .modules
|
48 | .add(MODULES)
|
49 | .end()
|
50 | .end()
|
51 | .module
|
52 | .rule('lint')
|
53 | .test(options.test || /\.(js|jsx)$/)
|
54 | .pre()
|
55 | .when(options.include, rule => rule.include.merge(options.include))
|
56 | .when(options.exclude, rule => rule.exclude.merge(options.exclude))
|
57 | .use('eslint')
|
58 | .loader(require.resolve('eslint-loader'))
|
59 | .options(merge({
|
60 | failOnError: failBuild,
|
61 | emitWarning: failBuild,
|
62 | emitError: failBuild,
|
63 | cwd: neutrino.options.root,
|
64 | useEslintrc: false,
|
65 | root: true,
|
66 |
|
67 |
|
68 | extensions: ['js', 'jsx'],
|
69 | plugins: ['babel'],
|
70 | baseConfig: {},
|
71 | envs: ['es6'],
|
72 | parser: 'babel-eslint',
|
73 | parserOptions: {
|
74 | ecmaVersion: 2017,
|
75 | sourceType: 'module',
|
76 | ecmaFeatures: {
|
77 | objectLiteralDuplicateProperties: false,
|
78 | generators: true,
|
79 | impliedStrict: true
|
80 | }
|
81 | },
|
82 | settings: {},
|
83 | globals: ['process'],
|
84 | rules: {}
|
85 | }, options.eslint || {}));
|
86 |
|
87 | neutrino.register('eslintrc', () => getEslintRcConfig(neutrino.config));
|
88 | neutrino.register('lint', () => {
|
89 | const { fix = false } = neutrino.options.args;
|
90 | const ignorePattern = (options.exclude || [])
|
91 | .map(exclude => join(
|
92 | basename(neutrino.options.source),
|
93 | basename(exclude),
|
94 | '**/*'
|
95 | ));
|
96 | const eslintConfig = merge(getEslintOptions(neutrino.config), { ignorePattern, fix });
|
97 |
|
98 | return Future
|
99 | .of(eslintConfig)
|
100 | .map(options => new CLIEngine(options))
|
101 | .chain(cli => Future.both(Future.of(cli.executeOnFiles(options.include)), Future.of(cli.getFormatter())))
|
102 | .map(([report, formatter]) => {
|
103 | fix && CLIEngine.outputFixes(report);
|
104 | return [report, formatter];
|
105 | })
|
106 | .chain(([report, formatter]) => {
|
107 | const errors = CLIEngine.getErrorResults(report.results);
|
108 | const formatted = formatter(report.results);
|
109 |
|
110 | return errors.length ? Future.reject(formatted) : Future.of(formatted);
|
111 | });
|
112 | });
|
113 | };
|