1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//! # Default Macro Implementation
//!
//! The `Default` macro generates a static `defaultValue()` factory method that creates
//! instances with default values. This is analogous to Rust's `Default` trait, providing
//! a standard way to create "zero" or "empty" instances of types.
//!
//! ## Generated Output
//!
//! | Type | Generated Code | Description |
//! |------|----------------|-------------|
//! | Class | `static defaultValue()` + `{className}DefaultValue()` | Static factory method + standalone function |
//! | Enum | `{enumName}DefaultValue(): EnumName` | Standalone function returning `@default` variant |
//! | Interface | `{ifaceName}DefaultValue(): InterfaceName` | Standalone function returning object literal |
//! | Type Alias | `{typeName}DefaultValue(): TypeName` | Standalone function with type-appropriate default |
//!
//! Names use **camelCase** conversion (e.g., `UserSettings` -> `userSettingsDefaultValue`).
//!
//!
//! ## Default Values by Type
//!
//! The macro uses Rust-like default semantics:
//!
//! | Type | Default Value |
//! |------|---------------|
//! | `string` | `""` (empty string) |
//! | `number` | `0` |
//! | `boolean` | `false` |
//! | `bigint` | `0n` |
//! | `T[]` | `[]` (empty array) |
//! | `Array<T>` | `[]` (empty array) |
//! | `Map<K,V>` | `new Map()` |
//! | `Set<T>` | `new Set()` |
//! | `Date` | `new Date()` (current time) |
//! | `T \| null` | `null` |
//! | `CustomType` | `CustomType.defaultValue()` (recursive) |
//!
//! ## Field-Level Options
//!
//! The `@default` decorator allows specifying explicit default values:
//!
//! - `@default(42)` - Use 42 as the default
//! - `@default("hello")` - Use "hello" as the default
//! - `@default([])` - Use empty array as the default
//! - `@default({ value: "test" })` - Named form for complex values
//!
//! ## Example
//!
//! ```typescript
//! /** @derive(Default) */
//! class UserSettings {
//! /** @default("light") */
//! theme: string;
//!
//! /** @default(10) */
//! pageSize: number;
//!
//! notifications: boolean; // Uses type default: false
//! }
//! ```
//!
//! Generated output:
//!
//! ```typescript
//! class UserSettings {
//! theme: string;
//!
//! pageSize: number;
//!
//! notifications: boolean; // Uses type default: false
//!
//! static defaultValue(): UserSettings {
//! const instance = new UserSettings();
//! instance.theme = 'light';
//! instance.pageSize = 10;
//! instance.notifications = false;
//! return instance;
//! }
//! }
//!
//! export function userSettingsDefaultValue(): UserSettings {
//! return UserSettings.defaultValue();
//! }
//! ```
//!
//! ## Enum Defaults
//!
//! For enums, mark one variant with `@default`:
//!
//! ```typescript
//! /** @derive(Default) */
//! enum Status {
//! /** @default */
//! Pending,
//! Active,
//! Completed
//! }
//! ```
//!
//! Generated output:
//!
//! ```typescript
//! enum Status {
//! /** @default */
//! Pending,
//! Active,
//! Completed
//! }
//!
//! export function statusDefaultValue(): Status {
//! return Status.Pending;
//! }
//! ```
//!
//! ## Error Handling
//!
//! The macro will return an error if:
//!
//! - A non-primitive field lacks `@default` and has no known default
//! - An enum has no variant marked with `@default`
//! - A union type has no `@default` on a variant