Skip to content

Commit

Permalink
refactor out customTypes into customTypes.mjs, remove PC specific code
Browse files Browse the repository at this point in the history
  • Loading branch information
kungfooman committed Dec 2, 2023
1 parent 607feea commit 1fcdb2b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 52 deletions.
5 changes: 5 additions & 0 deletions src-runtime/customTypes.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* @type {Record<string, (value: any) => boolean>}
*/
const customTypes = {};
export {customTypes};
1 change: 1 addition & 0 deletions src-runtime/index.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './assertType.mjs';
export * from './createDiv.mjs';
export * from './customTypes.mjs';
export * from './customValidations.mjs';
export * from './makeJSDoc.mjs';
export * from './registerClass.mjs';
Expand Down
4 changes: 0 additions & 4 deletions src-runtime/typecheckOptions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ const typecheckOptions = {
*/
warned: {},
logSuperfluousProperty: false,
/**
* @type {Record<string, (value: any) => boolean>}
*/
customTypes: {},
count: 0,
};
function typecheckReport() {
Expand Down
77 changes: 29 additions & 48 deletions src-runtime/validateType.mjs
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import {classes } from "./registerClass.mjs";
import {typedefs } from "./registerTypedef.mjs";
import {typecheckOptions} from "./typecheckOptions.mjs";
import {typecheckWarn } from "./typecheckWarn.mjs";
import {validateArray } from "./validateArray.mjs";
import {validateMap } from "./validateMap.mjs";
import {validateNumber } from "./validateNumber.mjs";
import {validateObject } from "./validateObject.mjs";
import {validateRecord } from "./validateRecord.mjs";
import {validateSet } from "./validateSet.mjs";
import {validateTuple } from "./validateTuple.mjs";
import {validateTypedef } from "./validateTypedef.mjs";
import {validateUnion } from "./validateUnion.mjs";
// For quickly checking props of Vec2/Vec3/Vec4/Quat/Mat3/Mat4 without GC
const propsXY = ['x', 'y'];
const propsXYZ = ['x', 'y', 'z'];
const propsXYZW = ['x', 'y', 'z', 'w'];
const props9 = [0, 1, 2, 3, 4, 5, 6, 7, 8];
const props16 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
import {customTypes } from "./customTypes.mjs";
import {customValidations} from "./customValidations.mjs";
import {classes } from "./registerClass.mjs";
import {typedefs } from "./registerTypedef.mjs";
import {typecheckWarn } from "./typecheckWarn.mjs";
import {validateArray } from "./validateArray.mjs";
import {validateMap } from "./validateMap.mjs";
import {validateNumber } from "./validateNumber.mjs";
import {validateObject } from "./validateObject.mjs";
import {validateRecord } from "./validateRecord.mjs";
import {validateSet } from "./validateSet.mjs";
import {validateTuple } from "./validateTuple.mjs";
import {validateTypedef } from "./validateTypedef.mjs";
import {validateUnion } from "./validateUnion.mjs";
let enabled = true;
export function disableTypeChecking() {
enabled = false;
}
export function enableTypeChecking() {
enabled = true;
}
/**
* @param {*} value - The actual value which we need to check.
* @param {object} expect - Can also be a string, but string|object is unsupported in VSCode
Expand All @@ -36,6 +38,9 @@ const props16 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
* @returns {boolean} Returns wether `value` is in the shape of `expect`.
*/
function validateType(value, expect, loc, name, critical = true) {
if (!enabled) {
return true;
}
if (typeof expect === 'string') {
expect = {
type: expect,
Expand All @@ -59,7 +64,7 @@ function validateType(value, expect, loc, name, critical = true) {
return true;
}
}
const customCheck = typecheckOptions.customTypes[type];
const customCheck = customTypes[type];
if (customCheck) {
return customCheck(value);
}
Expand All @@ -77,36 +82,12 @@ function validateType(value, expect, loc, name, critical = true) {
}
}
for (const customValidation of customValidations) {
customValidation(value, expect, loc, name, critical);
}
if (typeof window !== 'undefined' && window.pc) {
const {pc} = window;
/**
* @param {string|number} prop - Something like 'x', 'y', 'z', 'w', 0, 1, 2, 3, 4 etc.
* @returns {boolean} Wether prop is a valid number.
*/
const checkProp = (prop) => {
return validateNumber(value, prop);
};
// In a bundle pc can be defined while pc is not filled with all classes yet,
// therefore we need to check if class is added already.
if (pc.Vec2 && value instanceof pc.Vec2) {
return propsXY.every(checkProp);
}
if (pc.Vec3 && value instanceof pc.Vec3) {
return propsXYZ.every(checkProp);
}
if ((pc.Vec4 && value instanceof pc.Vec4) || (pc.Quat && value instanceof pc.Quat)) {
return propsXYZW.every(checkProp);
}
if (pc.Mat3 && value instanceof pc.Mat3) {
return props9.every(prop => validateNumber(value.data, prop));
}
if (pc.Mat4 && value instanceof pc.Mat4) {
return props16.every(prop => validateNumber(value.data, prop));
const ret = customValidation(value, expect, loc, name, critical);
if (!ret) {
typecheckWarn(`${loc}> customValidation failed`);
return false;
}
}
// todo: either switch or object lookup for custom hooks
if (type === "object") {
return validateObject(value, properties, loc, name, critical);
}
Expand Down

0 comments on commit 1fcdb2b

Please sign in to comment.