UNPKG

eslint-plugin-taro

Version:

Taro specific linting plugin for ESLint

37 lines (32 loc) 857 B
const { DEFAULT_Components_SET } = require('../constant') const { buildDocsMeta } = require('../utils/utils') const ERROR_MESSAGE = '不能在自定义组件中写 children' module.exports = { meta: { docs: buildDocsMeta(ERROR_MESSAGE, 'custom-component-children') }, create (context) { function isChildrenNotEmpty (children) { return !( (children.type === 'JSXText' || children.type === 'Literal') && children.value.trim() === '' ) } return { JSXElement (node) { const { name } = node.openingElement.name if ( !DEFAULT_Components_SET.has(name) && node.children .filter(isChildrenNotEmpty) .length > 0 ) { context.report({ message: ERROR_MESSAGE, node }) } } } } }