Skip to content

Commit 44f6c9f

Browse files
committed
Support proptype shape in a variable for sortShapeProp
Resolves jsx-eslint#1749
1 parent 60b5642 commit 44f6c9f

File tree

2 files changed

+141
-1
lines changed

2 files changed

+141
-1
lines changed

lib/rules/sort-prop-types.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,15 @@ module.exports = {
264264
if (!sortShapeProp || !isShapeProp(node) || !(node.arguments && node.arguments[0])) {
265265
return;
266266
}
267-
checkSorted(node.arguments[0].properties);
267+
268+
if (node.arguments[0].properties) {
269+
checkSorted(node.arguments[0].properties);
270+
} else if (node.arguments[0].type === 'Identifier') {
271+
const variable = variableUtil.findVariableByName(context, node.arguments[0].name);
272+
if (variable && variable.properties) {
273+
checkSorted(variable.properties);
274+
}
275+
}
268276
},
269277

270278
ClassProperty: function(node) {

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

+132
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,45 @@ ruleTester.run('sort-prop-types', rule, {
412412
options: [{
413413
noSortAlphabetically: true
414414
}]
415+
}, {
416+
code: `
417+
const shape = {
418+
a: PropTypes.any,
419+
b: PropTypes.bool,
420+
c: PropTypes.any,
421+
};
422+
class Component extends React.Component {
423+
static propTypes = {
424+
x: PropTypes.shape(shape),
425+
};
426+
render() {
427+
return <div />;
428+
}
429+
}
430+
`,
431+
options: [{
432+
sortShapeProp: true
433+
}],
434+
parser: 'babel-eslint'
435+
}, {
436+
code: `
437+
const shape = {
438+
a: PropTypes.any,
439+
b: PropTypes.bool,
440+
c: PropTypes.any,
441+
};
442+
class Component extends React.Component {
443+
render() {
444+
return <div />;
445+
}
446+
}
447+
Component.propTypes = {
448+
x: PropTypes.shape(shape)
449+
};
450+
`,
451+
options: [{
452+
sortShapeProp: true
453+
}]
415454
}],
416455

417456
invalid: [{
@@ -1567,5 +1606,98 @@ ruleTester.run('sort-prop-types', rule, {
15671606
' }',
15681607
'});'
15691608
].join('\n')
1609+
}, {
1610+
code: `
1611+
const shape = {
1612+
c: PropTypes.any,
1613+
a: PropTypes.any,
1614+
b: PropTypes.bool,
1615+
};
1616+
class Component extends React.Component {
1617+
static propTypes = {
1618+
x: PropTypes.shape(shape),
1619+
};
1620+
1621+
render() {
1622+
return <div />;
1623+
}
1624+
}
1625+
`,
1626+
options: [{
1627+
sortShapeProp: true
1628+
}],
1629+
parser: 'babel-eslint',
1630+
errors: [{
1631+
message: ERROR_MESSAGE,
1632+
line: 4,
1633+
column: 9,
1634+
type: 'Property'
1635+
}, {
1636+
message: ERROR_MESSAGE,
1637+
line: 5,
1638+
column: 9,
1639+
type: 'Property'
1640+
}],
1641+
output: `
1642+
const shape = {
1643+
a: PropTypes.any,
1644+
b: PropTypes.bool,
1645+
c: PropTypes.any,
1646+
};
1647+
class Component extends React.Component {
1648+
static propTypes = {
1649+
x: PropTypes.shape(shape),
1650+
};
1651+
1652+
render() {
1653+
return <div />;
1654+
}
1655+
}
1656+
`
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+
render() {
1666+
return <div />;
1667+
}
1668+
}
1669+
Component.propTypes = {
1670+
x: PropTypes.shape(shape)
1671+
};
1672+
`,
1673+
options: [{
1674+
sortShapeProp: true
1675+
}],
1676+
errors: [{
1677+
message: ERROR_MESSAGE,
1678+
line: 4,
1679+
column: 9,
1680+
type: 'Property'
1681+
}, {
1682+
message: ERROR_MESSAGE,
1683+
line: 5,
1684+
column: 9,
1685+
type: 'Property'
1686+
}],
1687+
output: `
1688+
const shape = {
1689+
a: PropTypes.any,
1690+
b: PropTypes.bool,
1691+
c: PropTypes.any,
1692+
};
1693+
class Component extends React.Component {
1694+
render() {
1695+
return <div />;
1696+
}
1697+
}
1698+
Component.propTypes = {
1699+
x: PropTypes.shape(shape)
1700+
};
1701+
`
15701702
}]
15711703
});

0 commit comments

Comments
 (0)