This repository has been archived by the owner on Oct 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(blaze-consistent-eventmap-params): Add rule
- Loading branch information
Showing
7 changed files
with
446 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Consistent event handler parameters (blaze-consistent-eventmap-params) | ||
|
||
Force consistent event handler parameters in [event maps](http://docs.meteor.com/#/full/eventmaps) | ||
|
||
|
||
## Rule Details | ||
|
||
Prevent the use of differently named parameters in event handlers to achieve more consistent code | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
// all on the client | ||
Template.foo.events({ | ||
// 'foo' does not match 'event' | ||
'submit form': function (foo) {} | ||
}) | ||
|
||
Template.foo.events({ | ||
// 'bar' does not match 'templateInstance' | ||
'submit form': function (event, bar) {} | ||
}) | ||
|
||
Template.foo.events({ | ||
// neither 'foo' nor 'bar' are correct | ||
'submit form': function (foo, bar) {} | ||
}) | ||
|
||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
// on the client | ||
Template.foo.events({ | ||
'submit form': function (event) {} | ||
}) | ||
|
||
// on the client | ||
Template.foo.events({ | ||
'submit form': function (event, templateInstance) {} | ||
}) | ||
|
||
``` | ||
|
||
### Options | ||
|
||
You can optionally set the names of the parameters. | ||
You can set the name of the event parameter using `eventParamName` and the name of the template-instance parameterusing `templateInstanceParamName`. | ||
Here are examples of how to do this: | ||
|
||
```js | ||
/* | ||
eslint meteor/blaze-consistent-eventmap-params: [2, {"eventParamName": "evt"}] | ||
*/ | ||
Template.foo.events({ | ||
'submit form': function (evt) {} | ||
}) | ||
|
||
/* | ||
eslint meteor/blaze-consistent-eventmap-params: [2, {"templateInstanceParamName": "tmplInst"}] | ||
*/ | ||
Template.foo.events({ | ||
'submit form': function (event, tmplInst) {} | ||
}) | ||
|
||
/* | ||
eslint meteor/blaze-consistent-eventmap-params: [2, {"eventParamName": "evt", "templateInstanceParamName": "tmplInst"}] | ||
*/ | ||
Template.foo.events({ | ||
'submit form': function (evt, tmplInst) {} | ||
}) | ||
|
||
``` | ||
|
||
## Limitations | ||
|
||
Checks client-side only. | ||
If you use an event map in a universal file (server and client) then the `Meteor.isClient` checks must happen in `if`-conditions with exactly one condition. | ||
|
||
## Further Reading | ||
|
||
* http://docs.meteor.com/#/full/eventmaps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
* @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' | ||
import {getExecutors} from '../util' | ||
import {NON_METEOR} from '../util/environment' | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Rule Definition | ||
// ----------------------------------------------------------------------------- | ||
|
||
module.exports = getMeta => context => { | ||
|
||
const {env} = getMeta(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)) { | ||
|
||
ensureParamName( | ||
eventHandler.params[0], | ||
context.options[0] ? context.options[0].eventParamName : 'event' | ||
) | ||
|
||
ensureParamName( | ||
eventHandler.params[1], | ||
context.options[0] ? context.options[0].templateInstanceParamName : 'templateInstance' | ||
) | ||
} | ||
|
||
} | ||
|
||
// --------------------------------------------------------------------------- | ||
// Public | ||
// --------------------------------------------------------------------------- | ||
|
||
if (env === NON_METEOR) { | ||
return {} | ||
} | ||
|
||
return { | ||
|
||
CallExpression: function (node) { | ||
|
||
if (node.arguments.length === 0 || !isTemplateProp(node.callee, 'events')) { | ||
return | ||
} | ||
const executors = getExecutors(env, context.getAncestors()) | ||
if (executors.has('browser') || executors.has('cordova')) { | ||
const eventMap = node.arguments[0] | ||
|
||
if (eventMap.type === 'ObjectExpression') { | ||
eventMap.properties.forEach((eventDef) => validateEventDef(eventDef)) | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
module.exports.schema = [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
eventParamName: { | ||
type: 'string' | ||
}, | ||
templateInstanceParamName: { | ||
type: 'string' | ||
} | ||
}, | ||
additionalProperties: false | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import getPropertyName from './getPropertyName' | ||
|
||
export default function (node, propName) { | ||
return ( | ||
node.type === 'MemberExpression' && | ||
node.object.type === 'MemberExpression' && | ||
node.object.object.type === 'Identifier' && node.object.object.name === 'Template' && | ||
getPropertyName(node.property) === propName | ||
) | ||
} |
Oops, something went wrong.