Skip to content

Commit

Permalink
Support proptype shape in a variable for sortShapeProp
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzherdev committed Jul 24, 2018
1 parent 4e80833 commit 336ff2f
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/rules/sort-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,15 @@ module.exports = {
if (!sortShapeProp || !isShapeProp(node) || !(node.arguments && node.arguments[0])) {
return;
}
checkSorted(node.arguments[0].properties);

if (node.arguments[0].properties) {
checkSorted(node.arguments[0].properties);
} else if (node.arguments[0].type === 'Identifier') {
const variable = variableUtil.findVariableByName(context, node.arguments[0].name);
if (variable && variable.properties) {
checkSorted(variable.properties);
}
}
},

ClassProperty: function(node) {
Expand Down
132 changes: 132 additions & 0 deletions tests/lib/rules/sort-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,45 @@ ruleTester.run('sort-prop-types', rule, {
options: [{
sortShapeProp: true
}]
}, {
code: `
const shape = {
a: PropTypes.any,
b: PropTypes.bool,
c: PropTypes.any,
};
class Component extends React.Component {
static propTypes = {
x: PropTypes.shape(shape),
};
render() {
return <div />;
}
}
`,
options: [{
sortShapeProp: true
}],
parser: 'babel-eslint'
}, {
code: `
const shape = {
a: PropTypes.any,
b: PropTypes.bool,
c: PropTypes.any,
};
class Component extends React.Component {
render() {
return <div />;
}
}
Component.propTypes = {
x: PropTypes.shape(shape)
};
`,
options: [{
sortShapeProp: true
}]
}],

invalid: [{
Expand Down Expand Up @@ -1471,5 +1510,98 @@ ruleTester.run('sort-prop-types', rule, {
}
}
`
}, {
code: `
const shape = {
c: PropTypes.any,
a: PropTypes.any,
b: PropTypes.bool,
};
class Component extends React.Component {
static propTypes = {
x: PropTypes.shape(shape),
};
render() {
return <div />;
}
}
`,
options: [{
sortShapeProp: true
}],
parser: 'babel-eslint',
errors: [{
message: ERROR_MESSAGE,
line: 4,
column: 9,
type: 'Property'
}, {
message: ERROR_MESSAGE,
line: 5,
column: 9,
type: 'Property'
}],
output: `
const shape = {
a: PropTypes.any,
b: PropTypes.bool,
c: PropTypes.any,
};
class Component extends React.Component {
static propTypes = {
x: PropTypes.shape(shape),
};
render() {
return <div />;
}
}
`
}, {
code: `
const shape = {
c: PropTypes.any,
a: PropTypes.any,
b: PropTypes.bool,
};
class Component extends React.Component {
render() {
return <div />;
}
}
Component.propTypes = {
x: PropTypes.shape(shape)
};
`,
options: [{
sortShapeProp: true
}],
errors: [{
message: ERROR_MESSAGE,
line: 4,
column: 9,
type: 'Property'
}, {
message: ERROR_MESSAGE,
line: 5,
column: 9,
type: 'Property'
}],
output: `
const shape = {
a: PropTypes.any,
b: PropTypes.bool,
c: PropTypes.any,
};
class Component extends React.Component {
render() {
return <div />;
}
}
Component.propTypes = {
x: PropTypes.shape(shape)
};
`
}]
});

0 comments on commit 336ff2f

Please sign in to comment.