authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-04-03 17:57:15+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-19 19:10:11-07:00
log67da1b8c88ffbb846e56802a6a1bc187714bbed2
tree99f773df6c1e0282da6edac88d9c3fe7176e3be6
parent80f9490e06c6190d176263ec6e7956a0d4b7887d

autodoc: fixed all type errors in main.js


1 files changed, 63 insertions(+), 37 deletions(-)

lib/docs/main.js+63-37
......@@ -73,7 +73,8 @@
7373 name: string,
7474 src: number,
7575 ret: WalkResult,
76 params: WalkResult[]
76 params: WalkResult[],
77 generic: boolean,
7778 }} Fn
7879*/
7980
......@@ -189,18 +190,15 @@
189190
190191/**
191192 * @typedef {{
192 name: string,
193 src?: number,
194 privDecls: number[],
195 pubDecls: number[],
196 fields?: WalkResult[],
193 typeRef: WalkResult,
194 fieldVals: WalkResult[],
197195 }} Struct
198196*/
199197
200198/**
201199 * @typedef {{
202 len: WalkResult,
203 child: WalkResult,
200 typeRef: WalkResult,
201 data: WalkResult[],
204202 }} ZigArray
205203*/
206204
......@@ -280,7 +278,7 @@ var zigAnalysis;
280278 var domSectSearchNoResults = /** @type HTMLElement */(document.getElementById("sectSearchNoResults"));
281279 var domSectInfo = /** @type HTMLElement */(document.getElementById("sectInfo"));
282280 var domTdTarget = /** @type HTMLElement */(document.getElementById("tdTarget"));
283 var domPrivDeclsBox = /** @type HTMLCheckboxElement */(document.getElementById("privDeclsBox"));
281 var domPrivDeclsBox = /** @type HTMLInputElement */(document.getElementById("privDeclsBox"));
284282 var domTdZigVer = /** @type HTMLElement */(document.getElementById("tdZigVer"));
285283 var domHdrName = /** @type HTMLElement */(document.getElementById("hdrName"));
286284 var domHelpModal = /** @type HTMLElement */(document.getElementById("helpDialog"));
......@@ -313,7 +311,7 @@ var zigAnalysis;
313311 * pkgNames: string[],
314312 * pkgObjs: Package[],
315313 * declNames: string[],
316 * declObjs: Decl[],
314 * declObjs: (Decl | Type)[],
317315 * callName: any,
318316 * }} CurNav
319317 */
......@@ -378,53 +376,68 @@ var zigAnalysis;
378376 }
379377 }
380378
379 /** @param {Type | Decl} x */
381380 function isDecl(x) {
382381 return "value" in x;
383382 }
384383
384 /** @param {Type | Decl} x */
385385 function isType(x) {
386386 return "kind" in x && !("value" in x);
387387 }
388388
389 /** @param {Type | Decl} x */
389390 function isContainerType(x) {
390 return isType(x) && typeKindIsContainer(x.kind) ;
391 return isType(x) && typeKindIsContainer(/** @type {Type} */(x).kind) ;
391392 }
392393
394 /** @param {Type} type */
393395 function typeShorthandName(type) {
394 var name = type.name;
396 var name = undefined;
395397 if (type.kind === typeKinds.Struct) {
396398 name = "struct";
397399 } else if (type.kind === typeKinds.Enum) {
398400 name = "enum";
399401 } else if (type.kind === typeKinds.Union) {
400 name= "union";
402 name = "union";
403 } else {
404 name = /** @type {any} */(type).name;
401405 }
402
403406 return escapeHtml(name);
404407 }
405408
409 /** @param {number} typeKind */
406410 function typeKindIsContainer(typeKind) {
407411 return typeKind === typeKinds.Struct ||
408412 typeKind === typeKinds.Union ||
409413 typeKind === typeKinds.Enum;
410414 }
411415
416 /** @param {number} typeKind */
412417 function declCanRepresentTypeKind(typeKind) {
413418 return typeKind === typeKinds.ErrorSet || typeKindIsContainer(typeKind);
414419 }
415420
421 /**
422 * @param {WalkResult[]} path
423 * @return {WalkResult | null}
424 */
416425 function findCteInRefPath(path) {
417426 for (var i = path.length - 1; i >= 0; i -= 1) {
418427 const ref = path[i];
419428 if ("string" in ref) continue;
420429 if ("comptimeExpr" in ref) return ref;
421 if ("refPath" in ref) return findCteinRefPath(ref.refPath);
430 if ("refPath" in ref) return findCteInRefPath(ref.refPath);
422431 return null;
423432 }
424433
425434 return null;
426435 }
427436
437 /**
438 * @param {WalkResult} value
439 * @return {WalkResult}
440 */
428441 function resolveValue(value) {
429442 var i = 0;
430443 while(i < 1000) {
......@@ -444,21 +457,26 @@ var zigAnalysis;
444457
445458 }
446459 console.assert(false);
460 return /** @type {WalkResult} */({});
447461 }
448462
463 /**
464 * @param {Decl} decl
465 * @return {WalkResult}
466 */
449467 function typeOfDecl(decl){
450468 var i = 0;
451469 while(i < 1000) {
452470 i += 1;
453471 console.assert(isDecl(decl));
454472 if ("type" in decl.value) {
455 return { type: typeTypeId };
473 return /** @type {WalkResult} */({ type: typeTypeId });
456474 }
457475
458476 if ("refPath" in decl.value) {
459 decl = {
477 decl = /** @type {Decl} */({
460478 value: decl.value.refPath[decl.value.refPath.length -1]
461 };
479 });
462480 continue;
463481 }
464482
......@@ -495,22 +513,22 @@ var zigAnalysis;
495513 fn_decl = zigAnalysis.decls[fn_call.func.declRef];
496514 } else if ("refPath" in fn_call.func) {
497515 console.assert("declRef" in fn_call.func.refPath[fn_call.func.refPath.length -1]);
498 fn_decl = zigAnalysis.decls[fn_call.func.refPath[fn_call.func.refPath.length -1].declRef.value];
516 fn_decl = zigAnalysis.decls[fn_call.func.refPath[fn_call.func.refPath.length -1].declRef];
499517 } else throw {};
500518
501519 const fn_decl_value = resolveValue(fn_decl.value);
502520 console.assert("type" in fn_decl_value); //TODO handle comptimeExpr
503 const fn_type = zigAnalysis.types[fn_decl_value.type];
521 const fn_type = /** @type {Fn} */(zigAnalysis.types[fn_decl_value.type]);
504522 console.assert(fn_type.kind === typeKinds.Fn);
505523 return fn_type.ret;
506524 }
507525
508526 if ("void" in decl.value) {
509 return { type: typeTypeId };
527 return /** @type {WalkResult} */({ type: typeTypeId });
510528 }
511529
512530 if ("bool" in decl.value) {
513 return { type: typeKinds.Bool };
531 return /** @type {WalkResult} */({ type: typeKinds.Bool });
514532 }
515533
516534 console.log("TODO: handle in `typeOfDecl` more cases: ", decl);
......@@ -518,6 +536,7 @@ var zigAnalysis;
518536 throw {};
519537 }
520538 console.assert(false);
539 return /** @type {WalkResult} */({});
521540 }
522541
523542 function render() {
......@@ -569,15 +588,18 @@ var zigAnalysis;
569588 curNav.pkgObjs.push(pkg);
570589 }
571590
591 /** @type {Decl | Type} */
572592 var currentType = zigAnalysis.types[pkg.main];
573593 curNav.declObjs = [currentType];
574594 for (var i = 0; i < curNav.declNames.length; i += 1) {
575 var childDecl = findSubDecl(currentType, curNav.declNames[i]);
595
596 /** @type {Decl | Type | null} */
597 var childDecl = findSubDecl(/** @type {ContainerType} */(currentType), curNav.declNames[i]);
576598 if (childDecl == null) {
577599 return render404();
578600 }
579601
580 var childDeclValue = resolveValue(childDecl.value);
602 var childDeclValue = resolveValue(/** @type {Decl} */(childDecl).value);
581603 if ("type" in childDeclValue) {
582604
583605 const t = zigAnalysis.types[childDeclValue.type];
......@@ -586,7 +608,7 @@ var zigAnalysis;
586608 }
587609 }
588610
589 currentType = childDecl;
611 currentType = /** @type {Decl | Type} */(childDecl);
590612 curNav.declObjs.push(currentType);
591613 }
592614
......@@ -598,31 +620,32 @@ var zigAnalysis;
598620 var lastIsContainerType = isContainerType(last);
599621
600622 if (lastIsContainerType) {
601 return renderContainer(last);
623 return renderContainer(/** @type {ContainerType} */(last));
602624 }
603625
604626 if (!lastIsDecl && !lastIsType) {
605 return renderUnknownDecl(last);
627 return renderUnknownDecl(/** @type {Decl} */(last));
606628 }
607629
608630 if (lastIsType) {
609 return renderType(last);
631 return renderType(/** @type {Type} */(last));
610632 }
611633
612634 if (lastIsDecl && last.kind === 'var') {
613 return renderVar(last);
635 return renderVar(/** @type {Decl} */(last));
614636 }
615637
616638 if (lastIsDecl && last.kind === 'const') {
617 var typeObj = zigAnalysis.types[resolveValue(last.value).type];
639 var typeObj = zigAnalysis.types[resolveValue(/** @type {Decl} */(last).value).type];
618640 if (typeObj && typeObj.kind === typeKinds.Fn) {
619 return renderFn(last);
641 return renderFn(/** @type {Decl} */(last));
620642 }
621643
622 return renderValue(last);
644 return renderValue(/** @type {Decl} */(last));
623645 }
624646 }
625647
648 /** @param {Decl} decl */
626649 function renderUnknownDecl(decl) {
627650 domDeclNoRef.classList.remove("hidden");
628651
......@@ -635,31 +658,34 @@ var zigAnalysis;
635658 domTldDocs.classList.remove("hidden");
636659 }
637660
661 /** @param {number} typeIndex */
638662 function typeIsErrSet(typeIndex) {
639663 var typeObj = zigAnalysis.types[typeIndex];
640664 return typeObj.kind === typeKinds.ErrorSet;
641665 }
642666
667 /** @param {number} typeIndex */
643668 function typeIsStructWithNoFields(typeIndex) {
644669 var typeObj = zigAnalysis.types[typeIndex];
645670 if (typeObj.kind !== typeKinds.Struct)
646671 return false;
647 return typeObj.fields.length == 0;
672 return /** @type {ContainerType} */(typeObj).fields.length == 0;
648673 }
649674
675 /** @param {number} typeIndex */
650676 function typeIsGenericFn(typeIndex) {
651677 var typeObj = zigAnalysis.types[typeIndex];
652678 if (typeObj.kind !== typeKinds.Fn) {
653679 return false;
654680 }
655 return typeObj.generic;
681 return /** @type {Fn} */(typeObj).generic;
656682 }
657683
658684 /** @param {Decl} fnDecl */
659685 function renderFn(fnDecl) {
660686 var value = resolveValue(fnDecl.value);
661687 console.assert("type" in value);
662 var typeObj = zigAnalysis.types[value.type];
688 var typeObj = /** @type {Fn} */(zigAnalysis.types[value.type]);
663689
664690 domFnProtoCode.innerHTML = typeValueName(value, true, true, fnDecl);
665691
......@@ -672,12 +698,12 @@ var zigAnalysis;
672698 var retIndex = resolveValue(typeObj.ret).type;
673699 renderFnParamDocs(fnDecl, typeObj);
674700
675 var errSetTypeIndex = null;
701 var errSetTypeIndex = /** @type {number | null} */(null);
676702 var retType = zigAnalysis.types[retIndex];
677703 if (retType.kind === typeKinds.ErrorSet) {
678704 errSetTypeIndex = retIndex;
679705 } else if (retType.kind === typeKinds.ErrorUnion) {
680 errSetTypeIndex = retType.err;
706 errSetTypeIndex = /** @type {ErrUnionType} */(retType).err.type;
681707 }
682708 if (errSetTypeIndex != null) {
683709 var errSetType = /** @type {ErrSetType} */(zigAnalysis.types[errSetTypeIndex]);