Skip to content

Commit

Permalink
print visibility specifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Oct 26, 2023
1 parent b417982 commit 1616bba
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/ddoc/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn main() {
},
)
.await;
let parser = DocParser::new(graph, false, analyzer.as_capturing_parser());
let parser = DocParser::new(graph, private, analyzer.as_capturing_parser());
let parse_result = parser.parse_with_reexports(&source_file);

let mut doc_nodes = match parse_result {
Expand Down
14 changes: 13 additions & 1 deletion src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ pub enum ReexportKind {
pub struct Reexport {
pub kind: ReexportKind,
pub src: String,
/// True for symbols that are not re-exported, but should be visible,
/// eg.
/// ```
/// class Other {}
///
/// export class Test {
/// foo: string;
/// }
/// ```
///
/// In the above example `Other` would have `implicit` equal to true.
pub implicit: bool,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand All @@ -63,7 +75,7 @@ pub struct ImportDef {
pub imported: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum DeclarationKind {
Private,
Expand Down
6 changes: 6 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ impl<'a> DocParser<'a> {
}
ImportKind::Namespace(name) => ReexportKind::Namespace(name),
},
implicit: true,
}))
}

Expand All @@ -692,13 +693,15 @@ impl<'a> DocParser<'a> {
module_export_name_value(&ns_export.name),
),
src: src_str.to_string(),
implicit: false,
},
ExportSpecifier::Default(specifier) => node::Reexport {
kind: node::ReexportKind::Named(
"default".to_string(),
Some(specifier.exported.sym.to_string()),
),
src: src_str.to_string(),
implicit: false,
},
ExportSpecifier::Named(named_export) => {
let export_name =
Expand All @@ -712,6 +715,7 @@ impl<'a> DocParser<'a> {
node::Reexport {
kind,
src: src_str.to_string(),
implicit: false,
}
}
})
Expand Down Expand Up @@ -749,6 +753,7 @@ impl<'a> DocParser<'a> {
ReexportKind::Namespace(name)
}
},
implicit: false,
})
} else {
None
Expand All @@ -764,6 +769,7 @@ impl<'a> DocParser<'a> {
let reexport = node::Reexport {
kind: node::ReexportKind::All,
src: export_all.src.value.to_string(),
implicit: false,
};
vec![reexport]
}
Expand Down
30 changes: 23 additions & 7 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::display::Indent;
use crate::display::SliceDisplayer;
use crate::js_doc::JsDoc;
use crate::js_doc::JsDocTag;
use crate::node::DeclarationKind;
use crate::node::DocNode;
use crate::node::DocNodeKind;

Expand Down Expand Up @@ -503,8 +504,9 @@ impl<'a> DocPrinter<'a> {
}
write!(
w,
"{}{}{} {}",
"{}{}{}{} {}",
Indent(indent),
fmt_visibility(node.declaration_kind),
display_abstract(class_def.is_abstract),
colors::magenta("class"),
colors::bold(&node.name),
Expand Down Expand Up @@ -548,8 +550,9 @@ impl<'a> DocPrinter<'a> {
) -> FmtResult {
writeln!(
w,
"{}{} {}",
"{}{}{} {}",
Indent(indent),
fmt_visibility(node.declaration_kind),
colors::magenta("enum"),
colors::bold(&node.name)
)
Expand All @@ -566,8 +569,9 @@ impl<'a> DocPrinter<'a> {
if !has_overloads || !function_def.has_body {
write!(
w,
"{}{}{}{} {}",
"{}{}{}{}{} {}",
Indent(indent),
fmt_visibility(node.declaration_kind),
display_async(function_def.is_async),
colors::magenta("function"),
display_generator(function_def.is_generator),
Expand Down Expand Up @@ -602,8 +606,9 @@ impl<'a> DocPrinter<'a> {
let interface_def = node.interface_def.as_ref().unwrap();
write!(
w,
"{}{} {}",
"{}{}{} {}",
Indent(indent),
fmt_visibility(node.declaration_kind),
colors::magenta("interface"),
colors::bold(&node.name)
)?;
Expand Down Expand Up @@ -648,8 +653,9 @@ impl<'a> DocPrinter<'a> {
let type_alias_def = node.type_alias_def.as_ref().unwrap();
write!(
w,
"{}{} {}",
"{}{}{} {}",
Indent(indent),
fmt_visibility(node.declaration_kind),
colors::magenta("type"),
colors::bold(&node.name),
)?;
Expand All @@ -673,8 +679,9 @@ impl<'a> DocPrinter<'a> {
) -> FmtResult {
writeln!(
w,
"{}{} {}",
"{}{}{} {}",
Indent(indent),
fmt_visibility(node.declaration_kind),
colors::magenta("namespace"),
colors::bold(&node.name)
)
Expand All @@ -689,8 +696,9 @@ impl<'a> DocPrinter<'a> {
let variable_def = node.variable_def.as_ref().unwrap();
write!(
w,
"{}{} {}",
"{}{}{} {}",
Indent(indent),
fmt_visibility(node.declaration_kind),
colors::magenta(match variable_def.kind {
deno_ast::swc::ast::VarDeclKind::Const => "const",
deno_ast::swc::ast::VarDeclKind::Let => "let",
Expand All @@ -710,3 +718,11 @@ impl<'a> Display for DocPrinter<'a> {
self.format(f)
}
}

fn fmt_visibility(decl_kind: DeclarationKind) -> impl std::fmt::Display {
colors::italic_gray(if decl_kind == DeclarationKind::Private {
"private "
} else {
""
})
}

0 comments on commit 1616bba

Please sign in to comment.