question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug]: `super()` in class methods is not reported by `@babel/eslint-parser`

See original GitHub issue

💻

  • Would you like to work on a fix?

How are you using Babel?

@babel/eslint-parser

Input code

        class Foo extends React.Component {
          contructor(props) {
            super(props);
            this.initialValues = {
              test: '',
            };
          }

          render = () => {
            return (
              <Component
                initialValues={this.props.initialValues || this.initialValues}
              >
                {formikProps => (
                  <Input {...formikProps} />
                )}
              </Component>
            );
          }
        }

Configuration file name

No response

Configuration

n/a

Current and expected behavior

I would expect a parse error, because super() is not valid except in constructor() {} and this is contructor() {}.

Environment

v7.16.0 of @babel/eslint-parser, eslint v8.1.10, node v17.0.1

Possible solution

Don’t allow super() outside of a class constructor() {}

Additional context

Confusion over this led me to file https://github.com/eslint/eslint/issues/15234, but eslint 8 is actually handling this correctly because of the typo.

related: https://github.com/typescript-eslint/typescript-eslint/issues/4072

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
nicolo-ribaudocommented, Nov 1, 2021

In your eslint config:

parserOpts: {
  babelOptions: {
    allowReturnOutsideFunction: false,
    allowSuperOutsideMethod: false,
  }
}
1reaction
ljharbcommented, Oct 30, 2021

In AST Explorer, when i select the babel eslint parser, as opposed to the babel parser, it does not throw.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bug: parse error on valid code in eslint 8 · Issue #15234 - GitHub
Parser correctly reports an error on use of super() call in a method. When the typo is fixed and the method becomes the...
Read more >
@babel/eslint-parser | Yarn - Package Manager
ESLint parser that allows for linting of experimental syntax transformed by Babel. readme. babel. The compiler for writing next generation JavaScript.
Read more >
Why does ESLint not recognize my class arrow functions?
I think you are getting that linting error because you are not using ECMA version 2022 (aka ECMA latest). Please check this link...
Read more >
eslint/eslint - Gitter
Guys I use eslint API with babel-parser. When I make a syntax error I get. Parsing error: Unexpected token, ...
Read more >
Changelog - JavaScript Standard Style
This pre-release exists to test out the ESLint 8 related changes and discover possible backwards incompatible changes that comes with it and mitigate...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found