• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * @fileoverview Check that common.skipIfInspectorDisabled is used if
3 *               the inspector module is required.
4 * @author Daniel Bevenius <daniel.bevenius@gmail.com>
5 */
6'use strict';
7
8const utils = require('./rules-utils.js');
9
10//------------------------------------------------------------------------------
11// Rule Definition
12//------------------------------------------------------------------------------
13const msg = 'Please add a skipIfInspectorDisabled() call to allow this ' +
14            'test to be skipped when Node is built \'--without-inspector\'.';
15
16module.exports = function(context) {
17  const missingCheckNodes = [];
18  let commonModuleNode = null;
19  let hasInspectorCheck = false;
20
21  function testInspectorUsage(context, node) {
22    if (utils.isRequired(node, ['inspector'])) {
23      missingCheckNodes.push(node);
24    }
25
26    if (utils.isCommonModule(node)) {
27      commonModuleNode = node;
28    }
29  }
30
31  function checkMemberExpression(context, node) {
32    if (utils.usesCommonProperty(node, ['skipIfInspectorDisabled'])) {
33      hasInspectorCheck = true;
34    }
35  }
36
37  function reportIfMissing(context) {
38    if (!hasInspectorCheck) {
39      missingCheckNodes.forEach((node) => {
40        context.report({
41          node,
42          message: msg,
43          fix: (fixer) => {
44            if (commonModuleNode) {
45              return fixer.insertTextAfter(
46                commonModuleNode,
47                '\ncommon.skipIfInspectorDisabled();'
48              );
49            }
50          }
51        });
52      });
53    }
54  }
55
56  return {
57    'CallExpression': (node) => testInspectorUsage(context, node),
58    'MemberExpression': (node) => checkMemberExpression(context, node),
59    'Program:exit': () => reportIfMissing(context)
60  };
61};
62
63module.exports.meta = {
64  fixable: 'code'
65};
66