Skip to content

Commit a3c6d7e

Browse files
alexzherdevljharb
authored andcommitted
[Fix] sort-prop-types: Support propType shape in a variable for sortShapeProp
Resolves jsx-eslint#1749
1 parent 3d6b371 commit a3c6d7e

File tree

2 files changed

+142
-1
lines changed

2 files changed

+142
-1
lines changed

lib/rules/sort-prop-types.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,16 @@ module.exports = {
270270
if (!sortShapeProp || !isShapeProp(node) || !(node.arguments && node.arguments[0])) {
271271
return;
272272
}
273-
checkSorted(node.arguments[0].properties);
273+
274+
const firstArg = node.arguments[0];
275+
if (firstArg.properties) {
276+
checkSorted(firstArg.properties);
277+
} else if (firstArg.type === 'Identifier') {
278+
const variable = variableUtil.findVariableByName(context, firstArg.name);
279+
if (variable && variable.properties) {
280+
checkSorted(variable.properties);
281+
}
282+
}
274283
},
275284

276285
ClassProperty(node) {

tests/lib/rules/sort-prop-types.js

+132
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,45 @@ ruleTester.run('sort-prop-types', rule, {
428428
options: [{
429429
ignoreCase: true
430430
}]
431+
}, {
432+
code: `
433+
const shape = {
434+
a: PropTypes.any,
435+
b: PropTypes.bool,
436+
c: PropTypes.any,
437+
};
438+
class Component extends React.Component {
439+
static propTypes = {
440+
x: PropTypes.shape(shape),
441+
};
442+
render() {
443+
return <div />;
444+
}
445+
}
446+
`,
447+
options: [{
448+
sortShapeProp: true
449+
}],
450+
parser: 'babel-eslint'
451+
}, {
452+
code: `
453+
const shape = {
454+
a: PropTypes.any,
455+
b: PropTypes.bool,
456+
c: PropTypes.any,
457+
};
458+
class Component extends React.Component {
459+
render() {
460+
return <div />;
461+
}
462+
}
463+
Component.propTypes = {
464+
x: PropTypes.shape(shape)
465+
};
466+
`,
467+
options: [{
468+
sortShapeProp: true
469+
}]
431470
}],
432471

433472
invalid: [{
@@ -1615,5 +1654,98 @@ ruleTester.run('sort-prop-types', rule, {
16151654
1: PropTypes.any,
16161655
};
16171656
`
1657+
}, {
1658+
code: `
1659+
const shape = {
1660+
c: PropTypes.any,
1661+
a: PropTypes.any,
1662+
b: PropTypes.bool,
1663+
};
1664+
class Component extends React.Component {
1665+
static propTypes = {
1666+
x: PropTypes.shape(shape),
1667+
};
1668+
1669+
render() {
1670+
return <div />;
1671+
}
1672+
}
1673+
`,
1674+
options: [{
1675+
sortShapeProp: true
1676+
}],
1677+
parser: 'babel-eslint',
1678+
errors: [{
1679+
message: ERROR_MESSAGE,
1680+
line: 4,
1681+
column: 9,
1682+
type: 'Property'
1683+
}, {
1684+
message: ERROR_MESSAGE,
1685+
line: 5,
1686+
column: 9,
1687+
type: 'Property'
1688+
}],
1689+
output: `
1690+
const shape = {
1691+
a: PropTypes.any,
1692+
b: PropTypes.bool,
1693+
c: PropTypes.any,
1694+
};
1695+
class Component extends React.Component {
1696+
static propTypes = {
1697+
x: PropTypes.shape(shape),
1698+
};
1699+
1700+
render() {
1701+
return <div />;
1702+
}
1703+
}
1704+
`
1705+
}, {
1706+
code: `
1707+
const shape = {
1708+
c: PropTypes.any,
1709+
a: PropTypes.any,
1710+
b: PropTypes.bool,
1711+
};
1712+
class Component extends React.Component {
1713+
render() {
1714+
return <div />;
1715+
}
1716+
}
1717+
Component.propTypes = {
1718+
x: PropTypes.shape(shape)
1719+
};
1720+
`,
1721+
options: [{
1722+
sortShapeProp: true
1723+
}],
1724+
errors: [{
1725+
message: ERROR_MESSAGE,
1726+
line: 4,
1727+
column: 9,
1728+
type: 'Property'
1729+
}, {
1730+
message: ERROR_MESSAGE,
1731+
line: 5,
1732+
column: 9,
1733+
type: 'Property'
1734+
}],
1735+
output: `
1736+
const shape = {
1737+
a: PropTypes.any,
1738+
b: PropTypes.bool,
1739+
c: PropTypes.any,
1740+
};
1741+
class Component extends React.Component {
1742+
render() {
1743+
return <div />;
1744+
}
1745+
}
1746+
Component.propTypes = {
1747+
x: PropTypes.shape(shape)
1748+
};
1749+
`
16181750
}]
16191751
});

0 commit comments

Comments
 (0)