| 1 | use serde::Deserialize; |
| 2 | use serde::Serialize; |
| 3 | |
| 4 | use crate::common::BaseNode; |
| 5 | use crate::common::RawNode; |
| 6 | use crate::expressions::Expression; |
| 7 | use crate::expressions::Identifier; |
| 8 | use crate::literals::StringLiteral; |
| 9 | |
| 10 | /// Union of Declaration types that can appear in export declarations |
| 11 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 12 | #[serde(tag = "type")] |
| 13 | pub enum Declaration { |
| 14 | FunctionDeclaration(crate::statements::FunctionDeclaration), |
| 15 | ClassDeclaration(crate::statements::ClassDeclaration), |
| 16 | VariableDeclaration(crate::statements::VariableDeclaration), |
| 17 | TSTypeAliasDeclaration(TSTypeAliasDeclaration), |
| 18 | TSInterfaceDeclaration(TSInterfaceDeclaration), |
| 19 | TSEnumDeclaration(TSEnumDeclaration), |
| 20 | TSModuleDeclaration(TSModuleDeclaration), |
| 21 | TSDeclareFunction(TSDeclareFunction), |
| 22 | TypeAlias(TypeAlias), |
| 23 | OpaqueType(OpaqueType), |
| 24 | InterfaceDeclaration(InterfaceDeclaration), |
| 25 | EnumDeclaration(EnumDeclaration), |
| 26 | } |
| 27 | |
| 28 | /// The declaration/expression that can appear in `export default <decl>` |
| 29 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 30 | #[serde(tag = "type")] |
| 31 | pub enum ExportDefaultDecl { |
| 32 | FunctionDeclaration(crate::statements::FunctionDeclaration), |
| 33 | ClassDeclaration(crate::statements::ClassDeclaration), |
| 34 | EnumDeclaration(EnumDeclaration), |
| 35 | #[serde(untagged)] |
| 36 | Expression(Box<Expression>), |
| 37 | } |
| 38 | |
| 39 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 40 | pub struct ImportDeclaration { |
| 41 | #[serde(flatten)] |
| 42 | pub base: BaseNode, |
| 43 | pub specifiers: Vec<ImportSpecifier>, |
| 44 | pub source: StringLiteral, |
| 45 | #[serde( |
| 46 | default, |
| 47 | skip_serializing_if = "Option::is_none", |
| 48 | rename = "importKind" |
| 49 | )] |
| 50 | pub import_kind: Option<ImportKind>, |
| 51 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 52 | pub assertions: Option<Vec<ImportAttribute>>, |
| 53 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 54 | pub attributes: Option<Vec<ImportAttribute>>, |
| 55 | } |
| 56 | |
| 57 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 58 | #[serde(rename_all = "lowercase")] |
| 59 | pub enum ImportKind { |
| 60 | Value, |
| 61 | Type, |
| 62 | Typeof, |
| 63 | } |
| 64 | |
| 65 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 66 | #[serde(tag = "type")] |
| 67 | pub enum ImportSpecifier { |
| 68 | ImportSpecifier(ImportSpecifierData), |
| 69 | ImportDefaultSpecifier(ImportDefaultSpecifierData), |
| 70 | ImportNamespaceSpecifier(ImportNamespaceSpecifierData), |
| 71 | } |
| 72 | |
| 73 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 74 | pub struct ImportSpecifierData { |
| 75 | #[serde(flatten)] |
| 76 | pub base: BaseNode, |
| 77 | pub local: Identifier, |
| 78 | pub imported: ModuleExportName, |
| 79 | #[serde(default, rename = "importKind")] |
| 80 | pub import_kind: Option<ImportKind>, |
| 81 | } |
| 82 | |
| 83 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 84 | pub struct ImportDefaultSpecifierData { |
| 85 | #[serde(flatten)] |
| 86 | pub base: BaseNode, |
| 87 | pub local: Identifier, |
| 88 | } |
| 89 | |
| 90 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 91 | pub struct ImportNamespaceSpecifierData { |
| 92 | #[serde(flatten)] |
| 93 | pub base: BaseNode, |
| 94 | pub local: Identifier, |
| 95 | } |
| 96 | |
| 97 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 98 | pub struct ImportAttribute { |
| 99 | #[serde(flatten)] |
| 100 | pub base: BaseNode, |
| 101 | pub key: Identifier, |
| 102 | pub value: StringLiteral, |
| 103 | } |
| 104 | |
| 105 | /// Identifier or StringLiteral used as module export names |
| 106 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 107 | #[serde(tag = "type")] |
| 108 | pub enum ModuleExportName { |
| 109 | Identifier(Identifier), |
| 110 | StringLiteral(StringLiteral), |
| 111 | } |
| 112 | |
| 113 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 114 | pub struct ExportNamedDeclaration { |
| 115 | #[serde(flatten)] |
| 116 | pub base: BaseNode, |
| 117 | pub declaration: Option<Box<Declaration>>, |
| 118 | pub specifiers: Vec<ExportSpecifier>, |
| 119 | pub source: Option<StringLiteral>, |
| 120 | #[serde(default, rename = "exportKind")] |
| 121 | pub export_kind: Option<ExportKind>, |
| 122 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 123 | pub assertions: Option<Vec<ImportAttribute>>, |
| 124 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 125 | pub attributes: Option<Vec<ImportAttribute>>, |
| 126 | } |
| 127 | |
| 128 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 129 | #[serde(rename_all = "lowercase")] |
| 130 | pub enum ExportKind { |
| 131 | Value, |
| 132 | Type, |
| 133 | } |
| 134 | |
| 135 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 136 | #[serde(tag = "type")] |
| 137 | pub enum ExportSpecifier { |
| 138 | ExportSpecifier(ExportSpecifierData), |
| 139 | ExportDefaultSpecifier(ExportDefaultSpecifierData), |
| 140 | ExportNamespaceSpecifier(ExportNamespaceSpecifierData), |
| 141 | } |
| 142 | |
| 143 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 144 | pub struct ExportSpecifierData { |
| 145 | #[serde(flatten)] |
| 146 | pub base: BaseNode, |
| 147 | pub local: ModuleExportName, |
| 148 | pub exported: ModuleExportName, |
| 149 | #[serde( |
| 150 | default, |
| 151 | skip_serializing_if = "Option::is_none", |
| 152 | rename = "exportKind" |
| 153 | )] |
| 154 | pub export_kind: Option<ExportKind>, |
| 155 | } |
| 156 | |
| 157 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 158 | pub struct ExportDefaultSpecifierData { |
| 159 | #[serde(flatten)] |
| 160 | pub base: BaseNode, |
| 161 | pub exported: Identifier, |
| 162 | } |
| 163 | |
| 164 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 165 | pub struct ExportNamespaceSpecifierData { |
| 166 | #[serde(flatten)] |
| 167 | pub base: BaseNode, |
| 168 | pub exported: ModuleExportName, |
| 169 | } |
| 170 | |
| 171 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 172 | pub struct ExportDefaultDeclaration { |
| 173 | #[serde(flatten)] |
| 174 | pub base: BaseNode, |
| 175 | pub declaration: Box<ExportDefaultDecl>, |
| 176 | #[serde( |
| 177 | default, |
| 178 | skip_serializing_if = "Option::is_none", |
| 179 | rename = "exportKind" |
| 180 | )] |
| 181 | pub export_kind: Option<ExportKind>, |
| 182 | } |
| 183 | |
| 184 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 185 | pub struct ExportAllDeclaration { |
| 186 | #[serde(flatten)] |
| 187 | pub base: BaseNode, |
| 188 | pub source: StringLiteral, |
| 189 | #[serde( |
| 190 | default, |
| 191 | skip_serializing_if = "Option::is_none", |
| 192 | rename = "exportKind" |
| 193 | )] |
| 194 | pub export_kind: Option<ExportKind>, |
| 195 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 196 | pub assertions: Option<Vec<ImportAttribute>>, |
| 197 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 198 | pub attributes: Option<Vec<ImportAttribute>>, |
| 199 | } |
| 200 | |
| 201 | // TypeScript declarations (pass-through via RawNode for bodies) |
| 202 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 203 | pub struct TSTypeAliasDeclaration { |
| 204 | #[serde(flatten)] |
| 205 | pub base: BaseNode, |
| 206 | pub id: Identifier, |
| 207 | #[serde(rename = "typeAnnotation")] |
| 208 | pub type_annotation: RawNode, |
| 209 | #[serde( |
| 210 | default, |
| 211 | skip_serializing_if = "Option::is_none", |
| 212 | rename = "typeParameters" |
| 213 | )] |
| 214 | pub type_parameters: Option<RawNode>, |
| 215 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 216 | pub declare: Option<bool>, |
| 217 | } |
| 218 | |
| 219 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 220 | pub struct TSInterfaceDeclaration { |
| 221 | #[serde(flatten)] |
| 222 | pub base: BaseNode, |
| 223 | pub id: Identifier, |
| 224 | pub body: RawNode, |
| 225 | #[serde( |
| 226 | default, |
| 227 | skip_serializing_if = "Option::is_none", |
| 228 | rename = "typeParameters" |
| 229 | )] |
| 230 | pub type_parameters: Option<RawNode>, |
| 231 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 232 | pub extends: Option<Vec<RawNode>>, |
| 233 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 234 | pub declare: Option<bool>, |
| 235 | } |
| 236 | |
| 237 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 238 | pub struct TSEnumDeclaration { |
| 239 | #[serde(flatten)] |
| 240 | pub base: BaseNode, |
| 241 | pub id: Identifier, |
| 242 | pub members: Vec<RawNode>, |
| 243 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 244 | pub declare: Option<bool>, |
| 245 | #[serde(default, skip_serializing_if = "Option::is_none", rename = "const")] |
| 246 | pub is_const: Option<bool>, |
| 247 | } |
| 248 | |
| 249 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 250 | pub struct TSModuleDeclaration { |
| 251 | #[serde(flatten)] |
| 252 | pub base: BaseNode, |
| 253 | pub id: RawNode, |
| 254 | pub body: RawNode, |
| 255 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 256 | pub declare: Option<bool>, |
| 257 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 258 | pub global: Option<bool>, |
| 259 | } |
| 260 | |
| 261 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 262 | pub struct TSDeclareFunction { |
| 263 | #[serde(flatten)] |
| 264 | pub base: BaseNode, |
| 265 | pub id: Option<Identifier>, |
| 266 | pub params: Vec<RawNode>, |
| 267 | #[serde(default, skip_serializing_if = "Option::is_none", rename = "async")] |
| 268 | pub is_async: Option<bool>, |
| 269 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 270 | pub declare: Option<bool>, |
| 271 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 272 | pub generator: Option<bool>, |
| 273 | #[serde( |
| 274 | default, |
| 275 | skip_serializing_if = "Option::is_none", |
| 276 | rename = "returnType" |
| 277 | )] |
| 278 | pub return_type: Option<RawNode>, |
| 279 | #[serde( |
| 280 | default, |
| 281 | skip_serializing_if = "Option::is_none", |
| 282 | rename = "typeParameters" |
| 283 | )] |
| 284 | pub type_parameters: Option<RawNode>, |
| 285 | } |
| 286 | |
| 287 | // Flow declarations (pass-through) |
| 288 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 289 | pub struct TypeAlias { |
| 290 | #[serde(flatten)] |
| 291 | pub base: BaseNode, |
| 292 | pub id: Identifier, |
| 293 | pub right: RawNode, |
| 294 | #[serde(default, rename = "typeParameters")] |
| 295 | pub type_parameters: Option<RawNode>, |
| 296 | } |
| 297 | |
| 298 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 299 | pub struct OpaqueType { |
| 300 | #[serde(flatten)] |
| 301 | pub base: BaseNode, |
| 302 | pub id: Identifier, |
| 303 | #[serde(rename = "supertype")] |
| 304 | pub supertype: Option<RawNode>, |
| 305 | pub impltype: RawNode, |
| 306 | #[serde( |
| 307 | default, |
| 308 | skip_serializing_if = "Option::is_none", |
| 309 | rename = "typeParameters" |
| 310 | )] |
| 311 | pub type_parameters: Option<RawNode>, |
| 312 | } |
| 313 | |
| 314 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 315 | pub struct InterfaceDeclaration { |
| 316 | #[serde(flatten)] |
| 317 | pub base: BaseNode, |
| 318 | pub id: Identifier, |
| 319 | pub body: RawNode, |
| 320 | #[serde( |
| 321 | default, |
| 322 | skip_serializing_if = "Option::is_none", |
| 323 | rename = "typeParameters" |
| 324 | )] |
| 325 | pub type_parameters: Option<RawNode>, |
| 326 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 327 | pub extends: Option<Vec<RawNode>>, |
| 328 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 329 | pub mixins: Option<Vec<RawNode>>, |
| 330 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 331 | pub implements: Option<Vec<RawNode>>, |
| 332 | } |
| 333 | |
| 334 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 335 | pub struct DeclareVariable { |
| 336 | #[serde(flatten)] |
| 337 | pub base: BaseNode, |
| 338 | pub id: Identifier, |
| 339 | } |
| 340 | |
| 341 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 342 | pub struct DeclareFunction { |
| 343 | #[serde(flatten)] |
| 344 | pub base: BaseNode, |
| 345 | pub id: Identifier, |
| 346 | #[serde( |
| 347 | default, |
| 348 | skip_serializing_if = "Option::is_none", |
| 349 | deserialize_with = "crate::common::nullable_value" |
| 350 | )] |
| 351 | pub predicate: Option<RawNode>, |
| 352 | } |
| 353 | |
| 354 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 355 | pub struct DeclareClass { |
| 356 | #[serde(flatten)] |
| 357 | pub base: BaseNode, |
| 358 | pub id: Identifier, |
| 359 | pub body: RawNode, |
| 360 | #[serde( |
| 361 | default, |
| 362 | skip_serializing_if = "Option::is_none", |
| 363 | rename = "typeParameters" |
| 364 | )] |
| 365 | pub type_parameters: Option<RawNode>, |
| 366 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 367 | pub extends: Option<Vec<RawNode>>, |
| 368 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 369 | pub mixins: Option<Vec<RawNode>>, |
| 370 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 371 | pub implements: Option<Vec<RawNode>>, |
| 372 | } |
| 373 | |
| 374 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 375 | pub struct DeclareModule { |
| 376 | #[serde(flatten)] |
| 377 | pub base: BaseNode, |
| 378 | pub id: RawNode, |
| 379 | pub body: RawNode, |
| 380 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 381 | pub kind: Option<String>, |
| 382 | } |
| 383 | |
| 384 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 385 | pub struct DeclareModuleExports { |
| 386 | #[serde(flatten)] |
| 387 | pub base: BaseNode, |
| 388 | #[serde(rename = "typeAnnotation")] |
| 389 | pub type_annotation: RawNode, |
| 390 | } |
| 391 | |
| 392 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 393 | pub struct DeclareExportDeclaration { |
| 394 | #[serde(flatten)] |
| 395 | pub base: BaseNode, |
| 396 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 397 | pub declaration: Option<RawNode>, |
| 398 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 399 | pub specifiers: Option<Vec<RawNode>>, |
| 400 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 401 | pub source: Option<StringLiteral>, |
| 402 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 403 | pub default: Option<bool>, |
| 404 | } |
| 405 | |
| 406 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 407 | pub struct DeclareExportAllDeclaration { |
| 408 | #[serde(flatten)] |
| 409 | pub base: BaseNode, |
| 410 | pub source: StringLiteral, |
| 411 | } |
| 412 | |
| 413 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 414 | pub struct DeclareInterface { |
| 415 | #[serde(flatten)] |
| 416 | pub base: BaseNode, |
| 417 | pub id: Identifier, |
| 418 | pub body: RawNode, |
| 419 | #[serde( |
| 420 | default, |
| 421 | skip_serializing_if = "Option::is_none", |
| 422 | rename = "typeParameters" |
| 423 | )] |
| 424 | pub type_parameters: Option<RawNode>, |
| 425 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 426 | pub extends: Option<Vec<RawNode>>, |
| 427 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 428 | pub mixins: Option<Vec<RawNode>>, |
| 429 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 430 | pub implements: Option<Vec<RawNode>>, |
| 431 | } |
| 432 | |
| 433 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 434 | pub struct DeclareTypeAlias { |
| 435 | #[serde(flatten)] |
| 436 | pub base: BaseNode, |
| 437 | pub id: Identifier, |
| 438 | pub right: RawNode, |
| 439 | #[serde( |
| 440 | default, |
| 441 | skip_serializing_if = "Option::is_none", |
| 442 | rename = "typeParameters" |
| 443 | )] |
| 444 | pub type_parameters: Option<RawNode>, |
| 445 | } |
| 446 | |
| 447 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 448 | pub struct DeclareOpaqueType { |
| 449 | #[serde(flatten)] |
| 450 | pub base: BaseNode, |
| 451 | pub id: Identifier, |
| 452 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 453 | pub supertype: Option<RawNode>, |
| 454 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 455 | pub impltype: Option<RawNode>, |
| 456 | #[serde( |
| 457 | default, |
| 458 | skip_serializing_if = "Option::is_none", |
| 459 | rename = "typeParameters" |
| 460 | )] |
| 461 | pub type_parameters: Option<RawNode>, |
| 462 | } |
| 463 | |
| 464 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 465 | pub struct EnumDeclaration { |
| 466 | #[serde(flatten)] |
| 467 | pub base: BaseNode, |
| 468 | pub id: Identifier, |
| 469 | pub body: RawNode, |
| 470 | } |