eslint-plugin-mdx
Version:
ESLint Plugin for MDX
59 lines • 2.18 kB
JavaScript
import { getRuleConfig } from './helpers.js';
const configuredStyle = (option) => {
if (option === 'one' || option === 'ordered' || option === 'single') {
return option;
}
};
const inferStyle = (first, second) => {
if (second != null && second === first) {
return 'single';
}
return 'ordered';
};
const expectedValue = (style, first, index) => {
if (style === 'one') {
return 1;
}
return style === 'single' ? first : first + index;
};
const markerValue = (item) => {
const match = /^(\d+)[.)]/u.exec(item.raw);
return Number(match[1]);
};
export const orderedListMarkerValue = {
meta: {
type: 'layout',
docs: { description: 'Enforce ordered list marker values' },
schema: [{ enum: ['one', 'ordered', 'single'], type: 'string' }],
},
create(context) {
const config = getRuleConfig(context, 'ordered-list-marker-value');
if (!config) {
return {};
}
return {
MDList(node) {
if (!node.ordered) {
return;
}
const values = node.children.map(item => markerValue(item));
const first = values[0];
const second = values[1];
const style = configuredStyle(config.options) ?? inferStyle(first, second);
for (const [index, actual] of values.entries()) {
const expected = expectedValue(style, first, index);
if (actual !== expected) {
const item = node.children[index];
const marker = /^\d+[.)]/u.exec(item.raw)[0];
const markerStart = context.sourceCode.getLocFromIndex(item.range[0] + marker.length - 1);
context.report({
loc: { start: markerStart, end: markerStart },
message: `Unexpected ordered list item value \`${actual}\`, expected \`${expected}\``,
});
}
}
},
};
},
};
//# sourceMappingURL=ordered-list-marker-value.js.map