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

Don't treat async functions as components #989

Merged
merged 1 commit into from
Dec 3, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,10 @@ function componentRule(rule, context) {
},

FunctionExpression: function(node) {
if (node.async) {
components.add(node, 0);
return;
}
var component = utils.getParentComponent();
if (
!component ||
Expand All @@ -509,6 +513,10 @@ function componentRule(rule, context) {
},

FunctionDeclaration: function(node) {
if (node.async) {
components.add(node, 0);
return;
}
node = utils.getParentComponent();
if (!node) {
return;
Expand All @@ -517,6 +525,10 @@ function componentRule(rule, context) {
},

ArrowFunctionExpression: function(node) {
if (node.async) {
components.add(node, 0);
return;
}
var component = utils.getParentComponent();
if (
!component ||
Expand Down
24 changes: 24 additions & 0 deletions tests/lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,30 @@ ruleTester.run('prop-types', rule, {
].join('\n'),
options: [{skipUndeclared: false}],
parserOptions: parserOptions
}, {
// Async functions can't be components.
code: [
'var Hello = async function(props) {',
' return <div>Hello {props.name}</div>;',
'}'
].join('\n'),
parser: 'babel-eslint'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's no need to use babel-eslint here - let's test this with eslint's default parser, which fully support async function now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was following the convention down the file in

    }, {
      code: [
        'var Hello = function(props) {',
        '  return <div>Hello {props.name}</div>;',
        '}'
      ].join('\n'),
      parser: 'babel-eslint',
      errors: [{
        message: '\'name\' is missing in props validation'
      }]
    }, {
      code: [
        'function Hello(props) {',
        '  return <div>Hello {props.name}</div>;',
        '}'
      ].join('\n'),
      parser: 'babel-eslint',
      errors: [{
        message: '\'name\' is missing in props validation'
      }]
    }, {
      code: [
        'var Hello = (props) => {',
        '  return <div>Hello {props.name}</div>;',
        '}'
      ].join('\n'),
      parser: 'babel-eslint',
      errors: [{
        message: '\'name\' is missing in props validation'
      }]
    }, {

Should those be changed as well?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, given "eslint": "^2.0.0 || ^3.0.0" in package.json, i think we probably do need to stick with babel-eslint here - we'd have to raise the peer dep requirement to be able to rely on the default parser. Let's leave this as-is for now.

}, {
// Async functions can't be components.
code: [
'async function Hello(props) {',
' return <div>Hello {props.name}</div>;',
'}'
].join('\n'),
parser: 'babel-eslint'
}, {
// Async functions can't be components.
code: [
'var Hello = async (props) => {',
' return <div>Hello {props.name}</div>;',
'}'
].join('\n'),
parser: 'babel-eslint'
}
],

Expand Down