@player-tools/xlr-utils 0.4.0-next.0 → 0.4.0-next.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +192 -0
- package/dist/index.d.ts +14 -2
- package/dist/index.esm.js +191 -2
- package/package.json +2 -2
- package/src/documentation.ts +241 -0
- package/src/index.ts +1 -0
package/dist/index.cjs.js
CHANGED
|
@@ -562,10 +562,201 @@ function setupTestEnv(sourceCode, mockFileName = "filename.ts") {
|
|
|
562
562
|
};
|
|
563
563
|
}
|
|
564
564
|
|
|
565
|
+
function insertBetweenElements(array, separator) {
|
|
566
|
+
return array.reduce((acc, item, index) => {
|
|
567
|
+
if (index === 0) {
|
|
568
|
+
return [item];
|
|
569
|
+
}
|
|
570
|
+
return [...acc, separator, item];
|
|
571
|
+
}, []);
|
|
572
|
+
}
|
|
573
|
+
function createTSDocString(node) {
|
|
574
|
+
if (node.type === "ref") {
|
|
575
|
+
return [
|
|
576
|
+
{
|
|
577
|
+
text: node.ref,
|
|
578
|
+
kind: ts.SymbolDisplayPartKind.keyword
|
|
579
|
+
}
|
|
580
|
+
];
|
|
581
|
+
}
|
|
582
|
+
if (node.type === "or" || node.type === "and") {
|
|
583
|
+
const items = node.type === "and" ? node.and : node.or;
|
|
584
|
+
return insertBetweenElements(items.map((subnode) => createTSDocString(subnode)), [
|
|
585
|
+
{
|
|
586
|
+
kind: ts.SymbolDisplayPartKind.punctuation,
|
|
587
|
+
text: node.type === "and" ? " & " : " | "
|
|
588
|
+
}
|
|
589
|
+
]).flat();
|
|
590
|
+
}
|
|
591
|
+
if (node.type === "function") {
|
|
592
|
+
return [
|
|
593
|
+
{
|
|
594
|
+
kind: ts.SymbolDisplayPartKind.keyword,
|
|
595
|
+
text: "function"
|
|
596
|
+
},
|
|
597
|
+
{
|
|
598
|
+
kind: ts.SymbolDisplayPartKind.space,
|
|
599
|
+
text: " "
|
|
600
|
+
},
|
|
601
|
+
...node.name ? [{ text: node.name, kind: ts.SymbolDisplayPartKind.methodName }] : [],
|
|
602
|
+
{
|
|
603
|
+
kind: ts.SymbolDisplayPartKind.punctuation,
|
|
604
|
+
text: "("
|
|
605
|
+
},
|
|
606
|
+
...insertBetweenElements(node.parameters.map((p) => {
|
|
607
|
+
if (p.name) {
|
|
608
|
+
return [
|
|
609
|
+
{
|
|
610
|
+
kind: ts.SymbolDisplayPartKind.parameterName,
|
|
611
|
+
text: p.name
|
|
612
|
+
},
|
|
613
|
+
{
|
|
614
|
+
kind: ts.SymbolDisplayPartKind.punctuation,
|
|
615
|
+
text: p.optional ? "?" : ""
|
|
616
|
+
},
|
|
617
|
+
{
|
|
618
|
+
kind: ts.SymbolDisplayPartKind.punctuation,
|
|
619
|
+
text: ": "
|
|
620
|
+
},
|
|
621
|
+
...createTSDocString(p.type)
|
|
622
|
+
];
|
|
623
|
+
}
|
|
624
|
+
return createTSDocString(p.type);
|
|
625
|
+
}), [
|
|
626
|
+
{
|
|
627
|
+
kind: ts.SymbolDisplayPartKind.punctuation,
|
|
628
|
+
text: ", "
|
|
629
|
+
}
|
|
630
|
+
]).flat(),
|
|
631
|
+
{
|
|
632
|
+
kind: ts.SymbolDisplayPartKind.punctuation,
|
|
633
|
+
text: ")"
|
|
634
|
+
},
|
|
635
|
+
...node.returnType ? [
|
|
636
|
+
{
|
|
637
|
+
kind: ts.SymbolDisplayPartKind.punctuation,
|
|
638
|
+
text: ": "
|
|
639
|
+
},
|
|
640
|
+
...createTSDocString(node.returnType)
|
|
641
|
+
] : []
|
|
642
|
+
];
|
|
643
|
+
}
|
|
644
|
+
if (node.type === "tuple") {
|
|
645
|
+
return [
|
|
646
|
+
{
|
|
647
|
+
kind: ts.SymbolDisplayPartKind.punctuation,
|
|
648
|
+
text: "["
|
|
649
|
+
},
|
|
650
|
+
...insertBetweenElements(node.elementTypes.map((t) => {
|
|
651
|
+
if (t.name) {
|
|
652
|
+
return [
|
|
653
|
+
{
|
|
654
|
+
kind: ts.SymbolDisplayPartKind.propertyName,
|
|
655
|
+
text: t.name
|
|
656
|
+
},
|
|
657
|
+
{
|
|
658
|
+
kind: ts.SymbolDisplayPartKind.punctuation,
|
|
659
|
+
text: ": "
|
|
660
|
+
},
|
|
661
|
+
...createTSDocString(t.type)
|
|
662
|
+
];
|
|
663
|
+
}
|
|
664
|
+
return createTSDocString(t.type);
|
|
665
|
+
}), [
|
|
666
|
+
{
|
|
667
|
+
kind: ts.SymbolDisplayPartKind.punctuation,
|
|
668
|
+
text: ", "
|
|
669
|
+
}
|
|
670
|
+
]).flat(),
|
|
671
|
+
{
|
|
672
|
+
kind: ts.SymbolDisplayPartKind.punctuation,
|
|
673
|
+
text: "]"
|
|
674
|
+
}
|
|
675
|
+
];
|
|
676
|
+
}
|
|
677
|
+
if (node.type === "array") {
|
|
678
|
+
return [
|
|
679
|
+
{
|
|
680
|
+
kind: ts.SymbolDisplayPartKind.interfaceName,
|
|
681
|
+
text: "Array"
|
|
682
|
+
},
|
|
683
|
+
{
|
|
684
|
+
kind: ts.SymbolDisplayPartKind.punctuation,
|
|
685
|
+
text: "<"
|
|
686
|
+
},
|
|
687
|
+
...createTSDocString(node.elementType),
|
|
688
|
+
{
|
|
689
|
+
kind: ts.SymbolDisplayPartKind.punctuation,
|
|
690
|
+
text: ">"
|
|
691
|
+
}
|
|
692
|
+
];
|
|
693
|
+
}
|
|
694
|
+
if (node.type === "record") {
|
|
695
|
+
return [
|
|
696
|
+
{
|
|
697
|
+
kind: ts.SymbolDisplayPartKind.interfaceName,
|
|
698
|
+
text: "Record"
|
|
699
|
+
},
|
|
700
|
+
{
|
|
701
|
+
kind: ts.SymbolDisplayPartKind.punctuation,
|
|
702
|
+
text: "<"
|
|
703
|
+
},
|
|
704
|
+
...createTSDocString(node.keyType),
|
|
705
|
+
{
|
|
706
|
+
kind: ts.SymbolDisplayPartKind.punctuation,
|
|
707
|
+
text: ", "
|
|
708
|
+
},
|
|
709
|
+
...createTSDocString(node.valueType),
|
|
710
|
+
{
|
|
711
|
+
kind: ts.SymbolDisplayPartKind.punctuation,
|
|
712
|
+
text: ">"
|
|
713
|
+
}
|
|
714
|
+
];
|
|
715
|
+
}
|
|
716
|
+
if ((node.type === "string" || node.type === "boolean" || node.type === "number") && node.const !== void 0) {
|
|
717
|
+
return [
|
|
718
|
+
{
|
|
719
|
+
kind: ts.SymbolDisplayPartKind.keyword,
|
|
720
|
+
text: typeof node.const === "string" ? `'${node.const}'` : String(node.const)
|
|
721
|
+
}
|
|
722
|
+
];
|
|
723
|
+
}
|
|
724
|
+
if (isPrimitiveTypeNode(node) && node.type !== "null") {
|
|
725
|
+
return [
|
|
726
|
+
{
|
|
727
|
+
kind: ts.SymbolDisplayPartKind.keyword,
|
|
728
|
+
text: node.type
|
|
729
|
+
}
|
|
730
|
+
];
|
|
731
|
+
}
|
|
732
|
+
if (node.type === "object" && node.name) {
|
|
733
|
+
return [
|
|
734
|
+
{
|
|
735
|
+
kind: ts.SymbolDisplayPartKind.interfaceName,
|
|
736
|
+
text: node.name
|
|
737
|
+
}
|
|
738
|
+
];
|
|
739
|
+
}
|
|
740
|
+
return [
|
|
741
|
+
{
|
|
742
|
+
kind: ts.SymbolDisplayPartKind.localName,
|
|
743
|
+
text: node.type
|
|
744
|
+
}
|
|
745
|
+
];
|
|
746
|
+
}
|
|
747
|
+
function symbolDisplayToString(displayParts) {
|
|
748
|
+
return ts.displayPartsToString(displayParts);
|
|
749
|
+
}
|
|
750
|
+
function createDocString(node) {
|
|
751
|
+
return symbolDisplayToString(createTSDocString(node));
|
|
752
|
+
}
|
|
753
|
+
|
|
565
754
|
exports.applyPartialOrRequiredToNodeType = applyPartialOrRequiredToNodeType;
|
|
566
755
|
exports.applyPickOrOmitToNodeType = applyPickOrOmitToNodeType;
|
|
567
756
|
exports.buildTemplateRegex = buildTemplateRegex;
|
|
568
757
|
exports.computeEffectiveObject = computeEffectiveObject;
|
|
758
|
+
exports.createDocString = createDocString;
|
|
759
|
+
exports.createTSDocString = createTSDocString;
|
|
569
760
|
exports.decorateNode = decorateNode;
|
|
570
761
|
exports.fillInGenerics = fillInGenerics;
|
|
571
762
|
exports.getReferencedType = getReferencedType;
|
|
@@ -588,5 +779,6 @@ exports.propertyToTuple = propertyToTuple;
|
|
|
588
779
|
exports.resolveConditional = resolveConditional;
|
|
589
780
|
exports.resolveReferenceNode = resolveReferenceNode;
|
|
590
781
|
exports.setupTestEnv = setupTestEnv;
|
|
782
|
+
exports.symbolDisplayToString = symbolDisplayToString;
|
|
591
783
|
exports.tsStripOptionalType = tsStripOptionalType;
|
|
592
784
|
//# sourceMappingURL=index.cjs.js.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as ts from 'typescript';
|
|
2
|
-
import ts__default from 'typescript';
|
|
2
|
+
import ts__default, { SymbolDisplayPart } from 'typescript';
|
|
3
3
|
import { Annotations, NodeType, NodeTypeWithGenerics, NamedType, NamedTypeWithGenerics, PrimitiveTypes, ConditionalType, RefNode, ObjectType } from '@player-tools/xlr';
|
|
4
4
|
import { Node } from 'jsonc-parser';
|
|
5
5
|
|
|
@@ -141,4 +141,16 @@ declare function setupTestEnv(sourceCode: string, mockFileName?: string): {
|
|
|
141
141
|
tc: ts.TypeChecker;
|
|
142
142
|
};
|
|
143
143
|
|
|
144
|
-
|
|
144
|
+
/**
|
|
145
|
+
* Generate a documentation string for a given node
|
|
146
|
+
*
|
|
147
|
+
* @param node - The source node to author the docs string for
|
|
148
|
+
* @returns - documentation string
|
|
149
|
+
*/
|
|
150
|
+
declare function createTSDocString(node: NodeType): Array<SymbolDisplayPart>;
|
|
151
|
+
/** Convert the TS SymbolDisplayParts into a single string */
|
|
152
|
+
declare function symbolDisplayToString(displayParts: Array<SymbolDisplayPart>): string;
|
|
153
|
+
/** Create a documentation string from node */
|
|
154
|
+
declare function createDocString(node: NodeType): string;
|
|
155
|
+
|
|
156
|
+
export { PropertyNode, SetupReturnType, TopLevelDeclaration, TopLevelNode, applyPartialOrRequiredToNodeType, applyPickOrOmitToNodeType, buildTemplateRegex, computeEffectiveObject, createDocString, createTSDocString, decorateNode, fillInGenerics, getReferencedType, getStringLiteralsFromUnion, isExportedDeclaration, isGenericInterfaceDeclaration, isGenericNamedType, isGenericNodeType, isGenericTypeDeclaration, isNode, isNodeExported, isNonNullable, isOptionalProperty, isPrimitiveTypeNode, isTopLevelDeclaration, isTopLevelNode, isTypeReferenceGeneric, makePropertyMap, propertyToTuple, resolveConditional, resolveReferenceNode, setupTestEnv, symbolDisplayToString, tsStripOptionalType };
|
package/dist/index.esm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as ts from 'typescript';
|
|
2
|
-
import ts__default from 'typescript';
|
|
2
|
+
import ts__default, { SymbolDisplayPartKind, displayPartsToString } from 'typescript';
|
|
3
3
|
import * as tsvfs from '@typescript/vfs';
|
|
4
4
|
|
|
5
5
|
var __defProp$2 = Object.defineProperty;
|
|
@@ -535,5 +535,194 @@ function setupTestEnv(sourceCode, mockFileName = "filename.ts") {
|
|
|
535
535
|
};
|
|
536
536
|
}
|
|
537
537
|
|
|
538
|
-
|
|
538
|
+
function insertBetweenElements(array, separator) {
|
|
539
|
+
return array.reduce((acc, item, index) => {
|
|
540
|
+
if (index === 0) {
|
|
541
|
+
return [item];
|
|
542
|
+
}
|
|
543
|
+
return [...acc, separator, item];
|
|
544
|
+
}, []);
|
|
545
|
+
}
|
|
546
|
+
function createTSDocString(node) {
|
|
547
|
+
if (node.type === "ref") {
|
|
548
|
+
return [
|
|
549
|
+
{
|
|
550
|
+
text: node.ref,
|
|
551
|
+
kind: SymbolDisplayPartKind.keyword
|
|
552
|
+
}
|
|
553
|
+
];
|
|
554
|
+
}
|
|
555
|
+
if (node.type === "or" || node.type === "and") {
|
|
556
|
+
const items = node.type === "and" ? node.and : node.or;
|
|
557
|
+
return insertBetweenElements(items.map((subnode) => createTSDocString(subnode)), [
|
|
558
|
+
{
|
|
559
|
+
kind: SymbolDisplayPartKind.punctuation,
|
|
560
|
+
text: node.type === "and" ? " & " : " | "
|
|
561
|
+
}
|
|
562
|
+
]).flat();
|
|
563
|
+
}
|
|
564
|
+
if (node.type === "function") {
|
|
565
|
+
return [
|
|
566
|
+
{
|
|
567
|
+
kind: SymbolDisplayPartKind.keyword,
|
|
568
|
+
text: "function"
|
|
569
|
+
},
|
|
570
|
+
{
|
|
571
|
+
kind: SymbolDisplayPartKind.space,
|
|
572
|
+
text: " "
|
|
573
|
+
},
|
|
574
|
+
...node.name ? [{ text: node.name, kind: SymbolDisplayPartKind.methodName }] : [],
|
|
575
|
+
{
|
|
576
|
+
kind: SymbolDisplayPartKind.punctuation,
|
|
577
|
+
text: "("
|
|
578
|
+
},
|
|
579
|
+
...insertBetweenElements(node.parameters.map((p) => {
|
|
580
|
+
if (p.name) {
|
|
581
|
+
return [
|
|
582
|
+
{
|
|
583
|
+
kind: SymbolDisplayPartKind.parameterName,
|
|
584
|
+
text: p.name
|
|
585
|
+
},
|
|
586
|
+
{
|
|
587
|
+
kind: SymbolDisplayPartKind.punctuation,
|
|
588
|
+
text: p.optional ? "?" : ""
|
|
589
|
+
},
|
|
590
|
+
{
|
|
591
|
+
kind: SymbolDisplayPartKind.punctuation,
|
|
592
|
+
text: ": "
|
|
593
|
+
},
|
|
594
|
+
...createTSDocString(p.type)
|
|
595
|
+
];
|
|
596
|
+
}
|
|
597
|
+
return createTSDocString(p.type);
|
|
598
|
+
}), [
|
|
599
|
+
{
|
|
600
|
+
kind: SymbolDisplayPartKind.punctuation,
|
|
601
|
+
text: ", "
|
|
602
|
+
}
|
|
603
|
+
]).flat(),
|
|
604
|
+
{
|
|
605
|
+
kind: SymbolDisplayPartKind.punctuation,
|
|
606
|
+
text: ")"
|
|
607
|
+
},
|
|
608
|
+
...node.returnType ? [
|
|
609
|
+
{
|
|
610
|
+
kind: SymbolDisplayPartKind.punctuation,
|
|
611
|
+
text: ": "
|
|
612
|
+
},
|
|
613
|
+
...createTSDocString(node.returnType)
|
|
614
|
+
] : []
|
|
615
|
+
];
|
|
616
|
+
}
|
|
617
|
+
if (node.type === "tuple") {
|
|
618
|
+
return [
|
|
619
|
+
{
|
|
620
|
+
kind: SymbolDisplayPartKind.punctuation,
|
|
621
|
+
text: "["
|
|
622
|
+
},
|
|
623
|
+
...insertBetweenElements(node.elementTypes.map((t) => {
|
|
624
|
+
if (t.name) {
|
|
625
|
+
return [
|
|
626
|
+
{
|
|
627
|
+
kind: SymbolDisplayPartKind.propertyName,
|
|
628
|
+
text: t.name
|
|
629
|
+
},
|
|
630
|
+
{
|
|
631
|
+
kind: SymbolDisplayPartKind.punctuation,
|
|
632
|
+
text: ": "
|
|
633
|
+
},
|
|
634
|
+
...createTSDocString(t.type)
|
|
635
|
+
];
|
|
636
|
+
}
|
|
637
|
+
return createTSDocString(t.type);
|
|
638
|
+
}), [
|
|
639
|
+
{
|
|
640
|
+
kind: SymbolDisplayPartKind.punctuation,
|
|
641
|
+
text: ", "
|
|
642
|
+
}
|
|
643
|
+
]).flat(),
|
|
644
|
+
{
|
|
645
|
+
kind: SymbolDisplayPartKind.punctuation,
|
|
646
|
+
text: "]"
|
|
647
|
+
}
|
|
648
|
+
];
|
|
649
|
+
}
|
|
650
|
+
if (node.type === "array") {
|
|
651
|
+
return [
|
|
652
|
+
{
|
|
653
|
+
kind: SymbolDisplayPartKind.interfaceName,
|
|
654
|
+
text: "Array"
|
|
655
|
+
},
|
|
656
|
+
{
|
|
657
|
+
kind: SymbolDisplayPartKind.punctuation,
|
|
658
|
+
text: "<"
|
|
659
|
+
},
|
|
660
|
+
...createTSDocString(node.elementType),
|
|
661
|
+
{
|
|
662
|
+
kind: SymbolDisplayPartKind.punctuation,
|
|
663
|
+
text: ">"
|
|
664
|
+
}
|
|
665
|
+
];
|
|
666
|
+
}
|
|
667
|
+
if (node.type === "record") {
|
|
668
|
+
return [
|
|
669
|
+
{
|
|
670
|
+
kind: SymbolDisplayPartKind.interfaceName,
|
|
671
|
+
text: "Record"
|
|
672
|
+
},
|
|
673
|
+
{
|
|
674
|
+
kind: SymbolDisplayPartKind.punctuation,
|
|
675
|
+
text: "<"
|
|
676
|
+
},
|
|
677
|
+
...createTSDocString(node.keyType),
|
|
678
|
+
{
|
|
679
|
+
kind: SymbolDisplayPartKind.punctuation,
|
|
680
|
+
text: ", "
|
|
681
|
+
},
|
|
682
|
+
...createTSDocString(node.valueType),
|
|
683
|
+
{
|
|
684
|
+
kind: SymbolDisplayPartKind.punctuation,
|
|
685
|
+
text: ">"
|
|
686
|
+
}
|
|
687
|
+
];
|
|
688
|
+
}
|
|
689
|
+
if ((node.type === "string" || node.type === "boolean" || node.type === "number") && node.const !== void 0) {
|
|
690
|
+
return [
|
|
691
|
+
{
|
|
692
|
+
kind: SymbolDisplayPartKind.keyword,
|
|
693
|
+
text: typeof node.const === "string" ? `'${node.const}'` : String(node.const)
|
|
694
|
+
}
|
|
695
|
+
];
|
|
696
|
+
}
|
|
697
|
+
if (isPrimitiveTypeNode(node) && node.type !== "null") {
|
|
698
|
+
return [
|
|
699
|
+
{
|
|
700
|
+
kind: SymbolDisplayPartKind.keyword,
|
|
701
|
+
text: node.type
|
|
702
|
+
}
|
|
703
|
+
];
|
|
704
|
+
}
|
|
705
|
+
if (node.type === "object" && node.name) {
|
|
706
|
+
return [
|
|
707
|
+
{
|
|
708
|
+
kind: SymbolDisplayPartKind.interfaceName,
|
|
709
|
+
text: node.name
|
|
710
|
+
}
|
|
711
|
+
];
|
|
712
|
+
}
|
|
713
|
+
return [
|
|
714
|
+
{
|
|
715
|
+
kind: SymbolDisplayPartKind.localName,
|
|
716
|
+
text: node.type
|
|
717
|
+
}
|
|
718
|
+
];
|
|
719
|
+
}
|
|
720
|
+
function symbolDisplayToString(displayParts) {
|
|
721
|
+
return displayPartsToString(displayParts);
|
|
722
|
+
}
|
|
723
|
+
function createDocString(node) {
|
|
724
|
+
return symbolDisplayToString(createTSDocString(node));
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
export { applyPartialOrRequiredToNodeType, applyPickOrOmitToNodeType, buildTemplateRegex, computeEffectiveObject, createDocString, createTSDocString, decorateNode, fillInGenerics, getReferencedType, getStringLiteralsFromUnion, isExportedDeclaration, isGenericInterfaceDeclaration, isGenericNamedType, isGenericNodeType, isGenericTypeDeclaration, isNode, isNodeExported, isNonNullable, isOptionalProperty, isPrimitiveTypeNode, isTopLevelDeclaration, isTopLevelNode, isTypeReferenceGeneric, makePropertyMap, propertyToTuple, resolveConditional, resolveReferenceNode, setupTestEnv, symbolDisplayToString, tsStripOptionalType };
|
|
539
728
|
//# sourceMappingURL=index.esm.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@player-tools/xlr-utils",
|
|
3
|
-
"version": "0.4.0-next.
|
|
3
|
+
"version": "0.4.0-next.2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"registry": "https://registry.npmjs.org"
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"jsonc-parser": "^2.3.1"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@player-tools/xlr": "0.4.0-next.
|
|
13
|
+
"@player-tools/xlr": "0.4.0-next.2",
|
|
14
14
|
"@typescript/vfs": "^1.4.0",
|
|
15
15
|
"@babel/runtime": "7.15.4"
|
|
16
16
|
},
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { SymbolDisplayPartKind, displayPartsToString } from 'typescript';
|
|
2
|
+
import type { SymbolDisplayPart } from 'typescript';
|
|
3
|
+
import type { NodeType } from '@player-tools/xlr';
|
|
4
|
+
import { isPrimitiveTypeNode } from './type-checks';
|
|
5
|
+
|
|
6
|
+
/** Like `.join()` but for arrays */
|
|
7
|
+
function insertBetweenElements<T>(array: Array<T>, separator: T): T[] {
|
|
8
|
+
return array.reduce((acc, item, index) => {
|
|
9
|
+
if (index === 0) {
|
|
10
|
+
return [item];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return [...acc, separator, item];
|
|
14
|
+
}, [] as T[]);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Generate a documentation string for a given node
|
|
19
|
+
*
|
|
20
|
+
* @param node - The source node to author the docs string for
|
|
21
|
+
* @returns - documentation string
|
|
22
|
+
*/
|
|
23
|
+
export function createTSDocString(node: NodeType): Array<SymbolDisplayPart> {
|
|
24
|
+
if (node.type === 'ref') {
|
|
25
|
+
return [
|
|
26
|
+
{
|
|
27
|
+
text: node.ref,
|
|
28
|
+
kind: SymbolDisplayPartKind.keyword as any,
|
|
29
|
+
},
|
|
30
|
+
];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (node.type === 'or' || node.type === 'and') {
|
|
34
|
+
const items = node.type === 'and' ? node.and : node.or;
|
|
35
|
+
|
|
36
|
+
return insertBetweenElements(
|
|
37
|
+
items.map((subnode) => createTSDocString(subnode)),
|
|
38
|
+
[
|
|
39
|
+
{
|
|
40
|
+
kind: SymbolDisplayPartKind.punctuation as any,
|
|
41
|
+
text: node.type === 'and' ? ' & ' : ' | ',
|
|
42
|
+
},
|
|
43
|
+
]
|
|
44
|
+
).flat();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (node.type === 'function') {
|
|
48
|
+
return [
|
|
49
|
+
{
|
|
50
|
+
kind: SymbolDisplayPartKind.keyword as any,
|
|
51
|
+
text: 'function',
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
kind: SymbolDisplayPartKind.space as any,
|
|
55
|
+
text: ' ',
|
|
56
|
+
},
|
|
57
|
+
...(node.name
|
|
58
|
+
? [{ text: node.name, kind: SymbolDisplayPartKind.methodName }]
|
|
59
|
+
: []),
|
|
60
|
+
{
|
|
61
|
+
kind: SymbolDisplayPartKind.punctuation as any,
|
|
62
|
+
text: '(',
|
|
63
|
+
},
|
|
64
|
+
...insertBetweenElements(
|
|
65
|
+
node.parameters.map((p) => {
|
|
66
|
+
if (p.name) {
|
|
67
|
+
return [
|
|
68
|
+
{
|
|
69
|
+
kind: SymbolDisplayPartKind.parameterName as any,
|
|
70
|
+
text: p.name,
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
kind: SymbolDisplayPartKind.punctuation as any,
|
|
74
|
+
text: p.optional ? '?' : '',
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
kind: SymbolDisplayPartKind.punctuation as any,
|
|
78
|
+
text: ': ',
|
|
79
|
+
},
|
|
80
|
+
...createTSDocString(p.type),
|
|
81
|
+
];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return createTSDocString(p.type);
|
|
85
|
+
}),
|
|
86
|
+
[
|
|
87
|
+
{
|
|
88
|
+
kind: SymbolDisplayPartKind.punctuation as any,
|
|
89
|
+
text: ', ',
|
|
90
|
+
},
|
|
91
|
+
]
|
|
92
|
+
).flat(),
|
|
93
|
+
{
|
|
94
|
+
kind: SymbolDisplayPartKind.punctuation as any,
|
|
95
|
+
text: ')',
|
|
96
|
+
},
|
|
97
|
+
...(node.returnType
|
|
98
|
+
? [
|
|
99
|
+
{
|
|
100
|
+
kind: SymbolDisplayPartKind.punctuation as any,
|
|
101
|
+
text: ': ',
|
|
102
|
+
},
|
|
103
|
+
...createTSDocString(node.returnType),
|
|
104
|
+
]
|
|
105
|
+
: []),
|
|
106
|
+
];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (node.type === 'tuple') {
|
|
110
|
+
return [
|
|
111
|
+
{
|
|
112
|
+
kind: SymbolDisplayPartKind.punctuation as any,
|
|
113
|
+
text: '[',
|
|
114
|
+
},
|
|
115
|
+
...insertBetweenElements(
|
|
116
|
+
node.elementTypes.map((t) => {
|
|
117
|
+
if (t.name) {
|
|
118
|
+
return [
|
|
119
|
+
{
|
|
120
|
+
kind: SymbolDisplayPartKind.propertyName as any,
|
|
121
|
+
text: t.name,
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
kind: SymbolDisplayPartKind.punctuation as any,
|
|
125
|
+
text: ': ',
|
|
126
|
+
},
|
|
127
|
+
...createTSDocString(t.type),
|
|
128
|
+
];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return createTSDocString(t.type);
|
|
132
|
+
}),
|
|
133
|
+
[
|
|
134
|
+
{
|
|
135
|
+
kind: SymbolDisplayPartKind.punctuation as any,
|
|
136
|
+
text: ', ',
|
|
137
|
+
},
|
|
138
|
+
]
|
|
139
|
+
).flat(),
|
|
140
|
+
{
|
|
141
|
+
kind: SymbolDisplayPartKind.punctuation as any,
|
|
142
|
+
text: ']',
|
|
143
|
+
},
|
|
144
|
+
];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (node.type === 'array') {
|
|
148
|
+
return [
|
|
149
|
+
{
|
|
150
|
+
kind: SymbolDisplayPartKind.interfaceName as any,
|
|
151
|
+
text: 'Array',
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
kind: SymbolDisplayPartKind.punctuation as any,
|
|
155
|
+
text: '<',
|
|
156
|
+
},
|
|
157
|
+
...createTSDocString(node.elementType),
|
|
158
|
+
{
|
|
159
|
+
kind: SymbolDisplayPartKind.punctuation as any,
|
|
160
|
+
text: '>',
|
|
161
|
+
},
|
|
162
|
+
];
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (node.type === 'record') {
|
|
166
|
+
return [
|
|
167
|
+
{
|
|
168
|
+
kind: SymbolDisplayPartKind.interfaceName as any,
|
|
169
|
+
text: 'Record',
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
kind: SymbolDisplayPartKind.punctuation as any,
|
|
173
|
+
text: '<',
|
|
174
|
+
},
|
|
175
|
+
...createTSDocString(node.keyType),
|
|
176
|
+
{
|
|
177
|
+
kind: SymbolDisplayPartKind.punctuation as any,
|
|
178
|
+
text: ', ',
|
|
179
|
+
},
|
|
180
|
+
...createTSDocString(node.valueType),
|
|
181
|
+
{
|
|
182
|
+
kind: SymbolDisplayPartKind.punctuation as any,
|
|
183
|
+
text: '>',
|
|
184
|
+
},
|
|
185
|
+
];
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (
|
|
189
|
+
(node.type === 'string' ||
|
|
190
|
+
node.type === 'boolean' ||
|
|
191
|
+
node.type === 'number') &&
|
|
192
|
+
node.const !== undefined
|
|
193
|
+
) {
|
|
194
|
+
return [
|
|
195
|
+
{
|
|
196
|
+
kind: SymbolDisplayPartKind.keyword as any,
|
|
197
|
+
text:
|
|
198
|
+
typeof node.const === 'string'
|
|
199
|
+
? `'${node.const}'`
|
|
200
|
+
: String(node.const),
|
|
201
|
+
},
|
|
202
|
+
];
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (isPrimitiveTypeNode(node) && node.type !== 'null') {
|
|
206
|
+
return [
|
|
207
|
+
{
|
|
208
|
+
kind: SymbolDisplayPartKind.keyword as any,
|
|
209
|
+
text: node.type,
|
|
210
|
+
},
|
|
211
|
+
];
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (node.type === 'object' && node.name) {
|
|
215
|
+
return [
|
|
216
|
+
{
|
|
217
|
+
kind: SymbolDisplayPartKind.interfaceName as any,
|
|
218
|
+
text: node.name,
|
|
219
|
+
},
|
|
220
|
+
];
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return [
|
|
224
|
+
{
|
|
225
|
+
kind: SymbolDisplayPartKind.localName as any,
|
|
226
|
+
text: node.type,
|
|
227
|
+
},
|
|
228
|
+
];
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Convert the TS SymbolDisplayParts into a single string */
|
|
232
|
+
export function symbolDisplayToString(
|
|
233
|
+
displayParts: Array<SymbolDisplayPart>
|
|
234
|
+
): string {
|
|
235
|
+
return displayPartsToString(displayParts);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Create a documentation string from node */
|
|
239
|
+
export function createDocString(node: NodeType): string {
|
|
240
|
+
return symbolDisplayToString(createTSDocString(node));
|
|
241
|
+
}
|
package/src/index.ts
CHANGED