Files
Donny/강동윤andGitHub 735600f6f9 feat(es/parser): finish flow strip support for core syntax gaps (#11689)
## 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`
2026-03-16 03:20:27 -07:00

271 lines
6.1 KiB
JSON

{
"type": "Script",
"span": {
"start": 1,
"end": 162
},
"body": [
{
"type": "FunctionDeclaration",
"identifier": {
"type": "Identifier",
"span": {
"start": 10,
"end": 18
},
"ctxt": 0,
"value": "isString",
"optional": false
},
"declare": false,
"params": [
{
"type": "Parameter",
"span": {
"start": 19,
"end": 27
},
"decorators": [],
"pat": {
"type": "Identifier",
"span": {
"start": 19,
"end": 20
},
"ctxt": 0,
"value": "x",
"optional": false,
"typeAnnotation": {
"type": "TsTypeAnnotation",
"span": {
"start": 20,
"end": 27
},
"typeAnnotation": {
"type": "TsTypeReference",
"span": {
"start": 22,
"end": 27
},
"typeName": {
"type": "Identifier",
"span": {
"start": 22,
"end": 27
},
"ctxt": 0,
"value": "mixed",
"optional": false
},
"typeParams": null
}
}
}
}
],
"decorators": [],
"span": {
"start": 1,
"end": 81
},
"ctxt": 0,
"body": {
"type": "BlockStatement",
"span": {
"start": 46,
"end": 81
},
"ctxt": 0,
"stmts": [
{
"type": "ReturnStatement",
"span": {
"start": 50,
"end": 79
},
"argument": {
"type": "BinaryExpression",
"span": {
"start": 57,
"end": 78
},
"operator": "===",
"left": {
"type": "UnaryExpression",
"span": {
"start": 57,
"end": 65
},
"operator": "typeof",
"argument": {
"type": "Identifier",
"span": {
"start": 64,
"end": 65
},
"ctxt": 0,
"value": "x",
"optional": false
}
},
"right": {
"type": "StringLiteral",
"span": {
"start": 70,
"end": 78
},
"value": "string",
"raw": "\"string\""
}
}
}
]
},
"generator": false,
"async": false,
"typeParameters": null,
"returnType": {
"type": "TsTypeAnnotation",
"span": {
"start": 28,
"end": 37
},
"typeAnnotation": {
"type": "TsKeywordType",
"span": {
"start": 30,
"end": 37
},
"kind": "boolean"
}
}
},
{
"type": "FunctionDeclaration",
"identifier": {
"type": "Identifier",
"span": {
"start": 92,
"end": 100
},
"ctxt": 0,
"value": "hasValue",
"optional": false
},
"declare": false,
"params": [
{
"type": "Parameter",
"span": {
"start": 101,
"end": 109
},
"decorators": [],
"pat": {
"type": "Identifier",
"span": {
"start": 101,
"end": 102
},
"ctxt": 0,
"value": "x",
"optional": false,
"typeAnnotation": {
"type": "TsTypeAnnotation",
"span": {
"start": 102,
"end": 109
},
"typeAnnotation": {
"type": "TsTypeReference",
"span": {
"start": 104,
"end": 109
},
"typeName": {
"type": "Identifier",
"span": {
"start": 104,
"end": 109
},
"ctxt": 0,
"value": "mixed",
"optional": false
},
"typeParams": null
}
}
}
}
],
"decorators": [],
"span": {
"start": 83,
"end": 162
},
"ctxt": 0,
"body": {
"type": "BlockStatement",
"span": {
"start": 139,
"end": 162
},
"ctxt": 0,
"stmts": [
{
"type": "ReturnStatement",
"span": {
"start": 143,
"end": 160
},
"argument": {
"type": "BinaryExpression",
"span": {
"start": 150,
"end": 159
},
"operator": "!=",
"left": {
"type": "Identifier",
"span": {
"start": 150,
"end": 151
},
"ctxt": 0,
"value": "x",
"optional": false
},
"right": {
"type": "NullLiteral",
"span": {
"start": 155,
"end": 159
}
}
}
}
]
},
"generator": false,
"async": false,
"typeParameters": null,
"returnType": {
"type": "TsTypeAnnotation",
"span": {
"start": 110,
"end": 119
},
"typeAnnotation": {
"type": "TsKeywordType",
"span": {
"start": 112,
"end": 119
},
"kind": "boolean"
}
}
}
],
"interpreter": null
}