Skip to content

Commit

Permalink
[Fix] sort-prop-types: Support propType shape in a variable for sor…
Browse files Browse the repository at this point in the history
…tShapeProp

Resolves jsx-eslint#1749
  • Loading branch information
alexzherdev authored and ljharb committed Jun 13, 2018
1 parent 3d6b371 commit a3c6d7e
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/rules/sort-prop-types.js
Expand Up @@ -270,7 +270,16 @@ module.exports = {
if (!sortShapeProp || !isShapeProp(node) || !(node.arguments && node.arguments[0])) {
return;
}
checkSorted(node.arguments[0].properties);

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

ClassProperty(node) {
Expand Down
132 changes: 132 additions & 0 deletions tests/lib/rules/sort-prop-types.js
Expand Up @@ -428,6 +428,45 @@ ruleTester.run('sort-prop-types', rule, {
options: [{
ignoreCase: 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 @@ -1615,5 +1654,98 @@ ruleTester.run('sort-prop-types', rule, {
1: PropTypes.any,
};
`
}, {
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 a3c6d7e

Please sign in to comment.