|
| 1 | +/** |
| 2 | + * @fileoverview Enforce stateless components to be written as a pure function |
| 3 | + * @author Yannick Croissant |
| 4 | + */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +var Components = require('../util/Components'); |
| 8 | + |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | +// Rule Definition |
| 11 | +// ------------------------------------------------------------------------------ |
| 12 | + |
| 13 | +module.exports = Components.detect(function(context, components, utils) { |
| 14 | + |
| 15 | + var sourceCode = context.getSourceCode(); |
| 16 | + |
| 17 | + var lifecycleMethods = [ |
| 18 | + 'state', |
| 19 | + 'getInitialState', |
| 20 | + 'componentWillMount', |
| 21 | + 'componentDidMount', |
| 22 | + 'componentWillReceiveProps', |
| 23 | + 'shouldComponentUpdate', |
| 24 | + 'componentWillUpdate', |
| 25 | + 'componentDidUpdate', |
| 26 | + 'componentWillUnmount' |
| 27 | + ]; |
| 28 | + |
| 29 | + // -------------------------------------------------------------------------- |
| 30 | + // Public |
| 31 | + // -------------------------------------------------------------------------- |
| 32 | + |
| 33 | + /** |
| 34 | + * Get properties name |
| 35 | + * @param {Object} node - Property. |
| 36 | + * @returns {String} Property name. |
| 37 | + */ |
| 38 | + function getPropertyName(node) { |
| 39 | + // Special case for class properties |
| 40 | + // (babel-eslint does not expose property name so we have to rely on tokens) |
| 41 | + if (node.type === 'ClassProperty') { |
| 42 | + var tokens = context.getFirstTokens(node, 2); |
| 43 | + return tokens[1] && tokens[1].type === 'Identifier' ? tokens[1].value : tokens[0].value; |
| 44 | + } |
| 45 | + |
| 46 | + return node.key.name; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Get properties for a given AST node |
| 51 | + * @param {ASTNode} node The AST node being checked. |
| 52 | + * @returns {Array} Properties array. |
| 53 | + */ |
| 54 | + function getComponentProperties(node) { |
| 55 | + switch (node.type) { |
| 56 | + case 'ClassDeclaration': |
| 57 | + return node.body.body; |
| 58 | + case 'ObjectExpression': |
| 59 | + return node.properties; |
| 60 | + default: |
| 61 | + return []; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Check if a given AST node have any lifecycle method |
| 67 | + * @param {ASTNode} node The AST node being checked. |
| 68 | + * @returns {Boolean} True if the node has at least one lifecycle method, false if not. |
| 69 | + */ |
| 70 | + function hasLifecycleMethod(node) { |
| 71 | + var properties = getComponentProperties(node); |
| 72 | + return properties.some(function(property) { |
| 73 | + return lifecycleMethods.indexOf(getPropertyName(property)) !== -1; |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Mark a setState as used |
| 79 | + * @param {ASTNode} node The AST node being checked. |
| 80 | + */ |
| 81 | + function markSetStateAsUsed(node) { |
| 82 | + components.set(node, { |
| 83 | + useSetState: true |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Mark a ref as used |
| 89 | + * @param {ASTNode} node The AST node being checked. |
| 90 | + */ |
| 91 | + function markRefAsUsed(node) { |
| 92 | + components.set(node, { |
| 93 | + useRef: true |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + return { |
| 98 | + CallExpression: function(node) { |
| 99 | + var callee = node.callee; |
| 100 | + if (callee.type !== 'MemberExpression') { |
| 101 | + return; |
| 102 | + } |
| 103 | + if (callee.object.type !== 'ThisExpression' || callee.property.name !== 'setState') { |
| 104 | + return; |
| 105 | + } |
| 106 | + markSetStateAsUsed(node); |
| 107 | + }, |
| 108 | + |
| 109 | + JSXAttribute: function(node) { |
| 110 | + var name = sourceCode.getText(node.name); |
| 111 | + if (name !== 'ref') { |
| 112 | + return; |
| 113 | + } |
| 114 | + markRefAsUsed(node); |
| 115 | + }, |
| 116 | + |
| 117 | + 'Program:exit': function() { |
| 118 | + var list = components.list(); |
| 119 | + for (var component in list) { |
| 120 | + if ( |
| 121 | + !list.hasOwnProperty(component) || |
| 122 | + hasLifecycleMethod(list[component].node) || |
| 123 | + list[component].useSetState || |
| 124 | + list[component].useRef || |
| 125 | + (!utils.isES5Component(list[component].node) && !utils.isES6Component(list[component].node)) |
| 126 | + ) { |
| 127 | + continue; |
| 128 | + } |
| 129 | + |
| 130 | + context.report({ |
| 131 | + node: list[component].node, |
| 132 | + message: 'Component should be written as a pure function' |
| 133 | + }); |
| 134 | + } |
| 135 | + } |
| 136 | + }; |
| 137 | + |
| 138 | +}); |
| 139 | + |
| 140 | +module.exports.schema = []; |
0 commit comments