Skip to content

Commit

Permalink
fix: Clean up whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
alkalinecoffee committed Jun 3, 2024
1 parent 04a005a commit b0fb2f0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
6 changes: 3 additions & 3 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ const promoteAndNameSubSchemas = (obj) => {
}
})
if (isSubSchema(method.result.schema)) {
addContentDescriptorSubSchema(method.result, '', obj)
addContentDescriptorSubSchema(method.result, '', obj)
}
else if (isEventMethod(method) && isSubSchema(getPayloadFromEvent(method))) {
// TODO: the `1` below is brittle... should find the index of the non-ListenResponse schema
Expand Down Expand Up @@ -1319,7 +1319,7 @@ function insertMethodMacros(template, methodObj, json, templates, type = '', exa
const pullsForParamType = pullsParams ? types.getSchemaType(pullsParams, json, { destination: state.destination, section: state.section }) : ''
const pullsForJsonType = pullsResult ? types.getSchemaType(pullsResult, json, { templateDir: 'json-types' }) : ''
const pullsForParamJsonType = pullsParams ? types.getSchemaType(pullsParams, json, { templateDir: 'json-types' }) : ''

const pullsEventParamName = event ? types.getSchemaInstantiation(event.result, json, event.name, { instantiationType: 'pull.param.name' }) : ''

let seeAlso = ''
Expand Down Expand Up @@ -1603,7 +1603,7 @@ function generateResultParams(result, json, templates, { name = '' } = {}) {
return Object.entries(result.properties).map( ([name, type]) => template
.replace(/\$\{method\.param\.name\}/g, name)
.replace(/\$\{method\.param\.type\}/g, types.getSchemaType(type, json, { moduleTitle: moduleTitle, result: true, namespace: true}))
).join(', ') // most languages separate params w/ a comma, so leaving this here for now
).join(', ') // most languages separate params w/ a comma, so leaving this here for now
}
// tuples get unwrapped
else if (config.unwrapResultObjects && result.type && result.type === 'array' && Array.isArray(result.items)) {
Expand Down
14 changes: 7 additions & 7 deletions src/macrofier/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ const macrofy = async (

const outputFiles = Object.fromEntries(Object.entries(await readFiles( staticCodeList, staticContent))
.map( ([n, v]) => [path.join(output, n), v]))

let primaryOutput = []

Object.keys(templates).forEach(file => {
Expand Down Expand Up @@ -220,7 +220,7 @@ const macrofy = async (

if (!checked.includes(entry)) {
imports = importedFiles(code, base)
checked.push(entry)
checked.push(entry)
}

imports = imports.map(imp => Array.from(new Set([imp, ...treeShake(imp, path.dirname(imp).substring(output.length), checked)]))).flat()
Expand All @@ -244,7 +244,7 @@ const macrofy = async (
// Grab all schema groups w/ a URI string. These came from some external json-schema that was bundled into the OpenRPC
const externalSchemas = {}
openrpc['x-schemas']
&& Object.entries(openrpc['x-schemas']).forEach(([name, schema]) => {
&& Object.entries(openrpc['x-schemas']).forEach(([name, schema]) => {
if (schema.uri) {
const id = schema.uri
externalSchemas[id] = externalSchemas[id] || { $id: id, info: {title: name }, methods: []}
Expand Down Expand Up @@ -278,19 +278,19 @@ const macrofy = async (
}
})
})

// Output any schema templates for each bundled external schema document
Object.values(externalSchemas).forEach( document => {
if (templatesPerSchema) {
templatesPerSchema.forEach( t => {
const macros = engine.generateMacros(document, templates, exampleTemplates, {hideExcluded: hideExcluded, createPolymorphicMethods: createPolymorphicMethods, destination: t})
let content = getTemplate('/schemas', t, templates)

// NOTE: whichever insert is called first also needs to be called again last, so each phase can insert recursive macros from the other
content = engine.insertMacros(content, macros)

const location = createModuleDirectories ? path.join(output, document.info.title, t) : path.join(output, t.replace(/module/, document.info.title.toLowerCase()).replace(/index/, document.info.title))

outputFiles[location] = content
logSuccess(`Generated macros for schema ${path.relative(output, location)}`)
})
Expand Down
34 changes: 17 additions & 17 deletions src/shared/typescript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function getMethodSignature(method, module, { destination, isInterface = false }

typescript += getMethodSignatureParams(method, module, { destination })
typescript += '): ' + (isSynchronous(method) ? getSchemaType(method.result.schema, module, {title: true}) : 'Promise<' + getSchemaType(method.result.schema, module, {title: true}) + '>')

return typescript
}

Expand Down Expand Up @@ -80,9 +80,9 @@ function getSchemaShape(schema = {}, module = {}, { name = '', level = 0, title,
}
else if (schema.type === 'object') {
let suffix = '{'

structure.push(' '.repeat(level) + `${prefix}${theTitle}${operator} ${suffix}`)

if (schema.properties) {
Object.entries(schema.properties).forEach(([name, prop]) => {
if (!schema.required || !schema.required.includes(name)) {
Expand All @@ -100,7 +100,7 @@ function getSchemaShape(schema = {}, module = {}, { name = '', level = 0, title,

if (schema.additionalProperties && (typeof schema.additionalProperties === 'object')) {
type = getSchemaType(schema.additionalProperties, module)
}
}

if (schema.patternProperties) {
Object.entries(schema.patternProperties).forEach(([pattern, schema]) => {
Expand All @@ -110,7 +110,7 @@ function getSchemaShape(schema = {}, module = {}, { name = '', level = 0, title,
}
})
}

structure.push(getSchemaShape({type: type}, module, {name: safeName(prop), descriptions: descriptions, level: level+1}))
})
}
Expand All @@ -119,7 +119,7 @@ function getSchemaShape(schema = {}, module = {}, { name = '', level = 0, title,
let type = getSchemaType(schema.additionalProperties, module, { destination })
structure.push(getSchemaShape({type: type}, module, {name: '[property: string]', descriptions: descriptions, level: level+1}))
}

structure.push(' '.repeat(level) + '}')
}
else if (schema.anyOf) {
Expand Down Expand Up @@ -155,11 +155,11 @@ function getSchemaShape(schema = {}, module = {}, { name = '', level = 0, title,
else if (schema.type || schema.const) {
const isArrayWithSchemaForItems = schema.type === 'array' && schema.items && !Array.isArray(schema.items)
const isArrayWithSpecificItems = schema.type === 'array' && schema.items && Array.isArray(schema.items)

// TODO: deal with fixed sized arrays vs arbitrary arrays
let suffix
let summary = ''

if (schema.const) {
suffix = JSON.stringify(schema.const)
}
Expand All @@ -172,26 +172,26 @@ function getSchemaShape(schema = {}, module = {}, { name = '', level = 0, title,
else {
suffix = getSchemaType(schema, module, { title: level ? true : false }) // prefer schema title over robust descriptor
}

// if there's a summary or description, append it as a comment (description only gets first line)
if (level > 0 && (summary || schema.description)) {
summary = `\t// ${summary || schema.description.split('\n')[0]}`
}

if (suffix === 'array') {
suffix = '[]'
}

if (theTitle === suffix) {
return ' '.repeat(level) + `${prefix}${theTitle}`
}
else {
return ' '.repeat(level) + `${prefix}${theTitle}${operator} ${suffix}${summary}`
}
}

structure = structure.join('\n').split('\n')

if (level === 0) {
const length = str => str.length
let max = Math.max(...structure.map(l => l.split('\t//')[0]).map(length)) + 2
Expand All @@ -202,7 +202,7 @@ function getSchemaShape(schema = {}, module = {}, { name = '', level = 0, title,

function getSchemaType(schema, module, { destination, link = false, title = false, code = false, asPath = false, event = false, expandEnums = true, baseUrl = '' } = {}) {
const wrap = (str, wrapper) => wrapper + str + wrapper

if (schema['$ref']) {
if (schema['$ref'][0] === '#') {
return getSchemaType(getPath(schema['$ref'], module), module, {title: true, link: link, code: code, destination})
Expand Down Expand Up @@ -259,11 +259,11 @@ function getSchemaShape(schema = {}, module = {}, { name = '', level = 0, title,
else if (schema.type === 'array' && schema.items) {
if (Array.isArray(schema.items)) {
let type = '[' + schema.items.map(x => getSchemaType(x, module, { destination })).join(', ') + ']' // no links, no code

if (code) {
type = wrap(type, '`')
}

return type
}
else {
Expand Down Expand Up @@ -358,4 +358,4 @@ function getSchemaShape(schema = {}, module = {}, { name = '', level = 0, title,
getSchemaType,
getJsonType,
getSchemaInstantiation
}
}

0 comments on commit b0fb2f0

Please sign in to comment.