Blame view

node_modules/eslint-plugin-react/lib/rules/no-did-update-set-state.js 1.46 KB
c39994410   Ryan Glover   wip converting to...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  /**
   * @fileoverview Prevent usage of setState in componentDidUpdate
   * @author Yannick Croissant
   */
  'use strict';
  
  // ------------------------------------------------------------------------------
  // Rule Definition
  // ------------------------------------------------------------------------------
  
  module.exports = function(context) {
  
    var mode = context.options[0] || 'never';
  
    // --------------------------------------------------------------------------
    // Public
    // --------------------------------------------------------------------------
  
    return {
  
      CallExpression: function(node) {
        var callee = node.callee;
        if (
          callee.type !== 'MemberExpression' ||
          callee.object.type !== 'ThisExpression' ||
          callee.property.name !== 'setState'
        ) {
          return;
        }
        var ancestors = context.getAncestors(callee).reverse();
        var depth = 0;
        for (var i = 0, j = ancestors.length; i < j; i++) {
          if (/Function(Expression|Declaration)$/.test(ancestors[i].type)) {
            depth++;
          }
          if (
            (ancestors[i].type !== 'Property' && ancestors[i].type !== 'MethodDefinition') ||
            ancestors[i].key.name !== 'componentDidUpdate' ||
            (mode === 'allow-in-func' && depth > 1)
          ) {
            continue;
          }
          context.report(callee, 'Do not use setState in componentDidUpdate');
          break;
        }
      }
    };
  
  };
  
  module.exports.schema = [{
    enum: ['allow-in-func']
  }];