Skip to content

Instantly share code, notes, and snippets.

@voldern
Created November 6, 2019 10:02
Show Gist options
  • Save voldern/03fcf61ad145424936ad7cb4da11599f to your computer and use it in GitHub Desktop.
Save voldern/03fcf61ad145424936ad7cb4da11599f to your computer and use it in GitHub Desktop.
const fs = require('fs');
const { uniq } = require('ramda');
const errors = require('./eslint2.json');
function insertBeforeLine(filename, line, text) {
const fileData = fs
.readFileSync(filename)
.toString()
.split('\n');
fileData.splice(line - 1, 0, text);
const newData = fileData.join('\n');
fs.writeFileSync(filename, fileData.join('\n'));
}
errors.forEach(file => {
if (!file.messages.length) {
return;
}
const messages = file.messages.reduce((acc, msg) => {
if (!acc.has(msg.line)) {
acc.set(msg.line, []);
}
acc.get(msg.line).push(msg.ruleId);
return acc;
}, new Map());
let offset = 0;
messages.forEach((ruleIds, line) => {
const uniqueRules = uniq(ruleIds);
const exclude = `// eslint-disable-next-line ${uniqueRules.join(', ')}`;
insertBeforeLine(file.filePath, line + offset, exclude);
offset += 1;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment