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 <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>
1 parent 4b2e886 commit f17a711

10 files changed

Lines changed: 104 additions & 8 deletions

‎test/.eslintrc.yaml‎

Lines changed: 1 addition & 0 deletions
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‎

Lines changed: 1 addition & 1 deletion
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‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

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

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

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

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

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

56
constRuleTester=require('../../tools/node_modules/eslint').RuleTester;
67
construle=require('../../tools/eslint-rules/documented-errors');
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
5+
common.skipIfEslintMissing();
6+
7+
constRuleTester=require('../../tools/node_modules/eslint').RuleTester;
8+
construle=require('../../tools/eslint-rules/eslint-check');
9+
10+
constmessage='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+
newRuleTester().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‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
'use strict';
22

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

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

89
constmessage='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
newRuleTester().run('inspector-check',rule,{

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

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

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

1717
constuseStrict='\'use strict\';\n\n';

‎tools/eslint-rules/eslint-check.js‎

Lines changed: 60 additions & 0 deletions
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+
constutils=require('./rules-utils.js');
8+
9+
//------------------------------------------------------------------------------
10+
// Rule Definition
11+
//------------------------------------------------------------------------------
12+
constmsg='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+
constmissingCheckNodes=[];
17+
varcommonModuleNode=null;
18+
varhasEslintCheck=false;
19+
20+
functiontestEslintUsage(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+
functioncheckMemberExpression(context,node){
31+
if(utils.usesCommonProperty(node,['skipIfEslintMissing'])){
32+
hasEslintCheck=true;
33+
}
34+
}
35+
36+
functionreportIfMissing(context){
37+
if(!hasEslintCheck){
38+
missingCheckNodes.forEach((node)=>{
39+
context.report({
40+
node,
41+
message: msg,
42+
fix: (fixer)=>{
43+
if(commonModuleNode){
44+
returnfixer.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‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const utils = require('./rules-utils.js');
1111
// Rule Definition
1212
//------------------------------------------------------------------------------
1313
constmsg='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
constmissingCheckNodes=[];

0 commit comments

Comments
 (0)