@tpzdsp/eslint-config-dsp
Version:
re-usable config for ESLint, Prettier and semantic-release
71 lines (58 loc) • 1.57 kB
JavaScript
export default {
meta: {
type: 'layout',
docs: {
description:
'Require blank lines before block statements unless they are the first statement in a block',
},
fixable: 'whitespace',
schema: [],
messages: {
missingPadding: 'Expected blank line before this {{keyword}} statement.',
},
},
create: (context) => {
const checkPadding = (node) => {
if (!node.loc || !node.parent) {
return;
}
const parent = node.parent;
if (parent.type !== 'BlockStatement' && parent.type !== 'Program') {
return;
}
const body = parent.body;
if (!Array.isArray(body)) {
return;
}
const index = body.indexOf(node);
if (index === -1 || index === 0) {
return;
}
const prevNode = body[index - 1];
if (!prevNode || !prevNode.loc) {
return;
}
const linesBetween = node.loc.start.line - prevNode.loc.end.line;
if (linesBetween < 2) {
context.report({
node,
messageId: 'missingPadding',
data: { keyword: node.type.toLowerCase() },
fix: (fixer) => {
return fixer.insertTextBefore(node, '\n');
},
});
}
};
return {
IfStatement: checkPadding,
ForStatement: checkPadding,
ForInStatement: checkPadding,
ForOfStatement: checkPadding,
WhileStatement: checkPadding,
SwitchStatement: checkPadding,
TryStatement: checkPadding,
WithStatement: checkPadding,
};
},
};