Skip to content

Commit 7e83e5b

Browse files
authored
Add/integrate validateTypeof (#222)
* Add/integrate `validateTypeof` * Add unit tests (need extra work though to run them)
1 parent 4ac6be1 commit 7e83e5b

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

src-runtime/validateType.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {validateObject } from "./validateObject.js";
1212
import {validateRecord } from "./validateRecord.js";
1313
import {validateSet } from "./validateSet.js";
1414
import {validateTuple } from "./validateTuple.js";
15+
import {validateTypeof } from "./validateTypeof.js";
1516
import {validateTypedef } from "./validateTypedef.js";
1617
import {validateUnion } from "./validateUnion.js";
1718
/**
@@ -125,6 +126,8 @@ function validateType(value, expect, loc, name, critical = true, warn, depth) {
125126
case 'tuple':
126127
// Trigger: pc.app.scene.setSkybox([1, 2, 3]);
127128
return validateTuple(value, expect, loc, name, critical, warn, depth + 1);
129+
case 'typeof':
130+
return validateTypeof(value, expect, loc, name, critical, warn, depth + 1);
128131
case '*':
129132
case 'any':
130133
return true;

src-runtime/validateTypeof.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {classes} from "./registerClass.js";
2+
/**
3+
* @param {*} value - The actual value that we need to validate.
4+
* @param {*} expect - The supposed type information of said value.
5+
* @param {string} loc - String like `BoundingBox#compute`
6+
* @param {string} name - Name of the argument
7+
* @param {boolean} critical - Only `false` for unions.
8+
* @param {console["warn"]} warn - Function to warn with.
9+
* @param {number} depth - The depth to detect recursion.
10+
* @returns {boolean} Boolean indicating if a type is correct.
11+
*/
12+
export function validateTypeof(value, expect, loc, name, critical, warn, depth) {
13+
// console.log("validateTypeof", {value, expect, loc, name, critical, warn, depth});
14+
return value === classes[expect.argument];
15+
}

test-with-conversion.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {readFileSync, writeFileSync} from 'fs';
2+
import {addTypeChecks} from "./src-transpiler/addTypeChecks.js";
3+
const files = [
4+
"test-typeof.js",
5+
];
6+
for (const file of files) {
7+
const content = readFileSync('./test/convert-first/' + file, 'utf-8');
8+
const contentWithTypeChecks = addTypeChecks(content);
9+
writeFileSync('./test/converted/' + file, contentWithTypeChecks);
10+
}

test/convert-first/test-typeof.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {options} from '@runtime-type-inspector/runtime';
2+
class TestClass1 {
3+
abc = 123;
4+
constructor(greeting = "hi1") {
5+
this.greeting = greeting;
6+
}
7+
}
8+
class TestClass2 {
9+
abc = 123;
10+
constructor(greeting = "hi2") {
11+
this.greeting = greeting;
12+
}
13+
}
14+
/**
15+
* @param {typeof TestClass1} someClass - Some class.
16+
* @param {...any} args - The arguments.
17+
* @returns {object} The object.
18+
*/
19+
function callNew(someClass, ...args) {
20+
return new someClass(...args);
21+
}
22+
const testClass1 = callNew(TestClass1, "hoi1");
23+
console.assert(options.count === 0, "Should have 0 errors here");
24+
const testClass2 = callNew(TestClass2, "hoi2");
25+
console.assert(options.count === 1, "Should have 1 error here");

test/converted/test-typeof.mjs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {inspectType, inspectTypeWithTemplates, youCanAddABreakpointHere, registerVariable, validateDivision, registerTypedef, registerClass, registerImportNamespaceSpecifier} from '@runtime-type-inspector/runtime';
2+
export * from '@runtime-type-inspector/runtime';
3+
import {options} from '@runtime-type-inspector/runtime';
4+
class TestClass1 {
5+
abc = 123;
6+
constructor(greeting = "hi1") {
7+
this.greeting = greeting;
8+
}
9+
}
10+
registerClass(TestClass1);
11+
class TestClass2 {
12+
abc = 123;
13+
constructor(greeting = "hi2") {
14+
this.greeting = greeting;
15+
}
16+
}
17+
registerClass(TestClass2);
18+
19+
/**
20+
* @param {typeof TestClass1} someClass - Some class.
21+
* @param {...any} args - The arguments.
22+
* @returns {object} The object.
23+
*/
24+
25+
function callNew(someClass, ...args) {
26+
if (!inspectType(someClass, {
27+
"type": "typeof",
28+
"argument": "TestClass1",
29+
"optional": false
30+
}, 'callNew', 'someClass')) {
31+
youCanAddABreakpointHere();
32+
}
33+
if (!inspectType(args, {
34+
"type": "array",
35+
"elementType": "any",
36+
"optional": false
37+
}, 'callNew', 'args')) {
38+
youCanAddABreakpointHere();
39+
}
40+
return new someClass(...args);
41+
}
42+
const testClass1 = callNew(TestClass1, "hoi1");
43+
console.assert(options.count === 0, "Should have 0 errors here");
44+
const testClass2 = callNew(TestClass2, "hoi2");
45+
console.assert(options.count === 1, "Should have 1 error here");

0 commit comments

Comments
 (0)