eslint-plugin-ava
Version:
ESLint rules for AVA
64 lines (56 loc) • 1.64 kB
JavaScript
import createAvaRule from '../create-ava-rule.js';
import util from '../util.js';
const MESSAGE_ID = 'no-commented-tests';
// The leading `\s*\*?\s*` can match the same whitespace twice, so a long line of whitespace
// makes this backtrack polynomially. `/^\s*(?:\*\s*)?(?:test|serial)\s*(?:\.\s*\w+\s*)*\(/v`
// is equivalent on every input and is unambiguous, but changing a shipped rule's matching
// pattern belongs in its own commit, not in the Node.js 22 migration.
// eslint-disable-next-line regexp/no-super-linear-backtracking
const commentedTestPattern = /^\s*\*?\s*(?:test|serial)\s*(?:\.\s*\w+\s*)*\(/v;
const create = context => {
const ava = createAvaRule(context.sourceCode);
return ava.merge({
'Program:exit'() {
if (!ava.isInTestFile()) {
return;
}
for (const comment of context.sourceCode.getAllComments()) {
if (
comment.type === 'Block'
&& context.sourceCode.getText(comment).startsWith('/**')
) {
continue;
}
const lines = comment.value.split('\n');
for (const [index, line] of lines.entries()) {
if (commentedTestPattern.test(line)) {
context.report({
loc: {
line: comment.loc.start.line + index,
column: 0,
},
messageId: MESSAGE_ID,
});
break;
}
}
}
},
});
};
export default {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Disallow commented-out tests.',
recommended: true,
url: util.getDocsUrl(import.meta.filename),
},
schema: [],
messages: {
[MESSAGE_ID]: 'Use `test.skip()` instead of commenting out a test.',
},
languages: ['js/js'],
},
};