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

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 node.callee.name === 'require' && node.arguments.length !== 0 &&
    modules.includes(node.arguments[0].value);
};

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

/**
 * Returns true if any of the passed in modules are used in
 * binding calls.
 */
module.exports.isBinding = function(node, modules) {
  if (node.callee.object) {
    return node.callee.object.name === 'process' &&
           node.callee.property.name === 'binding' &&
           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) {
  var hasSkipBlock = false;
  if (node.test &&
      node.test.type === 'UnaryExpression' &&
      node.test.operator === '!') {
    const consequent = node.consequent;
    if (consequent.body) {
      consequent.body.some(function(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');
}