Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: display non-exported types referenced in exported types #361

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Deno.test({
const entries = await doc(
"https://deno.land/[email protected]/fmt/colors.ts",
);
assertEquals(entries.length, 48);
assertEquals(entries.length, 49);
Copy link
Member Author

@dsherret dsherret Oct 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rgb type is used in a parameter, but not exported: https://deno.land/[email protected]/fmt/colors.ts?source=

const fnStripColor = entries.find((n) =>
n.kind === "function" && n.name === "stripColor"
);
Expand Down
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