Skip to content

Commit ab9040b

Browse files
committed
Support proptype shape in a variable for sortShapeProp
Resolves jsx-eslint#1749
1 parent c82746c commit ab9040b

File tree

2 files changed

+109
-1
lines changed

2 files changed

+109
-1
lines changed

lib/rules/sort-prop-types.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,15 @@ module.exports = {
169169
if (!sortShapeProp || !isShapeProp(node) || (!node.arguments && !node.arguments[0])) {
170170
return;
171171
}
172-
checkSorted(node.arguments[0].properties);
172+
173+
if (node.arguments[0].properties) {
174+
checkSorted(node.arguments[0].properties);
175+
} else if (node.arguments[0].type === 'Identifier') {
176+
const variable = variableUtil.findVariableByName(context, node.arguments[0].name);
177+
if (variable && variable.properties) {
178+
checkSorted(variable.properties);
179+
}
180+
}
173181
},
174182

175183
ClassProperty: function(node) {

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

+100
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: [{
@@ -1016,5 +1055,66 @@ ruleTester.run('sort-prop-types', rule, {
10161055
column: 13,
10171056
type: 'Property'
10181057
}]
1058+
}, {
1059+
code: `
1060+
const shape = {
1061+
c: PropTypes.any,
1062+
a: PropTypes.any,
1063+
b: PropTypes.bool,
1064+
};
1065+
class Component extends React.Component {
1066+
static propTypes = {
1067+
x: PropTypes.shape(shape),
1068+
};
1069+
render() {
1070+
return <div />;
1071+
}
1072+
}
1073+
`,
1074+
options: [{
1075+
sortShapeProp: true
1076+
}],
1077+
parser: 'babel-eslint',
1078+
errors: [{
1079+
message: ERROR_MESSAGE,
1080+
line: 4,
1081+
column: 9,
1082+
type: 'Property'
1083+
}, {
1084+
message: ERROR_MESSAGE,
1085+
line: 5,
1086+
column: 9,
1087+
type: 'Property'
1088+
}]
1089+
}, {
1090+
code: `
1091+
const shape = {
1092+
c: PropTypes.any,
1093+
a: PropTypes.any,
1094+
b: PropTypes.bool,
1095+
};
1096+
class Component extends React.Component {
1097+
render() {
1098+
return <div />;
1099+
}
1100+
}
1101+
Component.propTypes = {
1102+
x: PropTypes.shape(shape)
1103+
};
1104+
`,
1105+
options: [{
1106+
sortShapeProp: true
1107+
}],
1108+
errors: [{
1109+
message: ERROR_MESSAGE,
1110+
line: 4,
1111+
column: 9,
1112+
type: 'Property'
1113+
}, {
1114+
message: ERROR_MESSAGE,
1115+
line: 5,
1116+
column: 9,
1117+
type: 'Property'
1118+
}]
10191119
}]
10201120
});

0 commit comments

Comments
 (0)