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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: @babel/parser fails when loading options programatically #14082

Closed
1 task
mskelton opened this issue Dec 28, 2021 · 5 comments
Closed
1 task

[Bug]: @babel/parser fails when loading options programatically #14082

mskelton opened this issue Dec 28, 2021 · 5 comments
Labels
i: needs triage outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@mskelton
Copy link

馃捇

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

console.log('test')

Configuration file name

babel.config.js

Configuration

module.exports = {
  presets: ["@babel/preset-env"],
};

Current and expected behavior

When parsing with the following code:

const { parse } = require("@babel/parser");
const { loadOptions } = require("@babel/core");

const input = `
console.log('mark')
`;

parse(input, loadOptions())

Babel will fail with the following error:

> babel-repro@1.0.0 start
> node ./parse.js

/Users/mskelton/dev/babel-repro/node_modules/@babel/parser/lib/index.js:10658
      const [pluginName, pluginOptions] = p;
                                          ^

TypeError: p is not iterable
    at /Users/mskelton/dev/babel-repro/node_modules/@babel/parser/lib/index.js:10658:43
    at Array.some (<anonymous>)
    at hasPlugin (/Users/mskelton/dev/babel-repro/node_modules/@babel/parser/lib/index.js:10654:18)
    at validatePlugins (/Users/mskelton/dev/babel-repro/node_modules/@babel/parser/lib/index.js:10693:7)
    at getParser (/Users/mskelton/dev/babel-repro/node_modules/@babel/parser/lib/index.js:15287:5)
    at parse (/Users/mskelton/dev/babel-repro/node_modules/@babel/parser/lib/index.js:15258:12)
    at Object.<anonymous> (/Users/mskelton/dev/babel-repro/parse.js:8:13)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)

Environment

System:

  • OS: macOS 11.6.1

Binaries:

  • Node: 16.13.1 - ~/.local/share/nvm/v16.13.1/bin/node
  • Yarn: 1.22.17 - /usr/local/bin/yarn
  • npm: 8.1.2 - ~/.local/share/nvm/v16.13.1/bin/npm

npmPackages:

  • @babel/core: ^7.16.5 => 7.16.5
  • @babel/parser: ^7.16.6 => 7.16.6
  • @babel/preset-env: ^7.16.5 => 7.16.5

Possible solution

Update hasPlugin in packages/babel-parser/src/plugin-utils.js to test all types of plugin objects (string, array, ConfigItem, Plugin, etc.).

Additional context

A recent change to hasPlugin in #13982 broke certain programatic usages of the Babel API when loading config from a babel.config.js file. This does not occur when loading from a .babelrc file interestingly enough.

This is used in the wild as it is the cause of https://github.com/stylelint/postcss-css-in-js/issues/247.

A full working example is available here: https://github.com/mskelton/babel-repro/blob/working/parse.js

@babel-bot
Copy link
Collaborator

Hey @mskelton! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite.

@nicolo-ribaudo
Copy link
Member

const { parse } = require("@babel/parser");
const { loadOptions } = require("@babel/core");

const input = `
console.log('mark')
`;

parse(input, loadOptions())

This never worked. Maybe it didn't throw with your exact config, but it definitely didn't do what you expect.

loadOptions() returns an options objects to configure @babel/core (or packages that use @babel/core such us babel-loader); @babel/parser has a completely different set and options.

What you want is to use parseSync from @babel/core:

const { parseSync, loadOptions } = require("@babel/core");

const input = `
console.log('mark')
`;

parseSync(input, loadOptions())

@nicolo-ribaudo
Copy link
Member

Actually, we might throw/log an helpful message when passing @babel/core options to @babel/parser.

@mskelton
Copy link
Author

Okay, so looking back I see that my repro was slightly incorrect. The stylelint bug is happening when importing parse and loadOptions from from @babel/core and yet it still fails. Let me dig into it a bit more and see if I can get it reproducible again.

@mskelton
Copy link
Author

Looking deeper into this, seems like it isn't a Babel issue. Appreciate the help though!

@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 Mar 30, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: needs triage 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

3 participants