Skip to content

Commit 490f90c

Browse files
committed
Support proptype shape in a variable for sortShapeProp
Resolves jsx-eslint#1749
1 parent 95d3c3f commit 490f90c

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
@@ -261,7 +261,15 @@ module.exports = {
261261
if (!sortShapeProp || !isShapeProp(node) || !(node.arguments && node.arguments[0])) {
262262
return;
263263
}
264-
checkSorted(node.arguments[0].properties);
264+
265+
if (node.arguments[0].properties) {
266+
checkSorted(node.arguments[0].properties);
267+
} else if (node.arguments[0].type === 'Identifier') {
268+
const variable = variableUtil.findVariableByName(context, node.arguments[0].name);
269+
if (variable && variable.properties) {
270+
checkSorted(variable.properties);
271+
}
272+
}
265273
},
266274

267275
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: [{
@@ -1533,5 +1572,98 @@ ruleTester.run('sort-prop-types', rule, {
15331572
' }',
15341573
'});'
15351574
].join('\n')
1575+
}, {
1576+
code: `
1577+
const shape = {
1578+
c: PropTypes.any,
1579+
a: PropTypes.any,
1580+
b: PropTypes.bool,
1581+
};
1582+
class Component extends React.Component {
1583+
static propTypes = {
1584+
x: PropTypes.shape(shape),
1585+
};
1586+
1587+
render() {
1588+
return <div />;
1589+
}
1590+
}
1591+
`,
1592+
options: [{
1593+
sortShapeProp: true
1594+
}],
1595+
parser: 'babel-eslint',
1596+
errors: [{
1597+
message: ERROR_MESSAGE,
1598+
line: 4,
1599+
column: 9,
1600+
type: 'Property'
1601+
}, {
1602+
message: ERROR_MESSAGE,
1603+
line: 5,
1604+
column: 9,
1605+
type: 'Property'
1606+
}],
1607+
output: `
1608+
const shape = {
1609+
a: PropTypes.any,
1610+
b: PropTypes.bool,
1611+
c: PropTypes.any,
1612+
};
1613+
class Component extends React.Component {
1614+
static propTypes = {
1615+
x: PropTypes.shape(shape),
1616+
};
1617+
1618+
render() {
1619+
return <div />;
1620+
}
1621+
}
1622+
`
1623+
}, {
1624+
code: `
1625+
const shape = {
1626+
c: PropTypes.any,
1627+
a: PropTypes.any,
1628+
b: PropTypes.bool,
1629+
};
1630+
class Component extends React.Component {
1631+
render() {
1632+
return <div />;
1633+
}
1634+
}
1635+
Component.propTypes = {
1636+
x: PropTypes.shape(shape)
1637+
};
1638+
`,
1639+
options: [{
1640+
sortShapeProp: true
1641+
}],
1642+
errors: [{
1643+
message: ERROR_MESSAGE,
1644+
line: 4,
1645+
column: 9,
1646+
type: 'Property'
1647+
}, {
1648+
message: ERROR_MESSAGE,
1649+
line: 5,
1650+
column: 9,
1651+
type: 'Property'
1652+
}],
1653+
output: `
1654+
const shape = {
1655+
a: PropTypes.any,
1656+
b: PropTypes.bool,
1657+
c: PropTypes.any,
1658+
};
1659+
class Component extends React.Component {
1660+
render() {
1661+
return <div />;
1662+
}
1663+
}
1664+
Component.propTypes = {
1665+
x: PropTypes.shape(shape)
1666+
};
1667+
`
15361668
}]
15371669
});

0 commit comments

Comments
 (0)