UNPKG

eslint-plugin-mdx

Version:
81 lines 3.12 kB
import path from 'node:path'; import { DEFAULT_EXTENSIONS, MARKDOWN_EXTENSIONS, getPhysicalFilename, performSyncWork, } from 'eslint-mdx'; export const remark = { meta: { type: 'layout', docs: { description: 'Linter integration with remark plugins', category: 'Stylistic Issues', recommended: true, }, fixable: 'code', }, create(context) { const filename = context.getFilename(); const extname = path.extname(filename); const sourceCode = context.getSourceCode(); const options = context.parserOptions; const isMdx = [ ...DEFAULT_EXTENSIONS, ...(options.extensions || []), ].includes(extname); const isMarkdown = [ ...MARKDOWN_EXTENSIONS, ...(options.markdownExtensions || []), ].includes(extname); return { Program(node) { if (!isMdx && !isMarkdown) { return; } const ignoreRemarkConfig = Boolean(options.ignoreRemarkConfig); const sourceText = sourceCode.getText(node); const { messages, content: fixedText } = performSyncWork({ filePath: getPhysicalFilename(filename), code: sourceText, cwd: context.getCwd(), isMdx, process: true, ignoreRemarkConfig, }); let fixed = 0; for (const { source, reason, ruleId, fatal, line, column, place, } of messages) { const severity = fatal ? 2 : fatal == null ? 0 : 1; if (!severity) { continue; } const message = { reason, source, ruleId, severity, }; const point = { line, column: column - 1, }; context.report({ message: JSON.stringify(message), loc: place && 'start' in place ? { ...point, start: { ...place.start, column: place.start.column - 1 }, end: { ...place.end, column: place.end.column - 1 }, } : point, node, fix: fixedText == null || fixedText === sourceText ? null : () => fixed++ ? null : { range: [0, sourceText.length], text: fixedText, }, }); } }, }; }, }; //# sourceMappingURL=remark.js.map