Skip to content

Commit

Permalink
feat: display non-exported types referenced in exported types
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Oct 26, 2023
1 parent f5cdee5 commit 62adc5f
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 101 deletions.
46 changes: 24 additions & 22 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,15 +544,18 @@ impl<'a> DocParser<'a> {
TsModuleName::Str(str_) => str_.value.to_string(),
};
let mut elements = Vec::new();
let mut handled_symbols = HashSet::new();

for (export_name, export_symbol_id) in symbol.exports() {
handled_symbols.insert(*export_symbol_id);
let export_symbol = module_symbol.symbol(*export_symbol_id).unwrap();
let definitions = self.root_symbol.go_to_definitions(
&self.graph,
ModuleSymbolRef::Esm(module_symbol),
export_symbol,
);
for definition in definitions {
handled_symbols.insert(definition.symbol.symbol_id());
if definition.module.specifier() != module_symbol.specifier() {
continue;
}
Expand All @@ -572,18 +575,17 @@ impl<'a> DocParser<'a> {
}

let is_ambient = elements.is_empty() && !module_has_import(module_symbol);
if is_ambient || self.private {
let mut handled_symbols =
symbol.exports().values().copied().collect::<HashSet<_>>();
for child_id in symbol.child_decls() {
if !handled_symbols.insert(child_id) {
continue; // already handled
}
let child_symbol = module_symbol.symbol(child_id).unwrap();
for child_id in symbol.child_decls() {
if !handled_symbols.insert(child_id) {
continue; // already handled
}
let child_symbol = module_symbol.symbol(child_id).unwrap();
if child_symbol.is_public() || is_ambient || self.private {
for decl in child_symbol.decls() {
if let Some(node) = decl.maybe_node() {
let is_declared = self.get_declare_for_symbol_node(node);
if is_declared || self.private {
let is_declared =
is_ambient && self.get_declare_for_symbol_node(node);
if child_symbol.is_public() || is_declared || self.private {
if let Some(mut doc_node) = self.get_doc_for_symbol_node_ref(
module_symbol,
child_symbol,
Expand All @@ -592,7 +594,6 @@ impl<'a> DocParser<'a> {
doc_node.declaration_kind = if is_declared {
DeclarationKind::Declare
} else {
debug_assert!(self.private);
DeclarationKind::Private
};
elements.push(doc_node);
Expand Down Expand Up @@ -902,8 +903,10 @@ impl<'a> DocParser<'a> {
}
}

let mut handled_symbols = HashSet::new();
let exports = module_symbol.exports(&self.graph, &self.root_symbol);
for (export_name, (export_module, export_symbol_id)) in &exports {
handled_symbols.insert(*export_symbol_id);
let export_symbol = export_module.symbol(*export_symbol_id).unwrap();
let definitions = self.root_symbol.go_to_definitions(
&self.graph,
Expand All @@ -914,6 +917,7 @@ impl<'a> DocParser<'a> {
if definition.module.specifier() != module_symbol.specifier() {
continue;
}
handled_symbols.insert(definition.symbol.symbol_id());
let maybe_doc = self.doc_for_maybe_node(
definition.module,
definition.symbol,
Expand All @@ -929,18 +933,17 @@ impl<'a> DocParser<'a> {
}

let is_ambient = exports.is_empty() && !module_has_import(module_symbol);
if is_ambient || self.private {
let mut handled_symbols =
exports.values().map(|n| n.1).collect::<HashSet<_>>();
for child_id in module_symbol.child_decls() {
if !handled_symbols.insert(child_id) {
continue; // already handled
}
let child_symbol = module_symbol.symbol(child_id).unwrap();
for child_id in module_symbol.child_decls() {
if !handled_symbols.insert(child_id) {
continue; // already handled
}
let child_symbol = module_symbol.symbol(child_id).unwrap();
if child_symbol.is_public() || is_ambient || self.private {
for decl in child_symbol.decls() {
if let Some(node) = decl.maybe_node() {
let is_declared = self.get_declare_for_symbol_node(node);
if is_declared || self.private {
let is_declared =
is_ambient && self.get_declare_for_symbol_node(node);
if child_symbol.is_public() || is_declared || self.private {
if let Some(mut doc_node) = self.get_doc_for_symbol_node_ref(
module_symbol,
child_symbol,
Expand All @@ -949,7 +952,6 @@ impl<'a> DocParser<'a> {
doc_node.declaration_kind = if is_declared {
DeclarationKind::Declare
} else {
debug_assert!(self.private);
DeclarationKind::Private
};
doc_nodes.push(doc_node);
Expand Down
222 changes: 143 additions & 79 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3450,7 +3450,6 @@ export function foo(a: string, b?: number, cb: (...cbArgs: unknown[]) => void, .
r#"
interface AssignOpts {
a: string;
b: number;
}
export function foo([e,,f, ...g]: number[], { c, d: asdf, i = "asdf", ...rest}, ops: AssignOpts = {}): void {
Expand Down Expand Up @@ -3568,18 +3567,49 @@ export function foo([e,,f, ...g]: number[], { c, d: asdf, i = "asdf", ...rest},
"location": {
"col": 0,
"filename": "file:///test.ts",
"line": 7,
"line": 6,
},
"name": "foo",
}, {
"kind": "interface",
"name": "AssignOpts",
"location": {
"col": 0,
"filename": "file:///test.ts",
"line": 2,
},
"declarationKind": "private",
"interfaceDef": {
"extends": [],
"methods": [],
"properties": [{
"name": "a",
"location": {
"filename": "file:///test.ts",
"line": 3,
"col": 2,
},
"params": [],
"computed": false,
"optional": false,
"tsType": {
"repr": "string",
"kind": "keyword",
"keyword": "string",
},
"typeParams": [],
}],
"callSignatures": [],
"indexSignatures": [],
"typeParams": [],
}
}]);

json_test!(export_interface,
r#"
r#"
interface Foo {
foo(): void;
}
interface Bar {
bar(): void;
}
/**
* Interface js doc
Expand All @@ -3589,92 +3619,126 @@ export interface Reader extends Foo, Bar {
read?(buf: Uint8Array, something: unknown): Promise<number>
}
"#;
[{
"kind": "interface",
"name": "Reader",
"location": {
"filename": "file:///test.ts",
"line": 11,
"col": 0
},
"declarationKind": "export",
"jsDoc": {
"doc": "Interface js doc",
},
"interfaceDef": {
"extends": [
{
"repr": "Foo",
"kind": "typeRef",
"typeRef": {
"typeParams": null,
"typeName": "Foo"
}
[{
"kind": "interface",
"name": "Reader",
"location": {
"filename": "file:///test.ts",
"line": 9,
"col": 0
},
{
"repr": "Bar",
"kind": "typeRef",
"typeRef": {
"typeParams": null,
"typeName": "Bar"
}
}
],
"methods": [
{
"name": "read",
"kind": "method",
"location": {
"filename": "file:///test.ts",
"line": 13,
"col": 4
},
"optional": true,
"jsDoc": {
"doc": "Read n bytes",
},
"params": [
"declarationKind": "export",
"jsDoc": {
"doc": "Interface js doc",
},
"interfaceDef": {
"extends": [
{
"name": "buf",
"kind": "identifier",
"optional": false,
"tsType": {
"repr": "Uint8Array",
"kind": "typeRef",
"typeRef": {
"typeParams": null,
"typeName": "Uint8Array"
}
"repr": "Foo",
"kind": "typeRef",
"typeRef": {
"typeParams": null,
"typeName": "Foo"
}
},
{
"name": "something",
"kind": "identifier",
"optional": false,
"tsType": {
"repr": "unknown",
"kind": "keyword",
"keyword": "unknown"
"repr": "Bar",
"kind": "typeRef",
"typeRef": {
"typeParams": null,
"typeName": "Bar"
}
}
],
"typeParams": [],
"returnType": {
"repr": "Promise",
"kind": "typeRef",
"typeRef": {
"typeParams": [
"methods": [
{
"name": "read",
"kind": "method",
"location": {
"filename": "file:///test.ts",
"line": 11,
"col": 4
},
"optional": true,
"jsDoc": {
"doc": "Read n bytes",
},
"params": [
{
"repr": "number",
"kind": "keyword",
"keyword": "number"
"name": "buf",
"kind": "identifier",
"optional": false,
"tsType": {
"repr": "Uint8Array",
"kind": "typeRef",
"typeRef": {
"typeParams": null,
"typeName": "Uint8Array"
}
}
},
{
"name": "something",
"kind": "identifier",
"optional": false,
"tsType": {
"repr": "unknown",
"kind": "keyword",
"keyword": "unknown"
}
}
],
"typeName": "Promise"
"typeParams": [],
"returnType": {
"repr": "Promise",
"kind": "typeRef",
"typeRef": {
"typeParams": [
{
"repr": "number",
"kind": "keyword",
"keyword": "number"
}
],
"typeName": "Promise"
}
}
}
}
}
],
],
"properties": [],
"callSignatures": [],
"indexSignatures": [],
"typeParams": [],
}
}, {
"kind": "interface",
"name": "Foo",
"location": {
"filename": "file:///test.ts",
"line": 2,
"col": 0
},
"declarationKind": "private",
"interfaceDef": {
"extends": [],
"methods": [],
"properties": [],
"callSignatures": [],
"indexSignatures": [],
"typeParams": [],
}
}, {
"kind": "interface",
"name": "Bar",
"location": {
"filename": "file:///test.ts",
"line": 4,
"col": 0
},
"declarationKind": "private",
"interfaceDef": {
"extends": [],
"methods": [],
"properties": [],
"callSignatures": [],
"indexSignatures": [],
Expand Down

0 comments on commit 62adc5f

Please sign in to comment.