Skip to content

Commit e324a19

Browse files
vedadeeptaljharb
authored andcommitted
[Fix] destructuring-assignment, component detection: handle default exports edge case
Fixes #3024.
1 parent 49e4bb6 commit e324a19

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1616
* [`no-deprecated`]: fix crash on rest elements ([#3016][] @ljharb)
1717
* [`destructuring-assignment`]: get the contextName correctly ([#3025][] @ohhoney1)
1818
* [`no-typos`]: prevent crash on styled components and forwardRefs ([#3036][] @ljharb)
19+
* [`destructuring-assignment`], component detection: handle default exports edge case ([#3038][] @vedadeepta)
1920

2021
### Changed
2122
* [Docs] [`jsx-no-bind`]: updates discussion of refs ([#2998][] @dimitropoulos)
2223
* [Refactor] `utils/Components`: correct spelling and delete unused code ([#3026][] @ohhoney1)
2324
* [Docs] [`jsx-uses-react`], [`react-in-jsx-scope`]: document [`react/jsx-runtime`] config ([#3018][] @pkuczynski @ljharb)
2425
* [Docs] [`require-default-props`]: fix small typo ([#2994][] @evsasse)
2526

27+
[#3038]: https://github.com/yannickcr/eslint-plugin-react/pull/3038
2628
[#3036]: https://github.com/yannickcr/eslint-plugin-react/issues/3036
2729
[#3026]: https://github.com/yannickcr/eslint-plugin-react/pull/3026
2830
[#3025]: https://github.com/yannickcr/eslint-plugin-react/pull/3025

lib/util/Components.js

+7
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,13 @@ function componentRule(rule, context) {
617617
}
618618

619619
if (node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') {
620+
if (node.parent.type === 'ExportDefaultDeclaration') {
621+
if (utils.isReturningJSX(node)) {
622+
return node;
623+
}
624+
return undefined;
625+
}
626+
620627
if (node.parent.type === 'VariableDeclarator' && utils.isReturningJSXOrNull(node)) {
621628
if (isFirstLetterCapitalized(node.parent.id.name)) {
622629
return node;

tests/lib/rules/destructuring-assignment.js

+27
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,17 @@ ruleTester.run('destructuring-assignment', rule, {
233233
},
234234
];
235235
`
236+
}, {
237+
code: `
238+
export default (fileName) => {
239+
const match = fileName.match(/some expression/);
240+
if (match) {
241+
return fn;
242+
}
243+
return null;
244+
};
245+
`
246+
236247
}],
237248

238249
invalid: [{
@@ -425,5 +436,21 @@ ruleTester.run('destructuring-assignment', rule, {
425436
messageId: 'useDestructAssignment',
426437
data: {type: 'context'}
427438
}]
439+
}, {
440+
code: `
441+
export default (props) => {
442+
const match = props.str.match(/some expression/);
443+
if (match) {
444+
return <span>jsx</span>;
445+
}
446+
return null;
447+
};
448+
`,
449+
options: ['always'],
450+
parser: parsers.BABEL_ESLINT,
451+
errors: [{
452+
messageId: 'useDestructAssignment',
453+
data: {type: 'props'}
454+
}]
428455
}]
429456
});

0 commit comments

Comments
 (0)