Skip to content

Commit 35e323f

Browse files
committed
[Refactor] jsx-sort-default-props: remove unnecessary code
Fixes #1817
1 parent 2e6a391 commit 35e323f

File tree

3 files changed

+47
-21
lines changed

3 files changed

+47
-21
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
2929
* [Docs] [`display-name`]: improve examples ([#3189][] @golopot)
3030
* [Refactor] [`no-invalid-html-attribute`]: sort HTML_ELEMENTS and messages ([#3182][] @Primajin)
3131
* [Docs] [`forbid-foreign-prop-types`]: document `allowInPropTypes` option ([#1815][] @ljharb)
32+
* [Refactor] [`jsx-sort-default-props`]: remove unnecessary code ([#1817][] @ljharb)
3233

3334
[#3195]: https://github.com/yannickcr/eslint-plugin-react/pull/3195
3435
[#3191]: https://github.com/yannickcr/eslint-plugin-react/pull/3191
@@ -44,6 +45,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
4445
[#3133]: https://github.com/yannickcr/eslint-plugin-react/pull/3133
4546
[#2921]: https://github.com/yannickcr/eslint-plugin-react/pull/2921
4647
[#2753]: https://github.com/yannickcr/eslint-plugin-react/pull/2753
48+
[#1817]: https://github.com/yannickcr/eslint-plugin-react/issues/1817
4749
[#1815]: https://github.com/yannickcr/eslint-plugin-react/issues/1815
4850
[#1754]: https://github.com/yannickcr/eslint-plugin-react/issues/1754
4951
[#1046]: https://github.com/yannickcr/eslint-plugin-react/issues/1046

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

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
const variableUtil = require('../util/variable');
99
const docsUrl = require('../util/docsUrl');
10-
const propWrapperUtil = require('../util/propWrapper');
11-
// const propTypesSortUtil = require('../util/propTypesSort');
1210
const report = require('../util/report');
1311

1412
// ------------------------------------------------------------------------------
@@ -137,26 +135,16 @@ module.exports = {
137135
}
138136

139137
function checkNode(node) {
140-
switch (node && node.type) {
141-
case 'ObjectExpression':
142-
checkSorted(node.properties);
143-
break;
144-
case 'Identifier': {
145-
const propTypesObject = findVariableByName(node.name);
146-
if (propTypesObject && propTypesObject.properties) {
147-
checkSorted(propTypesObject.properties);
148-
}
149-
break;
150-
}
151-
case 'CallExpression': {
152-
const innerNode = node.arguments && node.arguments[0];
153-
if (propWrapperUtil.isPropWrapperFunction(context, node.callee.name) && innerNode) {
154-
checkNode(innerNode);
155-
}
156-
break;
138+
if (!node) {
139+
return;
140+
}
141+
if (node.type === 'ObjectExpression') {
142+
checkSorted(node.properties);
143+
} else if (node.type === 'Identifier') {
144+
const propTypesObject = findVariableByName(node.name);
145+
if (propTypesObject && propTypesObject.properties) {
146+
checkSorted(propTypesObject.properties);
157147
}
158-
default:
159-
break;
160148
}
161149
}
162150

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,22 @@ ruleTester.run('jsx-sort-default-props', rule, {
358358
First.propTypes = propTypes;
359359
First.defaultProps = defaultProps;
360360
`,
361+
},
362+
{
363+
code: `
364+
class First extends React.Component {
365+
render() {
366+
return <div />;
367+
}
368+
}
369+
370+
First.defaultProps = {
371+
a: PropTypes.any,
372+
onBar: PropTypes.func,
373+
onFoo: PropTypes.func,
374+
z: PropTypes.string,
375+
};
376+
`,
361377
}
362378
)),
363379

@@ -898,5 +914,25 @@ ruleTester.run('jsx-sort-default-props', rule, {
898914
},
899915
],
900916
},
917+
{
918+
code: `
919+
class First extends React.Component {
920+
render() {
921+
return <div />;
922+
}
923+
}
924+
925+
First.defaultProps = {
926+
a: PropTypes.any,
927+
z: PropTypes.string,
928+
onFoo: PropTypes.func,
929+
onBar: PropTypes.func,
930+
};
931+
`,
932+
errors: [
933+
{ messageId: 'propsNotSorted', line: 11 },
934+
{ messageId: 'propsNotSorted', line: 12 },
935+
],
936+
},
901937
]),
902938
});

0 commit comments

Comments
 (0)