-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
4b2e886
commit f17a711
Showing
10 changed files
with
104 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
common.skipIfEslintMissing(); | ||
|
||
const RuleTester = require('../../tools/node_modules/eslint').RuleTester; | ||
const rule = require('../../tools/eslint-rules/eslint-check'); | ||
|
||
const message = 'Please add a skipIfEslintMissing() call to allow this ' + | ||
'test to be skipped when Node.js is built ' + | ||
'from a source tarball.'; | ||
|
||
new RuleTester().run('eslint-check', rule, { | ||
valid: [ | ||
'foo;', | ||
'require("common")\n' + | ||
'common.skipIfEslintMissing();\n' + | ||
'require("../../tools/node_modules/eslint")' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'require("common")\n' + | ||
'require("../../tools/node_modules/eslint").RuleTester', | ||
errors: [{ message }], | ||
output: 'require("common")\n' + | ||
'common.skipIfEslintMissing();\n' + | ||
'require("../../tools/node_modules/eslint").RuleTester' | ||
} | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 = []; | ||
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) | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters