Skip to content

Commit

Permalink
fix(eslint): eslint 的 class method 也是 function, close #238
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche committed Jul 1, 2018
1 parent e6c62c3 commit 13ff0eb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
12 changes: 12 additions & 0 deletions packages/eslint-plugin-taro/__tests__/no-stateless-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ ruleTester.run('no-stateless-component', rule, {
return <View>{item}</View>
})
`
}, {
code: testComponent(`const searchbar = (
<View>
<View className="head">
<Input onConfirm="search_key" className="keyword" placeholder="目的地/关键字" placeholderClass="search-place" type="text" />
</View>
<View className="seek">
<Text className="seek_history">搜索历史:</Text>
{histories}
</View>
</View>
);`)
}],
invalid: testInvalid(ERROR_MESSAGE, [
`function Test () { return <View /> }`,
Expand Down
5 changes: 4 additions & 1 deletion packages/eslint-plugin-taro/rules/no-stateless-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ module.exports = {
JSXElement (node) {
const parents = context.getAncestors(node)
const funcDecl = parents.find(p => p.type === 'FunctionDeclaration')
if (parents.some(p => p.type === 'JSXElement')) {
return
}
if (funcDecl) {
context.report({
message: ERROR_MESSAGE,
Expand All @@ -21,7 +24,7 @@ module.exports = {

const funcExpression = parents.find(p => p.type === 'ArrowFunctionExpression' || p.type === 'FunctionExpression')

if (funcExpression) {
if (funcExpression && funcExpression.parent.type !== 'MethodDefinition') {
const arrowFuncParents = context.getAncestors(funcExpression)
const isMapCallExpr = arrowFuncParents.some(p =>
p.type === 'CallExpression' &&
Expand Down
2 changes: 0 additions & 2 deletions packages/eslint-plugin-taro/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ function testComponent (code) {
return `
class App extends Component {
render () {
return (
${code}
)
}
}
`
Expand Down

0 comments on commit 13ff0eb

Please sign in to comment.