# all files / utils/ common.js

12.96% Statements 7/54
0% Branches 0/41
9.09% Functions 1/11
13.21% Lines 7/53

    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
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138

    1×
    1×

    1×
    1×

    1×

    1×

    1×

     "use strict";

    function createRule(description, errorMessage, isViolation) {
      return {
        meta: {
          docs: {
            description,
            category: "",
            recommended: true,
          },
          schema: [
            {
              additionalProperties: false,
              properties: {
                nativeAllowList: {
                  oneOf: [
                    {
                      enum: ["all"],
                    },
                    {
                      type: "array",
                      items: {
                        type: "string",
                      },
                    },
                  ],
                },
              },
              type: "object",
            },
          ],
        },

        create: function (context) {
          const { options } = context;
          const { nativeAllowList } = options[0] || {};
          return {
            JSXAttribute: function (node) {
              if (!node.value || node.value.type !== "JSXExpressionContainer") {
                return;
              }
              if (
                isNativeElement(node) &&
                (nativeAllowList === "all" ||
                  (Array.isArray(nativeAllowList) &&
                    nativeAllowList.find(function (nativeExclude) {
                      return (
                        node.name.name.toLowerCase() === nativeExclude.toLowerCase()
                      );
                    })))
              ) {
                return;
              }

              var violationFound = false;
              findRelevantNodes(context, node.value.expression).forEach(function (
                node
              ) {
                if (isViolation(node)) {
                  violationFound = true;
                  context.report(node, errorMessage);
                }
              });
              return violationFound;
            },
          };
        },
      };
    }

    function findRelevantNodes(context, node) {
      function _findRelevantNodes(node) {
        if (node.type === "Literal") {
          // we have found a Literal (ex. 'foo', 1, false), bail
          return;
        }
        if (node.type === "Identifier") {
          var variable = context.getScope().variables.find(function (variable) {
            return variable.name === node.name;
          });
          if (variable) {
            variable.references.forEach(function (reference) {
              if (!reference.identifier.parent) return;
              switch (reference.identifier.parent.type) {
                case "AssignmentExpression":
                  nodes.push(reference.identifier.parent.right);
                  break;
                case "VariableDeclarator":
                  nodes.push(reference.identifier.parent.init);
                  break;
                case "AssignmentPattern":
                  nodes.push(reference.identifier.parent.right);
                  break;
              }
            });
          }
          return;
        }
        if (node.type === "LogicalExpression") {
          return _findRelevantNodes(node.left) || _findRelevantNodes(node.right);
        }
        if (node.type === "ConditionalExpression") {
          return (
            _findRelevantNodes(node.consequent) ||
            _findRelevantNodes(node.alternate)
          );
        }

        nodes.push(node);
      }

      var nodes = [];
      _findRelevantNodes(node);
      return nodes;
    }

    function checkConstructor(node, className) {
      if (node.callee && node.callee.name === className) {
        if (["NewExpression", "CallExpression"].indexOf(node.type) !== -1) {
          return true;
        }
      }
    }

    function isNativeElement(node) {
      if (node.parent.name.type !== "JSXIdentifier") {
        return false;
      }
      const nodeName = node.parent.name.name;
      const firstChar = nodeName.charAt(0);
      return firstChar === firstChar.toLowerCase();
    }

    module.exports = {
      createRule,
      checkConstructor,
    };