eslint-plugin-mdx
Version:
ESLint Plugin for MDX
143 lines • 5.97 kB
JavaScript
import { getPhysicalFilename, performSyncWork } from 'eslint-mdx';
import { generateDifferences } from 'prettier-linter-helpers';
import { NATIVE_REMARK_RULES, getParserOptions, isMarkdownFile, isMdxFile, } from './helpers.js';
const LARGE_DOCUMENT_THRESHOLD = 64 * 1024;
const MAX_FIXED_TEXT_RATIO = 2;
let warnedAboutDeprecatedFix = false;
const warnDeprecatedFix = (enabled) => {
if (!enabled || warnedAboutDeprecatedFix) {
return;
}
warnedAboutDeprecatedFix = true;
process.emitWarning('Stringify-based autofixes from mdx/remark are deprecated and will be removed in a future major release. Enable the native config for targeted fixes; reporting remains supported.', {
code: 'ESLINT_MDX_DEPRECATED_REMARK_FIX',
type: 'DeprecationWarning',
});
};
const normalizeDifference = (difference) => ({
deleteText: '',
insertText: '',
...difference,
});
const getDifferences = (sourceText, fixedText) => generateDifferences(sourceText, fixedText).map(normalizeDifference);
const getFixesForPlace = (differences, place) => {
if (place == null) {
return null;
}
const start = 'start' in place ? place.start : place;
const end = 'start' in place ? place.end : place;
if (start.offset == null || end.offset == null) {
return null;
}
const fixes = differences
.filter(({ deleteText, offset }) => offset <= end.offset && offset + deleteText.length >= start.offset)
.map(({ deleteText, insertText, offset }) => ({
range: [offset, offset + deleteText.length],
text: insertText,
}));
return fixes.length > 0 ? fixes : null;
};
export const remark = {
meta: {
type: 'layout',
docs: {
description: 'Linter integration with remark plugins',
category: 'Stylistic Issues',
recommended: true,
},
fixable: 'code',
schema: [
{
additionalProperties: false,
properties: {
deprecationWarnings: { type: 'boolean' },
native: { type: 'boolean' },
},
type: 'object',
},
],
},
create(context) {
const nativeOption = context.options[0];
const native = nativeOption?.native === true;
const filename = context.filename ??
context.getFilename();
const sourceCode = context.sourceCode ??
context.getSourceCode();
const { extensions, markdownExtensions, ignoreRemarkConfig, remarkConfigPath, } = getParserOptions(context);
const isMdx = isMdxFile(filename, extensions);
const isMarkdown = isMarkdownFile(filename, markdownExtensions);
return {
Program(node) {
if (!isMdx && !isMarkdown) {
return;
}
const sourceText = sourceCode.getText(node);
const { messages, content: fixedText } = performSyncWork({
filePath: getPhysicalFilename(filename),
code: sourceText,
cwd: context.cwd ??
context.getCwd(),
isMdx,
process: true,
ignoreRemarkConfig,
ignoreRemarkRules: native ? NATIVE_REMARK_RULES : new Set(),
remarkConfigPath,
});
let differences;
let wholeDocumentFixed = false;
const getDifferencesOnce = () => (differences ??= getDifferences(sourceText, fixedText));
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: () => {
warnDeprecatedFix(nativeOption?.deprecationWarnings !== false);
if (fixedText == null || fixedText === sourceText) {
return null;
}
if (fixedText.length > sourceText.length * MAX_FIXED_TEXT_RATIO) {
return null;
}
if (sourceText.length > LARGE_DOCUMENT_THRESHOLD) {
if (wholeDocumentFixed) {
return null;
}
wholeDocumentFixed = true;
return [
{
range: [0, sourceText.length],
text: fixedText,
},
];
}
return getFixesForPlace(getDifferencesOnce(), place);
},
});
}
},
};
},
};
//# sourceMappingURL=remark.js.map