alangpierce/sucrase

Support for Flow Enums

jeresig opened this issue · 2 comments

Hello - I was testing out the latest version of Sucrase (v3.21.0) and I noticed that it seems to be unable to parse Flow Enums. I end up with an error like:

ERROR in ./javascript/devadmin-package/internal/backend-source.jsx
Module build failed (from ./node_modules/@sucrase/webpack-loader/dist/index.js):
SyntaxError: Error transforming /Users/jeresig/khan/webapp/services/static/javascript/devadmin-package/internal/backend-source.jsx: Unexpected token, expected ";" (29:6)
    at unexpected (/Users/jeresig/khan/webapp/services/static/node_modules/sucrase/dist/parser/traverser/util.js:99:15)
    at semicolon (/Users/jeresig/khan/webapp/services/static/node_modules/sucrase/dist/parser/traverser/util.js:77:5)
    at flowParseIdentifierStatement (/Users/jeresig/khan/webapp/services/static/node_modules/sucrase/dist/parser/plugins/flow.js:806:19)
    at parseIdentifierStatement (/Users/jeresig/khan/webapp/services/static/node_modules/sucrase/dist/parser/traverser/statement.js:479:40)
    at parseStatementContent (/Users/jeresig/khan/webapp/services/static/node_modules/sucrase/dist/parser/traverser/statement.js:238:5)
    at parseStatement (/Users/jeresig/khan/webapp/services/static/node_modules/sucrase/dist/parser/traverser/statement.js:109:3)
    at parseBlockBody (/Users/jeresig/khan/webapp/services/static/node_modules/sucrase/dist/parser/traverser/statement.js:504:5)
    at parseTopLevel (/Users/jeresig/khan/webapp/services/static/node_modules/sucrase/dist/parser/traverser/statement.js:85:3)
    at parseFile (/Users/jeresig/khan/webapp/services/static/node_modules/sucrase/dist/parser/traverser/index.js:17:35)
    at parse (/Users/jeresig/khan/webapp/services/static/node_modules/sucrase/dist/parser/index.js:26:35)

and the code in question:

enum Source {
    DevBackend = "use_dev_backend",
    ProdBackend = "use_prod_backend",
}

Obviously this would require additional logic to support to turn the code into valid runtime code. It looks like similar logic was implemented to support TypeScript in #621. Thanks so much for your work on Sucrase, it's a great project!

Thanks for the request! I took a look and Flow enums do seem pretty reasonable to implement, probably quite a bit simpler than TS enums, so I'll see if I can get them implemented. They were actually added to Babel's parser a few years ago, but I skipped over them when porting changes because they were experimental and didn't have runtime behavior defined yet.

One thing I noticed is that the Babel plugin has a getRuntime configuration if you want to override the package name (or the expression more generally). I was planning on sticking with the default behavior of require('flow-enums-runtime') for now, but let me know if it's important for it to be configurable.

I just released flow enum support in version 3.22.0. Note that it doesn't do the same parse-time validation checks that Babel does since Sucrase generally leaves the error checking to other tools like Flow itself. I think I got all the cases right, but let me know if you run into any issues!