Flow parser fails to parse leading `|` in match cases
Describe the bug
The Flow parser (Syntax::Flow) fails to parse the optional leading | before the first pattern in both match expressions and match statements when pattern_matching is enabled.
This is valid Flow syntax and is used by React Native 0.87.1 in ReactNativeResponder.js. Removing only the first | makes both snippets parse successfully; | between alternative patterns is already supported.
Input code
// @flow
function isRelevant(kind: string): boolean {
return match (kind) {
| 'start'
| 'move' => true,
_ => false,
};
}
function handle(kind: string): void {
match (kind) {
| 'start'
| 'move' => {
console.log(kind);
}
_ => {}
}
}Config
Syntax::Flow(FlowSyntax {
pattern_matching: true,
..Default::default()
})Link to the code that reproduces this issue
https://github.com/leegeunhyeok/match-leading-pipe-repro
SWC Info output
This reproduction uses the Rust parser crates directly:
swc_common = "=26.0.0"
swc_ecma_ast = "=29.0.1"
swc_ecma_parser = { version = "=45.1.3", features = ["flow"] }Run cargo run --locked in the reproduction repository.
Expected behavior
Both the match expression and match statement should parse successfully.
Flow accepts an optional leading | before a match pattern.
Actual behavior
The command exits with status 1. The match expression reports TS1109 and then fails with Expected(";", "=>"). The match statement fails with Unexpected { got: "|", expected: "a pattern" }.
[failed] match expression
recovered: Error { error: (..., TS1109) }
fatal: Error { error: (..., Expected(";", "=>")) }
[failed] match statement
fatal: Error { error: (..., Unexpected { got: "|", expected: "a pattern" }) }Version
Additional context
- React Native usage: https://github.com/facebook/react-native/blob/v0.87.1/packages/react-native/src/private/renderer/events/ReactNativeResponder.js#L578-L587
- Flow parser behavior: https://github.com/facebook/flow/blob/fe34a2a459744ef56f42ea0ca85fd24354e4f1b4/rust_port/crates/flow_parser/src/match_pattern_parser.rs#L38-L50
- Flow's leading-bar parser fixture: https://github.com/facebook/flow/blob/fe34a2a459744ef56f42ea0ca85fd24354e4f1b4/src/parser/test/flow/match/pattern-leading-bar.js#L1-L7
- Flow match support was introduced in https://github.com/swc-project/swc/pull/11702
- Related prior React Native Flow parser coverage report: https://github.com/swc-project/swc/issues/11729
The same failure is present on the latest swc main branch at the time of reporting (55ffd3e272be7798bf9c83acee7897110222d118).
The current pattern parser parses a primary first and only consumes pipes that follow it: https://github.com/swc-project/swc/blob/55ffd3e272be7798bf9c83acee7897110222d118/crates/swc_ecma_parser/src/parser/stmt.rs#L1582-L1594
This is message for readers, not the author of ths issue.
Please read no +1 before lefting a comment, or your comment will be deleted and you will be blocked.
Source: swc-project/swc