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

[Bug]: Parsing error: Cannot read property 'push' of undefined #13331

Closed
1 task done
devfservant opened this issue May 18, 2021 · 11 comments
Closed
1 task done

[Bug]: Parsing error: Cannot read property 'push' of undefined #13331

devfservant opened this issue May 18, 2021 · 11 comments
Assignees
Labels
area: eslint i: regression outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@devfservant
Copy link
Contributor

devfservant commented May 18, 2021

💻

  • Would you like to work on a fix? (not sure if I can help on it but I can try 😛)

How are you using Babel?

@babel/eslint-parser

Input code

Here is the repository: https://github.com/devfservant/babel-eslint-parser-error/

Configuration file name

babel.config.js

Configuration

const getBaseConfig = () => ({
  presets: [
    [
      '@babel/preset-env',
      {
        debug: false,
        targets: {
          node: 'current',
          browsers: ['ie 11', '>0.15%', 'not dead', 'not op_mini all'],
        },
      },
    ],
    '@babel/preset-react',
  ],
  plugins: [
    ['babel-plugin-styled-components', { fileName: false }],
    ['@babel/plugin-transform-runtime', { corejs: 3 }],
    '@babel/plugin-syntax-dynamic-import',
  ],
})

module.exports = (api) => {
  api.cache(true)

  const baseConfig = getBaseConfig()

  if (process.env.NODE_ENV !== 'test') {
    baseConfig.ignore = [
      '**/__snapshots__/**',
      '**/*.test.js',
      '**/*.test.jsx',
      '**/*.test.ts',
      '**/*.test.tsx',
    ]

    // If not in tests, add the babel assets import plugin
    baseConfig.plugins.push([
      'transform-assets-import-to-string',
      { baseDir: '/assets' },
    ])
  }

  return baseConfig
}

Current and expected behavior

Current behaviour

The command npx eslint "packages/**" --debug is crashing with the error:

eslint:linter Parsing error: Cannot read property 'push' of undefined
eslint:linter TypeError: Cannot read property 'push' of undefined
  at maybeParse (C:\Users\me\Workspace\babel-eslint-parser-error\node_modules\@babel\eslint-parser\lib\worker\maybeParse.cjs:24:19)
  at handleMessage (C:\Users\me\Workspace\babel-eslint-parser-error\node_modules\@babel\eslint-parser\lib\worker\index.cjs:31:16)
  at exports.maybeParse (C:\Users\me\Workspace\babel-eslint-parser-error\node_modules\@babel\eslint-parser\lib\client.cjs:9:41)
  at baseParse (C:\Users\me\Workspace\babel-eslint-parser-error\node_modules\@babel\eslint-parser\lib\index.cjs:47:7)
  at Object.exports.parseForESLint (C:\Users\me\Workspace\babel-eslint-parser-error\node_modules\@babel\eslint-parser\lib\index.cjs:63:15)
  at parse (C:\Users\me\Workspace\babel-eslint-parser-error\node_modules\eslint\lib\linter\linter.js:655:22)
  at Linter._verifyWithoutProcessors (C:\Users\me\Workspace\babel-eslint-parser-error\node_modules\eslint\lib\linter\linter.js:1129:33)
  at Linter._verifyWithoutProcessors (C:\Users\me\Workspace\babel-eslint-parser-error\node_modules\eslint-plugin-eslint-comments\lib\utils\patch.js:166:36)       
  at Linter._verifyWithConfigArray (C:\Users\me\Workspace\babel-eslint-parser-error\node_modules\eslint\lib\linter\linter.js:1273:21)
  at Linter.verify (C:\Users\me\Workspace\babel-eslint-parser-error\node_modules\eslint\lib\linter\linter.js:1228:25) +5ms

(see babel-eslint-parser-error#debug-logs for more details)

Expected behaviour

No error

Environment

System:
OS: Windows 10 10.0.19042
Binaries:
Node: 12.20.1 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.10 - ~\AppData\Roaming\npm\yarn.CMD
npm: 6.14.10 - C:\Program Files\nodejs\npm.CMD
Monorepos:
Yarn Workspaces: 1.22.10
Lerna: 4.0.0
npmPackages:
@babel/cli: ^7.13.16 => 7.14.3
@babel/core: ^7.14.2 => 7.14.3
@babel/eslint-parser: ^7.14.2 => 7.14.3
@babel/eslint-plugin: ^7.13.16 => 7.13.16
@babel/plugin-syntax-dynamic-import: ^7.8.3 => 7.8.3
@babel/plugin-transform-runtime: ^7.14.2 => 7.14.3
@babel/preset-env: ^7.14.2 => 7.14.2
@babel/preset-react: ^7.13.13 => 7.13.13
@babel/runtime-corejs3: ^7.14.0 => 7.14.0
babel-plugin-styled-components: ^1.12.0 => 1.12.0
babel-plugin-transform-assets-import-to-string: ^1.2.0 => 1.2.0
eslint: ^7.26.0 => 7.26.0
jest: ^26.6.3 => 26.6.3
lerna: ^4.0.0 => 4.0.0

Possible solution

No response

Additional context

I faced this issue using a monorepo project (managed with lerna).
I got this error when switching from babel-eslint / babel-eslint-plugin
to @babel/eslint-parser / @babel/eslint-plugin (following https://babeljs.io/blog/2020/07/13/the-state-of-babel-eslint#the-future and https://stackoverflow.com/a/63489530/12292636)

Thanks a lot for your help,
if I can contribute just tell me and I'll try to do my best 🙏

@fedeci
Copy link
Member

fedeci commented May 18, 2021

Can you attach the entire stack trace? Because it may be an error in your configuration.

@devfservant
Copy link
Contributor Author

Sure, I've updated the description.

@fedeci
Copy link
Member

fedeci commented May 18, 2021

Yeah, I just gave another quick look to your babel configuration and .test.js files (the ones throwing) are ignored in babel.config.js while trying to be linted by eslint.
I would suggest you to ignore just snapshot files, so that eslint can work correctly on all the others and then ignoring .test.js files when transpiling from cli.

@JLHwung
Copy link
Contributor

JLHwung commented May 18, 2021

This is likely a regression introduced in #13199

The error is thrown from

options.plugins.push(extractParserOptionsConfigItem);

If options.plugins is undefined, we should create a new array.

If it is the first time that you contribute to Babel, you can follow these steps: (you need to have make and yarn available on your machine)

  1. Fork the repo
  2. Run git clone https://github.com/<YOUR_USERNAME>/babel.git && cd babel
  3. Run yarn && make bootstrap
  4. Wait ⏳
  5. Run make watch (or make build/yarn gulp build whenever you change a file). When updating @babel/helpers, I'm not 100% sure that make watch works.
  6. Add a test (only exec.js and/or input.js; output.js will be automatically generated)
  7. Update the code!
  8. yarn jest [name-of-the-package-to-test] to run the tests
    • If some test outputs don't match but the new results are correct, you can delete the bad output.js files and run the tests again
    • If you prefer, you can run OVERWRITE=true yarn jest [name-of-the-package-to-test] and they will be automatically updated.
  9. If it is working, run make test to run all the tests
  10. Run git push and open a PR!

@fedeci
Copy link
Member

fedeci commented May 18, 2021

Nice :)

@devfservant
Copy link
Contributor Author

devfservant commented May 18, 2021

Perfect, thanks for your analysis!
I'll open a PR as soon as possible (no later than tomorrow) 👍

@devfservant
Copy link
Contributor Author

Hello,

I would need some help on writing the tests,
I've put a message on BabelJS's Slack but I don't know if it is the right place to ask for help.

In babel/tree/main/eslint/babel-eslint-parser/test there is only one index.js file and I am not sure on how to extend it.
I'd like to add a test case where babel.loadPartialConfigSync would return null,
in order to reproduce the behaviour at the origin of the bug.
Can I just create a new test suite, where I would use jest.mock to mock this function?
Or do you have a simpler idea?
I don't how the CONTRIBUTING.md#writing-tests and its files {exec,input,output}.js are applicable for this issue.

For the moment, I have just pushed 46d5470e36d2435d59eb28028a8c9d8505b4d4f1.

Thanks for your help,

@nicolo-ribaudo
Copy link
Member

Yeah, in this case we need a test in https://github.com/babel/babel/blob/main/eslint/babel-eslint-parser/test/index.js and not an input/output fixture.

You could try passing babelOpts: { filename: "test.js", ignore: [/./] } and see if that is enough to reproduce the bug. When a file is ignored, loadPartialConfigSync returns null.

@devfservant
Copy link
Contributor Author

Hi, I've created #13338 which is now under review.
Please inform me if there is something more to fix, if there is something wrong :).
While writing, the test I noticed 3 more TypeErrors and tried to fix them.

Subsidiary question: why some parts of Babel are written in TypeScript, some others not?
I guess it could have prevented all these errors :)

@nicolo-ribaudo
Copy link
Member

why some parts of Babel are written in TypeScript, some others not?

We are slowly migrating to TS! Currently some files are plain JS, some use Flow and some us TS.

@nicolo-ribaudo
Copy link
Member

Fixed by #13338, it will be released in the next patch.

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Aug 20, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: eslint i: regression outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

4 participants