UNPKG

eslint-plugin-mdx

Version:
139 lines • 5.38 kB
import { collapseWhiteSpace } from 'collapse-white-space'; import { normalizeIdentifier } from 'micromark-util-normalize-identifier'; import { getRuleConfig } from './helpers.js'; const FULL_REFERENCE_LENGTH = 4; const labelFor = (before, after) => { if (before === '!') { return 'image'; } return after === '^' ? 'footnote' : 'link'; }; export const noUndefinedReferences = { meta: { type: 'problem', docs: { description: 'Disallow undefined references' }, schema: [ { additionalProperties: false, properties: { allow: { items: { type: ['string', 'object'] }, type: 'array', }, allowShortcutLink: { type: 'boolean' }, }, type: 'object', }, ], }, create(context) { const config = getRuleConfig(context, 'no-undefined-references'); if (!config) { return {}; } const sourceText = context.sourceCode.getText(); const options = config.options; const allowShortcutLink = options?.allowShortcutLink === true; const allowStrings = new Set(); const allowRegexes = []; for (const value of options?.allow ?? []) { if (typeof value === 'string') { allowStrings.add(normalizeIdentifier(value)); } else { allowRegexes.push(new RegExp(value.source, value.flags ?? 'i')); } } const definitions = new Set(); const footnoteDefinitions = new Set(); const report = (range, node) => { if (range.length === 2 && range[0] + 2 === range[1]) { return; } const startOffset = node.range[0]; const before = sourceText[startOffset + range[0] - 1]; const after = sourceText[startOffset + range[0] + 1]; const label = labelFor(before, after); const offset = range.length === FULL_REFERENCE_LENGTH && range[2] + 2 !== range[3] ? 2 : 0; let identifier = normalizeIdentifier(collapseWhiteSpace(sourceText.slice(startOffset + range[offset] + 1, startOffset + range[offset + 1] - 1), { style: 'html', trim: true })); let defined = definitions; if (label === 'footnote') { if (identifier.includes(' ')) { return; } defined = footnoteDefinitions; identifier = identifier.slice(1); } if ((allowShortcutLink && range.length === 2) || defined.has(identifier) || allowStrings.has(identifier) || allowRegexes.some(regex => regex.test(identifier))) { return; } context.report({ loc: { end: context.sourceCode.getLocFromIndex(startOffset + range.at(-1)), start: context.sourceCode.getLocFromIndex(startOffset + range[0]), }, message: `Unexpected reference to undefined definition, expected corresponding definition (\`${identifier.toLowerCase()}\`) for ${label === 'image' ? 'an' : 'a'} ${label} or escaped opening bracket (\`\\[\`) for regular text`, }); }; const scan = (value, node) => { const ranges = []; let index = 0; while (index < value.length) { const char = value[index]; switch (char) { case '[': { ranges.push([index]); index++; break; } case '\\': { const next = value[index + 1]; index++; if (next === '[' || next === ']') { index++; } break; } case ']': { const range = ranges.at(-1); if (!range) { index++; } else if (value[index + 1] === '[' && range.length !== 3) { index++; range.push(index, index); index++; } else { index++; range.push(index); report(range, node); ranges.pop(); } break; } default: { index++; } } } }; return { MDDefinition(node) { definitions.add(normalizeIdentifier(node.identifier)); }, MDFootnoteDefinition(node) { footnoteDefinitions.add(normalizeIdentifier(node.identifier)); }, MDText(node) { scan(node.raw, node); }, }; }, }; //# sourceMappingURL=no-undefined-references.js.map