mirror of
https://github.com/hanzoai/swc.git
synced 2026-10-30 19:42:53 +00:00
## Summary
This PR completes Flow stripping coverage in `swc_ecma_parser` for four
syntax gaps by normalizing Flow-only forms into existing TS-compatible
parser paths, so downstream strip behavior removes them consistently.
## What changed
- `declare module.exports: T;`
- Added Flow-mode handling in `parse_ts_decl` (`module` branch).
- Normalized to synthetic `TsTypeAliasDecl` with id
`__flow_module_exports`.
- `declare export default ...`
- Added dedicated default handling in Flow `export` declare branch.
- Supports:
- `declare export default class ...`
- `declare export default function ...`
- `declare export default <Type>;` (normalized via synthetic alias
`__flow_default_export`)
- Flow predicate `%checks` / `%checks(expr)`
- Added consume-only parsing after return type and before function body.
- No AST node introduced (strip-friendly behavior).
- Class field variance `+x` / `-y`
- Added Flow variance token consumption before class field names.
- `+` maps to readonly semantics on fields.
- `-` maps to writable semantics.
- Variance in method contexts emits error and continues.
## Tests
Added parser fixtures:
- `crates/swc_ecma_parser/tests/flow/class-variance/basic.js`
- `crates/swc_ecma_parser/tests/flow/declare-export-default/basic.js`
- `crates/swc_ecma_parser/tests/flow/module-exports/basic.js`
- `crates/swc_ecma_parser/tests/flow/predicate/basic.js`
Expanded flow-strip e2e fixture:
- `crates/swc/tests/fixture/flow-strip/input/index.js`
- `crates/swc/tests/fixture/flow-strip/output/index.js`
Validation run:
- `git submodule update --init --recursive`
- `UPDATE=1 cargo test -p swc_ecma_parser --test flow --features flow --
--ignored`
- `cargo test -p swc_ecma_parser --test flow --features flow --
--ignored`
- `UPDATE=1 cargo test -p swc --test projects -F flow -- --ignored
flow_strip`
- `cargo test -p swc --test projects -F flow -- --ignored flow_strip`
- `cargo fmt --all`
- `cargo clippy --all --all-targets -- -D warnings`
- `cargo test -p swc_ecma_parser`
105 lines
2.1 KiB
JSON
105 lines
2.1 KiB
JSON
{
|
|
"type": "Script",
|
|
"span": {
|
|
"start": 1,
|
|
"end": 79
|
|
},
|
|
"body": [
|
|
{
|
|
"type": "ClassDeclaration",
|
|
"identifier": {
|
|
"type": "Identifier",
|
|
"span": {
|
|
"start": 30,
|
|
"end": 31
|
|
},
|
|
"ctxt": 0,
|
|
"value": "C",
|
|
"optional": false
|
|
},
|
|
"declare": true,
|
|
"span": {
|
|
"start": 1,
|
|
"end": 48
|
|
},
|
|
"ctxt": 0,
|
|
"decorators": [],
|
|
"body": [
|
|
{
|
|
"type": "ClassProperty",
|
|
"span": {
|
|
"start": 36,
|
|
"end": 46
|
|
},
|
|
"key": {
|
|
"type": "Identifier",
|
|
"span": {
|
|
"start": 36,
|
|
"end": 37
|
|
},
|
|
"value": "x"
|
|
},
|
|
"value": null,
|
|
"typeAnnotation": {
|
|
"type": "TsTypeAnnotation",
|
|
"span": {
|
|
"start": 37,
|
|
"end": 45
|
|
},
|
|
"typeAnnotation": {
|
|
"type": "TsKeywordType",
|
|
"span": {
|
|
"start": 39,
|
|
"end": 45
|
|
},
|
|
"kind": "number"
|
|
}
|
|
},
|
|
"isStatic": false,
|
|
"decorators": [],
|
|
"accessibility": null,
|
|
"isAbstract": false,
|
|
"isOptional": false,
|
|
"isOverride": false,
|
|
"readonly": false,
|
|
"declare": false,
|
|
"definite": false
|
|
}
|
|
],
|
|
"superClass": null,
|
|
"isAbstract": false,
|
|
"typeParams": null,
|
|
"superTypeParams": null,
|
|
"implements": []
|
|
},
|
|
{
|
|
"type": "TsTypeAliasDeclaration",
|
|
"span": {
|
|
"start": 49,
|
|
"end": 79
|
|
},
|
|
"declare": true,
|
|
"id": {
|
|
"type": "Identifier",
|
|
"span": {
|
|
"start": 49,
|
|
"end": 79
|
|
},
|
|
"ctxt": 0,
|
|
"value": "__flow_default_export",
|
|
"optional": false
|
|
},
|
|
"typeParams": null,
|
|
"typeAnnotation": {
|
|
"type": "TsKeywordType",
|
|
"span": {
|
|
"start": 72,
|
|
"end": 78
|
|
},
|
|
"kind": "number"
|
|
}
|
|
}
|
|
],
|
|
"interpreter": null
|
|
}
|