From 4a539ddb3b17b53642b3981864ab3cd5a498d6fe Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Sat, 23 Jan 2021 12:19:13 -0700 Subject: [PATCH] fix: CommonJS export= with type exports Resolves #1476 --- src/lib/converter/converter.ts | 12 +- src/test/converter/js/export-eq-type.js | 6 + src/test/converter/js/specs.json | 980 +++++++++++++----------- 3 files changed, 550 insertions(+), 448 deletions(-) create mode 100644 src/test/converter/js/export-eq-type.js diff --git a/src/lib/converter/converter.ts b/src/lib/converter/converter.ts index 3d4837370..b84d26b5c 100644 --- a/src/lib/converter/converter.ts +++ b/src/lib/converter/converter.ts @@ -449,17 +449,21 @@ function getExports( node: ts.SourceFile | ts.ModuleBlock, symbol: ts.Symbol | undefined ): ts.Symbol[] { + const exports: ts.Symbol[] = []; + // The generated docs aren't great, but you really ought not be using // this in the first place... so it's better than nothing. const exportEq = symbol?.exports?.get("export=" as ts.__String); if (exportEq) { - return [exportEq]; + exports.push(exportEq); } if (symbol) { - return context.checker - .getExportsOfModule(symbol) - .filter((s) => !hasFlag(s.flags, ts.SymbolFlags.Prototype)); + return exports.concat( + context.checker + .getExportsOfModule(symbol) + .filter((s) => !hasFlag(s.flags, ts.SymbolFlags.Prototype)) + ); } // Global file with no inferred top level symbol, get all symbols declared in this file. diff --git a/src/test/converter/js/export-eq-type.js b/src/test/converter/js/export-eq-type.js new file mode 100644 index 000000000..3bf3ea400 --- /dev/null +++ b/src/test/converter/js/export-eq-type.js @@ -0,0 +1,6 @@ +/** @typedef {string} Foo */ + +/** @param {Foo} x */ +const foo = (x) => x; + +module.exports = foo; diff --git a/src/test/converter/js/specs.json b/src/test/converter/js/specs.json index 7577b2dcc..f8e8648cc 100644 --- a/src/test/converter/js/specs.json +++ b/src/test/converter/js/specs.json @@ -2,322 +2,571 @@ "id": 0, "name": "typedoc", "kind": 0, - "kindString": "Project", "flags": {}, - "comment": {}, "children": [ { - "id": 11, - "name": "AlsoInterfaceIsh", - "kind": 256, - "kindString": "Interface", + "id": 1, + "name": "export-eq-type", + "kind": 1, + "kindString": "Module", "flags": {}, - "comment": { - "shortText": "docs for interface" - }, + "comment": {}, "children": [ { - "id": 13, - "name": "bar", - "kind": 1024, - "kindString": "Property", + "id": 5, + "name": "Foo", + "kind": 4194304, + "kindString": "Type alias", "flags": {}, - "comment": { - "shortText": "can also use prop tag" - }, "type": { "type": "intrinsic", "name": "string" } }, { - "id": 12, - "name": "foo", - "kind": 1024, - "kindString": "Property", - "flags": {}, - "comment": { - "shortText": "docs for property" + "id": 2, + "name": "export=", + "kind": 64, + "kindString": "Function", + "flags": { + "isConst": true }, - "type": { - "type": "intrinsic", - "name": "string" - } + "signatures": [ + { + "id": 3, + "name": "export=", + "kind": 4096, + "kindString": "Call signature", + "flags": {}, + "comment": {}, + "parameters": [ + { + "id": 4, + "name": "x", + "kind": 32768, + "kindString": "Parameter", + "flags": {}, + "comment": {}, + "type": { + "type": "intrinsic", + "name": "string" + } + } + ], + "type": { + "type": "intrinsic", + "name": "string" + } + } + ] } ], "groups": [ { - "title": "Properties", - "kind": 1024, + "title": "Type aliases", + "kind": 4194304, "children": [ - 13, - 12 + 5 + ] + }, + { + "title": "Functions", + "kind": 64, + "children": [ + 2 ] } ] }, { - "id": 9, - "name": "InterfaceIsh", - "kind": 256, - "kindString": "Interface", + "id": 6, + "name": "index", + "kind": 1, + "kindString": "Module", "flags": {}, + "comment": {}, "children": [ { - "id": 10, - "name": "foo", - "kind": 1024, - "kindString": "Property", + "id": 17, + "name": "AlsoInterfaceIsh", + "kind": 256, + "kindString": "Interface", "flags": {}, "comment": { - "shortText": "docs for property\nmore docs for property" + "shortText": "docs for interface" }, - "type": { - "type": "intrinsic", - "name": "string" - } - } - ], - "groups": [ - { - "title": "Properties", - "kind": 1024, "children": [ - 10 - ] - } - ] - }, - { - "id": 8, - "name": "ColumnType", - "kind": 4194304, - "kindString": "Type alias", - "flags": {}, - "type": { - "type": "intrinsic", - "name": "string" - } - }, - { - "id": 38, - "name": "Foo", - "kind": 4194304, - "kindString": "Type alias", - "flags": {}, - "type": { - "type": "reflection", - "declaration": { - "id": 39, - "name": "__type", - "kind": 65536, - "kindString": "Type literal", - "flags": {}, - "signatures": [ { - "id": 40, - "name": "__type", - "kind": 4096, - "kindString": "Call signature", + "id": 19, + "name": "bar", + "kind": 1024, + "kindString": "Property", "flags": {}, - "parameters": [ - { - "id": 41, - "name": "args", - "kind": 32768, - "kindString": "Parameter", - "flags": { - "isRest": true - }, - "type": { - "type": "array", - "elementType": { - "type": "intrinsic", - "name": "string" - } - } - } - ], + "comment": { + "shortText": "can also use prop tag" + }, "type": { "type": "intrinsic", - "name": "number" + "name": "string" + } + }, + { + "id": 18, + "name": "foo", + "kind": 1024, + "kindString": "Property", + "flags": {}, + "comment": { + "shortText": "docs for property" + }, + "type": { + "type": "intrinsic", + "name": "string" } } + ], + "groups": [ + { + "title": "Properties", + "kind": 1024, + "children": [ + 19, + 18 + ] + } ] - } - } - }, - { - "id": 24, - "name": "HasReturnTag", - "kind": 4194304, - "kindString": "Type alias", - "flags": {}, - "type": { - "type": "reflection", - "declaration": { - "id": 25, - "name": "__type", - "kind": 65536, - "kindString": "Type literal", + }, + { + "id": 15, + "name": "InterfaceIsh", + "kind": 256, + "kindString": "Interface", "flags": {}, - "signatures": [ + "children": [ { - "id": 26, - "name": "__type", - "kind": 4096, - "kindString": "Call signature", + "id": 16, + "name": "foo", + "kind": 1024, + "kindString": "Property", "flags": {}, + "comment": { + "shortText": "docs for property\nmore docs for property" + }, "type": { "type": "intrinsic", "name": "string" } } + ], + "groups": [ + { + "title": "Properties", + "kind": 1024, + "children": [ + 16 + ] + } ] - } - } - }, - { - "id": 36, - "name": "Identity", - "kind": 4194304, - "kindString": "Type alias", - "flags": {}, - "comment": { - "shortText": "comment on alias" - }, - "typeParameter": [ + }, + { + "id": 14, + "name": "ColumnType", + "kind": 4194304, + "kindString": "Type alias", + "flags": {}, + "type": { + "type": "intrinsic", + "name": "string" + } + }, { - "id": 37, - "name": "T", - "kind": 131072, - "kindString": "Type parameter", + "id": 44, + "name": "Foo", + "kind": 4194304, + "kindString": "Type alias", + "flags": {}, + "type": { + "type": "reflection", + "declaration": { + "id": 45, + "name": "__type", + "kind": 65536, + "kindString": "Type literal", + "flags": {}, + "signatures": [ + { + "id": 46, + "name": "__type", + "kind": 4096, + "kindString": "Call signature", + "flags": {}, + "parameters": [ + { + "id": 47, + "name": "args", + "kind": 32768, + "kindString": "Parameter", + "flags": { + "isRest": true + }, + "type": { + "type": "array", + "elementType": { + "type": "intrinsic", + "name": "string" + } + } + } + ], + "type": { + "type": "intrinsic", + "name": "number" + } + } + ] + } + } + }, + { + "id": 30, + "name": "HasReturnTag", + "kind": 4194304, + "kindString": "Type alias", + "flags": {}, + "type": { + "type": "reflection", + "declaration": { + "id": 31, + "name": "__type", + "kind": 65536, + "kindString": "Type literal", + "flags": {}, + "signatures": [ + { + "id": 32, + "name": "__type", + "kind": 4096, + "kindString": "Call signature", + "flags": {}, + "type": { + "type": "intrinsic", + "name": "string" + } + } + ] + } + } + }, + { + "id": 42, + "name": "Identity", + "kind": 4194304, + "kindString": "Type alias", "flags": {}, "comment": { - "shortText": "comment on template" + "shortText": "comment on alias" + }, + "typeParameter": [ + { + "id": 43, + "name": "T", + "kind": 131072, + "kindString": "Type parameter", + "flags": {}, + "comment": { + "shortText": "comment on template" + } + } + ], + "type": { + "type": "reference", + "name": "T" } - } - ], - "type": { - "type": "reference", - "name": "T" - } - }, - { - "id": 27, - "name": "IdentityFn", - "kind": 4194304, - "kindString": "Type alias", - "flags": {}, - "typeParameter": [ + }, { - "id": 31, - "name": "T", - "kind": 131072, - "kindString": "Type parameter", - "flags": {} - } - ], - "type": { - "type": "reflection", - "declaration": { - "id": 28, - "name": "__type", - "kind": 65536, - "kindString": "Type literal", + "id": 33, + "name": "IdentityFn", + "kind": 4194304, + "kindString": "Type alias", "flags": {}, - "signatures": [ + "typeParameter": [ { - "id": 29, + "id": 37, + "name": "T", + "kind": 131072, + "kindString": "Type parameter", + "flags": {} + } + ], + "type": { + "type": "reflection", + "declaration": { + "id": 34, "name": "__type", - "kind": 4096, - "kindString": "Call signature", + "kind": 65536, + "kindString": "Type literal", "flags": {}, - "parameters": [ + "signatures": [ { - "id": 30, - "name": "data", - "kind": 32768, - "kindString": "Parameter", + "id": 35, + "name": "__type", + "kind": 4096, + "kindString": "Call signature", "flags": {}, + "parameters": [ + { + "id": 36, + "name": "data", + "kind": 32768, + "kindString": "Parameter", + "flags": {}, + "type": { + "type": "reference", + "name": "T" + } + } + ], "type": { "type": "reference", "name": "T" } } - ], - "type": { - "type": "reference", - "name": "T" - } + ] } - ] - } - } - }, - { - "id": 16, - "name": "IntersectionType", - "kind": 4194304, - "kindString": "Type alias", - "flags": {}, - "comment": { - "shortText": "docs for alias" - }, - "type": { - "type": "intersection", - "types": [ - { + } + }, + { + "id": 22, + "name": "IntersectionType", + "kind": 4194304, + "kindString": "Type alias", + "flags": {}, + "comment": { + "shortText": "docs for alias" + }, + "type": { + "type": "intersection", + "types": [ + { + "type": "reflection", + "declaration": { + "id": 23, + "name": "__type", + "kind": 65536, + "kindString": "Type literal", + "flags": {}, + "children": [ + { + "id": 24, + "name": "x", + "kind": 1024, + "kindString": "Property", + "flags": {}, + "type": { + "type": "intrinsic", + "name": "string" + } + } + ], + "groups": [ + { + "title": "Properties", + "kind": 1024, + "children": [ + 24 + ] + } + ] + } + }, + { + "type": "reflection", + "declaration": { + "id": 25, + "name": "__type", + "kind": 65536, + "kindString": "Type literal", + "flags": {}, + "children": [ + { + "id": 26, + "name": "y", + "kind": 1024, + "kindString": "Property", + "flags": {}, + "type": { + "type": "intrinsic", + "name": "number" + } + } + ], + "groups": [ + { + "title": "Properties", + "kind": 1024, + "children": [ + 26 + ] + } + ] + } + } + ] + } + }, + { + "id": 27, + "name": "NoReturnTag", + "kind": 4194304, + "kindString": "Type alias", + "flags": {}, + "type": { "type": "reflection", "declaration": { - "id": 17, + "id": 28, "name": "__type", "kind": 65536, "kindString": "Type literal", "flags": {}, - "children": [ + "signatures": [ { - "id": 18, - "name": "x", - "kind": 1024, - "kindString": "Property", + "id": 29, + "name": "__type", + "kind": 4096, + "kindString": "Call signature", "flags": {}, "type": { "type": "intrinsic", - "name": "string" + "name": "any" } } - ], - "groups": [ + ] + } + } + }, + { + "id": 20, + "name": "ObjectAlias", + "kind": 4194304, + "kindString": "Type alias", + "flags": {}, + "comment": { + "shortText": "type alias since it doesn't have a property tag" + }, + "type": { + "type": "reference", + "name": "Object" + } + }, + { + "id": 38, + "name": "OptionalArg", + "kind": 4194304, + "kindString": "Type alias", + "flags": {}, + "type": { + "type": "reflection", + "declaration": { + "id": 39, + "name": "__type", + "kind": 65536, + "kindString": "Type literal", + "flags": {}, + "signatures": [ { - "title": "Properties", - "kind": 1024, - "children": [ - 18 - ] + "id": 40, + "name": "__type", + "kind": 4096, + "kindString": "Call signature", + "flags": {}, + "parameters": [ + { + "id": 41, + "name": "data", + "kind": 32768, + "kindString": "Parameter", + "flags": { + "isOptional": true + }, + "type": { + "type": "intrinsic", + "name": "string" + } + } + ], + "type": { + "type": "intrinsic", + "name": "any" + } } ] } + } + }, + { + "id": 21, + "name": "UnionType", + "kind": 4194304, + "kindString": "Type alias", + "flags": {}, + "comment": { + "shortText": "docs for alias" + }, + "type": { + "type": "union", + "types": [ + { + "type": "intrinsic", + "name": "string" + }, + { + "type": "intrinsic", + "name": "number" + } + ] + } + }, + { + "id": 10, + "name": "ColumnType", + "kind": 32, + "kindString": "Variable", + "flags": { + "isConst": true }, - { + "comment": {}, + "type": { "type": "reflection", "declaration": { - "id": 19, + "id": 11, "name": "__type", "kind": 65536, "kindString": "Type literal", "flags": {}, "children": [ { - "id": 20, - "name": "y", + "id": 13, + "name": "NUMBER", "kind": 1024, "kindString": "Property", "flags": {}, "type": { "type": "intrinsic", - "name": "number" - } + "name": "string" + }, + "defaultValue": "\"number\"" + }, + { + "id": 12, + "name": "STRING", + "kind": 1024, + "kindString": "Property", + "flags": {}, + "type": { + "type": "intrinsic", + "name": "string" + }, + "defaultValue": "\"string\"" } ], "groups": [ @@ -325,263 +574,106 @@ "title": "Properties", "kind": 1024, "children": [ - 20 + 13, + 12 ] } ] } - } - ] - } - }, - { - "id": 21, - "name": "NoReturnTag", - "kind": 4194304, - "kindString": "Type alias", - "flags": {}, - "type": { - "type": "reflection", - "declaration": { - "id": 22, - "name": "__type", - "kind": 65536, - "kindString": "Type literal", - "flags": {}, - "signatures": [ - { - "id": 23, - "name": "__type", - "kind": 4096, - "kindString": "Call signature", - "flags": {}, - "type": { - "type": "intrinsic", - "name": "any" - } - } - ] - } - } - }, - { - "id": 14, - "name": "ObjectAlias", - "kind": 4194304, - "kindString": "Type alias", - "flags": {}, - "comment": { - "shortText": "type alias since it doesn't have a property tag" - }, - "type": { - "type": "reference", - "name": "Object" - } - }, - { - "id": 32, - "name": "OptionalArg", - "kind": 4194304, - "kindString": "Type alias", - "flags": {}, - "type": { - "type": "reflection", - "declaration": { - "id": 33, - "name": "__type", - "kind": 65536, - "kindString": "Type literal", - "flags": {}, + }, + "defaultValue": "..." + }, + { + "id": 7, + "name": "usedFoo", + "kind": 64, + "kindString": "Function", + "flags": { + "isConst": true + }, "signatures": [ { - "id": 34, - "name": "__type", + "id": 8, + "name": "usedFoo", "kind": 4096, "kindString": "Call signature", "flags": {}, + "comment": {}, "parameters": [ { - "id": 35, - "name": "data", + "id": 9, + "name": "args", "kind": 32768, "kindString": "Parameter", "flags": { - "isOptional": true + "isRest": true }, "type": { - "type": "intrinsic", - "name": "string" + "type": "array", + "elementType": { + "type": "intrinsic", + "name": "string" + } } } ], "type": { "type": "intrinsic", - "name": "any" + "name": "number" } } ] } - } - }, - { - "id": 15, - "name": "UnionType", - "kind": 4194304, - "kindString": "Type alias", - "flags": {}, - "comment": { - "shortText": "docs for alias" - }, - "type": { - "type": "union", - "types": [ - { - "type": "intrinsic", - "name": "string" - }, - { - "type": "intrinsic", - "name": "number" - } - ] - } - }, - { - "id": 4, - "name": "ColumnType", - "kind": 32, - "kindString": "Variable", - "flags": { - "isConst": true - }, - "comment": {}, - "type": { - "type": "reflection", - "declaration": { - "id": 5, - "name": "__type", - "kind": 65536, - "kindString": "Type literal", - "flags": {}, + ], + "groups": [ + { + "title": "Interfaces", + "kind": 256, "children": [ - { - "id": 7, - "name": "NUMBER", - "kind": 1024, - "kindString": "Property", - "flags": {}, - "type": { - "type": "intrinsic", - "name": "string" - }, - "defaultValue": "\"number\"" - }, - { - "id": 6, - "name": "STRING", - "kind": 1024, - "kindString": "Property", - "flags": {}, - "type": { - "type": "intrinsic", - "name": "string" - }, - "defaultValue": "\"string\"" - } - ], - "groups": [ - { - "title": "Properties", - "kind": 1024, - "children": [ - 7, - 6 - ] - } + 17, + 15 ] - } - }, - "defaultValue": "..." - }, - { - "id": 1, - "name": "usedFoo", - "kind": 64, - "kindString": "Function", - "flags": { - "isConst": true - }, - "signatures": [ + }, { - "id": 2, - "name": "usedFoo", - "kind": 4096, - "kindString": "Call signature", - "flags": {}, - "comment": {}, - "parameters": [ - { - "id": 3, - "name": "args", - "kind": 32768, - "kindString": "Parameter", - "flags": { - "isRest": true - }, - "type": { - "type": "array", - "elementType": { - "type": "intrinsic", - "name": "string" - } - } - } - ], - "type": { - "type": "intrinsic", - "name": "number" - } + "title": "Type aliases", + "kind": 4194304, + "children": [ + 14, + 44, + 30, + 42, + 33, + 22, + 27, + 20, + 38, + 21 + ] + }, + { + "title": "Variables", + "kind": 32, + "children": [ + 10 + ] + }, + { + "title": "Functions", + "kind": 64, + "children": [ + 7 + ] } ] } ], "groups": [ { - "title": "Interfaces", - "kind": 256, - "children": [ - 11, - 9 - ] - }, - { - "title": "Type aliases", - "kind": 4194304, - "children": [ - 8, - 38, - 24, - 36, - 27, - 16, - 21, - 14, - 32, - 15 - ] - }, - { - "title": "Variables", - "kind": 32, - "children": [ - 4 - ] - }, - { - "title": "Functions", - "kind": 64, + "title": "Modules", + "kind": 1, "children": [ - 1 + 1, + 6 ] } ]