Skip to content

Commit

Permalink
feat(eslint): jsx-handler-names 加入到 eslint-plugin-taro
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche committed Jun 10, 2018
1 parent 5b62991 commit a8b7ead
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/eslint-config-taro/rules/jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ module.exports = {
// Enforce event handler naming conventions in JSX
// https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-handler-names.md
'react/jsx-handler-names': ['error', {
eventHandlerPropPrefix: 'on'
eventHandlerPropPrefix: 'on',
eventHandlerPrefix: ''
}],

// Enforce PascalCase for user-defined JSX components
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin-taro/rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const allRules = {
'no-jsx-in-props': require('./no-jsx-in-props'),
'no-ref': require('./no-ref'),
'no-spread-in-props': require('./no-spread-in-props'),
'no-stateless-component': require('./no-stateless-component')
'no-stateless-component': require('./no-stateless-component'),
'jsx-handler-names': require('./jsx-handler-names')
}

function configureAsError (rules) {
Expand Down
40 changes: 40 additions & 0 deletions packages/eslint-plugin-taro/rules/jsx-handler-names.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const { buildDocsMeta } = require('../utils/utils')

const ERROR_MESSAGE = 'JSX 事件名需以 `on` 命名'

module.exports = {
meta: {
docs: {
url: buildDocsMeta(ERROR_MESSAGE, 'jsx-handler-names')
}
},

create (context) {
const eventHandlerPropPrefix = 'on'

const PROP_EVENT_HANDLER_REGEX = new RegExp(`^(${eventHandlerPropPrefix}[A-Z].*|ref)$`)

return {
JSXAttribute (node) {
if (!node.value || !node.value.expression || !node.value.expression.object) {
return
}

const propKey = typeof node.name === 'object' ? node.name.name : node.name

if (propKey === 'ref') {
return
}

const propIsEventHandler = PROP_EVENT_HANDLER_REGEX.test(propKey)

if (propIsEventHandler) {
context.report({
node: node,
message: ERROR_MESSAGE
})
}
}
}
}
}

0 comments on commit a8b7ead

Please sign in to comment.