Skip to content

Commit 588577c

Browse files
authored
feat(es/parser): support Flow declare export default interface strip path (#11692)
## Summary This PR completes the remaining Flow strip parser gap for `declare export default interface ...`. ### What changed - Added a Flow-specific `declare export default` branch for `interface` in `swc_ecma_parser`. - Parses `declare export default interface Foo { ... }` as `Decl::TsInterface`, then marks it `declare` via the existing `make_decl_declare` path. - Kept existing fallback behavior for `declare export default <type>;` unchanged. - Added a new parser fixture suite: - `crates/swc_ecma_parser/tests/flow/declare-export-default-interface/basic.js` - Extended flow-strip e2e fixture input: - `crates/swc/tests/fixture/flow-strip/input/index.js` ## Scope notes - Intentionally **not** changing behavior for: - `declare export default opaque type ...` - semicolon-separated declare-export specifier lists like `declare export { Foo; Bar }` ## Testing - `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` - `cargo test -p swc` *(fails in `source_map` tests in this environment due missing Node module `sourcemap-validator`)*
1 parent 99e61c4 commit 588577c

4 files changed

Lines changed: 89 additions & 0 deletions

File tree

crates/swc/tests/fixture/flow-strip/input/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ opaque type ID = string;
33
type Box = {| +a: number, ...Other |};
44
declare module.exports: { value: number };
55
declare export default number;
6+
declare export default interface Foo {
7+
x: number;
8+
}
69
declare export { Foo, type Bar as Baz } from "./foo";
710
declare export * from "./foo";
811
declare export * as ns from "./foo";

crates/swc_ecma_parser/src/parser/typescript.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3060,6 +3060,15 @@ impl<I: Tokens> Parser<I> {
30603060
.map(Some);
30613061
}
30623062

3063+
if p.input().is(Token::Interface) {
3064+
p.assert_and_bump(Token::Interface);
3065+
return p
3066+
.parse_ts_interface_decl(declare_start)
3067+
.map(Decl::from)
3068+
.map(make_decl_declare)
3069+
.map(Some);
3070+
}
3071+
30633072
let type_ann = p.in_type(Self::parse_ts_type)?;
30643073
p.expect_general_semi()?;
30653074

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare export default interface Foo {
2+
x: number;
3+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"type": "Script",
3+
"span": {
4+
"start": 1,
5+
"end": 54
6+
},
7+
"body": [
8+
{
9+
"type": "TsInterfaceDeclaration",
10+
"span": {
11+
"start": 1,
12+
"end": 54
13+
},
14+
"id": {
15+
"type": "Identifier",
16+
"span": {
17+
"start": 34,
18+
"end": 37
19+
},
20+
"ctxt": 0,
21+
"value": "Foo",
22+
"optional": false
23+
},
24+
"declare": true,
25+
"typeParams": null,
26+
"extends": [],
27+
"body": {
28+
"type": "TsInterfaceBody",
29+
"span": {
30+
"start": 38,
31+
"end": 54
32+
},
33+
"body": [
34+
{
35+
"type": "TsPropertySignature",
36+
"span": {
37+
"start": 42,
38+
"end": 52
39+
},
40+
"readonly": false,
41+
"key": {
42+
"type": "Identifier",
43+
"span": {
44+
"start": 42,
45+
"end": 43
46+
},
47+
"ctxt": 0,
48+
"value": "x",
49+
"optional": false
50+
},
51+
"computed": false,
52+
"optional": false,
53+
"typeAnnotation": {
54+
"type": "TsTypeAnnotation",
55+
"span": {
56+
"start": 43,
57+
"end": 51
58+
},
59+
"typeAnnotation": {
60+
"type": "TsKeywordType",
61+
"span": {
62+
"start": 45,
63+
"end": 51
64+
},
65+
"kind": "number"
66+
}
67+
}
68+
}
69+
]
70+
}
71+
}
72+
],
73+
"interpreter": null
74+
}

0 commit comments

Comments
 (0)