Skip to content

Commit 336ff2f

Browse files
committed
Support proptype shape in a variable for sortShapeProp
Resolves jsx-eslint#1749
1 parent 4e80833 commit 336ff2f

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
@@ -256,7 +256,15 @@ module.exports = {
256256
if (!sortShapeProp || !isShapeProp(node) || !(node.arguments && node.arguments[0])) {
257257
return;
258258
}
259-
checkSorted(node.arguments[0].properties);
259+
260+
if (node.arguments[0].properties) {
261+
checkSorted(node.arguments[0].properties);
262+
} else if (node.arguments[0].type === 'Identifier') {
263+
const variable = variableUtil.findVariableByName(context, node.arguments[0].name);
264+
if (variable && variable.properties) {
265+
checkSorted(variable.properties);
266+
}
267+
}
260268
},
261269

262270
ClassProperty: function(node) {

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

+132
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,45 @@ ruleTester.run('sort-prop-types', rule, {
382382
options: [{
383383
sortShapeProp: true
384384
}]
385+
}, {
386+
code: `
387+
const shape = {
388+
a: PropTypes.any,
389+
b: PropTypes.bool,
390+
c: PropTypes.any,
391+
};
392+
class Component extends React.Component {
393+
static propTypes = {
394+
x: PropTypes.shape(shape),
395+
};
396+
render() {
397+
return <div />;
398+
}
399+
}
400+
`,
401+
options: [{
402+
sortShapeProp: true
403+
}],
404+
parser: 'babel-eslint'
405+
}, {
406+
code: `
407+
const shape = {
408+
a: PropTypes.any,
409+
b: PropTypes.bool,
410+
c: PropTypes.any,
411+
};
412+
class Component extends React.Component {
413+
render() {
414+
return <div />;
415+
}
416+
}
417+
Component.propTypes = {
418+
x: PropTypes.shape(shape)
419+
};
420+
`,
421+
options: [{
422+
sortShapeProp: true
423+
}]
385424
}],
386425

387426
invalid: [{
@@ -1471,5 +1510,98 @@ ruleTester.run('sort-prop-types', rule, {
14711510
}
14721511
}
14731512
`
1513+
}, {
1514+
code: `
1515+
const shape = {
1516+
c: PropTypes.any,
1517+
a: PropTypes.any,
1518+
b: PropTypes.bool,
1519+
};
1520+
class Component extends React.Component {
1521+
static propTypes = {
1522+
x: PropTypes.shape(shape),
1523+
};
1524+
1525+
render() {
1526+
return <div />;
1527+
}
1528+
}
1529+
`,
1530+
options: [{
1531+
sortShapeProp: true
1532+
}],
1533+
parser: 'babel-eslint',
1534+
errors: [{
1535+
message: ERROR_MESSAGE,
1536+
line: 4,
1537+
column: 9,
1538+
type: 'Property'
1539+
}, {
1540+
message: ERROR_MESSAGE,
1541+
line: 5,
1542+
column: 9,
1543+
type: 'Property'
1544+
}],
1545+
output: `
1546+
const shape = {
1547+
a: PropTypes.any,
1548+
b: PropTypes.bool,
1549+
c: PropTypes.any,
1550+
};
1551+
class Component extends React.Component {
1552+
static propTypes = {
1553+
x: PropTypes.shape(shape),
1554+
};
1555+
1556+
render() {
1557+
return <div />;
1558+
}
1559+
}
1560+
`
1561+
}, {
1562+
code: `
1563+
const shape = {
1564+
c: PropTypes.any,
1565+
a: PropTypes.any,
1566+
b: PropTypes.bool,
1567+
};
1568+
class Component extends React.Component {
1569+
render() {
1570+
return <div />;
1571+
}
1572+
}
1573+
Component.propTypes = {
1574+
x: PropTypes.shape(shape)
1575+
};
1576+
`,
1577+
options: [{
1578+
sortShapeProp: true
1579+
}],
1580+
errors: [{
1581+
message: ERROR_MESSAGE,
1582+
line: 4,
1583+
column: 9,
1584+
type: 'Property'
1585+
}, {
1586+
message: ERROR_MESSAGE,
1587+
line: 5,
1588+
column: 9,
1589+
type: 'Property'
1590+
}],
1591+
output: `
1592+
const shape = {
1593+
a: PropTypes.any,
1594+
b: PropTypes.bool,
1595+
c: PropTypes.any,
1596+
};
1597+
class Component extends React.Component {
1598+
render() {
1599+
return <div />;
1600+
}
1601+
}
1602+
Component.propTypes = {
1603+
x: PropTypes.shape(shape)
1604+
};
1605+
`
14741606
}]
14751607
});

0 commit comments

Comments
 (0)