summaryrefslogtreecommitdiff
path: root/tools/eslint-rules/rules-utils.js
blob: 462ab7c2ff3ab8c7e31a6d780e34033e2a655d99 (plain)
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
/**
 * Utility functions common to ESLint rules.
 */
'use strict';

function isRequireCall(node) {
  return node.callee.type === 'Identifier' && node.callee.name === 'require';
}
module.exports.isRequireCall = isRequireCall;

module.exports.isString = function(node) {
  return node && node.type === 'Literal' && typeof node.value === 'string';
};

module.exports.isDefiningError = function(node) {
  return node.expression &&
         node.expression.type === 'CallExpression' &&
         node.expression.callee &&
         node.expression.callee.name === 'E' &&
         node.expression.arguments.length !== 0;
};

/**
 * Returns true if any of the passed in modules are used in
 * require calls.
 */
module.exports.isRequired = function(node, modules) {
  return isRequireCall(node) && node.arguments.length !== 0 &&
    modules.includes(node.arguments[0].value);
};

/**
* Return true if common module is required
* in AST Node under inspection
*/
const commonModuleRegExp = new RegExp(/^(\.\.\/)*common(\.js)?$/);
module.exports.isCommonModule = function(node) {
  return isRequireCall(node) &&
         node.arguments.length !== 0 &&
         commonModuleRegExp.test(node.arguments[0].value);
};

/**
 * Returns true if any of the passed in modules are used in
 * process.binding() or internalBinding() calls.
 */
module.exports.isBinding = function(node, modules) {
  const isProcessBinding = node.callee.object &&
                           node.callee.object.name === 'process' &&
                           node.callee.property.name === 'binding';

  return (isProcessBinding || node.callee.name === 'internalBinding') &&
         modules.includes(node.arguments[0].value);
};

/**
 * Returns true is the node accesses any property in the properties
 * array on the 'common' object.
 */
module.exports.usesCommonProperty = function(node, properties) {
  if (node.name) {
    return properties.includes(node.name);
  }
  if (node.property) {
    return properties.includes(node.property.name);
  }
  return false;
};

/**
 * Returns true if the passed in node is inside an if statement block,
 * and the block also has a call to skip.
 */
module.exports.inSkipBlock = function(node) {
  let hasSkipBlock = false;
  if (node.test &&
      node.test.type === 'UnaryExpression' &&
      node.test.operator === '!') {
    const consequent = node.consequent;
    if (consequent.body) {
      consequent.body.some((expressionStatement) => {
        if (hasSkip(expressionStatement.expression)) {
          return hasSkipBlock = true;
        }
        return false;
      });
    } else if (hasSkip(consequent.expression)) {
      hasSkipBlock = true;
    }
  }
  return hasSkipBlock;
};

function hasSkip(expression) {
  return expression &&
         expression.callee &&
         (expression.callee.name === 'skip' ||
         expression.callee.property &&
         expression.callee.property.name === 'skip');
}