Skip to content

Commit db2eb3d

Browse files
authored
refactor: simplify DeclarationDef (#776)
1 parent 2da2c61 commit db2eb3d

181 files changed

Lines changed: 618 additions & 670 deletions

File tree

Some content is hidden

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

examples/ddoc/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ async fn run() -> anyhow::Result<()> {
214214
doc_nodes_by_url.into_values().flatten().collect::<Vec<_>>();
215215

216216
doc_nodes.retain(|doc_node| {
217-
!matches!(doc_node.declarations[0].def, DeclarationDef::Import { .. })
217+
!matches!(doc_node.declarations[0].def, DeclarationDef::Import(..))
218218
});
219219

220220
if let Some(filter) = filter {

js/test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ Deno.test({
1818
assert(fnStripColor, "unable to locate specific node");
1919
const decl = fnStripColor.declarations[0];
2020
assert(decl.kind === "function");
21-
assert(decl.functionDef);
22-
assertEquals(decl.functionDef.params, [{
21+
assert(decl.def);
22+
assertEquals(decl.def.params, [{
2323
kind: "identifier",
2424
name: "string",
2525
optional: false,

js/types.d.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ export interface DeclarationModuleDoc extends DeclarationBase {
4949

5050
export interface DeclarationFunction extends DeclarationBase {
5151
kind: "function";
52-
functionDef: FunctionDef;
52+
def: FunctionDef;
5353
}
5454

5555
export interface DeclarationVariable extends DeclarationBase {
5656
kind: "variable";
57-
variableDef: VariableDef;
57+
def: VariableDef;
5858
}
5959

6060
export interface DeclarationEnum extends DeclarationBase {
@@ -64,27 +64,27 @@ export interface DeclarationEnum extends DeclarationBase {
6464

6565
export interface DeclarationClass extends DeclarationBase {
6666
kind: "class";
67-
classDef: ClassDef;
67+
def: ClassDef;
6868
}
6969

7070
export interface DeclarationTypeAlias extends DeclarationBase {
7171
kind: "typeAlias";
72-
typeAliasDef: TypeAliasDef;
72+
def: TypeAliasDef;
7373
}
7474

7575
export interface DeclarationNamespace extends DeclarationBase {
7676
kind: "namespace";
77-
namespaceDef: NamespaceDef;
77+
def: NamespaceDef;
7878
}
7979

8080
export interface DeclarationInterface extends DeclarationBase {
8181
kind: "interface";
82-
interfaceDef: InterfaceDef;
82+
def: InterfaceDef;
8383
}
8484

8585
export interface DeclarationImport extends DeclarationBase {
8686
kind: "import";
87-
importDef: ImportDef;
87+
def: ImportDef;
8888
}
8989

9090
export interface DeclarationReference extends DeclarationBase {
@@ -139,7 +139,7 @@ export interface ClassMethodDef {
139139
isOverride?: boolean;
140140
name: string;
141141
kind: MethodKind;
142-
functionDef: FunctionDef;
142+
def: FunctionDef;
143143
location: Location;
144144
}
145145

src/diagnostics.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ impl DiagnosticDocNodeVisitor<'_, '_> {
369369
let fn_decl_count = doc_node
370370
.declarations
371371
.iter()
372-
.filter(|d| matches!(d.def, DeclarationDef::Function { .. }))
372+
.filter(|d| matches!(d.def, DeclarationDef::Function(..)))
373373
.count();
374374
let has_fn_overloads = fn_decl_count > 1;
375375

@@ -395,16 +395,16 @@ impl DiagnosticDocNodeVisitor<'_, '_> {
395395
fn visit_decl(&mut self, decl: &crate::node::Declaration) {
396396
fn is_js_docable_kind(def: &DeclarationDef) -> bool {
397397
match def {
398-
DeclarationDef::Class { .. }
399-
| DeclarationDef::Enum { .. }
400-
| DeclarationDef::Function { .. }
401-
| DeclarationDef::Interface { .. }
402-
| DeclarationDef::Namespace { .. }
403-
| DeclarationDef::TypeAlias { .. }
404-
| DeclarationDef::Variable { .. } => true,
405-
DeclarationDef::Import { .. }
398+
DeclarationDef::Class(..)
399+
| DeclarationDef::Enum(..)
400+
| DeclarationDef::Function(..)
401+
| DeclarationDef::Interface(..)
402+
| DeclarationDef::Namespace(..)
403+
| DeclarationDef::TypeAlias(..)
404+
| DeclarationDef::Variable(..) => true,
405+
DeclarationDef::Import(..)
406406
| DeclarationDef::ModuleDoc
407-
| DeclarationDef::Reference { .. } => false,
407+
| DeclarationDef::Reference(..) => false,
408408
}
409409
}
410410

src/diff/mod.rs

Lines changed: 26 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -484,31 +484,23 @@ impl DeclarationDefDiff {
484484
(DeclarationDefDiff::TypeAlias(d), _, _) => d.change_percentage(),
485485
(
486486
DeclarationDefDiff::Enum(d),
487-
DeclarationDef::Enum { enum_def: old_def },
488-
DeclarationDef::Enum { enum_def: new_def },
487+
DeclarationDef::Enum(old_def),
488+
DeclarationDef::Enum(new_def),
489489
) => d.change_percentage(old_def, new_def),
490490
(
491491
DeclarationDefDiff::Class(d),
492-
DeclarationDef::Class { class_def: old_def },
493-
DeclarationDef::Class { class_def: new_def },
492+
DeclarationDef::Class(old_def),
493+
DeclarationDef::Class(new_def),
494494
) => d.change_percentage(old_def, new_def),
495495
(
496496
DeclarationDefDiff::Interface(d),
497-
DeclarationDef::Interface {
498-
interface_def: old_def,
499-
},
500-
DeclarationDef::Interface {
501-
interface_def: new_def,
502-
},
497+
DeclarationDef::Interface(old_def),
498+
DeclarationDef::Interface(new_def),
503499
) => d.change_percentage(old_def, new_def),
504500
(
505501
DeclarationDefDiff::Namespace(d),
506-
DeclarationDef::Namespace {
507-
namespace_def: old_def,
508-
},
509-
DeclarationDef::Namespace {
510-
namespace_def: new_def,
511-
},
502+
DeclarationDef::Namespace(old_def),
503+
DeclarationDef::Namespace(new_def),
512504
) => d.change_percentage(old_def, new_def),
513505
_ => 1.0,
514506
}
@@ -566,74 +558,50 @@ impl DeclarationDefDiff {
566558
pub fn diff(old: &DeclarationDef, new: &DeclarationDef) -> Option<Self> {
567559
match (old, new) {
568560
(
569-
DeclarationDef::Function {
570-
function_def: old_def,
571-
},
572-
DeclarationDef::Function {
573-
function_def: new_def,
574-
},
561+
DeclarationDef::Function(old_def),
562+
DeclarationDef::Function(new_def),
575563
) => {
576564
FunctionDiff::diff(old_def, new_def).map(DeclarationDefDiff::Function)
577565
}
578566

579567
(
580-
DeclarationDef::Variable {
581-
variable_def: old_def,
582-
},
583-
DeclarationDef::Variable {
584-
variable_def: new_def,
585-
},
568+
DeclarationDef::Variable(old_def),
569+
DeclarationDef::Variable(new_def),
586570
) => {
587571
VariableDiff::diff(old_def, new_def).map(DeclarationDefDiff::Variable)
588572
}
589573

590-
(
591-
DeclarationDef::Enum { enum_def: old_def },
592-
DeclarationDef::Enum { enum_def: new_def },
593-
) => EnumDiff::diff(old_def, new_def).map(DeclarationDefDiff::Enum),
574+
(DeclarationDef::Enum(old_def), DeclarationDef::Enum(new_def)) => {
575+
EnumDiff::diff(old_def, new_def).map(DeclarationDefDiff::Enum)
576+
}
594577

595-
(
596-
DeclarationDef::Class { class_def: old_def },
597-
DeclarationDef::Class { class_def: new_def },
598-
) => ClassDiff::diff(old_def, new_def).map(DeclarationDefDiff::Class),
578+
(DeclarationDef::Class(old_def), DeclarationDef::Class(new_def)) => {
579+
ClassDiff::diff(old_def, new_def).map(DeclarationDefDiff::Class)
580+
}
599581

600582
(
601-
DeclarationDef::TypeAlias {
602-
type_alias_def: old_def,
603-
},
604-
DeclarationDef::TypeAlias {
605-
type_alias_def: new_def,
606-
},
583+
DeclarationDef::TypeAlias(old_def),
584+
DeclarationDef::TypeAlias(new_def),
607585
) => {
608586
TypeAliasDiff::diff(old_def, new_def).map(DeclarationDefDiff::TypeAlias)
609587
}
610588

611589
(
612-
DeclarationDef::Namespace {
613-
namespace_def: old_def,
614-
},
615-
DeclarationDef::Namespace {
616-
namespace_def: new_def,
617-
},
590+
DeclarationDef::Namespace(old_def),
591+
DeclarationDef::Namespace(new_def),
618592
) => {
619593
NamespaceDiff::diff(old_def, new_def).map(DeclarationDefDiff::Namespace)
620594
}
621595

622596
(
623-
DeclarationDef::Interface {
624-
interface_def: old_def,
625-
},
626-
DeclarationDef::Interface {
627-
interface_def: new_def,
628-
},
597+
DeclarationDef::Interface(old_def),
598+
DeclarationDef::Interface(new_def),
629599
) => {
630600
InterfaceDiff::diff(old_def, new_def).map(DeclarationDefDiff::Interface)
631601
}
632602

633-
(DeclarationDef::Import { .. }, DeclarationDef::Import { .. }) => None,
634-
(DeclarationDef::Reference { .. }, DeclarationDef::Reference { .. }) => {
635-
None
636-
}
603+
(DeclarationDef::Import(..), DeclarationDef::Import(..)) => None,
604+
(DeclarationDef::Reference(..), DeclarationDef::Reference(..)) => None,
637605
(DeclarationDef::ModuleDoc, DeclarationDef::ModuleDoc) => None,
638606

639607
_ => unreachable!(),

src/html/mod.rs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -359,19 +359,17 @@ impl GenerateCtx {
359359
kind == &crate::ts_type::TsTypeDefKind::FnOrConstructor
360360
})
361361
{
362-
let DeclarationDef::Variable { variable_def } =
363-
std::mem::replace(
364-
&mut declaration.def,
365-
DeclarationDef::ModuleDoc,
366-
)
367-
else {
362+
let DeclarationDef::Variable(variable_def) = std::mem::replace(
363+
&mut declaration.def,
364+
DeclarationDef::ModuleDoc,
365+
) else {
368366
unreachable!()
369367
};
370368
let fn_or_constructor =
371369
variable_def.ts_type.unwrap().fn_or_constructor.unwrap();
372370

373-
declaration.def = DeclarationDef::Function {
374-
function_def: crate::function::FunctionDef {
371+
declaration.def =
372+
DeclarationDef::Function(crate::function::FunctionDef {
375373
def_name: None,
376374
params: fn_or_constructor.params,
377375
return_type: Some(fn_or_constructor.ts_type),
@@ -380,8 +378,7 @@ impl GenerateCtx {
380378
is_generator: false,
381379
type_params: fn_or_constructor.type_params,
382380
decorators: Box::new([]),
383-
},
384-
};
381+
});
385382
}
386383
}
387384

@@ -517,7 +514,7 @@ impl GenerateCtx {
517514
.entry(decl.location.clone())
518515
.or_default()
519516
.push((depth, node.clone()));
520-
if matches!(decl.def, DeclarationDef::Namespace { .. })
517+
if matches!(decl.def, DeclarationDef::Namespace(..))
521518
&& let Some(children) = &node.namespace_children
522519
{
523520
for child in children {
@@ -728,7 +725,7 @@ fn apply_namespace_diff_inner(
728725
if child
729726
.declarations
730727
.iter()
731-
.any(|decl| matches!(decl.def, DeclarationDef::Namespace { .. }))
728+
.any(|decl| matches!(decl.def, DeclarationDef::Namespace(..)))
732729
{
733730
let child_ns_diff =
734731
symbol_diff.declarations.as_ref().and_then(|decls_diff| {
@@ -945,9 +942,7 @@ impl DocNodeWithContext {
945942
.inner
946943
.declarations
947944
.iter()
948-
.filter(|d| {
949-
!matches!(d.def, crate::node::DeclarationDef::Import { .. })
950-
})
945+
.filter(|d| !matches!(d.def, crate::node::DeclarationDef::Import(..)))
951946
.map(|d| DocNodeKindCtx::from(crate::node::DocNodeKind::from(&d.def)))
952947
.collect()
953948
}
@@ -1067,7 +1062,7 @@ impl DocNodeWithContext {
10671062

10681063
for decl in &self.inner.declarations {
10691064
match &decl.def {
1070-
DeclarationDef::Class { class_def } => {
1065+
DeclarationDef::Class(class_def) => {
10711066
symbols.extend(class_def.methods.iter().map(|method| {
10721067
self.create_child_method(
10731068
Symbol::function(
@@ -1089,7 +1084,7 @@ impl DocNodeWithContext {
10891084
)
10901085
}));
10911086
}
1092-
DeclarationDef::Interface { interface_def } => {
1087+
DeclarationDef::Interface(interface_def) => {
10931088
symbols.extend(interface_def.methods.iter().map(|method| {
10941089
self.create_child_method(
10951090
Symbol::from(method.clone()),
@@ -1101,7 +1096,7 @@ impl DocNodeWithContext {
11011096
self.create_child_property(Symbol::from(property.clone()), true)
11021097
}));
11031098
}
1104-
DeclarationDef::TypeAlias { type_alias_def } => {
1099+
DeclarationDef::TypeAlias(type_alias_def) => {
11051100
if let Some(ts_type_literal) =
11061101
type_alias_def.ts_type.type_literal.as_ref()
11071102
{
@@ -1117,7 +1112,7 @@ impl DocNodeWithContext {
11171112
}));
11181113
}
11191114
}
1120-
DeclarationDef::Variable { variable_def } => {
1115+
DeclarationDef::Variable(variable_def) => {
11211116
if let Some(ts_type_literal) = variable_def
11221117
.ts_type
11231118
.as_ref()

src/html/pages.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ pub fn generate_symbol_pages_for_module(
550550
if symbol
551551
.declarations
552552
.iter()
553-
.any(|decl| matches!(decl.def, DeclarationDef::Class { .. }))
553+
.any(|decl| matches!(decl.def, DeclarationDef::Class(..)))
554554
{
555555
let name = symbol.get_qualified_name();
556556
let prototype_name = format!("{name}.prototype");

src/html/partition.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ where
3333
for decl in &node.declarations {
3434
if matches!(
3535
decl.def,
36-
DeclarationDef::ModuleDoc | DeclarationDef::Import { .. }
36+
DeclarationDef::ModuleDoc | DeclarationDef::Import(..)
3737
) {
3838
continue 'outer;
3939
}
4040

4141
if flatten_namespaces
42-
&& matches!(decl.def, DeclarationDef::Namespace { .. })
42+
&& matches!(decl.def, DeclarationDef::Namespace(..))
4343
{
4444
partitioner_inner(
4545
ctx,
@@ -228,12 +228,12 @@ pub fn flatten_namespace<'a>(
228228
for decl in &node.declarations {
229229
if matches!(
230230
decl.def,
231-
DeclarationDef::ModuleDoc | DeclarationDef::Import { .. }
231+
DeclarationDef::ModuleDoc | DeclarationDef::Import(..)
232232
) {
233233
continue 'outer;
234234
}
235235

236-
if matches!(decl.def, DeclarationDef::Namespace { .. }) {
236+
if matches!(decl.def, DeclarationDef::Namespace(..)) {
237237
let children: Vec<_> =
238238
node.namespace_children.as_ref().unwrap().clone();
239239
partitioner_inner(

0 commit comments

Comments
 (0)