From 47ed2963c4059517a9aebf5296e96e077b288444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Kr=C3=B6g?= Date: Thu, 27 Feb 2020 22:50:03 +0100 Subject: [PATCH] Default exported classes have same prettyname as modules --- config/jsdoc/api/plugins/api.js | 23 +++++++++++++++++++++++ config/jsdoc/api/template/publish.js | 20 ++++++++------------ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/config/jsdoc/api/plugins/api.js b/config/jsdoc/api/plugins/api.js index 0ebda4f356..b5ff2190ab 100644 --- a/config/jsdoc/api/plugins/api.js +++ b/config/jsdoc/api/plugins/api.js @@ -106,6 +106,20 @@ function includeTypes(doclet) { } } +const defaultExports = {}; +const path = require('path'); +const moduleRoot = path.join(process.cwd(), 'src'); + +// Tag default exported Identifiers because their name should be the same as the module name. +exports.astNodeVisitor = { + visitNode: function(node, e, parser, currentSourceName) { + if (node.type === 'Identifier' && node.parent.type === 'ExportDefaultDeclaration') { + const modulePath = path.relative(moduleRoot, currentSourceName).replace(/\.js$/, ''); + defaultExports['module:' + modulePath + '~' + node.name] = true; + } + } +}; + exports.handlers = { newDoclet: function(e) { @@ -169,6 +183,15 @@ exports.handlers = { delete doclet.undocumented; } } + }, + + processingComplete(e) { + const byLongname = e.doclets.index.longname; + for (const name in defaultExports) { + byLongname[name].forEach(function(doclet) { + doclet.isDefaultExport = true; + }); + } } }; diff --git a/config/jsdoc/api/template/publish.js b/config/jsdoc/api/template/publish.js index d8222784dc..3ac0e65570 100644 --- a/config/jsdoc/api/template/publish.js +++ b/config/jsdoc/api/template/publish.js @@ -188,14 +188,10 @@ function attachModuleSymbols(doclets, modules) { }); } -function getPrettyName(longname) { - const fullname = longname.replace('module:', ''); - const parts = fullname.split(/[~\.]/); - if (parts.length > 1) { - const pathParts = parts[0].split('/'); - if (parts[parts.length - 1] === pathParts[pathParts.length - 1]) { - return parts[0]; - } +function getPrettyName(doclet) { + const fullname = doclet.longname.replace('module:', ''); + if (doclet.isDefaultExport) { + return fullname.split('~')[0]; } return fullname; } @@ -218,8 +214,8 @@ function buildNav(members) { // merge namespaces and classes, then sort const merged = members.modules.concat(members.classes); merged.sort(function(a, b) { - const prettyNameA = getPrettyName(a.longname).toLowerCase(); - const prettyNameB = getPrettyName(b.longname).toLowerCase(); + const prettyNameA = getPrettyName(a).toLowerCase(); + const prettyNameB = getPrettyName(b).toLowerCase(); if (prettyNameA > prettyNameB) { return 1; } @@ -235,7 +231,7 @@ function buildNav(members) { nav.push({ type: 'class', longname: v.longname, - prettyname: getPrettyName(v.longname), + prettyname: getPrettyName(v), name: v.name, module: find({ kind: 'module', @@ -286,7 +282,7 @@ function buildNav(members) { nav.push({ type: 'module', longname: v.longname, - prettyname: getPrettyName(v.longname), + prettyname: getPrettyName(v), name: v.name, members: members, methods: methods,