Skip to content

Asserter#generateTypeChecks: Handle @typedef's for ObjectPattern + more unit tests + nice names for potential warning #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 42 additions & 34 deletions src-transpiler/Asserter.js
Original file line number Diff line number Diff line change
@@ -443,6 +443,8 @@ class Asserter extends Stringifier {
// ${JSON.stringify(jsdoc)}\n${parent}\n${spaces}*/\n`;
for (let name in params) {
const type = params[name];
// Copy name for warnings, `name` may become `arguments[${paramIndex}]`.
const nameFancy = name;
const hasParam = this.nodeHasParamName(node, name);
if (!hasParam) {
let testNode = node;
@@ -480,47 +482,53 @@ class Asserter extends Stringifier {
}
const t = JSON.stringify(type.elementType, null, 2).replaceAll('\n', '\n' + spaces);
if (templates) {
out += `${spaces}if (!inspectTypeWithTemplates(${element.name}, ${t}, '${loc}', '${name}', rtiTemplates)) {\n`;
out += `${spaces}if (!inspectTypeWithTemplates(${element.name}, ${t}, '${loc}', '${nameFancy}', rtiTemplates)) {\n`;
} else {
out += `${spaces}if (!inspectType(${element.name}, ${t}, '${loc}', '${name}')) {\n`;
out += `${spaces}if (!inspectType(${element.name}, ${t}, '${loc}', '${nameFancy}')) {\n`;
}
out += `${spaces} youCanAddABreakpointHere();\n${spaces}}\n`;
}
continue;
} else if (param.left.type === 'ObjectPattern' && type.type === 'object') {
// Add a type assertion for each property of the ObjectPattern
for (const property of param.left.properties) {
if (property.key.type !== 'Identifier') {
this.warn('ObjectPattern> Only Identifier case handled right now');
continue;
}
const keyName = property.key.name;
if (type.type !== 'object' || !type.properties) {
this.warn(
"missing subtype information in JSDoc> in type",
JSON.stringify(type, null, 2),
"for ObjectPattern:", this.toSource(property).trim()
);
continue;
}
const subType = type.properties[keyName];
if (!subType) {
this.warn("missing subtype information in JSDoc");
continue;
} else if (param.left.type === 'ObjectPattern') {
if (type.type === 'object') {
// Add a type assertion for each property of the ObjectPattern
for (const property of param.left.properties) {
if (property.key.type !== 'Identifier') {
this.warn('ObjectPattern> Only Identifier case handled right now');
continue;
}
const keyName = property.key.name;
if (type.type !== 'object' || !type.properties) {
this.warn(
"missing subtype information in JSDoc> in type",
JSON.stringify(type, null, 2),
"for ObjectPattern:", this.toSource(property).trim()
);
continue;
}
const subType = type.properties[keyName];
if (!subType) {
this.warn("missing subtype information in JSDoc");
continue;
}
const t = JSON.stringify(subType, null, 2).replaceAll('\n', '\n' + spaces);
if (templates) {
out += `${spaces}if (!inspectTypeWithTemplates(${keyName}, ${t}, '${loc}', '${nameFancy}', rtiTemplates)) {\n`;
} else {
out += `${spaces}if (!inspectType(${keyName}, ${t}, '${loc}', '${nameFancy}')) {\n`;
}
out += `${spaces} youCanAddABreakpointHere();\n${spaces}}\n`;
}
const t = JSON.stringify(subType, null, 2).replaceAll('\n', '\n' + spaces);
if (templates) {
out += `${spaces}if (!inspectTypeWithTemplates(${keyName}, ${t}, '${loc}', '${name}', rtiTemplates)) {\n`;
} else {
out += `${spaces}if (!inspectType(${keyName}, ${t}, '${loc}', '${name}')) {\n`;
}
out += `${spaces} youCanAddABreakpointHere();\n${spaces}}\n`;
continue;
} else if (typeof type === 'string') {
// The case when we have an ObjectPattern with a @typedef
name = `arguments[${paramIndex}]`;
}
} else {
this.warn(`generateTypeChecks> ${loc}> todo implement`,
`AssignmentPattern for parameter ${name}`);
continue;
}
this.warn(`generateTypeChecks> ${loc}> todo implement`,
`AssignmentPattern for parameter ${name}`);
continue;
}
} else {
const loc = this.getName(node);
@@ -552,9 +560,9 @@ class Asserter extends Stringifier {
first = false;
}
if (templates) {
out += `${spaces}if (${prevCheck}!inspectTypeWithTemplates(${name}, ${t}, '${loc}', '${name}', rtiTemplates)) {\n`;
out += `${spaces}if (${prevCheck}!inspectTypeWithTemplates(${name}, ${t}, '${loc}', '${nameFancy}', rtiTemplates)) {\n`;
} else {
out += `${spaces}if (${prevCheck}!inspectType(${name}, ${t}, '${loc}', '${name}')) {\n`;
out += `${spaces}if (${prevCheck}!inspectType(${name}, ${t}, '${loc}', '${nameFancy}')) {\n`;
}
out += `${spaces} youCanAddABreakpointHere();\n${spaces}}\n`;
}
4 changes: 4 additions & 0 deletions test/typechecking.json
Original file line number Diff line number Diff line change
@@ -115,6 +115,10 @@
"input": "./test/typechecking/simple-ObjectPattern-input.mjs",
"output": "./test/typechecking/simple-ObjectPattern-output.mjs"
},
{
"input": "./test/typechecking/simple-ObjectPattern-typedef-input.mjs",
"output": "./test/typechecking/simple-ObjectPattern-typedef-output.mjs"
},
{
"input": "./test/typechecking/template-types-1-input.mjs",
"output": "./test/typechecking/template-types-1-output.mjs"
2 changes: 1 addition & 1 deletion test/typechecking/functions-output.mjs
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ function fetchSomething(url, {
"optional": true
}
}
}, 'fetchSomething', 'arguments[1]')) {
}, 'fetchSomething', 'options')) {
youCanAddABreakpointHere();
}
function inner() {}
2 changes: 1 addition & 1 deletion test/typechecking/simple-ArrayPattern-output.mjs
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ function center_to_corners_format([centerX, centerY, width, height]) {
"type": "array",
"elementType": "number",
"optional": false
}, 'center_to_corners_format', 'arguments[0]')) {
}, 'center_to_corners_format', 'arr')) {
youCanAddABreakpointHere();
}
return [
2 changes: 1 addition & 1 deletion test/typechecking/simple-ObjectPattern-output.mjs
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ function fetchSomething(url, {
"optional": true
}
}
}, 'fetchSomething', 'arguments[1]')) {
}, 'fetchSomething', 'options')) {
youCanAddABreakpointHere();
}
console.log('fetchSomething>', {
30 changes: 30 additions & 0 deletions test/typechecking/simple-ObjectPattern-typedef-input.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Tokenizer {
/**
* Loads a pre-trained tokenizer from the given `pretrained_model_name_or_path`.
*
* @param {string} pretrained_model_name_or_path The path to the pre-trained tokenizer.
* @param {PretrainedTokenizerOptions} options Additional options for loading the tokenizer.
*
* @throws {Error} Throws an error if the tokenizer.json or tokenizer_config.json files are not found in the `pretrained_model_name_or_path`.
* @returns {Promise<PreTrainedTokenizer>} A new instance of the `PreTrainedTokenizer` class.
*/
static async from_pretrained(pretrained_model_name_or_path, {
progress_callback = null,
config = null,
cache_dir = null,
local_files_only = false,
revision = 'main',
legacy = null,
} = {}) {
const info = await loadTokenizer(pretrained_model_name_or_path, {
progress_callback,
config,
cache_dir,
local_files_only,
revision,
legacy,
});
// @ts-ignore
return new this(...info);
}
}
37 changes: 37 additions & 0 deletions test/typechecking/simple-ObjectPattern-typedef-output.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Tokenizer {
/**
* Loads a pre-trained tokenizer from the given `pretrained_model_name_or_path`.
*
* @param {string} pretrained_model_name_or_path The path to the pre-trained tokenizer.
* @param {PretrainedTokenizerOptions} options Additional options for loading the tokenizer.
*
* @throws {Error} Throws an error if the tokenizer.json or tokenizer_config.json files are not found in the `pretrained_model_name_or_path`.
* @returns {Promise<PreTrainedTokenizer>} A new instance of the `PreTrainedTokenizer` class.
*/
static async from_pretrained(pretrained_model_name_or_path, {
progress_callback = null,
config = null,
cache_dir = null,
local_files_only = false,
revision = 'main',
legacy = null,
} = {}) {
if (!inspectType(pretrained_model_name_or_path, "string", 'Tokenizer#from_pretrained', 'pretrained_model_name_or_path')) {
youCanAddABreakpointHere();
}
if (!inspectType(arguments[1], "PretrainedTokenizerOptions", 'Tokenizer#from_pretrained', 'options')) {
youCanAddABreakpointHere();
}
const info = await loadTokenizer(pretrained_model_name_or_path, {
progress_callback,
config,
cache_dir,
local_files_only,
revision,
legacy,
});
// @ts-ignore
return new this(...info);
}
}
registerClass(Tokenizer);