UNPKG

eslint-plugin-mdx

Version:
69 lines • 2.75 kB
import { getRuleConfig } from './helpers.js'; const CHARS_PER_TAB = 4; const pluralize = (count) => (count === 1 ? 'space' : 'spaces'); const getListKind = (style, loose) => { if (style !== 'mixed') { return ''; } return loose ? ' in loose list' : ' in tight list'; }; export const listItemIndent = { meta: { type: 'layout', docs: { description: 'Enforce list item indentation' }, schema: [{ enum: ['mixed', 'one', 'tab'], type: 'string' }], }, create(context) { const config = getRuleConfig(context, 'list-item-indent'); if (!config) { return {}; } const configured = config.options === 'mixed' || config.options === 'one' || config.options === 'tab' ? config.options : undefined; const style = configured ?? 'one'; const checkItem = (item, loose) => { const head = item.children[0]; if (!head) { return; } let slice = item.raw.slice(0, head.range[0] - item.range[0]); const checkbox = slice.indexOf('['); if (checkbox !== -1) { slice = slice.slice(0, checkbox); } let end = slice.length; while (end > 0 && (slice[end - 1] === ' ' || slice[end - 1] === '\t')) { end--; } let expectedIndent = end + 1; if (style === 'tab' || (style === 'mixed' && loose)) { expectedIndent = Math.ceil(expectedIndent / CHARS_PER_TAB) * CHARS_PER_TAB; } const expectedSpaces = expectedIndent - end; const actualSpaces = slice.length - end; if (actualSpaces !== expectedSpaces) { const difference = expectedSpaces - actualSpaces; const listKind = getListKind(style, loose); const direction = difference > 0 ? 'add' : 'remove'; context.report({ loc: head.loc, message: `Unexpected \`${actualSpaces}\` ${pluralize(actualSpaces)} between list item marker and content${listKind}, expected \`${expectedSpaces}\` ${pluralize(expectedSpaces)}, ${direction} \`${Math.abs(difference)}\` ${pluralize(Math.abs(difference))}`, }); } }; return { MDList(list) { const loose = list.spread || list.children.some(item => 'spread' in item && item.spread); for (const item of list.children) { checkItem(item, loose); } }, }; }, }; //# sourceMappingURL=list-item-indent.js.map