Skip to content

Commit c46efb8

Browse files
committed
Change multiline property from exclusive booleans to enums
1 parent 20cef0c commit c46efb8

File tree

3 files changed

+76
-36
lines changed

3 files changed

+76
-36
lines changed

docs/rules/jsx-sort-props.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ Examples of **correct** code for this rule:
2929
"callbacksLast": <boolean>,
3030
"shorthandFirst": <boolean>,
3131
"shorthandLast": <boolean>,
32-
"multilineFirst": <boolean>,
33-
"multilineLast": <boolean>,
32+
"multiline": "ignore" | "first" | "last",
3433
"ignoreCase": <boolean>,
3534
"noSortAlphabetically": <boolean>,
3635
"reservedFirst": <boolean>|<array<string>>,
@@ -72,11 +71,20 @@ When `true`, short hand props must be listed after all other props (unless `call
7271
<Hello name="John" tel={5555555} active validate />
7372
```
7473

75-
### `multilineFirst`
74+
### `multiline`
7675

77-
When `true`, multiline props must be listed before all other props (unless `shorthandFirst` is set), but still respecting the alphabetical order:
76+
Enforced sorting for multiline props
77+
78+
* `ignore`: Multiline props will not be taken in consideration for sorting.
79+
80+
* `first`: Multiline props must be listed before all other props (unless `shorthandFirst` is set), but still respecting the alphabetical order.
81+
82+
* `last`: Multiline props must be listed after all other props (unless either `callbacksLast` or `shorthandLast` are set), but still respecting the alphabetical order.
83+
84+
Defaults to `ignore`.
7885

7986
```jsx
87+
// 'jsx-sort-props': [1, { multiline: 'first' }]
8088
<Hello
8189
classes={{
8290
greetings: classes.greetings,
@@ -86,13 +94,8 @@ When `true`, multiline props must be listed before all other props (unless `shor
8694
name="John"
8795
tel={5555555}
8896
/>
89-
```
90-
91-
### `multilineLast`
9297

93-
When `true`, multiline props must be listed after all other props (unless either `callbacksLast` or `shorthandLast` are set), but still respecting the alphabetical order:
94-
95-
```jsx
98+
// 'jsx-sort-props': [1, { multiline: 'last' }]
9699
<Hello
97100
active
98101
validate

lib/rules/jsx-sort-props.js

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ function contextCompare(a, b, options) {
8181
}
8282
}
8383

84-
if (options.multilineFirst || options.multilineLast) {
85-
const multilineSign = options.multilineFirst ? -1 : 1;
84+
if (options.multiline === 'first' || options.multiline === 'last') {
85+
const multilineSign = options.multiline === 'first' ? -1 : 1;
8686
const aIsMultiline = isMultilineProp(a);
8787
const bIsMultiline = isMultilineProp(b);
8888
if (aIsMultiline && !bIsMultiline) {
@@ -145,8 +145,7 @@ const generateFixerFunction = (node, context, reservedList) => {
145145
const callbacksLast = configuration.callbacksLast || false;
146146
const shorthandFirst = configuration.shorthandFirst || false;
147147
const shorthandLast = configuration.shorthandLast || false;
148-
const multilineFirst = configuration.multilineFirst || false;
149-
const multilineLast = configuration.multilineLast || false;
148+
const multiline = configuration.multiline || 'ignore';
150149
const noSortAlphabetically = configuration.noSortAlphabetically || false;
151150
const reservedFirst = configuration.reservedFirst || false;
152151

@@ -158,8 +157,7 @@ const generateFixerFunction = (node, context, reservedList) => {
158157
callbacksLast,
159158
shorthandFirst,
160159
shorthandLast,
161-
multilineFirst,
162-
multilineLast,
160+
multiline,
163161
noSortAlphabetically,
164162
reservedFirst,
165163
reservedList,
@@ -263,13 +261,9 @@ module.exports = {
263261
shorthandLast: {
264262
type: 'boolean',
265263
},
266-
// Whether multiline properties should be listed first
267-
multilineFirst: {
268-
type: 'boolean',
269-
},
270-
// Whether multiline properties should be listed last
271-
multilineLast: {
272-
type: 'boolean',
264+
// Whether multiline properties should be listed first or last
265+
multiline: {
266+
enum: ['ignore', 'first', 'last'],
273267
},
274268
ignoreCase: {
275269
type: 'boolean',
@@ -292,8 +286,7 @@ module.exports = {
292286
const callbacksLast = configuration.callbacksLast || false;
293287
const shorthandFirst = configuration.shorthandFirst || false;
294288
const shorthandLast = configuration.shorthandLast || false;
295-
const multilineFirst = configuration.multilineFirst || false;
296-
const multilineLast = configuration.multilineLast || false;
289+
const multiline = configuration.multiline || 'ignore';
297290
const noSortAlphabetically = configuration.noSortAlphabetically || false;
298291
const reservedFirst = configuration.reservedFirst || false;
299292
const reservedFirstError = validateReservedFirstConfig(context, reservedFirst);
@@ -367,7 +360,7 @@ module.exports = {
367360
}
368361
if (!currentValue && previousValue) {
369362
report(context, messages.listShorthandFirst, 'listShorthandFirst', {
370-
node: memo.name,
363+
node: decl.name,
371364
fix: generateFixerFunction(node, context, reservedList),
372365
});
373366
return memo;
@@ -387,22 +380,22 @@ module.exports = {
387380
}
388381
}
389382

390-
if (multilineFirst) {
383+
if (multiline === 'first') {
391384
if (previousIsMultiline && !currentIsMultiline) {
392385
// Exiting the multiline prop section
393386
return decl;
394387
}
395388
if (!previousIsMultiline && currentIsMultiline) {
396389
// Encountered a non-multiline prop before a multiline prop
397390
report(context, messages.listMultilineFirst, 'listMultilineFirst', {
398-
node: memo.name,
391+
node: decl.name,
399392
fix: generateFixerFunction(node, context, reservedList),
400393
});
401394
return memo;
402395
}
403396
}
404397

405-
if (multilineLast) {
398+
if (multiline === 'last') {
406399
if (!previousIsMultiline && currentIsMultiline) {
407400
// Entering the multiline prop section
408401
return decl;

tests/lib/rules/jsx-sort-props.js

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,17 @@ const reservedFirstWithShorthandLast = [
103103
];
104104
const reservedFirstAsEmptyArrayArgs = [{ reservedFirst: [] }];
105105
const reservedFirstAsInvalidArrayArgs = [{ reservedFirst: ['notReserved'] }];
106-
const multilineFirstArgs = [{ multilineFirst: true }];
106+
const multilineFirstArgs = [{ multiline: 'first' }];
107107
const multilineAndShorthandFirstArgs = [
108108
{
109-
multilineFirst: true,
109+
multiline: 'first',
110110
shorthandFirst: true,
111111
},
112112
];
113-
const multilineLastArgs = [{ multilineLast: true }];
113+
const multilineLastArgs = [{ multiline: 'last' }];
114114
const multilineAndShorthandAndCallbackLastArgs = [
115115
{
116-
multilineLast: true,
116+
multiline: 'last',
117117
shorthandLast: true,
118118
callbacksLast: true,
119119
},
@@ -680,6 +680,7 @@ ruleTester.run('jsx-sort-props', rule, {
680680
aA: 1,
681681
}}
682682
b
683+
inline={1}
683684
onClick={() => ({
684685
c: 1
685686
})}
@@ -691,10 +692,28 @@ ruleTester.run('jsx-sort-props', rule, {
691692
/>
692693
`,
693694
options: multilineAndShorthandAndCallbackLastArgs,
694-
errors: 3,
695+
errors: [
696+
{
697+
messageId: 'listShorthandLast',
698+
line: 6,
699+
},
700+
{
701+
messageId: 'listCallbacksLast',
702+
line: 8,
703+
},
704+
{
705+
messageId: 'listCallbacksLast',
706+
line: 8,
707+
},
708+
{
709+
messageId: 'listCallbacksLast',
710+
line: 8,
711+
},
712+
],
695713
output: `
696714
<App
697715
d="dD"
716+
inline={1}
698717
a={{
699718
aA: 1,
700719
}}
@@ -738,7 +757,7 @@ ruleTester.run('jsx-sort-props', rule, {
738757
`,
739758
options: [
740759
{
741-
multilineLast: true,
760+
multiline: 'last',
742761
shorthandFirst: true,
743762
callbacksLast: true,
744763
reservedFirst: true,
@@ -771,7 +790,32 @@ ruleTester.run('jsx-sort-props', rule, {
771790
{...rest}
772791
/>
773792
`,
774-
errors: 6,
793+
errors: [
794+
{
795+
messageId: 'listMultilineLast',
796+
line: 4,
797+
},
798+
{
799+
messageId: 'listMultilineLast',
800+
line: 4,
801+
},
802+
{
803+
messageId: 'listReservedPropsFirst',
804+
line: 12,
805+
},
806+
{
807+
messageId: 'listShorthandFirst',
808+
line: 13,
809+
},
810+
{
811+
messageId: 'listCallbacksLast',
812+
line: 19,
813+
},
814+
{
815+
messageId: 'listCallbacksLast',
816+
line: 19,
817+
},
818+
],
775819
},
776820
]),
777821
});

0 commit comments

Comments
 (0)