eslint-plugin-mdx
Version:
ESLint Plugin for MDX
50 lines • 2.11 kB
JavaScript
import { getRuleConfig } from './helpers.js';
const pluralize = (count) => (count === 1 ? 'line' : 'lines');
export const listItemSpacing = {
meta: {
type: 'layout',
docs: { description: 'Enforce list item spacing' },
schema: [
{
additionalProperties: false,
properties: { checkBlanks: { type: 'boolean' } },
type: 'object',
},
],
},
create(context) {
const config = getRuleConfig(context, 'list-item-spacing');
if (!config) {
return {};
}
return {
MDList(node) {
const spaced = node.children.some(item => {
if (config.options?.checkBlanks) {
return item.spread;
}
const tail = item.children.at(-1);
return !!tail?.loc && tail.loc.end.line - item.loc.start.line > 0;
});
const expected = spaced ? 1 : 0;
let previous;
for (const item of node.children) {
if (previous?.loc) {
const actual = item.loc.start.line - previous.loc.end.line - 1;
if (actual !== expected) {
const difference = expected - actual;
const direction = difference > 0 ? 'add' : 'remove';
const differenceAbsolute = Math.abs(difference);
context.report({
loc: { start: previous.loc.end, end: item.loc.start },
message: `Unexpected \`${actual}\` blank ${pluralize(actual)} between list items, expected \`${expected}\` blank ${pluralize(expected)}, ${direction} \`${differenceAbsolute}\` blank ${pluralize(differenceAbsolute)}`,
});
}
}
previous = item;
}
},
};
},
};
//# sourceMappingURL=list-item-spacing.js.map