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 @@...@@ -73,7 +73,8 @@
73 name: string,73 name: string,
74 src: number,74 src: number,
75 ret: WalkResult,75 ret: WalkResult,
76 params: WalkResult[]76 params: WalkResult[],
77 generic: boolean,
77 }} Fn78 }} Fn
78*/79*/
7980
...@@ -189,18 +190,15 @@...@@ -189,18 +190,15 @@
189190
190/**191/**
191 * @typedef {{192 * @typedef {{
192 name: string,193 typeRef: WalkResult,
193 src?: number,194 fieldVals: WalkResult[],
194 privDecls: number[],
195 pubDecls: number[],
196 fields?: WalkResult[],
197 }} Struct195 }} Struct
198*/196*/
199197
200/**198/**
201 * @typedef {{199 * @typedef {{
202 len: WalkResult,200 typeRef: WalkResult,
203 child: WalkResult,201 data: WalkResult[],
204 }} ZigArray202 }} ZigArray
205*/203*/
206204
...@@ -280,7 +278,7 @@ var zigAnalysis;...@@ -280,7 +278,7 @@ var zigAnalysis;
280 var domSectSearchNoResults = /** @type HTMLElement */(document.getElementById("sectSearchNoResults"));278 var domSectSearchNoResults = /** @type HTMLElement */(document.getElementById("sectSearchNoResults"));
281 var domSectInfo = /** @type HTMLElement */(document.getElementById("sectInfo"));279 var domSectInfo = /** @type HTMLElement */(document.getElementById("sectInfo"));
282 var domTdTarget = /** @type HTMLElement */(document.getElementById("tdTarget"));280 var domTdTarget = /** @type HTMLElement */(document.getElementById("tdTarget"));
283 var domPrivDeclsBox = /** @type HTMLCheckboxElement */(document.getElementById("privDeclsBox"));281 var domPrivDeclsBox = /** @type HTMLInputElement */(document.getElementById("privDeclsBox"));
284 var domTdZigVer = /** @type HTMLElement */(document.getElementById("tdZigVer"));282 var domTdZigVer = /** @type HTMLElement */(document.getElementById("tdZigVer"));
285 var domHdrName = /** @type HTMLElement */(document.getElementById("hdrName"));283 var domHdrName = /** @type HTMLElement */(document.getElementById("hdrName"));
286 var domHelpModal = /** @type HTMLElement */(document.getElementById("helpDialog"));284 var domHelpModal = /** @type HTMLElement */(document.getElementById("helpDialog"));
...@@ -313,7 +311,7 @@ var zigAnalysis;...@@ -313,7 +311,7 @@ var zigAnalysis;
313 * pkgNames: string[],311 * pkgNames: string[],
314 * pkgObjs: Package[],312 * pkgObjs: Package[],
315 * declNames: string[],313 * declNames: string[],
316 * declObjs: Decl[],314 * declObjs: (Decl | Type)[],
317 * callName: any,315 * callName: any,
318 * }} CurNav316 * }} CurNav
319 */317 */
...@@ -378,53 +376,68 @@ var zigAnalysis;...@@ -378,53 +376,68 @@ var zigAnalysis;
378 }376 }
379 }377 }
380378
379 /** @param {Type | Decl} x */
381 function isDecl(x) {380 function isDecl(x) {
382 return "value" in x;381 return "value" in x;
383 }382 }
384383
384 /** @param {Type | Decl} x */
385 function isType(x) {385 function isType(x) {
386 return "kind" in x && !("value" in x);386 return "kind" in x && !("value" in x);
387 }387 }
388388
389 /** @param {Type | Decl} x */
389 function isContainerType(x) {390 function isContainerType(x) {
390 return isType(x) && typeKindIsContainer(x.kind) ;391 return isType(x) && typeKindIsContainer(/** @type {Type} */(x).kind) ;
391 }392 }
392393
394 /** @param {Type} type */
393 function typeShorthandName(type) {395 function typeShorthandName(type) {
394 var name = type.name;396 var name = undefined;
395 if (type.kind === typeKinds.Struct) {397 if (type.kind === typeKinds.Struct) {
396 name = "struct";398 name = "struct";
397 } else if (type.kind === typeKinds.Enum) {399 } else if (type.kind === typeKinds.Enum) {
398 name = "enum";400 name = "enum";
399 } else if (type.kind === typeKinds.Union) {401 } else if (type.kind === typeKinds.Union) {
400 name= "union";402 name = "union";
403 } else {
404 name = /** @type {any} */(type).name;
401 }405 }
402
403 return escapeHtml(name);406 return escapeHtml(name);
404 }407 }
405408
409 /** @param {number} typeKind */
406 function typeKindIsContainer(typeKind) {410 function typeKindIsContainer(typeKind) {
407 return typeKind === typeKinds.Struct ||411 return typeKind === typeKinds.Struct ||
408 typeKind === typeKinds.Union ||412 typeKind === typeKinds.Union ||
409 typeKind === typeKinds.Enum;413 typeKind === typeKinds.Enum;
410 }414 }
411415
416 /** @param {number} typeKind */
412 function declCanRepresentTypeKind(typeKind) {417 function declCanRepresentTypeKind(typeKind) {
413 return typeKind === typeKinds.ErrorSet || typeKindIsContainer(typeKind);418 return typeKind === typeKinds.ErrorSet || typeKindIsContainer(typeKind);
414 }419 }
415420
421 /**
422 * @param {WalkResult[]} path
423 * @return {WalkResult | null}
424 */
416 function findCteInRefPath(path) {425 function findCteInRefPath(path) {
417 for (var i = path.length - 1; i >= 0; i -= 1) {426 for (var i = path.length - 1; i >= 0; i -= 1) {
418 const ref = path[i];427 const ref = path[i];
419 if ("string" in ref) continue;428 if ("string" in ref) continue;
420 if ("comptimeExpr" in ref) return ref;429 if ("comptimeExpr" in ref) return ref;
421 if ("refPath" in ref) return findCteinRefPath(ref.refPath);430 if ("refPath" in ref) return findCteInRefPath(ref.refPath);
422 return null;431 return null;
423 }432 }
424433
425 return null;434 return null;
426 }435 }
427436
437 /**
438 * @param {WalkResult} value
439 * @return {WalkResult}
440 */
428 function resolveValue(value) {441 function resolveValue(value) {
429 var i = 0;442 var i = 0;
430 while(i < 1000) {443 while(i < 1000) {
...@@ -444,21 +457,26 @@ var zigAnalysis;...@@ -444,21 +457,26 @@ var zigAnalysis;
444457
445 }458 }
446 console.assert(false);459 console.assert(false);
460 return /** @type {WalkResult} */({});
447 }461 }
448462
463 /**
464 * @param {Decl} decl
465 * @return {WalkResult}
466 */
449 function typeOfDecl(decl){467 function typeOfDecl(decl){
450 var i = 0;468 var i = 0;
451 while(i < 1000) {469 while(i < 1000) {
452 i += 1;470 i += 1;
453 console.assert(isDecl(decl));471 console.assert(isDecl(decl));
454 if ("type" in decl.value) {472 if ("type" in decl.value) {
455 return { type: typeTypeId };473 return /** @type {WalkResult} */({ type: typeTypeId });
456 }474 }
457475
458 if ("refPath" in decl.value) {476 if ("refPath" in decl.value) {
459 decl = {477 decl = /** @type {Decl} */({
460 value: decl.value.refPath[decl.value.refPath.length -1]478 value: decl.value.refPath[decl.value.refPath.length -1]
461 };479 });
462 continue;480 continue;
463 }481 }
464482
...@@ -495,22 +513,22 @@ var zigAnalysis;...@@ -495,22 +513,22 @@ var zigAnalysis;
495 fn_decl = zigAnalysis.decls[fn_call.func.declRef];513 fn_decl = zigAnalysis.decls[fn_call.func.declRef];
496 } else if ("refPath" in fn_call.func) {514 } else if ("refPath" in fn_call.func) {
497 console.assert("declRef" in fn_call.func.refPath[fn_call.func.refPath.length -1]);515 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];
499 } else throw {};517 } else throw {};
500518
501 const fn_decl_value = resolveValue(fn_decl.value);519 const fn_decl_value = resolveValue(fn_decl.value);
502 console.assert("type" in fn_decl_value); //TODO handle comptimeExpr520 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]);
504 console.assert(fn_type.kind === typeKinds.Fn);522 console.assert(fn_type.kind === typeKinds.Fn);
505 return fn_type.ret;523 return fn_type.ret;
506 }524 }
507525
508 if ("void" in decl.value) {526 if ("void" in decl.value) {
509 return { type: typeTypeId };527 return /** @type {WalkResult} */({ type: typeTypeId });
510 }528 }
511529
512 if ("bool" in decl.value) {530 if ("bool" in decl.value) {
513 return { type: typeKinds.Bool };531 return /** @type {WalkResult} */({ type: typeKinds.Bool });
514 }532 }
515533
516 console.log("TODO: handle in `typeOfDecl` more cases: ", decl);534 console.log("TODO: handle in `typeOfDecl` more cases: ", decl);
...@@ -518,6 +536,7 @@ var zigAnalysis;...@@ -518,6 +536,7 @@ var zigAnalysis;
518 throw {};536 throw {};
519 }537 }
520 console.assert(false);538 console.assert(false);
539 return /** @type {WalkResult} */({});
521 }540 }
522541
523 function render() {542 function render() {
...@@ -569,15 +588,18 @@ var zigAnalysis;...@@ -569,15 +588,18 @@ var zigAnalysis;
569 curNav.pkgObjs.push(pkg);588 curNav.pkgObjs.push(pkg);
570 }589 }
571590
591 /** @type {Decl | Type} */
572 var currentType = zigAnalysis.types[pkg.main];592 var currentType = zigAnalysis.types[pkg.main];
573 curNav.declObjs = [currentType];593 curNav.declObjs = [currentType];
574 for (var i = 0; i < curNav.declNames.length; i += 1) {594 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]);
576 if (childDecl == null) {598 if (childDecl == null) {
577 return render404();599 return render404();
578 }600 }
579601
580 var childDeclValue = resolveValue(childDecl.value);602 var childDeclValue = resolveValue(/** @type {Decl} */(childDecl).value);
581 if ("type" in childDeclValue) {603 if ("type" in childDeclValue) {
582604
583 const t = zigAnalysis.types[childDeclValue.type];605 const t = zigAnalysis.types[childDeclValue.type];
...@@ -586,7 +608,7 @@ var zigAnalysis;...@@ -586,7 +608,7 @@ var zigAnalysis;
586 }608 }
587 }609 }
588610
589 currentType = childDecl;611 currentType = /** @type {Decl | Type} */(childDecl);
590 curNav.declObjs.push(currentType);612 curNav.declObjs.push(currentType);
591 }613 }
592614
...@@ -598,31 +620,32 @@ var zigAnalysis;...@@ -598,31 +620,32 @@ var zigAnalysis;
598 var lastIsContainerType = isContainerType(last);620 var lastIsContainerType = isContainerType(last);
599621
600 if (lastIsContainerType) {622 if (lastIsContainerType) {
601 return renderContainer(last);623 return renderContainer(/** @type {ContainerType} */(last));
602 }624 }
603625
604 if (!lastIsDecl && !lastIsType) {626 if (!lastIsDecl && !lastIsType) {
605 return renderUnknownDecl(last);627 return renderUnknownDecl(/** @type {Decl} */(last));
606 }628 }
607629
608 if (lastIsType) {630 if (lastIsType) {
609 return renderType(last);631 return renderType(/** @type {Type} */(last));
610 }632 }
611633
612 if (lastIsDecl && last.kind === 'var') {634 if (lastIsDecl && last.kind === 'var') {
613 return renderVar(last);635 return renderVar(/** @type {Decl} */(last));
614 }636 }
615637
616 if (lastIsDecl && last.kind === 'const') {638 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];
618 if (typeObj && typeObj.kind === typeKinds.Fn) {640 if (typeObj && typeObj.kind === typeKinds.Fn) {
619 return renderFn(last);641 return renderFn(/** @type {Decl} */(last));
620 }642 }
621643
622 return renderValue(last);644 return renderValue(/** @type {Decl} */(last));
623 }645 }
624 }646 }
625647
648 /** @param {Decl} decl */
626 function renderUnknownDecl(decl) {649 function renderUnknownDecl(decl) {
627 domDeclNoRef.classList.remove("hidden");650 domDeclNoRef.classList.remove("hidden");
628651
...@@ -635,31 +658,34 @@ var zigAnalysis;...@@ -635,31 +658,34 @@ var zigAnalysis;
635 domTldDocs.classList.remove("hidden");658 domTldDocs.classList.remove("hidden");
636 }659 }
637660
661 /** @param {number} typeIndex */
638 function typeIsErrSet(typeIndex) {662 function typeIsErrSet(typeIndex) {
639 var typeObj = zigAnalysis.types[typeIndex];663 var typeObj = zigAnalysis.types[typeIndex];
640 return typeObj.kind === typeKinds.ErrorSet;664 return typeObj.kind === typeKinds.ErrorSet;
641 }665 }
642666
667 /** @param {number} typeIndex */
643 function typeIsStructWithNoFields(typeIndex) {668 function typeIsStructWithNoFields(typeIndex) {
644 var typeObj = zigAnalysis.types[typeIndex];669 var typeObj = zigAnalysis.types[typeIndex];
645 if (typeObj.kind !== typeKinds.Struct)670 if (typeObj.kind !== typeKinds.Struct)
646 return false;671 return false;
647 return typeObj.fields.length == 0;672 return /** @type {ContainerType} */(typeObj).fields.length == 0;
648 }673 }
649674
675 /** @param {number} typeIndex */
650 function typeIsGenericFn(typeIndex) {676 function typeIsGenericFn(typeIndex) {
651 var typeObj = zigAnalysis.types[typeIndex];677 var typeObj = zigAnalysis.types[typeIndex];
652 if (typeObj.kind !== typeKinds.Fn) {678 if (typeObj.kind !== typeKinds.Fn) {
653 return false;679 return false;
654 }680 }
655 return typeObj.generic;681 return /** @type {Fn} */(typeObj).generic;
656 }682 }
657683
658 /** @param {Decl} fnDecl */684 /** @param {Decl} fnDecl */
659 function renderFn(fnDecl) {685 function renderFn(fnDecl) {
660 var value = resolveValue(fnDecl.value);686 var value = resolveValue(fnDecl.value);
661 console.assert("type" in value);687 console.assert("type" in value);
662 var typeObj = zigAnalysis.types[value.type];688 var typeObj = /** @type {Fn} */(zigAnalysis.types[value.type]);
663689
664 domFnProtoCode.innerHTML = typeValueName(value, true, true, fnDecl);690 domFnProtoCode.innerHTML = typeValueName(value, true, true, fnDecl);
665691
...@@ -672,12 +698,12 @@ var zigAnalysis;...@@ -672,12 +698,12 @@ var zigAnalysis;
672 var retIndex = resolveValue(typeObj.ret).type;698 var retIndex = resolveValue(typeObj.ret).type;
673 renderFnParamDocs(fnDecl, typeObj);699 renderFnParamDocs(fnDecl, typeObj);
674700
675 var errSetTypeIndex = null;701 var errSetTypeIndex = /** @type {number | null} */(null);
676 var retType = zigAnalysis.types[retIndex];702 var retType = zigAnalysis.types[retIndex];
677 if (retType.kind === typeKinds.ErrorSet) {703 if (retType.kind === typeKinds.ErrorSet) {
678 errSetTypeIndex = retIndex;704 errSetTypeIndex = retIndex;
679 } else if (retType.kind === typeKinds.ErrorUnion) {705 } else if (retType.kind === typeKinds.ErrorUnion) {
680 errSetTypeIndex = retType.err;706 errSetTypeIndex = /** @type {ErrUnionType} */(retType).err.type;
681 }707 }
682 if (errSetTypeIndex != null) {708 if (errSetTypeIndex != null) {
683 var errSetType = /** @type {ErrSetType} */(zigAnalysis.types[errSetTypeIndex]);709 var errSetType = /** @type {ErrSetType} */(zigAnalysis.types[errSetTypeIndex]);