eslint-plugin-mdx
Version:
ESLint Plugin for MDX
43 lines • 1.72 kB
JavaScript
import { ENABLED_OPTION_SCHEMA, getRuleConfig } from './helpers.js';
export const finalNewline = {
meta: {
type: 'layout',
docs: {
description: 'Require a final newline',
},
fixable: 'code',
schema: [ENABLED_OPTION_SCHEMA],
},
create(context) {
const config = getRuleConfig(context, 'final-newline');
if (!config) {
return {};
}
const sourceText = context.sourceCode.getText();
return {
Program() {
if (sourceText.length > 0 && !sourceText.endsWith('\n')) {
context.report({
loc: {
start: {
line: context.sourceCode.getLocFromIndex(sourceText.length)
.line,
column: context.sourceCode.getLocFromIndex(sourceText.length)
.column,
},
end: {
line: context.sourceCode.getLocFromIndex(sourceText.length)
.line,
column: context.sourceCode.getLocFromIndex(sourceText.length)
.column,
},
},
message: 'Unexpected missing final newline character, expected line feed (`\\n`) at end of file',
fix: fixer => fixer.insertTextAfterRange([0, sourceText.length], '\n'),
});
}
},
};
},
};
//# sourceMappingURL=final-newline.js.map