Skip to content

Commit

Permalink
Make "useArray" enforcement in collection-ordering optional by default
Browse files Browse the repository at this point in the history
  • Loading branch information
ganimomer committed Dec 5, 2018
1 parent 8896c5a commit bae2b12
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/rules/collection-ordering.md
Expand Up @@ -25,7 +25,7 @@ This rule takes one argument: an options object with two fields:
- `sortBy`: Prefer the `sortBy` method, if no ordering is descending.
- `orderBy`: Prefer the `orderBy` method, omitting the orders if all are ascending.
- `orderByExplicit`: Prefer the `orderBy` method, and always declare the ordering.
- `useArray`: When to wrap the iteratees and orders in arrays. Accepts `always` or `as-needed` (default is `always`).
- `useArray`: When to wrap the iteratees and orders in arrays. Accepts `always` or `as-needed`, and not enforced when the key is omitted (omitted by default).
- `always`: Wrap the iteratees and ordering in arrays, even if there is a single iteratee or ordering.
- `as-needed`: Wrap the iteratees and ordering in arrays only if there is more than one.

Expand Down
8 changes: 4 additions & 4 deletions src/rules/collection-ordering.js
Expand Up @@ -36,7 +36,7 @@ function canUseSortBy(orderingNode) {
}

function enforceArraysOption(node, useArray, method, context) {
if (node) {
if (node && !_.isUndefined(useArray)) {
if (useArray === 'always' && node.type !== 'ArrayExpression') {
context.report({node, messageId: 'useArrayAlways', data: {method}})
} else if (useArray === 'as-needed' && node.type === 'ArrayExpression' && node.elements.length === 1) {
Expand Down Expand Up @@ -66,12 +66,12 @@ module.exports = {
orderBy: 'Use _.orderBy for ordering in ascending order.',
omitOrders: 'Omit the order when all orders are ascending.',
orderByExplicit: 'Use _.orderBy and specify the orders.',
useArrayAlways: 'Use arrays when calling _.{{method}}.',
useArrayAsNeeded: 'Use an array when specifying iteratees and order in _.{{method}}'
useArrayAlways: 'Wrap ordering iteratees with arrays in _.{{method}}.',
useArrayAsNeeded: 'Do not wrap a single ordering iteratee with array in _.{{method}}.'
}
},
create(context) {
const {method: mode = 'sortBy', useArray = 'always'} = context.options[0] || {}
const {method: mode = 'sortBy', useArray} = context.options[0] || {}

return getLodashMethodVisitors(context, (node, iteratee, {version, method, callType}) => {
const mainAlias = getMainAlias(version, method)
Expand Down
23 changes: 19 additions & 4 deletions tests/lib/rules/collection-ordering.js
Expand Up @@ -17,11 +17,15 @@ const {withDefaultPragma, fromMessageId, fromOptions} = require('../testUtil/opt
const testCases = {
valid: {
sortBy: {
off: [
'_.sortBy(arr, f)',
'_.sortBy(arr, [f])'
].map(withDefaultPragma),
always: [
'_.sortBy(arr, [f])',
'_.sortBy(arr, ["name"])',
'_.orderBy(arr, ["name"], ["desc"])'
].map(withDefaultPragma),
].map(withDefaultPragma).map(fromOptions({options: [{useArray: 'always'}]})),
asNeeded: [
'_.sortBy(arr, f)',
'_.sortBy(arr, "name")',
Expand All @@ -31,23 +35,31 @@ const testCases = {
].map(withDefaultPragma).map(fromOptions({options: [{useArray: 'as-needed'}]}))
},
orderBy: {
off: [
'_.orderBy(arr, f)',
'_.orderBy(arr, [f])'
].map(withDefaultPragma).map(fromOptions({options: [{method: 'orderBy'}]})),
always: [
'_.orderBy(arr, [f])',
'_.orderBy(arr, ["name"])',
'_.orderBy(arr, ["name"], ["desc"])'
].map(withDefaultPragma).map(fromOptions({options: [{method: 'orderBy'}]})),
].map(withDefaultPragma).map(fromOptions({options: [{method: 'orderBy', useArray: 'always'}]})),
asNeeded: [
'_.orderBy(arr, f)',
'_.orderBy(arr, "name")',
'_.orderBy(arr, "name", "desc")'
].map(withDefaultPragma).map(fromOptions({options: [{method: 'orderBy', useArray: 'as-needed'}]}))
},
orderByExplicit: {
off: [
'_.orderBy(arr, f, "asc")',
'_.orderBy(arr, [f], ["asc"])'
].map(withDefaultPragma).map(fromOptions({options: [{method: 'orderByExplicit'}]})),
always: [
'_.orderBy(arr, [f], ["asc"])',
'_.orderBy(arr, [f], ["desc"])',
'_.orderBy(arr, [f, g], ["asc", "asc"])'
].map(withDefaultPragma).map(fromOptions({options: [{method: 'orderByExplicit'}]})),
].map(withDefaultPragma).map(fromOptions({options: [{method: 'orderByExplicit', useArray: 'always'}]})),
asNeeded: [
'_.orderBy(arr, f, "asc")',
'_.orderBy(arr, [f, g], ["asc", "asc"])'
Expand All @@ -73,18 +85,21 @@ const testCases = {
].map(withDefaultPragma).map(fromMessageId('orderByExplicit')).map(fromOptions({options: [{method: 'orderByExplicit'}]})),
useArrayAlways: [
'_.sortBy(a, f)'
].map(withDefaultPragma).map(fromMessageId('useArrayAlways')),
].map(withDefaultPragma).map(fromMessageId('useArrayAlways')).map(fromOptions({options: [{useArray: 'always'}]})),
useArrayAsNeeded: [
'_.sortBy(a, [f])'
].map(withDefaultPragma).map(fromMessageId('useArrayAsNeeded')).map(fromOptions({options: [{useArray: 'as-needed'}]}))
}
}
ruleTester.run('collection-ordering', rule, {
valid: [
...testCases.valid.sortBy.off,
...testCases.valid.sortBy.always,
...testCases.valid.sortBy.asNeeded,
...testCases.valid.orderBy.off,
...testCases.valid.orderBy.always,
...testCases.valid.orderBy.asNeeded,
...testCases.valid.orderByExplicit.off,
...testCases.valid.orderByExplicit.always,
...testCases.valid.orderByExplicit.asNeeded
],
Expand Down

0 comments on commit bae2b12

Please sign in to comment.