Blame view

node_modules/eslint-plugin-react/lib/rules/jsx-equals-spacing.js 2.14 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  /**
   * @fileoverview Disallow or enforce spaces around equal signs in JSX attributes.
   * @author ryym
   */
  'use strict';
  
  // ------------------------------------------------------------------------------
  // Rule Definition
  // ------------------------------------------------------------------------------
  
  module.exports = function(context) {
    var config = context.options[0];
    var sourceCode = context.getSourceCode();
  
    /**
     * Determines a given attribute node has an equal sign.
     * @param {ASTNode} attrNode - The attribute node.
     * @returns {boolean} Whether or not the attriute node has an equal sign.
     */
    function hasEqual(attrNode) {
      return attrNode.type !== 'JSXSpreadAttribute' && attrNode.value !== null;
    }
  
    // --------------------------------------------------------------------------
    // Public
    // --------------------------------------------------------------------------
  
    return {
      JSXOpeningElement: function(node) {
        node.attributes.forEach(function(attrNode) {
          if (!hasEqual(attrNode)) {
            return;
          }
  
          var equalToken = sourceCode.getTokenAfter(attrNode.name);
          var spacedBefore = sourceCode.isSpaceBetweenTokens(attrNode.name, equalToken);
          var spacedAfter = sourceCode.isSpaceBetweenTokens(equalToken, attrNode.value);
  
          switch (config) {
            default:
            case 'never':
              if (spacedBefore) {
                context.report(attrNode, equalToken.loc.start,
                  'There should be no space before \'=\'');
              }
              if (spacedAfter) {
                context.report(attrNode, equalToken.loc.start,
                  'There should be no space after \'=\'');
              }
              break;
            case 'always':
              if (!spacedBefore) {
                context.report(attrNode, equalToken.loc.start,
                  'A space is required before \'=\'');
              }
              if (!spacedAfter) {
                context.report(attrNode, equalToken.loc.start,
                  'A space is required after \'=\'');
              }
              break;
          }
        });
      }
    };
  };
  
  module.exports.schema = [{
    enum: ['always', 'never']
  }];