diff --git a/test/.eslintrc.yaml b/test/.eslintrc.yaml index 501c40781c6ebd..08c2dbbd77fa17 100644 --- a/test/.eslintrc.yaml +++ b/test/.eslintrc.yaml @@ -10,5 +10,6 @@ rules: prefer-assert-iferror: 2 prefer-assert-methods: 2 prefer-common-mustnotcall: 2 + inspector-check: 2 ## common module is mandatory in tests required-modules: [2, common] diff --git a/tools/eslint-rules/inspector-check.js b/tools/eslint-rules/inspector-check.js new file mode 100644 index 00000000000000..e0a6b406036fc2 --- /dev/null +++ b/tools/eslint-rules/inspector-check.js @@ -0,0 +1,43 @@ +/** + * @fileoverview Check that common.skipIfInspectorDisabled is used if + * the inspector module is required. + * @author Daniel Bevenius + */ +'use strict'; + +//------------------------------------------------------------------------------ +// Rule Definition +//------------------------------------------------------------------------------ +const msg = 'Please add a skipIfInspectorDisabled() call to allow this ' + + 'test to be skippped when Node is built \'--without-inspector\'.'; + +module.exports = function(context) { + var usesInspector = false; + var hasInspectorCheck = false; + + function testInspectorUsage(context, node) { + if (node.callee.name === 'require' && + node.arguments[0].value === 'inspector') { + usesInspector = true; + } + } + + function checkMemberExpression(context, node) { + if (node.parent.type === 'CallExpression' && + node.property.name === 'skipIfInspectorDisabled') { + hasInspectorCheck = true; + } + } + + function reportIfMissing(context, node) { + if (usesInspector && !hasInspectorCheck) { + context.report(node, msg); + } + } + + return { + 'CallExpression': (node) => testInspectorUsage(context, node), + 'MemberExpression': (node) => checkMemberExpression(context, node), + 'Program:exit': (node) => reportIfMissing(context, node) + }; +};