@swc-next/types 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -105
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,110 +6,12 @@ Shared ESTree declarations for SWC Next's parser, decoder, encoder, and codegen.
|
|
|
6
6
|
import type { Program, Node } from "@swc-next/types";
|
|
7
7
|
```
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
node-specific public shapes are described beside the AST definitions.
|
|
12
|
-
Encoder and decoder generation retain their existing implementation.
|
|
9
|
+
The declarations describe SWC Next's AST, including TypeScript, JSX, and error
|
|
10
|
+
recovery nodes. Assignability from other parsers' AST types is not guaranteed.
|
|
13
11
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
becomes `T | null`. Acronym prefixes use `TS`, `JSX`, and `JS`.
|
|
18
|
-
Rust AST enums become unions of their variants' public types. Scalar enums
|
|
19
|
-
marked with `#[estree]` become unions of string literals from their variant
|
|
20
|
-
names. `rename` changes those names; `ts_type` supplies other types such as
|
|
21
|
-
`null` or `true`. Rust discriminants do not affect declarations.
|
|
12
|
+
Types are generated from the Rust AST definitions and `#[estree]` annotations
|
|
13
|
+
by [ast_tools](../../tasks/ast_tools). Edit those definitions rather than the
|
|
14
|
+
generated declarations.
|
|
22
15
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
`rename` selects the `type` discriminator; its interface keeps the Rust-derived
|
|
26
|
-
name so multiple node shapes can share a discriminator such as `Literal`.
|
|
27
|
-
|
|
28
|
-
The annotations each control one part of a declaration:
|
|
29
|
-
|
|
30
|
-
| Annotation | Meaning |
|
|
31
|
-
| --- | --- |
|
|
32
|
-
| `rename = "..."` | Set a struct's discriminator, an enum's declaration name, a stored field name, or an enum variant name. Unit variants become string literals; payload variants select a public type reference. |
|
|
33
|
-
| `ts_type = "..."` | Override a field or enum variant type; on a struct, override the discriminator type. |
|
|
34
|
-
| `skip` | Hide a stored field from declarations. |
|
|
35
|
-
| `optional` | Allow a property to be absent, independently of whether its value may be `null`. |
|
|
36
|
-
| `ts_alias = "..."` | Replace the generated interface or union with a type alias. |
|
|
37
|
-
| `fields(name(...))` | Add public properties absent from Rust storage, using `ts_type` and optionally `optional`. |
|
|
38
|
-
| `add_ts_def = "..."` | Add declarations next to their owning AST definition. |
|
|
39
|
-
|
|
40
|
-
For example, computed properties specify their types directly:
|
|
41
|
-
|
|
42
|
-
```rust
|
|
43
|
-
#[ast]
|
|
44
|
-
#[estree(fields(sourceType(ts_type = "SourceType")))]
|
|
45
|
-
pub struct Program {
|
|
46
|
-
// ...
|
|
47
|
-
}
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
The same rename rule describes scalar names and contextual AST variants:
|
|
51
|
-
|
|
52
|
-
```rust
|
|
53
|
-
#[estree]
|
|
54
|
-
pub enum ImportPhase {
|
|
55
|
-
#[estree(rename = "source")]
|
|
56
|
-
Source,
|
|
57
|
-
#[estree(rename = "defer")]
|
|
58
|
-
Defer,
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
#[ast(repr)]
|
|
62
|
-
#[estree(rename = "Expression")]
|
|
63
|
-
pub enum Expr {
|
|
64
|
-
#[estree(rename = "FunctionExpression")]
|
|
65
|
-
Function(Function),
|
|
66
|
-
// ...
|
|
67
|
-
}
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
Single discriminator names use `rename`, while `ts_type` can describe a wider
|
|
71
|
-
discriminator union:
|
|
72
|
-
|
|
73
|
-
```rust
|
|
74
|
-
#[ast]
|
|
75
|
-
#[estree(
|
|
76
|
-
rename = "Literal",
|
|
77
|
-
fields(value(ts_type = "bigint | null"), bigint(ts_type = "string"))
|
|
78
|
-
)]
|
|
79
|
-
pub struct BigIntLiteral {
|
|
80
|
-
raw: AstUtf8,
|
|
81
|
-
}
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
This generates `interface BigIntLiteral` with `type: "Literal"`.
|
|
85
|
-
|
|
86
|
-
Generation rejects duplicate projected fields, including collisions with
|
|
87
|
-
`BaseNode` properties. Bare `skip` affects declarations. The existing
|
|
88
|
-
reason-valued `skip = "..."` form also preserves its codec behavior.
|
|
89
|
-
|
|
90
|
-
Public types must include decoder output and stay within the structures that
|
|
91
|
-
can be encoded into the corresponding Rust AST types. Grammar constraints may
|
|
92
|
-
be looser than other parsers' declarations when Rust can represent the data.
|
|
93
|
-
Cross-field constraints, such as computed keys and arrow function body forms,
|
|
94
|
-
are intentionally left independent to keep annotations and generation simple.
|
|
95
|
-
These annotations never change Rust storage or codec behavior. Assignability
|
|
96
|
-
from other parsers' AST declarations is not guaranteed.
|
|
97
|
-
|
|
98
|
-
Alignment with `@typescript-eslint/types` 8.71.2 retains decoder-specific differences,
|
|
99
|
-
including nullable TS fields, CommonJS programs, preserved parentheses,
|
|
100
|
-
JSDoc types, source-phase imports, BigInt property names, and error recovery
|
|
101
|
-
nodes. Optional TS fields accept omission only where the encoder can represent
|
|
102
|
-
it safely. Fixed Rust child types remain required even when typescript-eslint
|
|
103
|
-
allows omission or a wider node union.
|
|
104
|
-
|
|
105
|
-
Some grammar restrictions would exclude current output: chain expressions can
|
|
106
|
-
contain `TSInstantiationExpression`, `TSImportType.options` can be any expression,
|
|
107
|
-
and parameter properties can contain destructuring. Error recovery preserves
|
|
108
|
-
private names outside classes and delegated yields without an argument. These
|
|
109
|
-
shapes stay accepted rather than copying narrower declarations.
|
|
110
|
-
|
|
111
|
-
The decoder currently emits abstract methods with function bodies. These
|
|
112
|
-
remain in the declarations to cover its output. The encoder selects the Rust
|
|
113
|
-
method structure from the function value's type and preserves the abstract flag
|
|
114
|
-
independently. Directive encoding checks for a string literal before selecting
|
|
115
|
-
the Rust directive structure.
|
|
16
|
+
See the [`estree` attribute documentation](../../crates/swc_ast_macros/src/lib.rs)
|
|
17
|
+
for declaration overrides.
|