Skip to content

Commit

Permalink
[Refactor] jsx-sort-default-props: remove unnecessary code
Browse files Browse the repository at this point in the history
Fixes #1817
  • Loading branch information
ljharb committed Feb 6, 2022
1 parent 2e6a391 commit 35e323f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -29,6 +29,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
* [Docs] [`display-name`]: improve examples ([#3189][] @golopot)
* [Refactor] [`no-invalid-html-attribute`]: sort HTML_ELEMENTS and messages ([#3182][] @Primajin)
* [Docs] [`forbid-foreign-prop-types`]: document `allowInPropTypes` option ([#1815][] @ljharb)
* [Refactor] [`jsx-sort-default-props`]: remove unnecessary code ([#1817][] @ljharb)

[#3195]: https://github.com/yannickcr/eslint-plugin-react/pull/3195
[#3191]: https://github.com/yannickcr/eslint-plugin-react/pull/3191
Expand All @@ -44,6 +45,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
[#3133]: https://github.com/yannickcr/eslint-plugin-react/pull/3133
[#2921]: https://github.com/yannickcr/eslint-plugin-react/pull/2921
[#2753]: https://github.com/yannickcr/eslint-plugin-react/pull/2753
[#1817]: https://github.com/yannickcr/eslint-plugin-react/issues/1817
[#1815]: https://github.com/yannickcr/eslint-plugin-react/issues/1815
[#1754]: https://github.com/yannickcr/eslint-plugin-react/issues/1754
[#1046]: https://github.com/yannickcr/eslint-plugin-react/issues/1046
Expand Down
30 changes: 9 additions & 21 deletions lib/rules/jsx-sort-default-props.js
Expand Up @@ -7,8 +7,6 @@

const variableUtil = require('../util/variable');
const docsUrl = require('../util/docsUrl');
const propWrapperUtil = require('../util/propWrapper');
// const propTypesSortUtil = require('../util/propTypesSort');
const report = require('../util/report');

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -137,26 +135,16 @@ module.exports = {
}

function checkNode(node) {
switch (node && node.type) {
case 'ObjectExpression':
checkSorted(node.properties);
break;
case 'Identifier': {
const propTypesObject = findVariableByName(node.name);
if (propTypesObject && propTypesObject.properties) {
checkSorted(propTypesObject.properties);
}
break;
}
case 'CallExpression': {
const innerNode = node.arguments && node.arguments[0];
if (propWrapperUtil.isPropWrapperFunction(context, node.callee.name) && innerNode) {
checkNode(innerNode);
}
break;
if (!node) {
return;
}
if (node.type === 'ObjectExpression') {
checkSorted(node.properties);
} else if (node.type === 'Identifier') {
const propTypesObject = findVariableByName(node.name);
if (propTypesObject && propTypesObject.properties) {
checkSorted(propTypesObject.properties);
}
default:
break;
}
}

Expand Down
36 changes: 36 additions & 0 deletions tests/lib/rules/jsx-sort-default-props.js
Expand Up @@ -358,6 +358,22 @@ ruleTester.run('jsx-sort-default-props', rule, {
First.propTypes = propTypes;
First.defaultProps = defaultProps;
`,
},
{
code: `
class First extends React.Component {
render() {
return <div />;
}
}
First.defaultProps = {
a: PropTypes.any,
onBar: PropTypes.func,
onFoo: PropTypes.func,
z: PropTypes.string,
};
`,
}
)),

Expand Down Expand Up @@ -898,5 +914,25 @@ ruleTester.run('jsx-sort-default-props', rule, {
},
],
},
{
code: `
class First extends React.Component {
render() {
return <div />;
}
}
First.defaultProps = {
a: PropTypes.any,
z: PropTypes.string,
onFoo: PropTypes.func,
onBar: PropTypes.func,
};
`,
errors: [
{ messageId: 'propsNotSorted', line: 11 },
{ messageId: 'propsNotSorted', line: 12 },
],
},
]),
});

0 comments on commit 35e323f

Please sign in to comment.