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

no-arrow-function-lifecycle: implicit returns cause "fix" Parsing error #3145

Closed
wheeler opened this issue Nov 19, 2021 · 1 comment
Closed
Assignees
Labels

Comments

@wheeler
Copy link

wheeler commented Nov 19, 2021

I'm very excited to have this great new rule! Thank you @ngtan and @ljharb!

Unfortunately I've encountered a bug in the auto-fix code. The following test case demonstrates an error:

{
  code: `
    class Hello extends React.Component {
      render = () => <div />
    }
  `,
  features: ['class fields'],
  errors: [{ message: 'render is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' }],
  output: `
    class Hello extends React.Component {
      handleEventMethods = () => {}
      render() { return <div />; }
    }
  `,
},
AssertionError [ERR_ASSERTION]: A fatal parsing error occurred in autofix.
Error: Parsing error: Unexpected token <
Autofix output:

  class Hello extends React.Component {
    handleEventMethods = () => {}
    render() <div />
  }

The rule fix only really acts to modify the () part of the ArrowFunctionExpression. It does not touch the rest. In this case the body is an Expression and needs to be transformed into a BlockStatement+ReturnStatement.

I'd take a crack at this but my eslint ast modification is pretty weak (have some experience in jscodeshift)

@ljharb ljharb changed the title no-unused-class-component-methods: implicit returns cause "fix" Parsing error no-arrow-function-lifecycle: implicit returns cause "fix" Parsing error Nov 19, 2021
ljharb added a commit to ljharb/eslint-plugin-react that referenced this issue Nov 19, 2021
@ljharb ljharb added the bug label Nov 19, 2021
@ljharb ljharb self-assigned this Nov 19, 2021
@ljharb
Copy link
Member

ljharb commented Nov 19, 2021

Thanks, great catch! The test case helped immensely.

@ljharb ljharb closed this as completed in eb81897 Nov 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants