0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 07:27:22 +01:00
nodejs/tools/eslint-rules/eslint-check.js
Richard Lau 870ae72227 tools: add eslint check for skipIfEslintMissing
Add a custom eslint rule to check for `common.skipIfEslintMissing()` to
allow tests to run from source tarballs that do not include eslint.

Fix up rule tests that were failing the new check.

Refs: https://github.com/nodejs/node/issues/20336

PR-URL: https://github.com/nodejs/node/pull/20372
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2018-05-08 10:43:26 -04:00

61 lines
1.7 KiB
JavaScript

/**
* @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 = [];
var commonModuleNode = null;
var 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': (node) => reportIfMissing(context, node)
};
};