Skip to content

Commit

Permalink
feat(eslint): 新规则: manipulate-jsx-as-array
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche committed May 25, 2018
1 parent 83fd7a0 commit fb50d16
Showing 1 changed file with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const { buildDocsMeta } = require('../../utils/utils')
// eslint-disable-next-line
const { DEFAULT_Components_SET } = require('../../components')

const ERROR_MESSAGE = '不能使用 Array#map 之外的方法操作 JSX 数组'

const ARRAY_METHODS = new Set([
'concat',
'copyWithin',
'every',
'fill',
'filter',
'find',
'findIndex',
'flatMap',
'forEach',
'map',
'pop',
'push',
'reduce',
'reduceRight',
'some',
'shift',
'unshift'
])

module.exports = {
meta: {
docs: buildDocsMeta(ERROR_MESSAGE, 'manipulate-jsx-as-array')
},

create (context) {
return {
JSXElement (node) {
const parents = context.getAncestors(node)
const callExpression = parents.find(p => p.type === 'CallExpression' && p.callee.type === 'MemberExpression')
if (callExpression && ARRAY_METHODS.has(callExpression.callee.property.name)) {
context.report({
message: ERROR_MESSAGE,
node: callExpression
})
}
}
}
}
}

0 comments on commit fb50d16

Please sign in to comment.