Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reservedFirst for jsx-sort-props rule #1883

Merged
merged 2 commits into from Jul 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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