Use hermes-parser via babel-plugin-syntax-hermes-parser for Flow files#81
Open
jonreading81 wants to merge 2 commits into
Open
Use hermes-parser via babel-plugin-syntax-hermes-parser for Flow files#81jonreading81 wants to merge 2 commits into
jonreading81 wants to merge 2 commits into
Conversation
Fixes blazejkustra#80. The previous parse step used @babel/parser with the "flow" plugin, which lags behind Flow's own parser and rejects modern syntax (readonly, component, hook, renders, match, keyof, T[K], x is T, <T extends U>, etc). The extension errored out on any file using those constructs and no marker appeared. Switch to a single transformSync() with babel-plugin-syntax-hermes-parser in the plugin list. Its parserOverride hook swaps in Hermes (Flow's canonical parser) for .js / .jsx / .mjs files and is a no-op for .ts / .tsx, so TypeScript continues to be parsed by Babel's built-in parser (configured via parserOpts.plugins). Same output shape, same logger contract — only the parser changes. Verified locally against: - Modern Flow: `type Props = { ref?: React.RefObject<{ readonly ... }> }` now compiles cleanly (was throwing at parse time). - TypeScript: `React.FC<Props>` with type annotations continues to compile without regression. - Legit bailout (ref access during render): still emits the correct "Cannot access refs during render" diagnostic.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #80.
Summary
The previous parse step used
@babel/parserwith theflowplugin, which lags behind Flow's own parser and rejects modern syntax (readonly,component,hook,renders,match,keyof,T[K],x is T,<T extends U>, etc). The extension errored out on any file using those constructs and no marker appeared.This PR switches to a single
transformSync()withbabel-plugin-syntax-hermes-parserin the plugin list. ItsparserOverridehook swaps in Hermes (Flow's canonical parser) for.js/.jsx/.mjsfiles, and is a no-op for.ts/.tsx, so TypeScript continues to be parsed by Babel's built-in parser (configured viaparserOpts.plugins). Same output shape, same logger contract — only the parser changes.Verified locally
type Props = { ref?: React.RefObject<{ readonly closeMenu: () => void }> }— now compiles cleanly (was throwing at parse time).React.FC<Props>with type annotations — continues to compile without regression."Cannot access refs during render"diagnostic.Alternatives considered
The original suggestion in #80 was a separate SWC Flow-strip pass. Your feedback pointed at
babel-plugin-syntax-hermes-parseras cleaner — same Babel run, no SWC dependency, uses Flow's canonical parser. I agree and went with that.Notes
babel-plugin-syntax-hermes-parser@^0.36.1topackages/server/dependencies.BabelParser.parse()step — now a singletransformSync().@babel/parserremains a dep but is unused in the server source after this change; left in to keep the diff minimal.