Blame view

node_modules/eslint-plugin-react/lib/rules/jsx-pascal-case.js 1.25 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
  /**
   * @fileoverview Enforce PasalCase for user-defined JSX components
   * @author Jake Marsh
   */
  
  'use strict';
  
  // ------------------------------------------------------------------------------
  // Constants
  // ------------------------------------------------------------------------------
  
  var PASCAL_CASE_REGEX = /^[A-Z0-9]+[a-z0-9]+(?:[A-Z0-9]+[a-z0-9]*)*$/;
  var COMPAT_TAG_REGEX = /^[a-z]|\-/;
  
  // ------------------------------------------------------------------------------
  // Rule Definition
  // ------------------------------------------------------------------------------
  
  module.exports = function(context) {
  
    return {
      JSXOpeningElement: function(node) {
        switch (node.name.type) {
          case 'JSXIdentifier':
            node = node.name;
            break;
          case 'JSXMemberExpression':
            node = node.name.object;
            break;
          case 'JSXNamespacedName':
            node = node.name.namespace;
            break;
          default:
            break;
        }
  
        var isPascalCase = PASCAL_CASE_REGEX.test(node.name);
        var isCompatTag = COMPAT_TAG_REGEX.test(node.name);
  
        if (!isPascalCase && !isCompatTag) {
          context.report(node, 'Imported JSX component ' + node.name + ' must be in PascalCase');
        }
      }
    };
  
  };