-
Notifications
You must be signed in to change notification settings - Fork 230
Feature: allow specifying to use before or after other use #42
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
const merge = require('deepmerge'); | ||
const Chainable = require('./Chainable'); | ||
|
||
module.exports = class extends Chainable { | ||
|
@@ -53,8 +54,23 @@ module.exports = class extends Chainable { | |
return this; | ||
} | ||
|
||
merge(obj) { | ||
Object.keys(obj).forEach(key => this.set(key, obj[key])); | ||
merge(obj, omit = []) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This and the other merge/omit parts could be a separate commit in the PR from the before/after parts :-) |
||
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; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,16 +19,16 @@ module.exports = class extends ChainedMap { | |
return this; | ||
} | ||
|
||
merge(obj) { | ||
if (obj.plugin) { | ||
merge(obj, omit = []) { | ||
if ('plugin' in obj) { | ||
this.set('plugin', obj.plugin); | ||
} | ||
|
||
if (obj.args) { | ||
if ('args' in obj) { | ||
this.set('args', obj.args); | ||
} | ||
|
||
return this; | ||
return super.merge(obj, [...omit, 'args', 'plugin']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing semicolon. I wonder if we should add some linting to this repo? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I've put it off for a while. I can do that in another PR, although it would be easier to either use Neutrino (seems circular) or move this repo into the monorepo. |
||
} | ||
|
||
toConfig() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,45 +40,36 @@ module.exports = class Rule extends ChainedMap { | |
return this.clean(Object.assign(this.entries() || {}, { | ||
include: this.include.values(), | ||
exclude: this.exclude.values(), | ||
oneOf: this.oneOfs.values().map(r => r.toConfig()), | ||
oneOf: this.oneOfs.values().map(oneOf => oneOf.toConfig()), | ||
use: this.uses.values().map(use => use.toConfig()) | ||
})); | ||
} | ||
|
||
merge(obj) { | ||
Object | ||
.keys(obj) | ||
.forEach(key => { | ||
const value = obj[key]; | ||
|
||
switch (key) { | ||
case 'include': | ||
case 'exclude': { | ||
return this[key].merge(value); | ||
} | ||
merge(obj, omit = []) { | ||
if (!omit.includes('include') && 'include' in obj) { | ||
this.include.merge(obj.include); | ||
} | ||
|
||
case 'use': { | ||
return Object | ||
.keys(value) | ||
.forEach(name => this.use(name).merge(value[name])); | ||
} | ||
if (!omit.includes('exclude') && 'exclude' in obj) { | ||
this.exclude.merge(obj.exclude); | ||
} | ||
|
||
case 'oneOf': { | ||
return Object | ||
.keys(value) | ||
.forEach(name => this.oneOf(name).merge(value[name])) | ||
} | ||
if (!omit.includes('use') && 'use' in obj) { | ||
Object | ||
.keys(obj.use) | ||
.forEach(name => this.use(name).merge(obj.use[name])); | ||
} | ||
|
||
case 'test': { | ||
return this.test(value instanceof RegExp ? value : new RegExp(value)); | ||
} | ||
if (!omit.includes('oneOf') && 'oneOf' in obj) { | ||
Object | ||
.keys(obj.oneOf) | ||
.forEach(name => this.oneOf(name).merge(obj.oneOf[name])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing semicolon |
||
} | ||
|
||
default: { | ||
this.set(key, value); | ||
} | ||
} | ||
}); | ||
if (!omit.includes('test') && 'test' in obj) { | ||
this.test(obj.test instanceof RegExp ? obj.test : new RegExp(obj.test)); | ||
} | ||
|
||
return this; | ||
return super.merge(obj, [...omit, 'include', 'exclude', 'use', 'oneOf', 'test']); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/an array to the second argument/an array as the second argument/ maybe? Up to you :-)