Files
meteor/lib/rules/eventmap-params.js
Dominik Ferber 4bd25a9f75 fix(rule): allow overwriting only one param in options of eventmap-params
Previously the rule would not work when only one param name was overwritten using the options.
2016-03-10 11:54:29 +01:00

73 lines
2.0 KiB
JavaScript

/**
* @fileoverview Ensures consistent parameter names in blaze event maps
* @author Philipp Sporrer, Dominik Ferber
* @copyright 2015 Philipp Sporrer. All rights reserved.
* See LICENSE file in root directory for full license.
*/
import { isFunction, isTemplateProp } from '../util/ast'
// -----------------------------------------------------------------------------
// Rule Definition
// -----------------------------------------------------------------------------
export default context => {
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function ensureParamName(param, expectedParamName) {
if (param && param.name !== expectedParamName) {
context.report(
param,
`Invalid parameter name, use "${expectedParamName}" instead`
)
}
}
function validateEventDef(eventDefNode) {
const eventHandler = eventDefNode.value
if (isFunction(eventHandler.type)) {
const {
eventParamName = 'event',
templateInstanceParamName = 'templateInstance',
} = context.options[0] || {}
ensureParamName(eventHandler.params[0], eventParamName)
ensureParamName(eventHandler.params[1], templateInstanceParamName)
}
}
// ---------------------------------------------------------------------------
// Public
// ---------------------------------------------------------------------------
return {
CallExpression: (node) => {
if (node.arguments.length === 0 || !isTemplateProp(node.callee, 'events')) {
return
}
const eventMap = node.arguments[0]
if (eventMap.type === 'ObjectExpression') {
eventMap.properties.forEach((eventDef) => validateEventDef(eventDef))
}
},
}
}
export const schema = [
{
type: 'object',
properties: {
eventParamName: {
type: 'string',
},
templateInstanceParamName: {
type: 'string',
},
},
additionalProperties: false,
},
]