Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support proptype shape in a variable for sortShapeProp #1823

Merged
merged 1 commit into from May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
};
`
}]
});