datagrok-tools 4.14.66 → 4.14.67
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/_deprecated/upload.js +0 -0
- package/bin/utils/func-generation.js +20 -3
- package/bin/utils/utils.js +13 -3
- package/package.json +1 -1
|
File without changes
|
|
@@ -136,7 +136,13 @@ function getFuncAnnotation(data, comment = '//', sep = '\n') {
|
|
|
136
136
|
}
|
|
137
137
|
for (const input of data.inputs ?? []) {
|
|
138
138
|
if (!input) continue;
|
|
139
|
-
let type = input?.type;
|
|
139
|
+
let type = input?.type?.replaceAll(" ", "");
|
|
140
|
+
if (type?.includes(`|undefined`)) {
|
|
141
|
+
type = type.replaceAll(`|undefined`, '');
|
|
142
|
+
// modify original payload
|
|
143
|
+
input.optional = true;
|
|
144
|
+
input.type = type;
|
|
145
|
+
}
|
|
140
146
|
let isArray = false;
|
|
141
147
|
if (type?.includes(`[]`)) {
|
|
142
148
|
type = type.replace(/\[\]$/, '');
|
|
@@ -470,8 +476,19 @@ const primitives = exports.primitives = new Set(['string', 'string[]', 'number',
|
|
|
470
476
|
|
|
471
477
|
/** Generates a DG function. */
|
|
472
478
|
function generateFunc(annotation, funcName, sep = '\n', className = '', inputs = [], output, isAsync = false) {
|
|
473
|
-
//
|
|
474
|
-
|
|
479
|
+
// FuncCall can have an optional followed by mandatory args for ui reasons, typescript cannot
|
|
480
|
+
let firstTsValidOptionalIdx = inputs.length - 1;
|
|
481
|
+
for (; firstTsValidOptionalIdx >= 0; firstTsValidOptionalIdx--) {
|
|
482
|
+
const e = inputs[firstTsValidOptionalIdx];
|
|
483
|
+
if (!e.optional) break;
|
|
484
|
+
}
|
|
485
|
+
const funcSigNature = inputs.map((e, idx) => {
|
|
486
|
+
const namePart = `${e.name}${e.optional && idx >= firstTsValidOptionalIdx ? '?' : ''}`;
|
|
487
|
+
// eslint-disable-next-line max-len
|
|
488
|
+
let typePart = `${primitives.has(e.type ?? '') && !typesToAny.includes(e.type ?? '') ? e.type : typesToAnnotation[e.type?.replace('[]', '') ?? ''] && !typesToAny.includes(e.type ?? '') ? e.type : 'any'}`;
|
|
489
|
+
if (e.optional && idx < firstTsValidOptionalIdx) typePart += ' | undefined';
|
|
490
|
+
return `${namePart}: ${typePart}`;
|
|
491
|
+
}).join(', ');
|
|
475
492
|
const funcArguments = inputs.map(e => e.name).join(', ');
|
|
476
493
|
const returnType = output ? primitives.has(output) ? !isAsync ? `: ${output} ` : `: Promise<${output}> ` : !isAsync ? `: any ` : `: Promise<any> ` : '';
|
|
477
494
|
// eslint-disable-next-line max-len
|
package/bin/utils/utils.js
CHANGED
|
@@ -110,7 +110,7 @@ const replacers = exports.replacers = {
|
|
|
110
110
|
FUNC_NAME_LOWERCASE: (s, name) => s.replace(/#{FUNC_NAME_LOWERCASE}/g, friendlyNameToName(name, false)),
|
|
111
111
|
PARAMS_OBJECT: (s, params) => s.replace(/#{PARAMS_OBJECT}/g, params.length ? `{ ${params.map(p => p.name).join(', ')} }` : `{}`),
|
|
112
112
|
OUTPUT_TYPE: (s, type) => s.replace(/#{OUTPUT_TYPE}/g, type),
|
|
113
|
-
TYPED_PARAMS: (s, params) => s.replace(/#{TYPED_PARAMS}/g, params.map(p => `${p.name}${p.isOptional ? '?' : ''}: ${p.type} ${p.nullable ? '| null' : ''}`).join(', '))
|
|
113
|
+
TYPED_PARAMS: (s, params) => s.replace(/#{TYPED_PARAMS}/g, params.map(p => `${p.name}${p.isOptional ? '?' : ''}: ${p.type} ${p.undefinable ? '| undefined' : ''}${p.nullable ? '| null' : ''}`).join(', '))
|
|
114
114
|
};
|
|
115
115
|
class TemplateBuilder {
|
|
116
116
|
static sep = '\n';
|
|
@@ -219,9 +219,18 @@ function getScriptOutputType(script, comment = '#') {
|
|
|
219
219
|
;
|
|
220
220
|
function getScriptInputs(script, comment = '#') {
|
|
221
221
|
const regex = new RegExp(`${comment}\\s*input:\\s?([a-z_]+)(?:<[^>]*>)?\\s+(\\w+)(?:[^{\\n]*{[^}\\n]*})?`, 'g');
|
|
222
|
+
const testOptional = inputAnnotation => /isOptional\s*:\s*true/.test(inputAnnotation) || /optional\s*:\s*true/.test(inputAnnotation);
|
|
223
|
+
const inputAnnotations = [...script.matchAll(regex)];
|
|
224
|
+
let firstTsValidOptionalIdx = inputAnnotations.length - 1;
|
|
225
|
+
for (; firstTsValidOptionalIdx >= 0; firstTsValidOptionalIdx--) {
|
|
226
|
+
const annotation = inputAnnotations[firstTsValidOptionalIdx][0];
|
|
227
|
+
if (!testOptional(annotation)) break;
|
|
228
|
+
}
|
|
222
229
|
const inputs = [];
|
|
223
|
-
for (const match of
|
|
224
|
-
const
|
|
230
|
+
for (const [idx, match] of inputAnnotations.entries()) {
|
|
231
|
+
const hasOptionalAnnotation = testOptional(match[0]);
|
|
232
|
+
const isOptional = hasOptionalAnnotation && idx >= firstTsValidOptionalIdx;
|
|
233
|
+
const undefinable = hasOptionalAnnotation && idx < firstTsValidOptionalIdx;
|
|
225
234
|
const nullable = /nullable\s*:\s*true/.test(match[0]);
|
|
226
235
|
const type = dgToTsTypeMap[match[1]] || 'any';
|
|
227
236
|
const name = match[2];
|
|
@@ -229,6 +238,7 @@ function getScriptInputs(script, comment = '#') {
|
|
|
229
238
|
type,
|
|
230
239
|
name,
|
|
231
240
|
isOptional,
|
|
241
|
+
undefinable,
|
|
232
242
|
nullable
|
|
233
243
|
});
|
|
234
244
|
}
|
package/package.json
CHANGED