From 049d95e23e7838449f61ff7acb4eca6e7ef64468 Mon Sep 17 00:00:00 2001 From: Karl Fleischmann Date: Thu, 12 Jul 2018 16:30:42 +0200 Subject: [PATCH 1/2] [jsx-sort-props] Test reservedFirst/shorthandLast Add tests, that surface the incorrect behavior of the combinations `reservedFirst` and `shorthandLast` for the `jsx-sort-props` rule. --- tests/lib/rules/jsx-sort-props.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/lib/rules/jsx-sort-props.js b/tests/lib/rules/jsx-sort-props.js index 412ef4aefd..2f32e6b2ac 100644 --- a/tests/lib/rules/jsx-sort-props.js +++ b/tests/lib/rules/jsx-sort-props.js @@ -88,6 +88,10 @@ const reservedFirstWithNoSortAlphabeticallyArgs = [{ noSortAlphabetically: true, reservedFirst: true }]; +const reservedFirstWithShorthandLast = [{ + reservedFirst: true, + shorthandLast: true +}]; const reservedFirstAsEmptyArrayArgs = [{ reservedFirst: [] }]; @@ -149,6 +153,10 @@ ruleTester.run('jsx-sort-props', rule, { { code: '
} b a c />', options: reservedFirstWithNoSortAlphabeticallyArgs + }, + { + code: '', + options: reservedFirstWithShorthandLast } ], invalid: [ @@ -230,6 +238,16 @@ ruleTester.run('jsx-sort-props', rule, { `, errors: 3 }, + { + code: '', + errors: [expectedShorthandLastError], + options: reservedFirstWithShorthandLast + }, + { + code: '', + errors: [expectedError, expectedShorthandLastError], + options: reservedFirstWithShorthandLast + }, { code: ';', errors: [expectedError], @@ -293,7 +311,7 @@ ruleTester.run('jsx-sort-props', rule, { code: '', options: reservedFirstAsBooleanArgs, output: '', - errors: [expectedReservedFirstError] + errors: [expectedReservedFirstError, expectedError] }, { code: '} />', From 21faf023a3f188a2305ffb6d3027e88655eaa2b9 Mon Sep 17 00:00:00 2001 From: Karl Fleischmann Date: Thu, 12 Jul 2018 16:33:05 +0200 Subject: [PATCH 2/2] [jsx-sort-props] Fix rule's behavior - Remove unneccessary handling of sort-checking in jsx props, as this is being handled later by the general case anyways. - Fix erroneous call to `generateFixerFunction` in general sort-checking, which was missing the `reservedList` parameter. --- lib/rules/jsx-sort-props.js | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/lib/rules/jsx-sort-props.js b/lib/rules/jsx-sort-props.js index 99aba6b070..d6e5a982ef 100644 --- a/lib/rules/jsx-sort-props.js +++ b/lib/rules/jsx-sort-props.js @@ -245,15 +245,8 @@ module.exports = { const previousIsReserved = isReservedPropName(previousPropName, reservedList); const currentIsReserved = isReservedPropName(currentPropName, reservedList); - if ((previousIsReserved && currentIsReserved) || (!previousIsReserved && !currentIsReserved)) { - if (!noSortAlphabetically && currentPropName < previousPropName) { - context.report({ - node: decl, - message: 'Props should be sorted alphabetically', - fix: generateFixerFunction(node, context, reservedList) - }); - return memo; - } + if (previousIsReserved && !currentIsReserved) { + return decl; } if (!previousIsReserved && currentIsReserved) { context.report({ @@ -261,8 +254,8 @@ module.exports = { message: 'Reserved props must be listed before all other props', fix: generateFixerFunction(node, context, reservedList) }); + return memo; } - return decl; } if (callbacksLast) { @@ -310,7 +303,7 @@ module.exports = { context.report({ node: decl, message: 'Props should be sorted alphabetically', - fix: generateFixerFunction(node, context) + fix: generateFixerFunction(node, context, reservedList) }); return memo; }