Skip to content

Commit c8c94ed

Browse files
fix: Filter non-context parameters needed for the event callback
1 parent 4093a4a commit c8c94ed

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/macrofier/engine.mjs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,10 +1592,27 @@ function insertMethodMacros(template, methodObj, platformApi, appApi, templates,
15921592

15931593
let eventParams = event.params && event.params.length ? getTemplate('/sections/parameters', templates) + event.params.map(p => insertParameterMacros(getTemplate('/parameters/default', templates), p, event, currentModuleApiForEvent)).join('') : ''
15941594
const eventForSubscriber = getNotifierForMethod(method, appApi)
1595-
let eventAllParams = (eventForSubscriber && eventForSubscriber.params && eventForSubscriber.params.length) ? getTemplate('/sections/parameters', templates) + eventForSubscriber.params.map(p => insertParameterMacros(getTemplate('/parameters/default', templates), p, eventForSubscriber, appApi)).join(', ') : ''
1595+
1596+
// Filter parameters to remove context parameters, because only non-context parameters are parameters for the event callback
1597+
let xContextParams = 0
1598+
let eventNonContextualParams = ''
1599+
if (eventForSubscriber) {
1600+
if (eventForSubscriber.tags) {
1601+
const tag = eventForSubscriber.tags.find(tag => tag.name === 'notifier');
1602+
xContextParams = tag['x-contextual-params'] || 0
1603+
}
1604+
1605+
if (eventForSubscriber.params && eventForSubscriber.params.length) {
1606+
if (xContextParams > 0) {
1607+
const nonContextParamsArray = eventForSubscriber.params.slice(xContextParams, eventForSubscriber.params.length)
1608+
eventNonContextualParams = getTemplate('/sections/parameters', templates) + nonContextParamsArray.map(p => insertParameterMacros(getTemplate('/parameters/default', templates), p, eventForSubscriber, appApi)).join(', ')
1609+
} else
1610+
eventNonContextualParams = getTemplate('/sections/parameters', templates) + eventForSubscriber.params.map(p => insertParameterMacros(getTemplate('/parameters/default', templates), p, eventForSubscriber, appApi)).join(', ')
1611+
}
1612+
}
15961613

15971614
if (isGeneratingDocs(languages)) {
1598-
eventAllParams = isEventMethod(methodObj) && event.result ? Types.getMethodSignatureResult(event, currentModuleApiForEvent, { callback: true, namespace: !config.copySchemasIntoModules }) : ''
1615+
eventNonContextualParams = isEventMethod(methodObj) && event.result ? Types.getMethodSignatureResult(event, currentModuleApiForEvent, { callback: true, namespace: !config.copySchemasIntoModules }) : ''
15991616
// If there's a notifier for this method, and it has examples, use its params for the eventParams section
16001617
if (eventForSubscriber && eventForSubscriber.examples && eventForSubscriber.examples.length) {
16011618
eventParams = (eventForSubscriber.params && eventForSubscriber.params.length) ?
@@ -1800,7 +1817,7 @@ function insertMethodMacros(template, methodObj, platformApi, appApi, templates,
18001817
.replace(/\$\{method\.result\.type\}/g, Types.getSchemaType(result.schema, platformApi, { templateDir: state.typeTemplateDir, title: true, asPath: false, result: true, namespace: false })) //, baseUrl: options.baseUrl
18011818
.replace(/\$\{method\.result\.json\}/g, Types.getSchemaType(result.schema.type === 'null' ? getNonNullSchema(methodObj, event, platformApi, appApi) : result.schema, platformApi, { templateDir: 'json-types', title: true, code: false, link: false, asPath: false, expandEnums: false, namespace: true }))
18021819
// todo: what does prefix do?
1803-
.replace(/\$\{event\.result\.type\}/g, isEventMethod(methodObj) && eventAllParams ? eventAllParams : '')
1820+
.replace(/\$\{event\.result\.type\}/g, isEventMethod(methodObj) && eventNonContextualParams ? eventNonContextualParams : '')
18041821
.replace(/\$\{event\.result\.json\.type\}/g, resultJsonType)
18051822
.replace(/\$\{event\.result\.json\.type\}/g, callbackResultJsonType)
18061823
.replace(/\$\{event\.pulls\.param\.name\}/g, pullsEventParamName)

0 commit comments

Comments
 (0)