eslint-plugin-lob
Version:
Custom ESLint rules for Lob repositories
49 lines (40 loc) • 1.2 kB
JavaScript
;
var ERROR_MESSAGE = 'It and describe blocks must be separated by new lines.';
function isMochaCall (node) {
return node.callee.name === 'it' ||
node.callee.name === 'describe' ||
node.callee.name === 'beforeEach' ||
node.callee.name === 'afterEach' ||
node.callee.name === 'before' ||
node.callee.name === 'after';
}
function isLastNode (node) {
return node.parent.body[node.parent.body.length - 1] === node;
}
module.exports = {
meta: {
type: 'layout',
schema: []
},
create (ctx) {
return {
CallExpression (node) {
var sourceCode = ctx.sourceCode;
var statement = node.parent;
if (!isMochaCall(node)) {
return;
}
if (isLastNode(statement)) {
return;
}
var lastToken = sourceCode.getLastToken(statement);
var nextToken = sourceCode.getTokenAfter(statement);
var expectedNextLineNum = lastToken.loc.end.line + 1;
var hasNewLine = nextToken.loc.start.line > expectedNextLineNum;
if (!hasNewLine) {
ctx.report({ node, message: ERROR_MESSAGE, data: { identifier: node.name } });
}
}
};
}
};