summaryrefslogtreecommitdiff
path: root/tools/eslint-rules/eslint-check.js
blob: fcfd7f3f9000fef74d4dcad2c2051984b05b2688 (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
/**
 * @fileoverview Check that common.skipIfEslintMissing is used if
 *               the eslint module is required.
 */
'use strict';

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

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
const msg = 'Please add a skipIfEslintMissing() call to allow this test to ' +
            'be skipped when Node.js is built from a source tarball.';

module.exports = function(context) {
  const missingCheckNodes = [];
  let commonModuleNode = null;
  let hasEslintCheck = false;

  function testEslintUsage(context, node) {
    if (utils.isRequired(node, ['../../tools/node_modules/eslint'])) {
      missingCheckNodes.push(node);
    }

    if (utils.isCommonModule(node)) {
      commonModuleNode = node;
    }
  }

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

  function reportIfMissing(context) {
    if (!hasEslintCheck) {
      missingCheckNodes.forEach((node) => {
        context.report({
          node,
          message: msg,
          fix: (fixer) => {
            if (commonModuleNode) {
              return fixer.insertTextAfter(
                commonModuleNode,
                '\ncommon.skipIfEslintMissing();'
              );
            }
          }
        });
      });
    }
  }

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