babel / babylon

PSA: moved into babel/babel as @babel/parser -->

Home Page:https://github.com/babel/babel/tree/master/packages/babel-parser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Specify ecma version before parsing

Alphapage opened this issue · comments

Hello,

Is there a way to tell babylon to parse only es2015 and rejects when es2016 syntaxes are reached ?

Thank you in advance for your help.

commented

Hey @Alphapage! 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.

No, there isn't. Babylon parses standard JavaScript by default, and you can opt-in to parse other syntax extensions; there isn't a way to specify that you don't want some JavaScript features.

I'd suggest traversing the AST (using babel-traverse, a Babel plugin visitor or any other traverser) to check that there aren't the nodes you want to forbid.

Alternatively if you're not using any experimental syntax, you can always use ESLint to enforce this kind of thing.

Thank you very much those infos. You help me a lot.
Best regards.

Let me make sure I understand this correctly. Every time babylon updates to support a new version of ECMAScript, I now have to make sure to add each language feature in that version to no-restricted-syntax (eslint-plugin-compat doesn't seem to catch async functions) or else they will silently succeed at build time and break any old browsers that I support?

EDIT: Since I depend on a select few babel syntax plugins, I have to use babel-eslint. Otherwise, I could just use the default espree parser for eslint and set ecmaVersion to 2015 to catch these.

Yes, it ships new standards like a browser does: the browsers you use to debug your code may silently add support for the latest standard, so you won't notice you are using new JS features and it will break in older browsers.

Someone may create an eslint plugin which can be used to set the max accepted ECMAScript version, to avoid maintaining no-restricted-syntax updated by hand.

Sure, but that seems antithetical to babel's purpose of allowing me to write whatever I want and transpiling down to lowest-common-denominator code. If I've set my babel config to only load babel-preset-es2015 then it seems reasonable to expect that anything beyond that would fail to build.