From ed4258c495c6c951aff52391381bc5d27478dcb8 Mon Sep 17 00:00:00 2001 From: Jeremy LaCivita Date: Tue, 28 May 2024 15:51:21 -0400 Subject: [PATCH] fix: Documentation --- .../templates/codeblocks/interface.md | 3 ++ .../templates/declarations/default.md | 1 + .../markdown/templates/interfaces/default.mjs | 1 + .../templates/interfaces/focusable.mjs | 1 + languages/markdown/templates/types/default.md | 3 -- languages/markdown/templates/types/enum.md | 5 --- languages/markdown/templates/types/object.md | 3 -- src/docs/index.mjs | 1 + src/macrofier/engine.mjs | 33 +++++++++++++++---- src/macrofier/index.mjs | 21 +++++++++--- src/macrofier/types.mjs | 2 +- 11 files changed, 51 insertions(+), 23 deletions(-) create mode 100644 languages/markdown/templates/codeblocks/interface.md create mode 100644 languages/markdown/templates/declarations/default.md create mode 100644 languages/markdown/templates/interfaces/default.mjs create mode 100644 languages/markdown/templates/interfaces/focusable.mjs delete mode 100644 languages/markdown/templates/types/default.md delete mode 100644 languages/markdown/templates/types/enum.md delete mode 100644 languages/markdown/templates/types/object.md diff --git a/languages/markdown/templates/codeblocks/interface.md b/languages/markdown/templates/codeblocks/interface.md new file mode 100644 index 00000000..8c69cda0 --- /dev/null +++ b/languages/markdown/templates/codeblocks/interface.md @@ -0,0 +1,3 @@ +interface ${name} { + ${methods} +} \ No newline at end of file diff --git a/languages/markdown/templates/declarations/default.md b/languages/markdown/templates/declarations/default.md new file mode 100644 index 00000000..27a3c4a7 --- /dev/null +++ b/languages/markdown/templates/declarations/default.md @@ -0,0 +1 @@ +function ${method.name}(${method.params}): Promise<${method.result.type}> \ No newline at end of file diff --git a/languages/markdown/templates/interfaces/default.mjs b/languages/markdown/templates/interfaces/default.mjs new file mode 100644 index 00000000..63f63093 --- /dev/null +++ b/languages/markdown/templates/interfaces/default.mjs @@ -0,0 +1 @@ + ${method.name}(${method.signature.params}, session: ProviderSession): Promise<${method.result.type}> diff --git a/languages/markdown/templates/interfaces/focusable.mjs b/languages/markdown/templates/interfaces/focusable.mjs new file mode 100644 index 00000000..b81737b1 --- /dev/null +++ b/languages/markdown/templates/interfaces/focusable.mjs @@ -0,0 +1 @@ + ${method.name}(${method.signature.params}, session: FocusableProviderSession): Promise<${method.result.type}> diff --git a/languages/markdown/templates/types/default.md b/languages/markdown/templates/types/default.md deleted file mode 100644 index f2a4644e..00000000 --- a/languages/markdown/templates/types/default.md +++ /dev/null @@ -1,3 +0,0 @@ -```typescript -${type} -``` \ No newline at end of file diff --git a/languages/markdown/templates/types/enum.md b/languages/markdown/templates/types/enum.md deleted file mode 100644 index 59861d52..00000000 --- a/languages/markdown/templates/types/enum.md +++ /dev/null @@ -1,5 +0,0 @@ -${name} Enumeration: - -| key | value | -|-----|-------| -| ${key} | ${value} | diff --git a/languages/markdown/templates/types/object.md b/languages/markdown/templates/types/object.md deleted file mode 100644 index 2b15f271..00000000 --- a/languages/markdown/templates/types/object.md +++ /dev/null @@ -1,3 +0,0 @@ -| Property | Type | Description | -|----------|------|-------------| -| `${property}` | [${type}](${type.link}) | ${description} | diff --git a/src/docs/index.mjs b/src/docs/index.mjs index 66dd4136..fdf71acd 100755 --- a/src/docs/index.mjs +++ b/src/docs/index.mjs @@ -59,6 +59,7 @@ const run = async ({ examples: examples, templatesPerModule: config.templatesPerModule, templatesPerSchema: config.templatesPerSchema, + operators: config.operators, libraryName: libraryName, hidePrivate: false }) diff --git a/src/macrofier/engine.mjs b/src/macrofier/engine.mjs index e2427125..fc6ecaec 100644 --- a/src/macrofier/engine.mjs +++ b/src/macrofier/engine.mjs @@ -573,7 +573,7 @@ const generateMacros = (obj, templates, languages, options = {}) => { const eventsEnum = generateEvents(obj, templates) const examples = generateExamples(obj, templates, languages) - const allMethodsArray = generateMethods(obj, examples, templates, options.type) + const allMethodsArray = generateMethods(obj, examples, templates, languages, options.type) Array.from(new Set(['methods'].concat(config.additionalMethodTemplates))).filter(dir => dir).forEach(dir => { @@ -1186,7 +1186,7 @@ function generateMethodResult(type, templates) { return result } -function generateMethods(json = {}, examples = {}, templates = {}, type = '') { +function generateMethods(json = {}, examples = {}, templates = {}, languages = [], type = '') { const methods = compose( option([]), getMethods @@ -1215,7 +1215,7 @@ function generateMethods(json = {}, examples = {}, templates = {}, type = '') { else if (dir.includes('methods') && (suffix && config.templateExtensionMap[dir] ? config.templateExtensionMap[dir].includes(suffix) : true)) { const template = getTemplateForMethod(methodObj, templates, dir) if (template && template.length) { - result.body[dir] = insertMethodMacros(template, methodObj, json, templates, type, examples) + result.body[dir] = insertMethodMacros(template, methodObj, json, templates, type, examples, languages) } } }) @@ -1242,7 +1242,7 @@ function generateMethods(json = {}, examples = {}, templates = {}, type = '') { } // TODO: this is called too many places... let's reduce that to just generateMethods -function insertMethodMacros(template, methodObj, json, templates, type = '', examples = {}) { +function insertMethodMacros(template, methodObj, json, templates, type = '', examples = {}, languages = {}) { const moduleName = getModuleName(json) const info = { @@ -1378,6 +1378,23 @@ function insertMethodMacros(template, methodObj, json, templates, type = '', exa itemType = types.getSchemaType(result.schema.items, json, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section }) } + let signature + + if (Object.keys(languages).length && template.indexOf('${method.signature}') >= 0) { + const lang = languages[Object.keys(languages)[0]] + signature = getTemplateForDeclaration(methodObj, templates, 'declarations') + types.setTemplates(lang) + const currentConfig = JSON.parse(JSON.stringify(config)) + config.operators = config.operators || {} + config.operators.paramDelimiter = ', ' + signature = insertMethodMacros(signature, methodObj, json, lang, type) + config = currentConfig + types.setTemplates(templates) + } + else { + signature = '' + } + template = insertExampleMacros(template, examples[methodObj.name] || [], methodObj, json, templates) template = template.replace(/\$\{method\.name\}/g, method.name) .replace(/\$\{method\.rpc\.name\}/g, methodObj.rpc_name || methodObj.name) @@ -1403,6 +1420,7 @@ function insertMethodMacros(template, methodObj, json, templates, type = '', exa .replace(/\$\{method\.params\.serialization\}/g, serializedParams) .replace(/\$\{method\.params\.serialization\.with\.indent\}/g, indent(serializedParams, ' ')) // Typed signature stuff + .replace(/\$\{method\.signature\}/g, signature) .replace(/\$\{method\.signature\.params\}/g, types.getMethodSignatureParams(methodObj, json, { destination: state.destination, section: state.section })) .replace(/\$\{method\.signature\.result\}/g, types.getMethodSignatureResult(methodObj, json, { destination: state.destination, section: state.section })) .replace(/\$\{method\.context\}/g, method.context.join(', ')) @@ -1691,7 +1709,7 @@ function insertParameterMacros(template, param, method, module) { constraints = '
' + constraints } - return template + template = template .replace(/\$\{method.param.name\}/g, param.name) .replace(/\$\{method.param.Name\}/g, param.name[0].toUpperCase() + param.name.substring(1)) .replace(/\$\{method.param.summary\}/g, param.summary || '') @@ -1700,7 +1718,10 @@ function insertParameterMacros(template, param, method, module) { .replace(/\$\{json.param.type\}/g, jsonType) .replace(/\$\{method.param.link\}/g, getLinkForSchema(param.schema, module)) //getType(param)) .replace(/\$\{method.param.constraints\}/g, constraints) //getType(param)) -} + + return template + + } function insertCapabilityMacros(template, capabilities, method, module) { const content = [] diff --git a/src/macrofier/index.mjs b/src/macrofier/index.mjs index 99a1a3e0..298241f6 100644 --- a/src/macrofier/index.mjs +++ b/src/macrofier/index.mjs @@ -110,11 +110,6 @@ const macrofy = async ( const templates = Object.assign(await readFiles(sharedTemplateList, sharedTemplates), await readFiles(sdkTemplateList, template)) // sdkTemplates are second so they win ties - typer.setTemplates && typer.setTemplates(templates) - typer.setPrimitives(primitives) - typer.setAllocatedPrimitiveProxies(allocatedPrimitiveProxies) - typer.setConvertTuples(convertTuplesToArraysOrObjects) - let templatesPermission = {} if (persistPermission) { templatesPermission = Object.assign(await readFilesPermissions(sharedTemplateList, sharedTemplates), @@ -133,6 +128,22 @@ const macrofy = async ( exampleTemplates[config.name]['__config'] = config } + // check if this is a "real" language or just documentation broiler-plate, e.g. markdown + if (Object.keys(templates).find(key => key.startsWith('/types/primitive'))) { + typer.setTemplates && typer.setTemplates(templates) + typer.setPrimitives(primitives) + } + else { + const lang = Object.entries(exampleTemplates)[0][1] + const prims = Object.entries(exampleTemplates)[0][1]['__config'].primitives + // add the templates from the first example language and the wrapper langauage + typer.setTemplates && typer.setTemplates(lang) + typer.setTemplates && typer.setTemplates(templates) + typer.setPrimitives(prims) + } + typer.setAllocatedPrimitiveProxies(allocatedPrimitiveProxies) + typer.setConvertTuples(convertTuplesToArraysOrObjects) + const staticCodeList = staticContent ? await readDir(staticContent, { recursive: true }) : [] const staticModules = staticModuleNames.map(name => ( { info: { title: name } } )) diff --git a/src/macrofier/types.mjs b/src/macrofier/types.mjs index 55ef7b7e..6e854d15 100644 --- a/src/macrofier/types.mjs +++ b/src/macrofier/types.mjs @@ -724,7 +724,7 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin const theTitle = insertSchemaMacros(namespaceStr + getTemplate(path.join(templateDir, 'title' + suffix)), schema, module, { name: schema.title, parent: getXSchemaGroup(schema, module), recursive: false }) const allocatedProxy = event || result - const title = schema.type === "object" || Array.isArray(schema.type) && schema.type.includes("object") || schema.enum ? true : false + const title = schema.type === "object" || schema.anyOf || schema.oneOf || Array.isArray(schema.type) && schema.type.includes("object") || schema.enum ? true : false if (schema['$ref']) { if (schema['$ref'][0] === '#') {