authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-03-31 19:07:57+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-19 19:10:11-07:00
log919b8e400ca7b2885221a4a93ba5ab3192046480
treeca16326ccc5769b3f03510a910576ca19e9b0821
parent72e5b4fb747908d4cf4e5039c671ec729e5b7716

autodoc: more typechecking in main.js


1 files changed, 200 insertions(+), 133 deletions(-)

lib/docs/main.js+200-133
......@@ -56,31 +56,39 @@
5656*/
5757
5858/**
59 * @typedef {
60 | { kind: number, name: string; src: number; privDecls: number[]; pubDecls: number[]; fields: WalkResult[] } // Struct, Enum, Union
61 } ContainerType
59 * @typedef {{
60 kind: number,
61 name: string,
62 src: number,
63 privDecls: number[],
64 pubDecls: number[],
65 fields: WalkResult[]
66}} ContainerType
67*/
68
69/**
70 * @typedef {{
71 kind: number,
72 name: string,
73 src: number,
74 ret: WalkResult,
75 params: WalkResult[]
76 }} Fn
6277*/
6378
79
6480/**
6581 * @typedef {
6682 | { kind: number, name: string } // Type, Void, Bool, NoReturn, Int, Float, ComptimeExpr, ComptimeFloat, ComptimeInt, Undefined, Null, ErrorUnion, BoundFn, Opaque, Frame, AnyFrame, Vector, EnumLiteral
67 | { kind: number, name: string; child: TypeRef } // Optional
68 | { kind: number, len: WalkResult; child: TypeRef } // Array
83 | { kind: number, name: string; child: WalkResult } // Optional
84 | { kind: number, len: WalkResult; child: WalkResult } // Array
6985 | { kind: number, name: string; fields: { name: string; docs: string }[] } // ErrorSet
70 | { kind: number, size: "One" | "Many" | "Slice" | "C"; child: TypeRef } // Pointer
86 | { kind: number, size: "One" | "Many" | "Slice" | "C"; child: WalkResult } // Pointer
7187 | ContainerType
72 | { kind: number, name: string; src: number; ret: WalkResult; params: WalkResult[] } // Fn
88 | Fn
7389 } Type
7490*/
7591
76/**
77 * @typedef {{
78 name: string,
79 src: number | null,
80 ret: WalkResult,
81 params: WalkResult[] | null,
82 }} Fn
83*/
8492
8593/**
8694 * @typedef {{
......@@ -109,6 +117,7 @@
109117 src: number,
110118 value: WalkResult,
111119 decltest?: number,
120 isTest: bool,
112121 }} Decl
113122*/
114123
......@@ -117,7 +126,7 @@
117126 name: string,
118127 file: number,
119128 main: number,
120 table: { root: number },
129 table: Record<string, number>,
121130 }} Package
122131*/
123132
......@@ -225,17 +234,34 @@ var zigAnalysis;
225234 /** @type Object<string, string> */
226235 var escapeHtmlReplacements = { "&": "&amp;", '"': "&quot;", "<": "&lt;", ">": "&gt;" };
227236
228 var typeKinds = indexTypeKinds();
229 var typeTypeId = findTypeTypeId();
237 var typeKinds = /** @type {Record<string, number>} */(indexTypeKinds());
238 var typeTypeId = /** @type {number} */ (findTypeTypeId());
230239 var pointerSizeEnum = { One: 0, Many: 1, Slice: 2, C: 3 };
231240
232241 // for each package, is an array with packages to get to this one
233242 var canonPkgPaths = computeCanonicalPackagePaths();
243
244 /** @typedef {{declNames: string[], pkgNames: string[]}} CanonDecl */
245
234246 // for each decl, is an array with {declNames, pkgNames} to get to this one
247 /** @type CanonDecl[] | null */
235248 var canonDeclPaths = null; // lazy; use getCanonDeclPath
249
236250 // for each type, is an array with {declNames, pkgNames} to get to this one
251 /** @type number[] | null */
237252 var canonTypeDecls = null; // lazy; use getCanonTypeDecl
238253
254 /** @typedef {{
255 * showPrivDecls: bool,
256 * pkgNames: string[],
257 * pkgObjs: Package[],
258 * declNames: string[],
259 * declObjs: Decl[],
260 * callName: any,
261 * }} CurNav
262 */
263
264 /** @type {CurNav} */
239265 var curNav = {
240266 showPrivDecls: false,
241267 // each element is a package name, e.g. @import("a") then within there @import("b")
......@@ -252,6 +278,7 @@ var zigAnalysis;
252278 // (a, b, c, d) comptime call; result is the value the docs refer to
253279 callName: null,
254280 };
281
255282 var curNavSearch = "";
256283 var curSearchIndex = -1;
257284 var imFeelingLucky = false;
......@@ -606,7 +633,7 @@ var zigAnalysis;
606633 if (instantiations == null && calls == null) {
607634 domFnNoExamples.classList.remove("hidden");
608635 } else if (calls != null) {
609 if (fnObj.combined === undefined) fnObj.combined = allCompTimeFnCallsResult(calls);
636 // if (fnObj.combined === undefined) fnObj.combined = allCompTimeFnCallsResult(calls);
610637 if (fnObj.combined != null) renderContainer(fnObj.combined);
611638
612639 resizeDomList(domListFnExamples, calls.length, '<li></li>');
......@@ -848,6 +875,14 @@ var zigAnalysis;
848875 }
849876 }
850877
878 /**
879 * @param {WalkResult} typeValue,
880 * @param {boolean} wantHtml,
881 * @param {boolean} wantLink,
882 * @param {number} [fnDecl],
883 * @param {string} [linkFnNameDecl],
884 * @return {string}
885 */
851886 function typeValueName(typeValue, wantHtml, wantLink, fnDecl, linkFnNameDecl) {
852887
853888 if ("int" in typeValue) {
......@@ -984,6 +1019,14 @@ var zigAnalysis;
9841019 }
9851020 }
9861021
1022 /**
1023 * @param {Type} typeObj,
1024 * @param {boolean} wantHtml,
1025 * @param {boolean} wantSubLink,
1026 * @param {number} [fnDecl],
1027 * @param {string} [linkFnNameDecl],
1028 * @return {string}
1029 */
9871030 function typeName(typeObj, wantHtml, wantSubLink, fnDecl, linkFnNameDecl) {
9881031 switch (typeObj.kind) {
9891032 case typeKinds.Array:
......@@ -1294,111 +1337,113 @@ var zigAnalysis;
12941337 domSectFnErrors.classList.remove("hidden");
12951338 }
12961339
1297 function allCompTimeFnCallsHaveTypeResult(typeIndex, value) {
1298 var srcIndex = zigAnalysis.fns[value].src;
1299 var calls = nodesToCallsMap[srcIndex];
1300 if (calls == null) return false;
1301 for (var i = 0; i < calls.length; i += 1) {
1302 var call = zigAnalysis.calls[calls[i]];
1303 if (call.result.type !== typeTypeId) return false;
1304 }
1305 return true;
1306 }
1307
1308 function allCompTimeFnCallsResult(calls) {
1309 var firstTypeObj = null;
1310 var containerObj = {
1311 privDecls: [],
1312 };
1313 for (var callI = 0; callI < calls.length; callI += 1) {
1314 var call = zigAnalysis.calls[calls[callI]];
1315 if (call.result.type !== typeTypeId) return null;
1316 var typeObj = zigAnalysis.types[call.result.value];
1317 if (!typeKindIsContainer(typeObj.kind)) return null;
1318 if (firstTypeObj == null) {
1319 firstTypeObj = typeObj;
1320 containerObj.src = typeObj.src;
1321 } else if (firstTypeObj.src !== typeObj.src) {
1322 return null;
1323 }
1324
1325 if (containerObj.fields == null) {
1326 containerObj.fields = (typeObj.fields || []).concat([]);
1327 } else for (var fieldI = 0; fieldI < typeObj.fields.length; fieldI += 1) {
1328 var prev = containerObj.fields[fieldI];
1329 var next = typeObj.fields[fieldI];
1330 if (prev === next) continue;
1331 if (typeof(prev) === 'object') {
1332 if (prev[next] == null) prev[next] = typeObj;
1333 } else {
1334 containerObj.fields[fieldI] = {};
1335 containerObj.fields[fieldI][prev] = firstTypeObj;
1336 containerObj.fields[fieldI][next] = typeObj;
1337 }
1338 }
1339
1340 if (containerObj.pubDecls == null) {
1341 containerObj.pubDecls = (typeObj.pubDecls || []).concat([]);
1342 } else for (var declI = 0; declI < typeObj.pubDecls.length; declI += 1) {
1343 var prev = containerObj.pubDecls[declI];
1344 var next = typeObj.pubDecls[declI];
1345 if (prev === next) continue;
1346 // TODO instead of showing "examples" as the public declarations,
1347 // do logic like this:
1348 //if (typeof(prev) !== 'object') {
1349 // var newDeclId = zigAnalysis.decls.length;
1350 // prev = clone(zigAnalysis.decls[prev]);
1351 // prev.id = newDeclId;
1352 // zigAnalysis.decls.push(prev);
1353 // containerObj.pubDecls[declI] = prev;
1354 //}
1355 //mergeDecls(prev, next, firstTypeObj, typeObj);
1356 }
1357 }
1358 for (var declI = 0; declI < containerObj.pubDecls.length; declI += 1) {
1359 var decl = containerObj.pubDecls[declI];
1360 if (typeof(decl) === 'object') {
1361 containerObj.pubDecls[declI] = containerObj.pubDecls[declI].id;
1362 }
1363 }
1364 return containerObj;
1365 }
1340// function allCompTimeFnCallsHaveTypeResult(typeIndex, value) {
1341// var srcIndex = zigAnalysis.fns[value].src;
1342// var calls = nodesToCallsMap[srcIndex];
1343// if (calls == null) return false;
1344// for (var i = 0; i < calls.length; i += 1) {
1345// var call = zigAnalysis.calls[calls[i]];
1346// if (call.result.type !== typeTypeId) return false;
1347// }
1348// return true;
1349// }
1350//
1351// function allCompTimeFnCallsResult(calls) {
1352// var firstTypeObj = null;
1353// var containerObj = {
1354// privDecls: [],
1355// };
1356// for (var callI = 0; callI < calls.length; callI += 1) {
1357// var call = zigAnalysis.calls[calls[callI]];
1358// if (call.result.type !== typeTypeId) return null;
1359// var typeObj = zigAnalysis.types[call.result.value];
1360// if (!typeKindIsContainer(typeObj.kind)) return null;
1361// if (firstTypeObj == null) {
1362// firstTypeObj = typeObj;
1363// containerObj.src = typeObj.src;
1364// } else if (firstTypeObj.src !== typeObj.src) {
1365// return null;
1366// }
1367//
1368// if (containerObj.fields == null) {
1369// containerObj.fields = (typeObj.fields || []).concat([]);
1370// } else for (var fieldI = 0; fieldI < typeObj.fields.length; fieldI += 1) {
1371// var prev = containerObj.fields[fieldI];
1372// var next = typeObj.fields[fieldI];
1373// if (prev === next) continue;
1374// if (typeof(prev) === 'object') {
1375// if (prev[next] == null) prev[next] = typeObj;
1376// } else {
1377// containerObj.fields[fieldI] = {};
1378// containerObj.fields[fieldI][prev] = firstTypeObj;
1379// containerObj.fields[fieldI][next] = typeObj;
1380// }
1381// }
1382//
1383// if (containerObj.pubDecls == null) {
1384// containerObj.pubDecls = (typeObj.pubDecls || []).concat([]);
1385// } else for (var declI = 0; declI < typeObj.pubDecls.length; declI += 1) {
1386// var prev = containerObj.pubDecls[declI];
1387// var next = typeObj.pubDecls[declI];
1388// if (prev === next) continue;
1389// // TODO instead of showing "examples" as the public declarations,
1390// // do logic like this:
1391// //if (typeof(prev) !== 'object') {
1392// // var newDeclId = zigAnalysis.decls.length;
1393// // prev = clone(zigAnalysis.decls[prev]);
1394// // prev.id = newDeclId;
1395// // zigAnalysis.decls.push(prev);
1396// // containerObj.pubDecls[declI] = prev;
1397// //}
1398// //mergeDecls(prev, next, firstTypeObj, typeObj);
1399// }
1400// }
1401// for (var declI = 0; declI < containerObj.pubDecls.length; declI += 1) {
1402// var decl = containerObj.pubDecls[declI];
1403// if (typeof(decl) === 'object') {
1404// containerObj.pubDecls[declI] = containerObj.pubDecls[declI].id;
1405// }
1406// }
1407// return containerObj;
1408// }
13661409
1367 function mergeDecls(declObj, nextDeclIndex, firstTypeObj, typeObj) {
1368 var nextDeclObj = zigAnalysis.decls[nextDeclIndex];
1369 if (declObj.type != null && nextDeclObj.type != null && declObj.type !== nextDeclObj.type) {
1370 if (typeof(declObj.type) !== 'object') {
1371 var prevType = declObj.type;
1372 declObj.type = {};
1373 declObj.type[prevType] = firstTypeObj;
1374 declObj.value = null;
1375 }
1376 declObj.type[nextDeclObj.type] = typeObj;
1377 } else if (declObj.type == null && nextDeclObj != null) {
1378 declObj.type = nextDeclObj.type;
1379 }
1380 if (declObj.value != null && nextDeclObj.value != null && declObj.value !== nextDeclObj.value) {
1381 if (typeof(declObj.value) !== 'object') {
1382 var prevValue = declObj.value;
1383 declObj.value = {};
1384 declObj.value[prevValue] = firstTypeObj;
1385 }
1386 declObj.value[nextDeclObj.value] = typeObj;
1387 } else if (declObj.value == null && nextDeclObj.value != null) {
1388 declObj.value = nextDeclObj.value;
1389 }
1390 }
13911410
1411 // function mergeDecls(declObj, nextDeclIndex, firstTypeObj, typeObj) {
1412 // var nextDeclObj = zigAnalysis.decls[nextDeclIndex];
1413 // if (declObj.type != null && nextDeclObj.type != null && declObj.type !== nextDeclObj.type) {
1414 // if (typeof(declObj.type) !== 'object') {
1415 // var prevType = declObj.type;
1416 // declObj.type = {};
1417 // declObj.type[prevType] = firstTypeObj;
1418 // declObj.value = null;
1419 // }
1420 // declObj.type[nextDeclObj.type] = typeObj;
1421 // } else if (declObj.type == null && nextDeclObj != null) {
1422 // declObj.type = nextDeclObj.type;
1423 // }
1424 // if (declObj.value != null && nextDeclObj.value != null && declObj.value !== nextDeclObj.value) {
1425 // if (typeof(declObj.value) !== 'object') {
1426 // var prevValue = declObj.value;
1427 // declObj.value = {};
1428 // declObj.value[prevValue] = firstTypeObj;
1429 // }
1430 // declObj.value[nextDeclObj.value] = typeObj;
1431 // } else if (declObj.value == null && nextDeclObj.value != null) {
1432 // declObj.value = nextDeclObj.value;
1433 // }
1434 // }
1435
1436 /** @param {Decl} decl */
13921437 function renderValue(decl) {
13931438
13941439 var declTypeRef = typeOfDecl(decl);
13951440 var declValueText = "";
13961441 switch(Object.keys(decl.value)[0]) {
13971442 case "int":
1398 declValueText += decl.value.int.value;
1443 declValueText += /** @type {{int: {value: number}}} */(decl.value).int.value;
13991444 break;
14001445 case "float":
1401 declValueText += decl.value.float.value;
1446 declValueText += /** @type {{float: {value: number}}} */(decl.value).float.value;
14021447 break;
14031448 case "comptimeExpr":
14041449 declValueText += "[ComptimeExpr]";
......@@ -1421,6 +1466,7 @@ var zigAnalysis;
14211466 domFnProto.classList.remove("hidden");
14221467 }
14231468
1469 /** @param {Decl} decl */
14241470 function renderVar(decl) {
14251471 var declTypeRef = typeOfDecl(decl);
14261472 domFnProtoCode.innerHTML = '<span class="tok-kw">var</span> ' +
......@@ -1438,7 +1484,13 @@ var zigAnalysis;
14381484
14391485 /**
14401486 * @param {number[]} decls
1441 * @param {Decl[]} typesList, namespacesList, errSetsList, fnsList, varsList, valsList, testsList
1487 * @param {Decl[]} typesList
1488 * @param {Decl[]} namespacesList,
1489 * @param {Decl[]} errSetsList,
1490 * @param {Decl[]} fnsList,
1491 * @param {Decl[]} varsList,
1492 * @param {Decl[]} valsList,
1493 * @param {Decl[]} testsList
14421494 */
14431495 function categorizeDecls(decls,
14441496 typesList, namespacesList, errSetsList,
......@@ -1482,7 +1534,7 @@ var zigAnalysis;
14821534 var kind = value.kind;
14831535 if (kind === typeKinds.Fn) {
14841536 // TODO: handle CTE return types when we know their type.
1485 const resVal = resolveValue(value.ret);
1537 const resVal = resolveValue(/** @type {Fn} */(value).ret);
14861538 if ("type" in resVal && resVal.type == typeTypeId) {
14871539 typesList.push(decl);
14881540 } else {
......@@ -1610,20 +1662,17 @@ var zigAnalysis;
16101662 for (var i = 0; i < containerNode.fields.length; i += 1) {
16111663 var fieldNode = zigAnalysis.astNodes[containerNode.fields[i]];
16121664 var divDom = domListFields.children[i];
1665 let fieldName = /** @type {string} */(fieldNode.name);
16131666
1614 var html = '<div class="mobile-scroll-container"><pre class="scroll-item">' + escapeHtml(fieldNode.name);
1667 var html = '<div class="mobile-scroll-container"><pre class="scroll-item">' + escapeHtml(fieldName);
16151668
16161669 if (container.kind === typeKinds.Enum) {
1617 html += ' = <span class="tok-number">' + field + '</span>';
1670 html += ' = <span class="tok-number">' + fieldName + '</span>';
16181671 } else {
16191672 var field = container.fields[i];
16201673 html += ": ";
1621 if (field.failure === true) {
1622 html += '<span class="tok-kw" style="color:red;">#FAILURE#</span>';
1623 } else {
1624 var name = typeValueName(field);
1625 html += '<span class="tok-kw">'+ name +'</span>';
1626 }
1674 var name = typeValueName(field);
1675 html += '<span class="tok-kw">'+ name +'</span>';
16271676 }
16281677
16291678 html += ',</pre></div>';
......@@ -1719,6 +1768,11 @@ var zigAnalysis;
17191768 }
17201769 }
17211770
1771
1772 /**
1773 * @param {string | number} a
1774 * @param {string | number} b
1775 */
17221776 function operatorCompare(a, b) {
17231777 if (a === b) {
17241778 return 0;
......@@ -1741,7 +1795,7 @@ var zigAnalysis;
17411795 }
17421796
17431797 function indexTypeKinds() {
1744 var map = {};
1798 var map = /** @type {Record<string, number>} */({});
17451799 for (var i = 0; i < zigAnalysis.typeKinds.length; i += 1) {
17461800 map[zigAnalysis.typeKinds[i]] = i;
17471801 }
......@@ -1765,12 +1819,14 @@ var zigAnalysis;
17651819 }
17661820
17671821 function updateCurNav() {
1822
17681823 curNav = {
17691824 showPrivDecls: false,
17701825 pkgNames: [],
17711826 pkgObjs: [],
17721827 declNames: [],
17731828 declObjs: [],
1829 callName: null,
17741830 };
17751831 curNavSearch = "";
17761832
......@@ -1814,6 +1870,10 @@ var zigAnalysis;
18141870 }
18151871 }
18161872
1873 /**
1874 * @param {ContainerType} parentType
1875 * @param {string} childName
1876 */
18171877 function findSubDecl(parentType, childName) {
18181878 if (!parentType.pubDecls) return null;
18191879 for (var i = 0; i < parentType.pubDecls.length; i += 1) {
......@@ -1843,11 +1903,11 @@ var zigAnalysis;
18431903 var rootPkg = zigAnalysis.packages[zigAnalysis.rootPkg];
18441904 // Breadth-first to keep the path shortest possible.
18451905 var stack = [{
1846 path: [],
1906 path: /** @type {string[]} */([]),
18471907 pkg: rootPkg,
18481908 }];
18491909 while (stack.length !== 0) {
1850 var item = stack.shift();
1910 var item = /** @type {{path: string[], pkg: Package}} */(stack.shift());
18511911 for (var key in item.pkg.table) {
18521912 var childPkgIndex = item.pkg.table[key];
18531913 if (list[childPkgIndex] != null) continue;
......@@ -1866,6 +1926,7 @@ var zigAnalysis;
18661926 }
18671927
18681928
1929 /** @return {CanonDecl[]} */
18691930 function computeCanonDeclPaths() {
18701931 var list = new Array(zigAnalysis.decls.length);
18711932 canonTypeDecls = new Array(zigAnalysis.types.length);
......@@ -1875,16 +1936,18 @@ var zigAnalysis;
18751936 var pkg = zigAnalysis.packages[pkgI];
18761937 var pkgNames = canonPkgPaths[pkgI];
18771938 var stack = [{
1878 declNames: [],
1939 declNames: /** @type {string[]} */([]),
18791940 type: zigAnalysis.types[pkg.main],
18801941 }];
18811942 while (stack.length !== 0) {
1882 var item = stack.shift();
1943 var item = /** @type {{declNames: string[], type: Type}} */(stack.shift());
18831944
18841945 if (isContainerType(item.type)) {
1885 var len = item.type.pubDecls ? item.type.pubDecls.length : 0;
1946 let t = /** @type {ContainerType} */(item.type);
1947
1948 var len = t.pubDecls ? t.pubDecls.length : 0;
18861949 for (var declI = 0; declI < len; declI += 1) {
1887 var mainDeclIndex = item.type.pubDecls[declI];
1950 var mainDeclIndex = t.pubDecls[declI];
18881951 if (list[mainDeclIndex] != null) continue;
18891952
18901953 var decl = zigAnalysis.decls[mainDeclIndex];
......@@ -1915,16 +1978,20 @@ var zigAnalysis;
19151978 return list;
19161979 }
19171980
1981 /** @param {number} index */
19181982 function getCanonDeclPath(index) {
19191983 if (canonDeclPaths == null) {
19201984 canonDeclPaths = computeCanonDeclPaths();
19211985 }
1922 return canonDeclPaths[index];
1986 let cd = /** @type {CanonDecl[]}*/(canonDeclPaths);
1987 return cd[index];
19231988 }
19241989
1990 /** @param {number} index */
19251991 function getCanonTypeDecl(index) {
19261992 getCanonDeclPath(0);
1927 return canonTypeDecls[index];
1993 let ct = /** @type {number[]}*/(canonTypeDecls);
1994 return ct[index];
19281995 }
19291996
19301997 /** @param {string} text */