UNPKG

2.05 kBJavaScriptView Raw
1/**
2 * @fileoverview Prevent usage of the return value of React.render
3 * @author Dustan Kasten
4 */
5
6'use strict';
7
8const versionUtil = require('../util/version');
9const docsUrl = require('../util/docsUrl');
10
11// ------------------------------------------------------------------------------
12// Rule Definition
13// ------------------------------------------------------------------------------
14
15module.exports = {
16 meta: {
17 docs: {
18 description: 'Prevent usage of the return value of React.render',
19 category: 'Best Practices',
20 recommended: true,
21 url: docsUrl('no-render-return-value')
22 },
23 schema: []
24 },
25
26 create(context) {
27 // --------------------------------------------------------------------------
28 // Public
29 // --------------------------------------------------------------------------
30
31 let calleeObjectName = /^ReactDOM$/;
32 if (versionUtil.testReactVersion(context, '15.0.0')) {
33 calleeObjectName = /^ReactDOM$/;
34 } else if (versionUtil.testReactVersion(context, '0.14.0')) {
35 calleeObjectName = /^React(DOM)?$/;
36 } else if (versionUtil.testReactVersion(context, '0.13.0')) {
37 calleeObjectName = /^React$/;
38 }
39
40 return {
41
42 CallExpression(node) {
43 const callee = node.callee;
44 const parent = node.parent;
45 if (callee.type !== 'MemberExpression') {
46 return;
47 }
48
49 if (
50 callee.object.type !== 'Identifier'
51 || !calleeObjectName.test(callee.object.name)
52 || callee.property.name !== 'render'
53 ) {
54 return;
55 }
56
57 if (
58 parent.type === 'VariableDeclarator'
59 || parent.type === 'Property'
60 || parent.type === 'ReturnStatement'
61 || parent.type === 'ArrowFunctionExpression'
62 || parent.type === 'AssignmentExpression'
63 ) {
64 context.report({
65 node: callee,
66 message: `Do not depend on the return value from ${callee.object.name}.render`
67 });
68 }
69 }
70 };
71 }
72};