From 8e527a0116a1fb3abcb235fb69706dc2f863fdec Mon Sep 17 00:00:00 2001 From: Eli Perelman Date: Tue, 15 May 2018 10:15:06 -0500 Subject: [PATCH] Linting fixes after rebase --- src/Config.js | 79 +++++++++++++++++++++++++------------------------ src/Plugin.js | 78 ++++++++++++++++++++++++------------------------ src/Rule.js | 37 +++++++++++++---------- src/Use.js | 56 ++++++++++++++++++----------------- test/Config.js | 56 +++++++++++++++++------------------ test/Resolve.js | 3 +- test/Rule.js | 2 +- test/Use.js | 6 ++-- 8 files changed, 164 insertions(+), 153 deletions(-) diff --git a/src/Config.js b/src/Config.js index 9476fb7..dc9ccea 100644 --- a/src/Config.js +++ b/src/Config.js @@ -82,53 +82,56 @@ module.exports = class extends ChainedMap { ); } - toString({ - verbose = false, - configPrefix = 'config' - } = {}) { + toString({ verbose = false, configPrefix = 'config' } = {}) { + // eslint-disable-next-line global-require const stringify = require('javascript-stringify'); - const config = this.toConfig(); - return stringify(config, (value, indent, stringify) => { - // improve plugin output - if (value && value.__pluginName) { - const prefix = `/* ${configPrefix}.plugin('${value.__pluginName}') */\n`; - const constructorName = value.__pluginConstructorName; - - if (constructorName) { - // get correct indentation for args by stringifying the args array and - // discarding the square brackets. - const args = stringify(value.__pluginArgs).slice(1, -1); - return prefix + `new ${constructorName}(${args})`; - } else { + return stringify( + config, + (value, indent, stringify) => { + // improve plugin output + if (value && value.__pluginName) { + const prefix = `/* ${configPrefix}.plugin('${ + value.__pluginName + }') */\n`; + const constructorName = value.__pluginConstructorName; + + if (constructorName) { + // get correct indentation for args by stringifying the args array and + // discarding the square brackets. + const args = stringify(value.__pluginArgs).slice(1, -1); + return `${prefix}new ${constructorName}(${args})`; + } return prefix + stringify({ args: value.__pluginArgs || [] }); } - } - // improve rule/use output - if (value && value.__ruleNames) { - const prefix = `/* ${configPrefix}.module.rule('${ - value.__ruleNames[0] - }')${ - value.__ruleNames.slice(1).map(r => `.oneOf('${r}')`).join('') - }${ - value.__useName ? `.use('${value.__useName}')` : `` - } */\n`; - return prefix + stringify(value); - } + // improve rule/use output + if (value && value.__ruleNames) { + const prefix = `/* ${configPrefix}.module.rule('${ + value.__ruleNames[0] + }')${value.__ruleNames + .slice(1) + .map(r => `.oneOf('${r}')`) + .join('')}${ + value.__useName ? `.use('${value.__useName}')` : `` + } */\n`; + return prefix + stringify(value); + } - // shorten long functions - if (typeof value === 'function') { - if (value.__expression) { - return value.__expression; - } else if (!verbose && value.toString().length > 100) { - return `function () { /* omitted long function */ }`; + // shorten long functions + if (typeof value === 'function') { + if (value.__expression) { + return value.__expression; + } else if (!verbose && value.toString().length > 100) { + return `function () { /* omitted long function */ }`; + } } - } - return stringify(value); - }, 2); + return stringify(value); + }, + 2 + ); } merge(obj = {}, omit = []) { diff --git a/src/Plugin.js b/src/Plugin.js index 276ef76..0f5c335 100644 --- a/src/Plugin.js +++ b/src/Plugin.js @@ -1,53 +1,53 @@ const ChainedMap = require('./ChainedMap'); const Orderable = require('./Orderable'); -module.exports = Orderable(class extends ChainedMap { - constructor(parent, name) { - super(parent); - this.name = name; - this.extend(['init']); +module.exports = Orderable( + class extends ChainedMap { + constructor(parent, name) { + super(parent); + this.name = name; + this.extend(['init']); + + this.init((Plugin, args = []) => new Plugin(...args)); + } - this.init((Plugin, args = []) => new Plugin(...args)); - } + use(plugin, args = []) { + return this.set('plugin', plugin).set('args', args); + } - use(plugin, args = []) { - return this - .set('plugin', plugin) - .set('args', args); - } + tap(f) { + this.set('args', f(this.get('args') || [])); + return this; + } - tap(f) { - this.set('args', f(this.get('args') || [])); - return this; - } + merge(obj, omit = []) { + if ('plugin' in obj) { + this.set('plugin', obj.plugin); + } - merge(obj, omit = []) { - if ('plugin' in obj) { - this.set('plugin', obj.plugin); - } + if ('args' in obj) { + this.set('args', obj.args); + } - if ('args' in obj) { - this.set('args', obj.args); + return super.merge(obj, [...omit, 'args', 'plugin']); } - return super.merge(obj, [...omit, 'args', 'plugin']); - } + toConfig() { + const init = this.get('init'); + const plugin = this.get('plugin'); + const constructorName = plugin.__expression + ? `(${plugin.__expression})` + : plugin.name; - toConfig() { - const init = this.get('init'); - const plugin = this.get('plugin'); - const constructorName = plugin.__expression - ? `(${plugin.__expression})` - : plugin.name; + const config = init(this.get('plugin'), this.get('args')); - const config = init(this.get('plugin'), this.get('args')); + Object.defineProperties(config, { + __pluginName: { value: this.name }, + __pluginArgs: { value: this.get('args') }, + __pluginConstructorName: { value: constructorName }, + }); - Object.defineProperties(config, { - __pluginName: { value: this.name }, - __pluginArgs: { value: this.get('args') }, - __pluginConstructorName: { value: constructorName } - }); - - return config; + return config; + } } -}); +); diff --git a/src/Rule.js b/src/Rule.js index 6667f50..f845961 100644 --- a/src/Rule.js +++ b/src/Rule.js @@ -26,7 +26,7 @@ module.exports = class Rule extends ChainedMap { 'resourceQuery', 'sideEffects', 'test', - 'type' + 'type', ]); } @@ -55,15 +55,17 @@ module.exports = class Rule extends ChainedMap { } toConfig() { - const config = this.clean(Object.assign(this.entries() || {}, { - include: this.include.values(), - exclude: this.exclude.values(), - oneOf: this.oneOfs.values().map(oneOf => oneOf.toConfig()), - use: this.uses.values().map(use => use.toConfig()) - })); + const config = this.clean( + Object.assign(this.entries() || {}, { + include: this.include.values(), + exclude: this.exclude.values(), + oneOf: this.oneOfs.values().map(oneOf => oneOf.toConfig()), + use: this.uses.values().map(use => use.toConfig()), + }) + ); Object.defineProperties(config, { - __ruleNames: { value: this.names } + __ruleNames: { value: this.names }, }); return config; @@ -79,21 +81,26 @@ module.exports = class Rule extends ChainedMap { } if (!omit.includes('use') && 'use' in obj) { - Object - .keys(obj.use) - .forEach(name => this.use(name).merge(obj.use[name])); + Object.keys(obj.use).forEach(name => this.use(name).merge(obj.use[name])); } if (!omit.includes('oneOf') && 'oneOf' in obj) { - Object - .keys(obj.oneOf) - .forEach(name => this.oneOf(name).merge(obj.oneOf[name])); + Object.keys(obj.oneOf).forEach(name => + this.oneOf(name).merge(obj.oneOf[name]) + ); } if (!omit.includes('test') && 'test' in obj) { this.test(obj.test instanceof RegExp ? obj.test : new RegExp(obj.test)); } - return super.merge(obj, [...omit, 'include', 'exclude', 'use', 'oneOf', 'test']); + return super.merge(obj, [ + ...omit, + 'include', + 'exclude', + 'use', + 'oneOf', + 'test', + ]); } }; diff --git a/src/Use.js b/src/Use.js index 3fe7bb2..c298a60 100644 --- a/src/Use.js +++ b/src/Use.js @@ -2,38 +2,40 @@ const ChainedMap = require('./ChainedMap'); const Orderable = require('./Orderable'); const merge = require('deepmerge'); -module.exports = Orderable(class extends ChainedMap { - constructor(parent, name) { - super(parent); - this.name = name; - this.extend(['loader', 'options']); - } - - tap(f) { - this.options(f(this.get('options'))); - return this; - } - - merge(obj, omit = []) { - if (!omit.includes('loader') && 'loader' in obj) { - this.loader(obj.loader); +module.exports = Orderable( + class extends ChainedMap { + constructor(parent, name) { + super(parent); + this.name = name; + this.extend(['loader', 'options']); } - if (!omit.includes('options') && 'options' in obj) { - this.options(merge(this.store.get('options') || {}, obj.options)); + tap(f) { + this.options(f(this.get('options'))); + return this; } - return super.merge(obj, [...omit, 'loader', 'options']); - } + merge(obj, omit = []) { + if (!omit.includes('loader') && 'loader' in obj) { + this.loader(obj.loader); + } - toConfig() { - const config = this.clean(this.entries() || {}); + if (!omit.includes('options') && 'options' in obj) { + this.options(merge(this.store.get('options') || {}, obj.options)); + } - Object.defineProperties(config, { - __useName: { value: this.name }, - __ruleNames: { value: this.parent && this.parent.names } - }); + return super.merge(obj, [...omit, 'loader', 'options']); + } + + toConfig() { + const config = this.clean(this.entries() || {}); - return config; + Object.defineProperties(config, { + __useName: { value: this.name }, + __ruleNames: { value: this.parent && this.parent.names }, + }); + + return config; + } } -}); +); diff --git a/test/Config.js b/test/Config.js index 8ff2b35..e386def 100644 --- a/test/Config.js +++ b/test/Config.js @@ -222,29 +222,28 @@ test('validate with values', t => { test('toString', t => { const config = new Config(); - config - .module - .rule('alpha') - .oneOf('beta') - .use('babel') - .loader('babel-loader'); + config.module + .rule('alpha') + .oneOf('beta') + .use('babel') + .loader('babel-loader'); class FooPlugin {} FooPlugin.__expression = `require('foo-plugin')`; config .plugin('gamma') - .use(FooPlugin) - .end() + .use(FooPlugin) + .end() .plugin('delta') - .use(class BarPlugin {}, ['bar']) - .end() + .use(class BarPlugin {}, ['bar']) + .end() .plugin('epsilon') - .use(class BazPlugin {}, [{ n: 1 }, [2, 3]]) + .use(class BazPlugin {}, [{ n: 1 }, [2, 3]]); - const string = config.toString(); - - t.is(config.toString().trim(), ` + t.is( + config.toString().trim(), + ` { module: { rules: [ @@ -283,22 +282,21 @@ test('toString', t => { ) ] } - `.trim()) + `.trim() + ); }); test('toString for functions with custom expression', t => { - const fn = function foo () {}; + const fn = function foo() {}; fn.__expression = `require('foo')`; const config = new Config(); - config - .module - .rule('alpha') - .include - .add(fn); + config.module.rule('alpha').include.add(fn); - t.is(config.toString().trim(), ` + t.is( + config.toString().trim(), + ` { module: { rules: [ @@ -311,22 +309,24 @@ test('toString for functions with custom expression', t => { ] } } - `.trim()); + `.trim() + ); }); test('toString with custom prefix', t => { const config = new Config(); - config - .plugin('foo') - .use(class TestPlugin {}); + config.plugin('foo').use(class TestPlugin {}); - t.is(config.toString({ configPrefix: 'neutrino.config' }).trim(), ` + t.is( + config.toString({ configPrefix: 'neutrino.config' }).trim(), + ` { plugins: [ /* neutrino.config.plugin('foo') */ new TestPlugin() ] } - `.trim()); + `.trim() + ); }); diff --git a/test/Resolve.js b/test/Resolve.js index ab809f6..bdd9c02 100644 --- a/test/Resolve.js +++ b/test/Resolve.js @@ -119,8 +119,7 @@ test('merge with omit', t => { test('plugin with name', t => { const resolve = new Resolve(); - resolve - .plugin('alpha'); + resolve.plugin('alpha'); t.is(resolve.plugins.get('alpha').name, 'alpha'); }); diff --git a/test/Rule.js b/test/Rule.js index bad2b78..82c8ea8 100644 --- a/test/Rule.js +++ b/test/Rule.js @@ -76,7 +76,7 @@ test('toConfig empty', t => { test('toConfig with name', t => { const parent = new Rule(null, 'alpha'); const child = parent.oneOf('beta'); - const grandChild = child.oneOf('gamma') + const grandChild = child.oneOf('gamma'); t.deepEqual(parent.toConfig().__ruleNames, ['alpha']); t.deepEqual(child.toConfig().__ruleNames, ['alpha', 'beta']); diff --git a/test/Use.js b/test/Use.js index 23f2c46..b5baa58 100644 --- a/test/Use.js +++ b/test/Use.js @@ -45,9 +45,9 @@ test('toConfig', t => { t.deepEqual(config, { loader: 'babel-loader', - options: { presets: ['alpha'] } + options: { presets: ['alpha'] }, }); - t.deepEqual(config.__ruleNames, ['alpha']) - t.is(config.__useName, 'beta') + t.deepEqual(config.__ruleNames, ['alpha']); + t.is(config.__useName, 'beta'); });