eslint-plugin-putout
Version:
ESLint plugin for 🐊Putout
85 lines (57 loc) • 1.88 kB
JavaScript
import {types} from 'putout';
const {
isBlockStatement,
isExpressionStatement,
isVariableDeclaration,
isObjectExpression,
isArrayExpression,
} = types;
const regExp = /^;?\n( +)?\n( +)?/;
export const category = 'layout';
export const report = () => 'Add newline after function call';
export const filter = ({node, getCommentsAfter, getSpacesAfterNode}) => {
if (!isExpressionStatement(node.parent))
return false;
if (getCommentsAfter(node.parent).length)
return false;
const {parent} = node.parent;
if (!isBlockStatement(parent))
return false;
const {body} = parent;
const n = body.length;
if (n < 3)
return false;
const spaces = getSpacesAfterNode(node);
if (regExp.test(spaces))
return false;
for (let i = 0; i < n; i++) {
const current = body[i];
if (current !== node.parent)
continue;
if (i === n - 1)
break;
const prev = body[i - 1];
const next = body[i + 1];
if (!isVariableDeclaration(next))
break;
const {init} = next.declarations[0];
if (isObjectExpression(init) && init.properties.length)
return true;
if (isArrayExpression(init) && init.elements.length)
return true;
const spacesAfterNext = getSpacesAfterNode(next);
if (regExp.test(spacesAfterNext))
break;
if (!prev)
return true;
const spacesAfterPrev = getSpacesAfterNode(prev);
return !regExp.test(spacesAfterPrev);
}
return false;
};
export const fix = ({text}) => {
return `${text};\n`;
};
export const include = () => [
'CallExpression',
];