UNPKG

docusaurus-plugin-typedoc-api

Version:

Docusaurus plugin that provides source code API documentation powered by TypeDoc.

456 lines (447 loc) 16.7 kB
'use strict'; const react = require('react'); const Link = require('@docusaurus/Link'); const useReflectionMap = require('../hooks/useReflectionMap.js'); const MemberSignatureTitle = require('./MemberSignatureTitle.js'); const jsxRuntime = require('react/jsx-runtime'); const _interopDefault = e => e && e.__esModule ? e : { default: e }; const Link__default = /*#__PURE__*/_interopDefault(Link); /* eslint-disable react/no-array-index-key */ function extractDeclarationFromType(type) { if (!type) { return undefined; } return type?.declaration; } function parens(element, needsParens) { if (!needsParens) { return element; } return /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [needsParens && /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: "(" }), element, needsParens && /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: ")" })] }); } // eslint-disable-next-line complexity function Type({ needsParens = false, type: base }) { const reflections = useReflectionMap.useReflectionMap(); if (!base) { return null; } // Cast to string since `type` doesnt include all string values in the union. // https://github.com/TypeStrong/typedoc/blob/master/src/lib/output/themes/default/partials/type.tsx switch (String(base.type)) { case 'array': { const type = base; return /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [/*#__PURE__*/jsxRuntime.jsx(Type, { needsParens: true, type: type.elementType }), /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: "[]" })] }); } case 'conditional': { const type = base; return parens( /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [/*#__PURE__*/jsxRuntime.jsx(Type, { needsParens: true, type: type.checkType }), /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: " extends " }), /*#__PURE__*/jsxRuntime.jsx(Type, { type: type.extendsType }), /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: " ? " }), /*#__PURE__*/jsxRuntime.jsx(Type, { type: type.trueType }), /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: " : " }), /*#__PURE__*/jsxRuntime.jsx(Type, { type: type.falseType })] }), needsParens); } case 'indexedAccess': { const type = base; return /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [/*#__PURE__*/jsxRuntime.jsx(Type, { type: type.objectType }), /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: "[" }), /*#__PURE__*/jsxRuntime.jsx(Type, { type: type.indexType }), /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: "]" })] }); } case 'inferred': { const type = base; return /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [/*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: "infer " }), " ", type.name] }); } case 'intersection': { const type = base; return parens( /*#__PURE__*/jsxRuntime.jsx(jsxRuntime.Fragment, { children: type.types.map((t, i) => /*#__PURE__*/jsxRuntime.jsxs(react.Fragment, { children: [i > 0 && /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: " & " }), /*#__PURE__*/jsxRuntime.jsx(Type, { needsParens: true, type: t })] }, t.type + i)) }), needsParens); } case 'intrinsic': { const type = base; return /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-type", children: type.name }); } case 'literal': { const type = base; return /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-type", children: String(type.value) }); } case 'mapped': { const type = base; return /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [/*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: '{ ' }), type.readonlyModifier === '+' && /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: "readonly " }), type.readonlyModifier === '-' && /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: "-readonly " }), /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: "[ " }), /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-type", children: type.parameter }), /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: " in " }), /*#__PURE__*/jsxRuntime.jsx(Type, { type: type.parameterType }), type.nameType && /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [/*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: " as " }), /*#__PURE__*/jsxRuntime.jsx(Type, { type: type.nameType })] }), /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: " ]" }), type.optionalModifier === '+' && /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: "?: " }), type.optionalModifier === '-' && /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: "-?: " }), !type.optionalModifier && /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: ": " }), /*#__PURE__*/jsxRuntime.jsx(Type, { type: type.templateType }), /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: ' }' })] }); } case 'optional': { const type = base; return /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [/*#__PURE__*/jsxRuntime.jsx(Type, { type: type.elementType }), /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: "?" })] }); } case 'predicate': { const type = base; return /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [type.asserts && /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: "asserts " }), /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-type", children: type.name }), type.targetType && /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [/*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: " is " }), /*#__PURE__*/jsxRuntime.jsx(Type, { type: type.targetType })] })] }); } case 'query': { const type = base; return /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [/*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: "typeof " }), /*#__PURE__*/jsxRuntime.jsx(Type, { type: type.queryType })] }); } case 'reference': { const type = base; const ref = type.target ? reflections[Number(type.target)] : null; const genericClass = ref?.id && !ref.sources ? 'tsd-signature-type-generic' : ''; return /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [ref?.permalink ? /*#__PURE__*/jsxRuntime.jsx(Link__default.default, { className: `tsd-signature-type ${genericClass}`, "data-tsd-kind": ref.kind, to: ref.permalink, children: type.name }) : /*#__PURE__*/jsxRuntime.jsx("span", { className: `tsd-signature-type ${genericClass}`, children: type.name }), type.typeArguments && type.typeArguments.length > 0 && /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [/*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: "<" }), type.typeArguments.map((t, i) => /*#__PURE__*/jsxRuntime.jsxs(react.Fragment, { children: [i > 0 && /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: ", " }), /*#__PURE__*/jsxRuntime.jsx(Type, { type: t })] }, t.type + i)), /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: ">" })] })] }); } case 'reflection': { const type = base; const decl = type.declaration; // object literal if (decl?.children && decl.children.length > 0) { return /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [/*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: '{ ' }), decl.children.map((child, i) => /*#__PURE__*/jsxRuntime.jsxs(react.Fragment, { children: [i > 0 && /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: "; " }), /*#__PURE__*/jsxRuntime.jsxs("span", { children: [child.name, /*#__PURE__*/jsxRuntime.jsxs("span", { className: "tsd-signature-symbol", children: [child.flags?.isOptional && '?', ": "] }), child.type ? /*#__PURE__*/jsxRuntime.jsx(Type, { type: child.type }) : 'any'] })] }, child.id ?? i)), /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: ' }' })] }); } if (decl?.signatures && decl.signatures.length === 1) { return /*#__PURE__*/jsxRuntime.jsx(MemberSignatureTitle.MemberSignatureTitle, { hideName: true, useArrow: true, sig: decl.signatures[0] }); } if (decl?.signatures && decl.signatures.length > 0) { return parens( /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [/*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: '{ ' }), decl.signatures.map((sig, i) => /*#__PURE__*/jsxRuntime.jsxs(react.Fragment, { children: [i > 0 && /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: "; " }), /*#__PURE__*/jsxRuntime.jsx(MemberSignatureTitle.MemberSignatureTitle, { sig: sig })] }, sig.id ?? i)), /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: ' }' })] }), needsParens); } return /*#__PURE__*/jsxRuntime.jsx(jsxRuntime.Fragment, { children: '{}' }); } case 'rest': { const type = base; return /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [/*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: "..." }), /*#__PURE__*/jsxRuntime.jsx(Type, { type: type.elementType })] }); } case 'tuple': { const type = base; return /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [/*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: "[" }), type.elements?.map((t, i) => /*#__PURE__*/jsxRuntime.jsxs(react.Fragment, { children: [i > 0 && /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: ", " }), /*#__PURE__*/jsxRuntime.jsx(Type, { type: t })] }, t.type + i)), /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: "]" })] }); } case 'typeOperator': { const type = base; return /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [/*#__PURE__*/jsxRuntime.jsxs("span", { className: "tsd-signature-symbol", children: [type.operator, " "] }), /*#__PURE__*/jsxRuntime.jsx(Type, { type: type.target })] }); } // case 'typeParameter': { // const type = base as JSONOutput.TypeParameterType; // return <span className="tsd-signature-type">{type.name}</span>; // } case 'union': { const type = base; return parens( /*#__PURE__*/jsxRuntime.jsx(jsxRuntime.Fragment, { children: type.types.map((t, i) => /*#__PURE__*/jsxRuntime.jsxs(react.Fragment, { children: [i > 0 && /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: " | " }), /*#__PURE__*/jsxRuntime.jsx(Type, { needsParens: true, type: t })] }, t.type + i)) }), needsParens); } case 'unknown': { const type = base; return /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-type", children: type.name }); } case 'named-tuple-member': case 'namedTupleMember': { const type = base; return /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [type.name, /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: type.isOptional ? '?: ' : ': ' }), /*#__PURE__*/jsxRuntime.jsx(Type, { type: type.element })] }); } case 'template-literal': case 'templateLiteral': { const type = base; return /*#__PURE__*/jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [/*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: "`" }), type.head && /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-type", children: type.head }), type.tail.map((t, i) => /*#__PURE__*/jsxRuntime.jsxs(react.Fragment, { children: [/*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: '${' }), typeof t[0] !== 'string' && /*#__PURE__*/jsxRuntime.jsx(Type, { type: t[0] }), /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: '}' }), typeof t[1] === 'string' && /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-type", children: t[1] })] }, i)), /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-symbol", children: "`" })] }); } default: return /*#__PURE__*/jsxRuntime.jsx("span", { className: "tsd-signature-type", children: "void" }); } } exports.Type = Type; exports.extractDeclarationFromType = extractDeclarationFromType; //# sourceMappingURL=Type.js.map