Skip to content

Commit 375e62e

Browse files
committed
refactor: add type resolution to TsTypeRefDef
1 parent 4124539 commit 375e62e

41 files changed

Lines changed: 684 additions & 67 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

js/types.d.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,9 +755,30 @@ export type TsTypeDefKind =
755755
| "typeLiteral"
756756
| "typePredicate";
757757

758+
export type TypeParamDeclaringKind =
759+
| "class"
760+
| "interface"
761+
| "function"
762+
| "typeAlias"
763+
| "method";
764+
765+
export type TypeRefResolution =
766+
| { kind: "local" }
767+
| {
768+
kind: "typeParam";
769+
declaringName?: string;
770+
declaringKind?: TypeParamDeclaringKind;
771+
}
772+
| {
773+
kind: "import";
774+
specifier: string;
775+
name?: string;
776+
};
777+
758778
export interface TsTypeRefDef {
759779
typeParams?: TsTypeDef[];
760780
typeName: string;
781+
resolution?: TypeRefResolution;
761782
}
762783

763784
export interface TypeAliasDef {

src/function.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub fn function_to_function_def(
6060
crate::ts_type::TsTypeRefDef {
6161
type_params: Some(Box::new([TsTypeDef::keyword("void")])),
6262
type_name: "Promise".to_string(),
63+
resolution: None,
6364
},
6465
),
6566
})

src/lib.rs

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,17 @@ fn get_children_of_node(node: Symbol) -> Vec<Symbol> {
126126
doc_nodes
127127
}
128128

129-
pub fn docnodes_v1_to_v2(value: serde_json::Value) -> Vec<Symbol> {
129+
pub fn docnodes_v1_to_v2(value: serde_json::Value) -> Document {
130130
let serde_json::Value::Array(arr) = value else {
131-
return vec![];
131+
return Document::default();
132132
};
133133

134134
// v1 format: flat array where each entry has "name", "kind", "location",
135135
// "declarationKind", "jsDoc", and def fields all at the top level.
136-
// v2 format: array of Symbol { name, isDefault, declarations: [...] }
137-
// where each declaration has the remaining fields.
136+
// v2 format: Document { module_doc, imports, symbols }
137+
// where symbols are Symbol { name, isDefault, declarations: [...] }.
138+
let mut module_doc = js_doc::JsDoc::default();
139+
let mut imports = Vec::new();
138140
let mut symbols: indexmap::IndexMap<Box<str>, Symbol> =
139141
indexmap::IndexMap::new();
140142

@@ -143,6 +145,51 @@ pub fn docnodes_v1_to_v2(value: serde_json::Value) -> Vec<Symbol> {
143145
continue;
144146
};
145147

148+
let kind = obj
149+
.get("kind")
150+
.and_then(|v| v.as_str())
151+
.map(|s| s.to_string());
152+
153+
// v1 moduleDoc nodes become Document.module_doc
154+
if kind.as_deref() == Some("moduleDoc") {
155+
if let Some(js_doc_val) = obj
156+
.remove("jsDoc")
157+
.and_then(|v| serde_json::from_value::<js_doc::JsDoc>(v).ok())
158+
{
159+
module_doc = js_doc_val;
160+
}
161+
continue;
162+
}
163+
164+
// v1 import nodes become Document.imports
165+
if kind.as_deref() == Some("import") {
166+
let imported_name: Box<str> = obj
167+
.remove("name")
168+
.and_then(|v| v.as_str().map(|s| s.into()))
169+
.unwrap_or_else(|| "".into());
170+
let import_def = obj.remove("importDef").unwrap_or_default();
171+
let src = import_def
172+
.get("src")
173+
.and_then(|v| v.as_str())
174+
.unwrap_or("")
175+
.to_string();
176+
let original_name = import_def
177+
.get("imported")
178+
.and_then(|v| v.as_str())
179+
.map(|s| s.to_string());
180+
let js_doc = obj
181+
.remove("jsDoc")
182+
.and_then(|v| serde_json::from_value::<js_doc::JsDoc>(v).ok())
183+
.unwrap_or_default();
184+
imports.push(node::Import {
185+
imported_name,
186+
original_name,
187+
src,
188+
js_doc,
189+
});
190+
continue;
191+
}
192+
146193
let name: Box<str> = obj
147194
.remove("name")
148195
.and_then(|v| v.as_str().map(|s| s.into()))
@@ -190,7 +237,11 @@ pub fn docnodes_v1_to_v2(value: serde_json::Value) -> Vec<Symbol> {
190237
}
191238
}
192239

193-
symbols.into_values().collect()
240+
Document {
241+
module_doc,
242+
imports,
243+
symbols: symbols.into_values().collect(),
244+
}
194245
}
195246

196247
/// Recursively walk JSON and convert v1 TsTypeDef objects (which use

0 commit comments

Comments
 (0)