Skip to content

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

@wheeler

Description

@wheeler

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)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions