d944d7e9bbe6f95de2f91c6e312f77089b561b12
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview Prevent extra closing tags for components without children
3  * @author Yannick Croissant
4  */
5 'use strict';
6
7 // ------------------------------------------------------------------------------
8 // Rule Definition
9 // ------------------------------------------------------------------------------
10
11 module.exports = function(context) {
12
13   var tagConvention = /^[a-z]|\-/;
14   function isTagName(name) {
15     return tagConvention.test(name);
16   }
17
18   function isComponent(node) {
19     return node.name && node.name.type === 'JSXIdentifier' && !isTagName(node.name.name);
20   }
21
22   function hasChildren(node) {
23     var childrens = node.parent.children;
24     if (
25       !childrens.length ||
26       (childrens.length === 1 && childrens[0].type === 'Literal' && !childrens[0].value.trim())
27     ) {
28       return false;
29     }
30     return true;
31   }
32
33   // --------------------------------------------------------------------------
34   // Public
35   // --------------------------------------------------------------------------
36
37   return {
38
39     JSXOpeningElement: function(node) {
40       if (!isComponent(node) || node.selfClosing || hasChildren(node)) {
41         return;
42       }
43       context.report(node, 'Empty components are self-closing');
44     }
45   };
46
47 };