Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 1x 1x 1x 1x 8x 4x 12x 1x 4x 1x 1x 1x 4x 4x 4x 1x 4x 4x 4x | /*
Signature Naming
While plain text is a readable method if fails to provide the appropriate information for each name at first glance. instead it seems providing a full signature to each component within the name is the most appropriate method... what does this mean however
take the following object for example
const Test: {
getName({name, maternal, paternal}: {
name: string
maternal: SomeOtherObject,
paternal: SomeOtherObject,
}):{
name: string,
lineage(options:{
branches: 'paternal' | 'maternal' | 'both'
});
}
} = {...}
This just an example of how anonymous objects can be abused to create something difficult to document.
If I were to use simple dot notation to symbolize these nodes I would encounter conflicts with name as it exists with the same syntax multiple times
Test.getName.name (the function argument property)
Test.getName.name (the function return value property)
It should be noted that vsCode resolves this be resolving destructuring to an any argument called prop.
I think having a method that better at differenciating such things.
*/
import { getDocPath, getFullName, getName, getTypeNode, isPrimitive } from "./node-tools";
import { ArrowFunction, ClassDeclaration, ClassExpression, FunctionDeclaration, FunctionExpression, FunctionTypeNode, MethodDeclaration, MethodSignature, ModifierableNode, Node, Type, TypeOperatorTypeNode } from "ts-morph";
import { bySyntax } from "./SyntaxKindDelegator";
import SK, { SKindMap } from "./SyntaxKindDelegator.types";
import { $href, $kd, $kind, $literal, $name, $type } from "./decorators";
import { gray, yellow } from "console-log-colors";
import { Nodely } from "./types";
import TS from "./TS";
import { typelit } from "./constants";
import { escape } from "./utils";
/**
* With typenodes not alway being provided this method acts as a point where I can change the logic if getting a typenode fails.
* @param node
* @returns
*/
const fromTypeNode = (node: Node): string => {
const tn = getTypeNode(node);
return tn ? sig(tn)
: getSignatureFromType(node) ?? ''
}
const objLiteral = () => $literal(typelit);
const genTypes = (nodes: Node[], pre: string ='<', post: string = '>') => nodes.map(sig).join(', ').wrap(pre, post);
const isAsync = (node: Node) => {
Iif(!('isAsync' in node) || typeof node.isAsync !== 'function') return false;
return node.isAsync();
}
const isGenerator = (node: Node) => {
Iif(!('isGenerator' in node) || typeof node.isGenerator !== 'function') return false;
return node.isGenerator();
}
const fnSignature = (node: FunctionDeclaration | FunctionExpression | MethodDeclaration | FunctionTypeNode | MethodSignature | ArrowFunction) => `${isAsync(node) ? 'async ':''}${isGenerator(node) ? '*':''}${genTypes(node.getTypeParameters())}(${genTypes(node.getParameters(), '', '')}) => ${sig(node.getReturnTypeNode()) || fromType(node.getReturnType()) || $literal('void')}`;
const classSignatue = (node: ClassDeclaration | ClassExpression) => {
const extensions = sig(node.getExtends());
const implementions = node.getImplements().map(i=>sig(i)).join(', ');
return `${extensions ? ' extends '+ extensions:''}${implementions ? ' implements ' + implementions:''}`
}
const typingMap: SKindMap<string> = {
//functional declarations
[SK.FunctionDeclaration]: fnSignature,
[SK.FunctionExpression]: fnSignature,
[SK.MethodDeclaration]: fnSignature,
[SK.FunctionType]: fnSignature,
[SK.MethodSignature]: fnSignature,
[SK.ArrowFunction]: fnSignature,
[SK.PropertySignature]: fromTypeNode,
[SK.BindingElement]: fromTypeNode,
[SK.PropertyAccessExpression]: node => `${sig(node.getNameNode())}`,
[SK.UnionType]: node=>node.getTypeNodes().map(sig).join(' | '),
[SK.LiteralType]: node=>$literal(node.getText()),
[SK.IntersectionType]: node=>node.getTypeNodes().map(sig).join(' & '),
[SK.NamedTupleMember]: node=>`${$name(node.getName())}: ${fromTypeNode(node)}`,
[SK.ArrayType]: node=>`${sig(node.getElementTypeNode())}[]`,
[SK.ArrayLiteralExpression]: ()=>'[]',
[SK.TupleType]: node=> genTypes(node.getElements(), '[',']'),
[SK.ParenthesizedType]: node=>`(${fromTypeNode(node)})`,
[SK.Constructor]: node=>`${$type("new")} (${node.getParameters().map(p=>sig(p)).join(', ')})=>${$type(getName(node.getParent()))}`,
[SK.SetAccessor]: node=> node.getParameters().map(p=>sig(p)).join(', '),
[SK.ConditionalType]: node => `${sig(node.getCheckType())} extends ${sig(node.getExtendsType())} ? ${sig(node.getTrueType())}<br/>: ${sig(node.getFalseType())}`,
[SK.ExpressionWithTypeArguments]: node => `${sig(node.getExpression())}${node.getTypeArguments().map(a=>sig(a)).join(', ').wrap('<','>')}`,
[SK.RestType]: node => `...${fromTypeNode(node)}`,
[SK.ArrayBindingPattern]: node => genTypes(node.getElements(), '[',']'),
[SK.QualifiedName]: node => `${sig(node.getLeft())}.${sig(node.getRight())}`,
[SK.TypePredicate]: node => `${sig(node.getParameterNameNode())} ${node.hasAssertsModifier() ? sig(node.getAssertsModifier()):'is'} ${sig(node.getTypeNode())}`,
[SK.TypeOperator]: node => `${$kind(getOperator(node))} ${fromTypeNode}`,
[SK.BinaryExpression]: node => `${sig(node.getLeft())} ${sig(node.getOperatorToken())} ${sig(node.getRight())}`,
[SK.CallExpression]: node => $type(escape(node.getReturnType().getText())),
[SK.IndexedAccessType]: node => `${sig(node.getObjectTypeNode())}[${node.getIndexTypeNode()}]`,
//object literat expressions or declarations or types
[SK.ObjectLiteralExpression]: objLiteral,
[SK.ObjectBindingPattern]: objLiteral,
[SK.TypeLiteral]: objLiteral,
//tokens
[SK.AsteriskToken]: () => '*',
[SK.AsteriskAsteriskToken]: ()=>'**',
[SK.AsteriskEqualsToken]: ()=> '*=',
[SK.AsteriskAsteriskEqualsToken]: ()=>'**=',
[SK.PlusToken]: ()=> '+',
[SK.PlusPlusToken]: ()=>'++',
[SK.PlusEqualsToken]: ()=>'+=',
[SK.MinusToken]: ()=>'-',
[SK.MinusMinusToken]: ()=>'--',
[SK.MinusEqualsToken]: ()=>'-=',
[SK.SlashToken]: ()=>'/',
[SK.SlashEqualsToken]: ()=>'/=',
[SK.LessThanToken]: ()=>'<',
[SK.LessThanEqualsToken]: ()=>'<=',
[SK.GreaterThanToken]: ()=>'>',
[SK.GreaterThanEqualsToken]: ()=>'>=',
[SK.TypeAliasDeclaration]: (node)=>`${genTypes(node.getTypeParameters())}: ${fromTypeNode(node)}`,
[SK.TypeReference]: node=>{
const typeName = node.getTypeName();
const args = node.getTypeArguments();
//corner cases
Iif(typeName.getText() === "Array"){
return sig(args[0])+"[]";
}
return sig(typeName) + args.map(sig).join(', ').wrap('<', '>')
},
[SK.Identifier]: node=>{
const def = node.getDefinitionNodes()[0]; //I guess its possible to have multiple definitions but I havent thought of a use case where I would have reference to all definitions in one location (extensions would have a link back to immediate source automatically)
Iif(!def) return $type(node.getText()); //no link
const href = getDocPath(def)
Iif(!href) return $type(node.getText()); //link outside scope
return $href(node.getText(), href);
},
[SK.TypeParameter]: node => {
const extension = node.getConstraint()
const modifiers = node.getModifiers()
return `${modifiers.map(sig).join(' ')}${modifiers.length ? ' ':''}${$name(node.getName())}${(extension ? ' extends ' + sig(extension):'')}`;
},
[SK.Parameter]: node=>{
const typeNode = fromTypeNode(node);
const initializer = sig(node.getInitializer());
return `${$name((node.isRestParameter() ? '...':'')+sig(node.getNameNode()))}: ${typeNode}${initializer ? ' = '+initializer:''}`;
},
[SK.ClassDeclaration]: node=>{
const extensions = sig(node.getExtends());
const implementions = node.getImplements().map(i=>sig(i)).join(', ');
return `${extensions ? ' extends '+ extensions:''}${implementions ? ' implements ' + implementions:''}`
},
[SK.ClassExpression]: node=>{
const extensions = node.getExtends();
const implementations = node.getImplements();
return `${$kd`class`}${extensions ? (' extends ' + sig(extensions)):''}${implementations.map(n=>` implements ${sig(n)}`)}${typelit}`;
},
[SK.InterfaceDeclaration]: node=>{
const extensions = node.getExtends();
return `${extensions.map(node => `extends ${sig(node)}`).join(' ')}`;
},
[SK.GetAccessor]: node=> {
const rtn = node.getReturnTypeNode();
return rtn ? sig(rtn)
: getSignatureFromType(node) ?? ''
},
[SK.VariableDeclaration]: node => {
const tn = fromTypeNode(node);
Iif(tn) return tn;
const init = node.getInitializer();
return sig(init);
},
[SK.PropertyAssignment]: node => `${sig(node.getNameNode())}: ${fromTypeNode(node)}`,
[SK.NewExpression]: node => `${sig(node.getExpression())}${genTypes(node.getTypeArguments())}`
}
export const getModifiers = (node: ModifierableNode) => {
return node.getModifiers().map(m=>m.getText()).join(' ')
}
const getOperator = (node: TypeOperatorTypeNode) =>{
switch(node.getOperator()){
case SK.ReadonlyKeyword: return 'readonly';
case SK.KeyOfKeyword: return 'keyof';
case SK.UniqueKeyword: return 'unique';
}
}
const Ignores = new Set([
SK.MultiLineCommentTrivia
]);
const defSig = (n: Nodely)=>{
if(!n || Ignores.has(n.getKind())) return '';
if(isPrimitive(n)) return $type(n.getText());
TS.err("Signature Missing type", yellow(n.getKindName()), gray(getFullName(n)), n.getText());
return "";
}
const sig = (node: Nodely) => bySyntax(node, typingMap, defSig);
/**
* Once a full signature name is resolved the typing of the object will be necessary. This typing however will be different for different declaration type. As such I will be handling these similar to the SyntaxKind... I need a SyntaxKind switching function
* @param node
*/
export const getSignature = (node: Nodely) => {
return sig(node);
}
export const isPrimitiveType = (t: Type) => t.isAny() || t.isBigInt() || t.isNumber() || t.isBoolean() || t.isString() || t.isNever() || t.isUndefined() || t.isUnknown() || t.isNull();
export const isPrimitiveLiteralType = (t: Type) => t.isStringLiteral() || t.isNumberLiteral() || t.isBoolean() || t.isBigIntLiteral();
export const fromType = (t: Type | undefined):string => {
//a node does exist it is just in a body but the return type or type should have a symbol to said declaration... why write a second parser when syntaxKind parser is more convenient.
const symbol = t?.getSymbol()?.getDeclarations()[0];
const aliasSymbol = t?.getAliasSymbol()?.getDeclarations()[0];
return sig(symbol ?? aliasSymbol);
}
export const getSignatureFromType = (node: Nodely) => {
Iif(!node) return '';
const t = node.getType();
return fromType(t);
} |