Blame view

node_modules/eslint-plugin-react/lib/rules/self-closing-comp.js 1.24 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
  /**
   * @fileoverview Prevent extra closing tags for components without children
   * @author Yannick Croissant
   */
  'use strict';
  
  // ------------------------------------------------------------------------------
  // Rule Definition
  // ------------------------------------------------------------------------------
  
  module.exports = function(context) {
  
    var tagConvention = /^[a-z]|\-/;
    function isTagName(name) {
      return tagConvention.test(name);
    }
  
    function isComponent(node) {
      return node.name && node.name.type === 'JSXIdentifier' && !isTagName(node.name.name);
    }
  
    function hasChildren(node) {
      var childrens = node.parent.children;
      if (
        !childrens.length ||
        (childrens.length === 1 && childrens[0].type === 'Literal' && !childrens[0].value.trim())
      ) {
        return false;
      }
      return true;
    }
  
    // --------------------------------------------------------------------------
    // Public
    // --------------------------------------------------------------------------
  
    return {
  
      JSXOpeningElement: function(node) {
        if (!isComponent(node) || node.selfClosing || hasChildren(node)) {
          return;
        }
        context.report(node, 'Empty components are self-closing');
      }
    };
  
  };
  
  module.exports.schema = [];