Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Lint with eslint, prettier, airbnb #52

Merged
merged 4 commits into from
May 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!.eslintrc.js
25 changes: 25 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
extends: [
'eslint-config-airbnb-base',
'eslint-config-prettier',
'plugin:ava/recommended',
],
plugins: ['eslint-plugin-prettier', 'eslint-plugin-ava'],
env: {
commonjs: true,
node: true,
},
rules: {
'class-methods-use-this': 'off',
'no-shadow': 'off',
'no-underscore-dangle': 'off',
'prettier/prettier': [
'error',
{
singleQuote: true,
trailingComma: 'es5',
bracketSpacing: true,
},
],
},
};
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ language: node_js
cache: yarn
node_js:
- '6.11.5'
- '7'
- '8'
- '10'
install:
- yarn install --frozen-lockfile
before_install:
# Required due to: https://github.com/travis-ci/travis-ci/issues/7951
- curl -sSfL https://yarnpkg.com/install.sh | bash
- export PATH=$HOME/.yarn/bin:$PATH
script:
- yarn lint
- yarn test
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"license": "MPL-2.0",
"scripts": {
"test": "ava test",
"lint": "eslint src test .eslintrc.js",
"changelog": "changelog mozilla-neutrino/webpack-chain all --markdown > CHANGELOG.md"
},
"dependencies": {
Expand All @@ -26,6 +27,13 @@
"devDependencies": {
"ava": "^0.25.0",
"changelog": "^1.4.0",
"eslint": "^4.19.1",
"eslint-config-airbnb-base": "^12.1.0",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-ava": "^4.5.1",
"eslint-plugin-import": "^2.11.0",
"eslint-plugin-prettier": "^2.6.0",
"prettier": "^1.12.1",
"webpack": "^4.5.0"
}
}
75 changes: 42 additions & 33 deletions src/ChainedMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = class extends Chainable {

extend(methods) {
this.shorthands = methods;
methods.map(method => {
methods.forEach(method => {
this[method] = value => this.set(method, value);
});
return this;
Expand Down Expand Up @@ -58,6 +58,8 @@ module.exports = class extends Chainable {
if (order.length) {
return entries;
}

return undefined;
}

values() {
Expand All @@ -80,50 +82,57 @@ module.exports = class extends Chainable {
}

merge(obj, omit = []) {
Object
.keys(obj)
.forEach(key => {
if (omit.includes(key)) {
return;
}

const value = obj[key];

if ((!Array.isArray(value) && typeof value !== 'object') || value === null || !this.has(key)) {
this.set(key, value);
} else {
this.set(key, merge(this.get(key), value));
}
});
Object.keys(obj).forEach(key => {
if (omit.includes(key)) {
return;
}

const value = obj[key];

if (
(!Array.isArray(value) && typeof value !== 'object') ||
value === null ||
!this.has(key)
) {
this.set(key, value);
} else {
this.set(key, merge(this.get(key), value));
}
});

return this;
}

clean(obj) {
return Object
.keys(obj)
.reduce((acc, key) => {
const value = obj[key];
return Object.keys(obj).reduce((acc, key) => {
const value = obj[key];

if (value === undefined) {
return acc;
}
if (value === undefined) {
return acc;
}

if (Array.isArray(value) && !value.length) {
return acc;
}
if (Array.isArray(value) && !value.length) {
return acc;
}

if (Object.prototype.toString.call(value) === '[object Object]' && !Object.keys(value).length) {
return acc;
}
if (
Object.prototype.toString.call(value) === '[object Object]' &&
!Object.keys(value).length
) {
return acc;
}

acc[key] = value;
acc[key] = value;

return acc;
}, {});
return acc;
}, {});
}

when(condition, whenTruthy = Function.prototype, whenFalsy = Function.prototype) {
when(
condition,
whenTruthy = Function.prototype,
whenFalsy = Function.prototype
) {
if (condition) {
whenTruthy(this);
} else {
Expand Down
6 changes: 5 additions & 1 deletion src/ChainedSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ module.exports = class extends Chainable {
return this;
}

when(condition, whenTruthy = Function.prototype, whenFalsy = Function.prototype) {
when(
condition,
whenTruthy = Function.prototype,
whenFalsy = Function.prototype
) {
if (condition) {
whenTruthy(this);
} else {
Expand Down
127 changes: 67 additions & 60 deletions src/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = class extends ChainedMap {
'stats',
'target',
'watch',
'watchOptions'
'watchOptions',
]);
}

Expand All @@ -62,69 +62,76 @@ module.exports = class extends ChainedMap {
toConfig() {
const entryPoints = this.entryPoints.entries() || {};

return this.clean(Object.assign(this.entries() || {}, {
node: this.node.entries(),
output: this.output.entries(),
resolve: this.resolve.toConfig(),
resolveLoader: this.resolveLoader.toConfig(),
devServer: this.devServer.toConfig(),
module: this.module.toConfig(),
optimization: this.optimization.entries(),
plugins: this.plugins.values().map(plugin => plugin.toConfig()),
performance: this.performance.entries(),
entry: Object
.keys(entryPoints)
.reduce((acc, key) => Object.assign(acc, { [key]: entryPoints[key].values() }), {})
}));
return this.clean(
Object.assign(this.entries() || {}, {
node: this.node.entries(),
output: this.output.entries(),
resolve: this.resolve.toConfig(),
resolveLoader: this.resolveLoader.toConfig(),
devServer: this.devServer.toConfig(),
module: this.module.toConfig(),
optimization: this.optimization.entries(),
plugins: this.plugins.values().map(plugin => plugin.toConfig()),
performance: this.performance.entries(),
entry: Object.keys(entryPoints).reduce(
(acc, key) =>
Object.assign(acc, { [key]: entryPoints[key].values() }),
{}
),
})
);
}

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 = []) {
Expand All @@ -136,19 +143,19 @@ module.exports = class extends ChainedMap {
'devServer',
'optimization',
'performance',
'module'
'module',
];

if (!omit.includes('entry') && 'entry' in obj) {
Object
.keys(obj.entry)
.forEach(name => this.entry(name).merge(obj.entry[name]));
Object.keys(obj.entry).forEach(name =>
this.entry(name).merge(obj.entry[name])
);
}

if (!omit.includes('plugin') && 'plugin' in obj) {
Object
.keys(obj.plugin)
.forEach(name => this.plugin(name).merge(obj.plugin[name]));
Object.keys(obj.plugin).forEach(name =>
this.plugin(name).merge(obj.plugin[name])
);
}

omissions.forEach(key => {
Expand Down
14 changes: 9 additions & 5 deletions src/DevServer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const ChainedMap = require('./ChainedMap');
const ChainedSet = require('./ChainedSet');
const merge = require('deepmerge');

module.exports = class extends ChainedMap {
constructor(parent) {
Expand Down Expand Up @@ -44,14 +43,19 @@ module.exports = class extends ChainedMap {
'stdin',
'useLocalIp',
'watchContentBase',
'watchOptions'
'watchOptions',
]);
}

toConfig() {
return this.clean(Object.assign({
allowedHosts: this.allowedHosts.values(),
}, this.entries() || {}));
return this.clean(
Object.assign(
{
allowedHosts: this.allowedHosts.values(),
},
this.entries() || {}
)
);
}

merge(obj, omit = []) {
Expand Down
Loading