Skip to content

Commit

Permalink
additionalDeclarationTemplates combined to additionalMethodTemplates
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseenaSainul committed Oct 20, 2023
1 parent ae3cfef commit 7895c79
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 43 deletions.
2 changes: 1 addition & 1 deletion languages/cpp/language.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
},
"langVersion" : "c++17",
"additionalSchemaTemplates": [ "json-types" ],
"additionalDeclarationTemplates": [ "declarations-override" ]
"additionalMethodTemplates": [ "declarations", "declarations-override" ]
}
3 changes: 2 additions & 1 deletion languages/javascript/language.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
"number": "number",
"string": "string",
"object": "object"
}
},
"additionalMethodTemplates": [ "declarations" ]
}
62 changes: 24 additions & 38 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -518,13 +518,11 @@ const generateMacros = (obj, templates, languages, options = {}) => {
const examples = generateExamples(obj, templates, languages)
const allMethodsArray = generateMethods(obj, examples, templates)

Array.from(new Set(['declarations'].concat(config.additionalDeclarationTemplates))).filter(dir => dir).forEach(dir => {
Array.from(new Set(['methods'].concat(config.additionalMethodTemplates))).filter(dir => dir).forEach(dir => {
const methodsArray = allMethodsArray.filter(m => m.body[dir] && !m.event && (!options.hideExcluded || !m.excluded))
const declarationsArray = allMethodsArray.filter(m => m.declaration[dir] && (!config.excludeDeclarations || (!options.hideExcluded || !m.excluded)))
macros.declarations[dir] = declarationsArray.length ? getTemplate('/sections/declarations', templates).replace(/\$\{declaration\.list\}/g, declarationsArray.map(m => m.declaration[dir]).join('\n')) : ''
})

Array.from(new Set(['methods'].concat(config.additionalMethodTemplates))).filter(dir => dir).forEach(dir => {
const methodsArray = allMethodsArray.filter(m => m.body[dir] && !m.event && (!options.hideExcluded || !m.excluded))
macros.methods[dir] = methodsArray.length ? getTemplate('/sections/methods', templates).replace(/\$\{method.list\}/g, methodsArray.map(m => m.body[dir]).join('\n')) : ''

const eventsArray = allMethodsArray.filter(m => m.body[dir] && m.event && (!options.hideExcluded || !m.excluded))
Expand Down Expand Up @@ -606,26 +604,17 @@ const insertMacros = (fContents = '', macros = {}) => {

fContents = fContents.replace(/\$\{if\.modules\}(.*?)\$\{end\.if\.modules\}/gms, (macros.methods.methods.trim() || macros.events.methods.trim()) ? '$1' : '')

// Output the originally supported non-configurable declarations macros
// Output the originally supported non-configurable declarations, methods & events macros
fContents = fContents.replace(/[ \t]*\/\* \$\{DECLARATIONS\} \*\/[ \t]*\n/, macros.declarations.declarations)
// Output all declarations with all dynamically configured templates
Array.from(new Set(['declarations'].concat(config.additionalDeclarationTemplates))).filter(dir => dir).forEach(dir => {
['DECLARATIONS'].forEach(type => {
const regex = new RegExp('[ \\t]*\\/\\* \\$\\{' + type + '\\:' + dir + '\\} \\*\\/[ \\t]*\\n', 'g')
fContents = fContents.replace(regex, macros[type.toLowerCase()][dir])
})
})

// Output the originally supported non-configurable methods & events macros
fContents = fContents.replace(/[ \t]*\/\* \$\{METHODS\} \*\/[ \t]*\n/, macros.methods.methods)
fContents = fContents.replace(/[ \t]*\/\* \$\{METHOD_LIST\} \*\/[ \t]*\n/, macros.methodList.join(',\n'))
fContents = fContents.replace(/[ \t]*\/\* \$\{EVENTS\} \*\/[ \t]*\n/, macros.events.methods)
fContents = fContents.replace(/[ \t]*\/\* \$\{EVENT_LIST\} \*\/[ \t]*\n/, macros.eventList.join(','))
fContents = fContents.replace(/[ \t]*\/\* \$\{EVENTS_ENUM\} \*\/[ \t]*\n/, macros.eventsEnum)

// Output all methods & events with all dynamically configured templates
// Output all declarations, methods & events with all dynamically configured templates
Array.from(new Set(['methods'].concat(config.additionalMethodTemplates))).filter(dir => dir).forEach(dir => {
['METHODS', 'EVENTS'].forEach(type => {
['DECLARATIONS', 'METHODS', 'EVENTS'].forEach(type => {
const regex = new RegExp('[ \\t]*\\/\\* \\$\\{' + type + '\\:' + dir + '\\} \\*\\/[ \\t]*\\n', 'g')
fContents = fContents.replace(regex, macros[type.toLowerCase()][dir])
})
Expand Down Expand Up @@ -1105,17 +1094,15 @@ function generateMethodResult(type, templates) {
declaration: {},
}

Array.from(new Set(['declarations'].concat(config.additionalDeclarationTemplates))).filter(dir => dir).forEach(dir => {
const template = getTemplate(('/' + dir + '/' + type), templates)
if (template) {
result.declaration[dir] = template
}
})

Array.from(new Set(['methods'].concat(config.additionalMethodTemplates))).filter(dir => dir).forEach(dir => {
const template = getTemplate(('/' + dir + '/' + type), templates)
if (template) {
result.body[dir] = template
if (dir.includes('declarations')) {
result.declaration[dir] = template
}
else if (dir.includes('methods')) {
result.body[dir] = template
}
}
})
return result
Expand All @@ -1137,21 +1124,21 @@ function generateMethods(json = {}, examples = {}, templates = {}) {
event: isEventMethod(methodObj)
}

// Generate declarations for both dynamic and static configured templates
Array.from(new Set(['declarations'].concat(config.additionalDeclarationTemplates))).filter(dir => dir).forEach(dir => {
const template = getTemplateForDeclaration(methodObj, templates, dir)
if (template && template.length) {
let javascript = insertMethodMacros(template, methodObj, json, templates, examples)
result.declaration[dir] = javascript
}
})

// Generate implementation of methods/events for both dynamic and static configured templates
Array.from(new Set(['methods'].concat(config.additionalMethodTemplates))).filter(dir => dir).forEach(dir => {
const template = getTemplateForMethod(methodObj, templates, dir)
if (template && template.length) {
let javascript = insertMethodMacros(template, methodObj, json, templates, examples)
result.body[dir] = javascript
if (dir.includes('declarations')) {
const template = getTemplateForDeclaration(methodObj, templates, dir)
if (template && template.length) {
let javascript = insertMethodMacros(template, methodObj, json, templates, examples)
result.declaration[dir] = javascript
}
}
else if (dir.includes('methods')) {
const template = getTemplateForMethod(methodObj, templates, dir)
if (template && template.length) {
let javascript = insertMethodMacros(template, methodObj, json, templates, examples)
result.body[dir] = javascript
}
}
})

Expand All @@ -1173,7 +1160,6 @@ function generateMethods(json = {}, examples = {}, templates = {}) {
}

results.sort((a, b) => a.name.localeCompare(b.name))

return results
}

Expand Down
2 changes: 0 additions & 2 deletions src/macrofier/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ const macrofy = async (
allocatedPrimitiveProxies,
convertTuplesToArraysOrObjects,
additionalSchemaTemplates,
additionalDeclarationTemplates,
additionalMethodTemplates,
excludeDeclarations,
aggregateFiles,
Expand Down Expand Up @@ -96,7 +95,6 @@ const macrofy = async (
primitives,
allocatedPrimitiveProxies,
additionalSchemaTemplates,
additionalDeclarationTemplates,
additionalMethodTemplates,
excludeDeclarations,
operators
Expand Down
1 change: 0 additions & 1 deletion src/sdk/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ const run = async ({
unwrapResultObjects: config.unwrapResultObjects,
allocatedPrimitiveProxies: config.allocatedPrimitiveProxies,
additionalSchemaTemplates: config.additionalSchemaTemplates,
additionalDeclarationTemplates: config.additionalDeclarationTemplates,
additionalMethodTemplates: config.additionalMethodTemplates,
excludeDeclarations: config.excludeDeclarations,
staticModuleNames: staticModuleNames,
Expand Down

0 comments on commit 7895c79

Please sign in to comment.