summaryrefslogtreecommitdiff
path: root/tools/eslint-rules/inspector-check.js
blob: f225b34cb6b0caec6b95c0dbac6d888020a551ad (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
/**
 * @fileoverview Check that common.skipIfInspectorDisabled is used if
 *               the inspector module is required.
 * @author Daniel Bevenius <daniel.bevenius@gmail.com>
 */
'use strict';

const utils = require('./rules-utils.js');

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
const msg = 'Please add a skipIfInspectorDisabled() call to allow this ' +
            'test to be skippped when Node is built \'--without-inspector\'.';

module.exports = function(context) {
  var usesInspector = false;
  var hasInspectorCheck = false;

  function testInspectorUsage(context, node) {
    if (utils.isRequired(node, ['inspector'])) {
      usesInspector = true;
    }
  }

  function checkMemberExpression(context, node) {
    if (utils.usesCommonProperty(node, ['skipIfInspectorDisabled'])) {
      hasInspectorCheck = true;
    }
  }

  function reportIfMissing(context, node) {
    if (usesInspector && !hasInspectorCheck) {
      context.report(node, msg);
    }
  }

  return {
    'CallExpression': (node) => testInspectorUsage(context, node),
    'MemberExpression': (node) => checkMemberExpression(context, node),
    'Program:exit': (node) => reportIfMissing(context, node)
  };
};