Skip to content

Commit

Permalink
Merge pull request #1883 from fleischie/jsx-sort-props/fix-reserved-f…
Browse files Browse the repository at this point in the history
…irst

Fix `reservedFirst` for `jsx-sort-props` rule
  • Loading branch information
ljharb committed Jul 16, 2018
2 parents b1f2b6f + 21faf02 commit 0ebcb62
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
15 changes: 4 additions & 11 deletions lib/rules/jsx-sort-props.js
Expand Up @@ -245,24 +245,17 @@ 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({
node: decl,
message: 'Reserved props must be listed before all other props',
fix: generateFixerFunction(node, context, reservedList)
});
return memo;
}
return decl;
}

if (callbacksLast) {
Expand Down Expand Up @@ -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;
}
Expand Down
20 changes: 19 additions & 1 deletion tests/lib/rules/jsx-sort-props.js
Expand Up @@ -88,6 +88,10 @@ const reservedFirstWithNoSortAlphabeticallyArgs = [{
noSortAlphabetically: true,
reservedFirst: true
}];
const reservedFirstWithShorthandLast = [{
reservedFirst: true,
shorthandLast: true
}];
const reservedFirstAsEmptyArrayArgs = [{
reservedFirst: []
}];
Expand Down Expand Up @@ -149,6 +153,10 @@ ruleTester.run('jsx-sort-props', rule, {
{
code: '<div ref="r" dangerouslySetInnerHTML={{__html: "EPR"}} key={0} children={<App />} b a c />',
options: reservedFirstWithNoSortAlphabeticallyArgs
},
{
code: '<App key="key" c="c" b />',
options: reservedFirstWithShorthandLast
}
],
invalid: [
Expand Down Expand Up @@ -230,6 +238,16 @@ ruleTester.run('jsx-sort-props', rule, {
`,
errors: 3
},
{
code: '<App key="key" b c="c" />',
errors: [expectedShorthandLastError],
options: reservedFirstWithShorthandLast
},
{
code: '<App ref="ref" key="key" isShorthand veryLastAttribute="yes" />',
errors: [expectedError, expectedShorthandLastError],
options: reservedFirstWithShorthandLast
},
{
code: '<App a z onFoo onBar />;',
errors: [expectedError],
Expand Down Expand Up @@ -293,7 +311,7 @@ ruleTester.run('jsx-sort-props', rule, {
code: '<App dangerouslySetInnerHTML={{__html: "EPR"}} e key={2} b />',
options: reservedFirstAsBooleanArgs,
output: '<App key={2} b dangerouslySetInnerHTML={{__html: "EPR"}} e />',
errors: [expectedReservedFirstError]
errors: [expectedReservedFirstError, expectedError]
},
{
code: '<App key={3} children={<App />} />',
Expand Down

0 comments on commit 0ebcb62

Please sign in to comment.