Skip to content

Commit

Permalink
WIP v4
Browse files Browse the repository at this point in the history
  • Loading branch information
levibuzolic committed Jun 5, 2024
1 parent 709c025 commit 2b860dc
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module.exports = {
rules: {
'no-only-tests': require('./rules/no-only-tests')
'no-only-tests': require('./rules/no-only-tests'),
'no-skipped-tests': require('./rules/no-skipped-tests')
}
};
88 changes: 88 additions & 0 deletions rules/no-skipped-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* @fileoverview Rule to flag use of skipped test blocks, preventing ignored tests being committed accidentally
* @author Levi Buzolic
*/

'use strict';

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

const defaultOptions = {
match: ['xit', 'xdescribe', 'xcontext', 'xtape', 'xtest'],
fix: false,
};

module.exports = {
meta: {
docs: {
description: 'disallow skipped tests',
category: 'Possible Errors',
recommended: true,
url: 'https://github.com/levibuzolic/eslint-plugin-no-only-tests',
},
fixable: true,
schema: [
{
type: 'object',
properties: {
match: {
type: 'array',
items: {
type: 'string',
},
uniqueItems: true,
default: defaultOptions.match,
},
fix: {
type: 'boolean',
default: defaultOptions.fix,
},
},
additionalProperties: false,
},
],
},
create(context) {
const options = Object.assign({}, defaultOptions, context.options[0]);
const blocks = options.block || [];
const focus = options.focus || [];
const fix = !!options.fix;

return {
Identifier(node) {
const parentObject = node.parent && node.parent.object;
if (parentObject == null) return;
if (focus.indexOf(node.name) === -1) return;

const callPath = getCallPath(node.parent).join('.');

// comparison guarantees that matching is done with the beginning of call path
if (
blocks.find(block => {
// Allow wildcard tail matching of blocks when ending in a `*`
if (block.endsWith('*')) return callPath.startsWith(block.replace(/\*$/, ''));
return callPath.startsWith(`${block}.`);
})
) {
context.report({
node,
message: callPath + ' not permitted',
fix: fix ? fixer => fixer.removeRange([node.range[0] - 1, node.range[1]]) : undefined,
});
}
},
};
},
};

function getCallPath(node, path = []) {
if (node) {
const nodeName = node.name || (node.property && node.property.name);
if (node.object) return getCallPath(node.object, [nodeName, ...path]);
if (node.callee) return getCallPath(node.callee, path);
return [nodeName, ...path];
}
return path;
}

0 comments on commit 2b860dc

Please sign in to comment.