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

function-component-definition: ignore properties #2771

Merged
merged 1 commit into from Aug 30, 2020
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,10 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
### Added
* [`button-has-type`]: support trivial ternary expressions ([#2748][] @Hypnosphi)

### Fixed
* [`function-component-definition`]: ignore object properties ([#2771][] @stefan-wullems)

[#2771]: https://github.com/yannickcr/eslint-plugin-react/pull/2771
[#2748]: https://github.com/yannickcr/eslint-plugin-react/pull/2748

## [7.20.6] - 2020.08.12
Expand Down
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'}]
stefan-wullems marked this conversation as resolved.
Show resolved Hide resolved
}, {
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