Skip to content

Commit b26ac62

Browse files
committed
fix: error when use object rest properties
1 parent a6a773a commit b26ac62

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

lib/rules/require-default-props.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,19 @@ module.exports = {
123123
break;
124124
}
125125
case 'ObjectPattern': {
126-
props.properties.forEach((prop) => {
127-
const propName = prop.key.name;
128-
const propType = propTypes[propName];
129-
if (!propType || propType.isRequired) {
130-
return;
126+
props.properties.filter((prop) => {
127+
if (prop.type === 'RestElement') {
128+
return false;
131129
}
132-
if (prop.value.type === 'AssignmentPattern') {
133-
return;
130+
const propType = propTypes[prop.key.name];
131+
if (!propType || propType.isRequired) {
132+
return false;
134133
}
134+
return prop.value.type !== 'AssignmentPattern';
135+
}).forEach((prop) => {
135136
report(context, messages.shouldAssignObjectDefault, 'shouldAssignObjectDefault', {
136137
node: prop,
137-
data: { name: propName },
138+
data: { name: prop.key.name },
138139
});
139140
});
140141
break;

tests/lib/rules/require-default-props.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,34 @@ ruleTester.run('require-default-props', rule, {
261261
`,
262262
options: [{ forceDefaultPropArguments: true }],
263263
},
264+
{
265+
code: `
266+
function MyStatelessComponent({ foo, bar = 'asdf', ...props }) {
267+
return <div>{foo}{bar}</div>;
268+
}
269+
MyStatelessComponent.propTypes = {
270+
foo: PropTypes.string.isRequired,
271+
bar: PropTypes.string,
272+
hello: PropTypes.string.isRequired,
273+
world: PropTypes.string.isRequired
274+
}
275+
`,
276+
options: [{ forceDefaultPropArguments: true }],
277+
},
278+
{
279+
code: `
280+
function MyStatelessComponent({ foo, bar, ...props }) {
281+
return <div>{foo}{bar}</div>;
282+
}
283+
MyStatelessComponent.propTypes = {
284+
foo: PropTypes.string.isRequired,
285+
bar: PropTypes.string.isRequired,
286+
hello: PropTypes.string.isRequired,
287+
world: PropTypes.string.isRequired
288+
}
289+
`,
290+
options: [{ forceDefaultPropArguments: true }],
291+
},
264292

265293
// stateless components as function expressions
266294
{
@@ -1346,6 +1374,27 @@ ruleTester.run('require-default-props', rule, {
13461374
},
13471375
],
13481376
},
1377+
{
1378+
code: `
1379+
function MyStatelessComponent({ foo, bar, ...props }) {
1380+
return <div>{foo}{bar}</div>;
1381+
}
1382+
MyStatelessComponent.propTypes = {
1383+
foo: PropTypes.string,
1384+
bar: PropTypes.string.isRequired,
1385+
hello: PropTypes.string.isRequired,
1386+
world: PropTypes.string.isRequired
1387+
}
1388+
`,
1389+
options: [{ forceDefaultPropArguments: true }],
1390+
errors: [
1391+
{
1392+
messageId: 'shouldAssignObjectDefault',
1393+
line: 2,
1394+
column: 41,
1395+
},
1396+
],
1397+
},
13491398

13501399
// createReactClass components
13511400
{

0 commit comments

Comments
 (0)