-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add/integrate `validateTypeof` * Add unit tests (need extra work though to run them)
- Loading branch information
1 parent
4ac6be1
commit 7e83e5b
Showing
5 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {classes} from "./registerClass.js"; | ||
/** | ||
* @param {*} value - The actual value that we need to validate. | ||
* @param {*} expect - The supposed type information of said value. | ||
* @param {string} loc - String like `BoundingBox#compute` | ||
* @param {string} name - Name of the argument | ||
* @param {boolean} critical - Only `false` for unions. | ||
* @param {console["warn"]} warn - Function to warn with. | ||
* @param {number} depth - The depth to detect recursion. | ||
* @returns {boolean} Boolean indicating if a type is correct. | ||
*/ | ||
export function validateTypeof(value, expect, loc, name, critical, warn, depth) { | ||
// console.log("validateTypeof", {value, expect, loc, name, critical, warn, depth}); | ||
return value === classes[expect.argument]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {readFileSync, writeFileSync} from 'fs'; | ||
import {addTypeChecks} from "./src-transpiler/addTypeChecks.js"; | ||
const files = [ | ||
"test-typeof.js", | ||
]; | ||
for (const file of files) { | ||
const content = readFileSync('./test/convert-first/' + file, 'utf-8'); | ||
const contentWithTypeChecks = addTypeChecks(content); | ||
writeFileSync('./test/converted/' + file, contentWithTypeChecks); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {options} from '@runtime-type-inspector/runtime'; | ||
class TestClass1 { | ||
abc = 123; | ||
constructor(greeting = "hi1") { | ||
this.greeting = greeting; | ||
} | ||
} | ||
class TestClass2 { | ||
abc = 123; | ||
constructor(greeting = "hi2") { | ||
this.greeting = greeting; | ||
} | ||
} | ||
/** | ||
* @param {typeof TestClass1} someClass - Some class. | ||
* @param {...any} args - The arguments. | ||
* @returns {object} The object. | ||
*/ | ||
function callNew(someClass, ...args) { | ||
return new someClass(...args); | ||
} | ||
const testClass1 = callNew(TestClass1, "hoi1"); | ||
console.assert(options.count === 0, "Should have 0 errors here"); | ||
const testClass2 = callNew(TestClass2, "hoi2"); | ||
console.assert(options.count === 1, "Should have 1 error here"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import {inspectType, inspectTypeWithTemplates, youCanAddABreakpointHere, registerVariable, validateDivision, registerTypedef, registerClass, registerImportNamespaceSpecifier} from '@runtime-type-inspector/runtime'; | ||
export * from '@runtime-type-inspector/runtime'; | ||
import {options} from '@runtime-type-inspector/runtime'; | ||
class TestClass1 { | ||
abc = 123; | ||
constructor(greeting = "hi1") { | ||
this.greeting = greeting; | ||
} | ||
} | ||
registerClass(TestClass1); | ||
class TestClass2 { | ||
abc = 123; | ||
constructor(greeting = "hi2") { | ||
this.greeting = greeting; | ||
} | ||
} | ||
registerClass(TestClass2); | ||
|
||
/** | ||
* @param {typeof TestClass1} someClass - Some class. | ||
* @param {...any} args - The arguments. | ||
* @returns {object} The object. | ||
*/ | ||
|
||
function callNew(someClass, ...args) { | ||
if (!inspectType(someClass, { | ||
"type": "typeof", | ||
"argument": "TestClass1", | ||
"optional": false | ||
}, 'callNew', 'someClass')) { | ||
youCanAddABreakpointHere(); | ||
} | ||
if (!inspectType(args, { | ||
"type": "array", | ||
"elementType": "any", | ||
"optional": false | ||
}, 'callNew', 'args')) { | ||
youCanAddABreakpointHere(); | ||
} | ||
return new someClass(...args); | ||
} | ||
const testClass1 = callNew(TestClass1, "hoi1"); | ||
console.assert(options.count === 0, "Should have 0 errors here"); | ||
const testClass2 = callNew(TestClass2, "hoi2"); | ||
console.assert(options.count === 1, "Should have 1 error here"); |