Skip to content

Commit

Permalink
[Fix] function-component-definition: ignore object properties
Browse files Browse the repository at this point in the history
Fixes #2765.
  • Loading branch information
stefanwullems authored and ljharb committed Aug 26, 2020
1 parent 911f66e commit a00e067
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/rules/function-component-definition.js
Expand Up @@ -156,6 +156,9 @@ module.exports = {

function validate(node, functionType) {
if (!components.get(node)) return;

if (node.parent && node.parent.type === 'Property') return;

if (hasName(node) && namedConfig !== functionType) {
report(node, {
message: ERROR_MESSAGES[namedConfig],
Expand Down
138 changes: 138 additions & 0 deletions tests/lib/rules/function-component-definition.js
Expand Up @@ -166,6 +166,144 @@ ruleTester.run('function-component-definition', rule, {
code: 'function Hello(props): ReactNode { return <p/> }',
options: [{namedComponents: 'function-declaration'}],
parser: parsers.TYPESCRIPT_ESLINT
},
// https://github.com/yannickcr/eslint-plugin-react/issues/2765
{
code: [
'const obj = {',
' serialize: (el) => {',
' return <p/>',
' }',
'}'
].join('\n'),
options: [{namedComponents: 'function-declaration'}]
}, {
code: [
'const obj = {',
' serialize: (el) => {',
' return <p/>',
' }',
'}'
].join('\n'),
options: [{namedComponents: 'arrow-function'}]
}, {
code: [
'const obj = {',
' serialize: (el) => {',
' return <p/>',
' }',
'}'
].join('\n'),
options: [{namedComponents: 'function-expression'}]
},
{
code: [
'const obj = {',
' serialize: function (el) {',
' return <p/>',
' }',
'}'
].join('\n'),
options: [{namedComponents: 'function-declaration'}]
}, {
code: [
'const obj = {',
' serialize: function (el) {',
' return <p/>',
' }',
'}'
].join('\n'),
options: [{namedComponents: 'arrow-function'}]
}, {
code: [
'const obj = {',
' serialize: function (el) {',
' return <p/>',
' }',
'}'
].join('\n'),
options: [{namedComponents: 'function-expression'}]
}, {
code: [
'const obj = {',
' serialize(el) {',
' return <p/>',
' }',
'}'
].join('\n'),
options: [{namedComponents: 'function-declaration'}]
}, {
code: [
'const obj = {',
' serialize(el) {',
' return <p/>',
' }',
'}'
].join('\n'),
options: [{namedComponents: 'arrow-function'}]
}, {
code: [
'const obj = {',
' serialize(el) {',
' return <p/>',
' }',
'}'
].join('\n'),
options: [{namedComponents: 'function-expression'}]
}, {
code: [
'const obj = {',
' serialize(el) {',
' return <p/>',
' }',
'}'
].join('\n'),
options: [{unnamedComponents: 'arrow-function'}]
}, {
code: [
'const obj = {',
' serialize(el) {',
' return <p/>',
' }',
'}'
].join('\n'),
options: [{unnamedComponents: 'function-expression'}]
}, {
code: [
'const obj = {',
' serialize: (el) => {',
' return <p/>',
' }',
'}'
].join('\n'),
options: [{unnamedComponents: 'arrow-function'}]
}, {
code: [
'const obj = {',
' serialize: (el) => {',
' return <p/>',
' }',
'}'
].join('\n'),
options: [{unnamedComponents: 'function-expression'}]
}, {
code: [
'const obj = {',
' serialize: function (el) {',
' return <p/>',
' }',
'}'
].join('\n'),
options: [{unnamedComponents: 'arrow-function'}]
}, {
code: [
'const obj = {',
' serialize: function (el) {',
' return <p/>',
' }',
'}'
].join('\n'),
options: [{unnamedComponents: 'function-expression'}]
}],

invalid: [{
Expand Down

0 comments on commit a00e067

Please sign in to comment.