Skip to content

Commit f17a711

Browse files
richardlauMylesBorins
authored andcommitted
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: #20336 PR-URL: #20372 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 4b2e886 commit f17a711

10 files changed

+104
-8
lines changed

test/.eslintrc.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ rules:
1313
node-core/prefer-common-expectserror: error
1414
node-core/prefer-common-mustnotcall: error
1515
node-core/crypto-check: error
16+
node-core/eslint-check: error
1617
node-core/inspector-check: error
1718
node-core/number-isnan: error
1819
## common module is mandatory in tests

test/common/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ exports.fileExists = function(pathname) {
494494

495495
exports.skipIfEslintMissing = function() {
496496
if (!exports.fileExists(
497-
path.join('..', '..', 'tools', 'node_modules', 'eslint')
497+
path.join(__dirname, '..', '..', 'tools', 'node_modules', 'eslint')
498498
)) {
499499
exports.skip('missing ESLint');
500500
}

test/parallel/test-eslint-alphabetize-errors.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

3-
require('../common');
3+
const common = require('../common');
4+
common.skipIfEslintMissing();
45

56
const RuleTester = require('../../tools/node_modules/eslint').RuleTester;
67
const rule = require('../../tools/eslint-rules/alphabetize-errors');

test/parallel/test-eslint-buffer-constructor.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

3-
require('../common');
3+
const common = require('../common');
4+
common.skipIfEslintMissing();
45

56
const RuleTester = require('../../tools/node_modules/eslint').RuleTester;
67
const rule = require('../../tools/eslint-rules/buffer-constructor');

test/parallel/test-eslint-documented-errors.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

3-
require('../common');
3+
const common = require('../common');
4+
common.skipIfEslintMissing();
45

56
const RuleTester = require('../../tools/node_modules/eslint').RuleTester;
67
const rule = require('../../tools/eslint-rules/documented-errors');
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
common.skipIfEslintMissing();
6+
7+
const RuleTester = require('../../tools/node_modules/eslint').RuleTester;
8+
const rule = require('../../tools/eslint-rules/eslint-check');
9+
10+
const message = 'Please add a skipIfEslintMissing() call to allow this ' +
11+
'test to be skipped when Node.js is built ' +
12+
'from a source tarball.';
13+
14+
new RuleTester().run('eslint-check', rule, {
15+
valid: [
16+
'foo;',
17+
'require("common")\n' +
18+
'common.skipIfEslintMissing();\n' +
19+
'require("../../tools/node_modules/eslint")'
20+
],
21+
invalid: [
22+
{
23+
code: 'require("common")\n' +
24+
'require("../../tools/node_modules/eslint").RuleTester',
25+
errors: [{ message }],
26+
output: 'require("common")\n' +
27+
'common.skipIfEslintMissing();\n' +
28+
'require("../../tools/node_modules/eslint").RuleTester'
29+
}
30+
]
31+
});

test/parallel/test-eslint-inspector-check.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
'use strict';
22

3-
require('../common');
3+
const common = require('../common');
4+
common.skipIfEslintMissing();
45

56
const RuleTester = require('../../tools/node_modules/eslint').RuleTester;
67
const rule = require('../../tools/eslint-rules/inspector-check');
78

89
const message = 'Please add a skipIfInspectorDisabled() call to allow this ' +
9-
'test to be skippped when Node is built ' +
10+
'test to be skipped when Node is built ' +
1011
'\'--without-inspector\'.';
1112

1213
new RuleTester().run('inspector-check', rule, {

test/parallel/test-eslint-require-buffer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const ruleTester = new RuleTester({
1111
env: { node: true }
1212
});
1313

14-
const message = "Use const Buffer = require('buffer').Buffer; " +
14+
const message = "Use const { Buffer } = require('buffer'); " +
1515
'at the beginning of this file';
1616

1717
const useStrict = '\'use strict\';\n\n';

tools/eslint-rules/eslint-check.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @fileoverview Check that common.skipIfEslintMissing is used if
3+
* the eslint module is required.
4+
*/
5+
'use strict';
6+
7+
const utils = require('./rules-utils.js');
8+
9+
//------------------------------------------------------------------------------
10+
// Rule Definition
11+
//------------------------------------------------------------------------------
12+
const msg = 'Please add a skipIfEslintMissing() call to allow this test to ' +
13+
'be skipped when Node.js is built from a source tarball.';
14+
15+
module.exports = function(context) {
16+
const missingCheckNodes = [];
17+
var commonModuleNode = null;
18+
var hasEslintCheck = false;
19+
20+
function testEslintUsage(context, node) {
21+
if (utils.isRequired(node, ['../../tools/node_modules/eslint'])) {
22+
missingCheckNodes.push(node);
23+
}
24+
25+
if (utils.isCommonModule(node)) {
26+
commonModuleNode = node;
27+
}
28+
}
29+
30+
function checkMemberExpression(context, node) {
31+
if (utils.usesCommonProperty(node, ['skipIfEslintMissing'])) {
32+
hasEslintCheck = true;
33+
}
34+
}
35+
36+
function reportIfMissing(context) {
37+
if (!hasEslintCheck) {
38+
missingCheckNodes.forEach((node) => {
39+
context.report({
40+
node,
41+
message: msg,
42+
fix: (fixer) => {
43+
if (commonModuleNode) {
44+
return fixer.insertTextAfter(
45+
commonModuleNode,
46+
'\ncommon.skipIfEslintMissing();'
47+
);
48+
}
49+
}
50+
});
51+
});
52+
}
53+
}
54+
55+
return {
56+
'CallExpression': (node) => testEslintUsage(context, node),
57+
'MemberExpression': (node) => checkMemberExpression(context, node),
58+
'Program:exit': (node) => reportIfMissing(context, node)
59+
};
60+
};

tools/eslint-rules/inspector-check.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const utils = require('./rules-utils.js');
1111
// Rule Definition
1212
//------------------------------------------------------------------------------
1313
const msg = 'Please add a skipIfInspectorDisabled() call to allow this ' +
14-
'test to be skippped when Node is built \'--without-inspector\'.';
14+
'test to be skipped when Node is built \'--without-inspector\'.';
1515

1616
module.exports = function(context) {
1717
const missingCheckNodes = [];

0 commit comments

Comments
 (0)