Skip to content

Commit 20103c5

Browse files
stefanwullemsljharb
authored andcommitted
[Fix] function-component-definition: ignore object properties
Fixes #2765.
1 parent 911f66e commit 20103c5

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
88
### Added
99
* [`button-has-type`]: support trivial ternary expressions ([#2748][] @Hypnosphi)
1010

11+
### Fixed
12+
* [`function-component-definition`]: ignore object properties ([#2771][] @stefan-wullems)
13+
14+
[#2771]: https://github.com/yannickcr/eslint-plugin-react/pull/2771
1115
[#2748]: https://github.com/yannickcr/eslint-plugin-react/pull/2748
1216

1317
## [7.20.6] - 2020.08.12

lib/rules/function-component-definition.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ module.exports = {
156156

157157
function validate(node, functionType) {
158158
if (!components.get(node)) return;
159+
160+
if (node.parent && node.parent.type === 'Property') return;
161+
159162
if (hasName(node) && namedConfig !== functionType) {
160163
report(node, {
161164
message: ERROR_MESSAGES[namedConfig],

tests/lib/rules/function-component-definition.js

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,144 @@ ruleTester.run('function-component-definition', rule, {
166166
code: 'function Hello(props): ReactNode { return <p/> }',
167167
options: [{namedComponents: 'function-declaration'}],
168168
parser: parsers.TYPESCRIPT_ESLINT
169+
},
170+
// https://github.com/yannickcr/eslint-plugin-react/issues/2765
171+
{
172+
code: [
173+
'const obj = {',
174+
' serialize: (el) => {',
175+
' return <p/>',
176+
' }',
177+
'}'
178+
].join('\n'),
179+
options: [{namedComponents: 'function-declaration'}]
180+
}, {
181+
code: [
182+
'const obj = {',
183+
' serialize: (el) => {',
184+
' return <p/>',
185+
' }',
186+
'}'
187+
].join('\n'),
188+
options: [{namedComponents: 'arrow-function'}]
189+
}, {
190+
code: [
191+
'const obj = {',
192+
' serialize: (el) => {',
193+
' return <p/>',
194+
' }',
195+
'}'
196+
].join('\n'),
197+
options: [{namedComponents: 'function-expression'}]
198+
},
199+
{
200+
code: [
201+
'const obj = {',
202+
' serialize: function (el) {',
203+
' return <p/>',
204+
' }',
205+
'}'
206+
].join('\n'),
207+
options: [{namedComponents: 'function-declaration'}]
208+
}, {
209+
code: [
210+
'const obj = {',
211+
' serialize: function (el) {',
212+
' return <p/>',
213+
' }',
214+
'}'
215+
].join('\n'),
216+
options: [{namedComponents: 'arrow-function'}]
217+
}, {
218+
code: [
219+
'const obj = {',
220+
' serialize: function (el) {',
221+
' return <p/>',
222+
' }',
223+
'}'
224+
].join('\n'),
225+
options: [{namedComponents: 'function-expression'}]
226+
}, {
227+
code: [
228+
'const obj = {',
229+
' serialize(el) {',
230+
' return <p/>',
231+
' }',
232+
'}'
233+
].join('\n'),
234+
options: [{namedComponents: 'function-declaration'}]
235+
}, {
236+
code: [
237+
'const obj = {',
238+
' serialize(el) {',
239+
' return <p/>',
240+
' }',
241+
'}'
242+
].join('\n'),
243+
options: [{namedComponents: 'arrow-function'}]
244+
}, {
245+
code: [
246+
'const obj = {',
247+
' serialize(el) {',
248+
' return <p/>',
249+
' }',
250+
'}'
251+
].join('\n'),
252+
options: [{namedComponents: 'function-expression'}]
253+
}, {
254+
code: [
255+
'const obj = {',
256+
' serialize(el) {',
257+
' return <p/>',
258+
' }',
259+
'}'
260+
].join('\n'),
261+
options: [{unnamedComponents: 'arrow-function'}]
262+
}, {
263+
code: [
264+
'const obj = {',
265+
' serialize(el) {',
266+
' return <p/>',
267+
' }',
268+
'}'
269+
].join('\n'),
270+
options: [{unnamedComponents: 'function-expression'}]
271+
}, {
272+
code: [
273+
'const obj = {',
274+
' serialize: (el) => {',
275+
' return <p/>',
276+
' }',
277+
'}'
278+
].join('\n'),
279+
options: [{unnamedComponents: 'arrow-function'}]
280+
}, {
281+
code: [
282+
'const obj = {',
283+
' serialize: (el) => {',
284+
' return <p/>',
285+
' }',
286+
'}'
287+
].join('\n'),
288+
options: [{unnamedComponents: 'function-expression'}]
289+
}, {
290+
code: [
291+
'const obj = {',
292+
' serialize: function (el) {',
293+
' return <p/>',
294+
' }',
295+
'}'
296+
].join('\n'),
297+
options: [{unnamedComponents: 'arrow-function'}]
298+
}, {
299+
code: [
300+
'const obj = {',
301+
' serialize: function (el) {',
302+
' return <p/>',
303+
' }',
304+
'}'
305+
].join('\n'),
306+
options: [{unnamedComponents: 'function-expression'}]
169307
}],
170308

171309
invalid: [{

0 commit comments

Comments
 (0)