autodoc: fixed all type errors in main.js

This commit is contained in:
Loris Cro 2022-04-03 17:57:15 +02:00 committed by Andrew Kelley
parent 80f9490e06
commit 67da1b8c88

View File

@ -73,7 +73,8 @@
name: string,
src: number,
ret: WalkResult,
params: WalkResult[]
params: WalkResult[],
generic: boolean,
}} Fn
*/
@ -189,18 +190,15 @@
/**
* @typedef {{
name: string,
src?: number,
privDecls: number[],
pubDecls: number[],
fields?: WalkResult[],
typeRef: WalkResult,
fieldVals: WalkResult[],
}} Struct
*/
/**
* @typedef {{
len: WalkResult,
child: WalkResult,
typeRef: WalkResult,
data: WalkResult[],
}} ZigArray
*/
@ -280,7 +278,7 @@ var zigAnalysis;
var domSectSearchNoResults = /** @type HTMLElement */(document.getElementById("sectSearchNoResults"));
var domSectInfo = /** @type HTMLElement */(document.getElementById("sectInfo"));
var domTdTarget = /** @type HTMLElement */(document.getElementById("tdTarget"));
var domPrivDeclsBox = /** @type HTMLCheckboxElement */(document.getElementById("privDeclsBox"));
var domPrivDeclsBox = /** @type HTMLInputElement */(document.getElementById("privDeclsBox"));
var domTdZigVer = /** @type HTMLElement */(document.getElementById("tdZigVer"));
var domHdrName = /** @type HTMLElement */(document.getElementById("hdrName"));
var domHelpModal = /** @type HTMLElement */(document.getElementById("helpDialog"));
@ -313,7 +311,7 @@ var zigAnalysis;
* pkgNames: string[],
* pkgObjs: Package[],
* declNames: string[],
* declObjs: Decl[],
* declObjs: (Decl | Type)[],
* callName: any,
* }} CurNav
*/
@ -378,53 +376,68 @@ var zigAnalysis;
}
}
/** @param {Type | Decl} x */
function isDecl(x) {
return "value" in x;
}
/** @param {Type | Decl} x */
function isType(x) {
return "kind" in x && !("value" in x);
}
/** @param {Type | Decl} x */
function isContainerType(x) {
return isType(x) && typeKindIsContainer(x.kind) ;
return isType(x) && typeKindIsContainer(/** @type {Type} */(x).kind) ;
}
/** @param {Type} type */
function typeShorthandName(type) {
var name = type.name;
var name = undefined;
if (type.kind === typeKinds.Struct) {
name = "struct";
} else if (type.kind === typeKinds.Enum) {
name = "enum";
} else if (type.kind === typeKinds.Union) {
name= "union";
name = "union";
} else {
name = /** @type {any} */(type).name;
}
return escapeHtml(name);
}
/** @param {number} typeKind */
function typeKindIsContainer(typeKind) {
return typeKind === typeKinds.Struct ||
typeKind === typeKinds.Union ||
typeKind === typeKinds.Enum;
}
/** @param {number} typeKind */
function declCanRepresentTypeKind(typeKind) {
return typeKind === typeKinds.ErrorSet || typeKindIsContainer(typeKind);
}
/**
* @param {WalkResult[]} path
* @return {WalkResult | null}
*/
function findCteInRefPath(path) {
for (var i = path.length - 1; i >= 0; i -= 1) {
const ref = path[i];
if ("string" in ref) continue;
if ("comptimeExpr" in ref) return ref;
if ("refPath" in ref) return findCteinRefPath(ref.refPath);
if ("refPath" in ref) return findCteInRefPath(ref.refPath);
return null;
}
return null;
}
/**
* @param {WalkResult} value
* @return {WalkResult}
*/
function resolveValue(value) {
var i = 0;
while(i < 1000) {
@ -444,21 +457,26 @@ var zigAnalysis;
}
console.assert(false);
return /** @type {WalkResult} */({});
}
/**
* @param {Decl} decl
* @return {WalkResult}
*/
function typeOfDecl(decl){
var i = 0;
while(i < 1000) {
i += 1;
console.assert(isDecl(decl));
if ("type" in decl.value) {
return { type: typeTypeId };
return /** @type {WalkResult} */({ type: typeTypeId });
}
if ("refPath" in decl.value) {
decl = {
decl = /** @type {Decl} */({
value: decl.value.refPath[decl.value.refPath.length -1]
};
});
continue;
}
@ -495,22 +513,22 @@ var zigAnalysis;
fn_decl = zigAnalysis.decls[fn_call.func.declRef];
} else if ("refPath" in fn_call.func) {
console.assert("declRef" in fn_call.func.refPath[fn_call.func.refPath.length -1]);
fn_decl = zigAnalysis.decls[fn_call.func.refPath[fn_call.func.refPath.length -1].declRef.value];
fn_decl = zigAnalysis.decls[fn_call.func.refPath[fn_call.func.refPath.length -1].declRef];
} else throw {};
const fn_decl_value = resolveValue(fn_decl.value);
console.assert("type" in fn_decl_value); //TODO handle comptimeExpr
const fn_type = zigAnalysis.types[fn_decl_value.type];
const fn_type = /** @type {Fn} */(zigAnalysis.types[fn_decl_value.type]);
console.assert(fn_type.kind === typeKinds.Fn);
return fn_type.ret;
}
if ("void" in decl.value) {
return { type: typeTypeId };
return /** @type {WalkResult} */({ type: typeTypeId });
}
if ("bool" in decl.value) {
return { type: typeKinds.Bool };
return /** @type {WalkResult} */({ type: typeKinds.Bool });
}
console.log("TODO: handle in `typeOfDecl` more cases: ", decl);
@ -518,6 +536,7 @@ var zigAnalysis;
throw {};
}
console.assert(false);
return /** @type {WalkResult} */({});
}
function render() {
@ -569,15 +588,18 @@ var zigAnalysis;
curNav.pkgObjs.push(pkg);
}
/** @type {Decl | Type} */
var currentType = zigAnalysis.types[pkg.main];
curNav.declObjs = [currentType];
for (var i = 0; i < curNav.declNames.length; i += 1) {
var childDecl = findSubDecl(currentType, curNav.declNames[i]);
/** @type {Decl | Type | null} */
var childDecl = findSubDecl(/** @type {ContainerType} */(currentType), curNav.declNames[i]);
if (childDecl == null) {
return render404();
}
var childDeclValue = resolveValue(childDecl.value);
var childDeclValue = resolveValue(/** @type {Decl} */(childDecl).value);
if ("type" in childDeclValue) {
const t = zigAnalysis.types[childDeclValue.type];
@ -586,7 +608,7 @@ var zigAnalysis;
}
}
currentType = childDecl;
currentType = /** @type {Decl | Type} */(childDecl);
curNav.declObjs.push(currentType);
}
@ -598,31 +620,32 @@ var zigAnalysis;
var lastIsContainerType = isContainerType(last);
if (lastIsContainerType) {
return renderContainer(last);
return renderContainer(/** @type {ContainerType} */(last));
}
if (!lastIsDecl && !lastIsType) {
return renderUnknownDecl(last);
return renderUnknownDecl(/** @type {Decl} */(last));
}
if (lastIsType) {
return renderType(last);
return renderType(/** @type {Type} */(last));
}
if (lastIsDecl && last.kind === 'var') {
return renderVar(last);
return renderVar(/** @type {Decl} */(last));
}
if (lastIsDecl && last.kind === 'const') {
var typeObj = zigAnalysis.types[resolveValue(last.value).type];
var typeObj = zigAnalysis.types[resolveValue(/** @type {Decl} */(last).value).type];
if (typeObj && typeObj.kind === typeKinds.Fn) {
return renderFn(last);
return renderFn(/** @type {Decl} */(last));
}
return renderValue(last);
return renderValue(/** @type {Decl} */(last));
}
}
/** @param {Decl} decl */
function renderUnknownDecl(decl) {
domDeclNoRef.classList.remove("hidden");
@ -635,31 +658,34 @@ var zigAnalysis;
domTldDocs.classList.remove("hidden");
}
/** @param {number} typeIndex */
function typeIsErrSet(typeIndex) {
var typeObj = zigAnalysis.types[typeIndex];
return typeObj.kind === typeKinds.ErrorSet;
}
/** @param {number} typeIndex */
function typeIsStructWithNoFields(typeIndex) {
var typeObj = zigAnalysis.types[typeIndex];
if (typeObj.kind !== typeKinds.Struct)
return false;
return typeObj.fields.length == 0;
return /** @type {ContainerType} */(typeObj).fields.length == 0;
}
/** @param {number} typeIndex */
function typeIsGenericFn(typeIndex) {
var typeObj = zigAnalysis.types[typeIndex];
if (typeObj.kind !== typeKinds.Fn) {
return false;
}
return typeObj.generic;
return /** @type {Fn} */(typeObj).generic;
}
/** @param {Decl} fnDecl */
function renderFn(fnDecl) {
var value = resolveValue(fnDecl.value);
console.assert("type" in value);
var typeObj = zigAnalysis.types[value.type];
var typeObj = /** @type {Fn} */(zigAnalysis.types[value.type]);
domFnProtoCode.innerHTML = typeValueName(value, true, true, fnDecl);
@ -672,12 +698,12 @@ var zigAnalysis;
var retIndex = resolveValue(typeObj.ret).type;
renderFnParamDocs(fnDecl, typeObj);
var errSetTypeIndex = null;
var errSetTypeIndex = /** @type {number | null} */(null);
var retType = zigAnalysis.types[retIndex];
if (retType.kind === typeKinds.ErrorSet) {
errSetTypeIndex = retIndex;
} else if (retType.kind === typeKinds.ErrorUnion) {
errSetTypeIndex = retType.err;
errSetTypeIndex = /** @type {ErrUnionType} */(retType).err.type;
}
if (errSetTypeIndex != null) {
var errSetType = /** @type {ErrSetType} */(zigAnalysis.types[errSetTypeIndex]);