Flow parser: component parameter that is both renamed and optional (`foo as bar?: T`) fails to parse
Describe the bug
The Flow parser rejects a component parameter that is both renamed and optional — foo as bar?: T.
Each feature parses on its own. Only the combination fails. The failure applies to an identifier key (foo as bar?: T) and to a string-literal key ('data-testid' as dataTestId?: T).
The example below comes verbatim from the "String Parameters/Renaming Parameters" section of the Flow documentation: https://flow.org/en/docs/react/component-syntax/
Input code
import * as React from 'react';
component RenamedParameter(
'required-renamed' as foo: number,
'optional-renamed' as bar?: number,
'optional-with-default-renamed' as baz?: number = 3,
) {
foo as number; // OK
bar as number | void; // OK
baz as number; // OK
return null;
}Parsed with:
await transform(code, {
filename: 'doc.js',
jsc: { parser: { syntax: 'flow', components: true }, target: 'esnext' },
});Actual behavior
x Expected ',', got '?'
,-[doc.js:5:1]
4 | 'required-renamed' as foo: number,
5 | 'optional-renamed' as bar?: number,
: ^
6 | 'optional-with-default-renamed' as baz?: number = 3,An identifier key gives a different message at the same cause:
component F(foo as bar?: string) {}
x Expected '{', got 'ident'Expected behavior
The code parses. Flow 0.331.0 accepts all three parameters and reports no errors.
Parse matrix
| Parameter | Result |
|---|---|
component F(foo: string) |
pass |
component F(foo?: string) |
pass |
component F(foo as bar: string) |
pass |
component F(foo as bar: string = "a") |
pass |
component F('data-x' as bar: string) |
pass |
component F(foo as bar?: string) |
fail |
component F('data-x' as bar?: string) |
fail |
The ? token is the trigger, but only after an as rename.
Version
@swc/core 1.15.41, 1.16.2 and 1.16.4-nightly-20260913.1 all fail. Flow 0.331.0 accepts the input.
Additional context
This breaks a production Vite/Rolldown build. Rolldown cannot parse Flow, so we run @swc/core first to strip Flow types and to desugar component, hook and match. A source file that Flow, Babel (flow-parser) and ESLint all accept therefore fails only at bundle time.
Related, previously fixed Flow parser gaps: #11928, #11834, #11791, #11785.
Source: swc-project/swc