forked from jsx-eslint/eslint-plugin-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
no-render-return-value. closes jsx-eslint#531
As documented initially in facebook/react#6400 and on the documentation website at http://facebook.github.io/react/docs/top-level-api.html#reactdom.render
- Loading branch information
Showing
3 changed files
with
198 additions
and
0 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,62 @@ | ||
/** | ||
* @fileoverview Prevent usage of the return value of React.render | ||
* @author Dustan Kasten | ||
*/ | ||
'use strict'; | ||
|
||
var versionUtil = require('../util/version'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { | ||
|
||
// -------------------------------------------------------------------------- | ||
// Public | ||
// -------------------------------------------------------------------------- | ||
|
||
return { | ||
|
||
CallExpression: function(node) { | ||
var callee = node.callee; | ||
var parent = node.parent; | ||
if (callee.type !== 'MemberExpression') { | ||
return; | ||
} | ||
|
||
var calleeObjectName; | ||
if (versionUtil.test(context, '15.0.0')) { | ||
calleeObjectName = /^ReactDOM$/; | ||
} else if (versionUtil.test(context, '0.14.0')) { | ||
calleeObjectName = /^React(DOM)?$/; | ||
} else if (versionUtil.test(context, '0.13.0')) { | ||
calleeObjectName = /^React$/; | ||
} | ||
|
||
if ( | ||
callee.object.type !== 'Identifier' || | ||
!calleeObjectName.test(callee.object.name) || | ||
callee.property.name !== 'render' | ||
) { | ||
return; | ||
} | ||
|
||
if ( | ||
parent.type === 'VariableDeclarator' || | ||
parent.type === 'Property' || | ||
parent.type === 'ReturnStatement' || | ||
parent.type === 'ArrowFunctionExpression' | ||
) { | ||
context.report({ | ||
node: callee, | ||
message: 'Do not depend on the return value from render' | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
}; | ||
|
||
module.exports.schema = []; | ||
|
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,134 @@ | ||
/** | ||
* @fileoverview Prevent usage of setState | ||
* @author Mark Dalgleish | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../../../lib/rules/no-render-return-value'); | ||
var RuleTester = require('eslint').RuleTester; | ||
|
||
var parserOptions = { | ||
ecmaVersion: 6, | ||
ecmaFeatures: { | ||
jsx: true | ||
} | ||
}; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester(); | ||
ruleTester.run('no-render-return-value', rule, { | ||
|
||
valid: [{ | ||
code: [ | ||
'ReactDOM.render(<div />, document.body);' | ||
].join('\n'), | ||
parserOptions: parserOptions | ||
}, { | ||
code: [ | ||
'let node;', | ||
'ReactDOM.render(<div ref={ref => node = ref}/>, document.body);' | ||
].join('\n'), | ||
parserOptions: parserOptions | ||
}, { | ||
code: 'ReactDOM.render(<div ref={ref => this.node = ref}/>, document.body);', | ||
parserOptions: parserOptions, | ||
settings: { | ||
react: { | ||
version: '0.14.0' | ||
} | ||
} | ||
}, { | ||
code: 'React.render(<div ref={ref => this.node = ref}/>, document.body);', | ||
parserOptions: parserOptions, | ||
settings: { | ||
react: { | ||
version: '0.14.0' | ||
} | ||
} | ||
}, { | ||
code: 'React.render(<div ref={ref => this.node = ref}/>, document.body);', | ||
parserOptions: parserOptions, | ||
settings: { | ||
react: { | ||
version: '0.13.0' | ||
} | ||
} | ||
} | ||
], | ||
|
||
invalid: [{ | ||
code: [ | ||
'var Hello = ReactDOM.render(<div />, document.body);' | ||
].join('\n'), | ||
parserOptions: parserOptions, | ||
errors: [{ | ||
message: 'Do not depend on the return value from render' | ||
}] | ||
}, { | ||
code: [ | ||
'var o = {', | ||
' inst: ReactDOM.render(<div />, document.body)', | ||
'};' | ||
].join('\n'), | ||
parserOptions: parserOptions, | ||
errors: [{ | ||
message: 'Do not depend on the return value from render' | ||
}] | ||
}, { | ||
code: [ | ||
'function render () {', | ||
' return ReactDOM.render(<div />, document.body)', | ||
'}' | ||
].join('\n'), | ||
parserOptions: parserOptions, | ||
errors: [{ | ||
message: 'Do not depend on the return value from render' | ||
}] | ||
}, { | ||
code: 'var render = (a, b) => ReactDOM.render(a, b)', | ||
parserOptions: parserOptions, | ||
errors: [{ | ||
message: 'Do not depend on the return value from render' | ||
}] | ||
}, { | ||
code: 'var inst = React.render(<div />, document.body);', | ||
parserOptions: parserOptions, | ||
settings: { | ||
react: { | ||
version: '0.14.0' | ||
} | ||
}, | ||
errors: [{ | ||
message: 'Do not depend on the return value from render' | ||
}] | ||
}, { | ||
code: 'var inst = ReactDOM.render(<div />, document.body);', | ||
parserOptions: parserOptions, | ||
settings: { | ||
react: { | ||
version: '0.14.0' | ||
} | ||
}, | ||
errors: [{ | ||
message: 'Do not depend on the return value from render' | ||
}] | ||
}, { | ||
code: 'var inst = React.render(<div />, document.body);', | ||
parserOptions: parserOptions, | ||
settings: { | ||
react: { | ||
version: '0.13.0' | ||
} | ||
}, | ||
errors: [{ | ||
message: 'Do not depend on the return value from render' | ||
}] | ||
}] | ||
}); |