Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
eslint suppresor
/* eslint-env node */
const fs = require(`fs`);
// Generated using: node node_modules/eslint/bin/eslint.js -c .eslintrc.js media/**/*.es5.js -f json > _errors.json
const errors = JSON.parse(fs.readFileSync(`_errors.json`));
for (const error of errors) {
const {filePath, messages, source} = error;
const fileRuleTally = {};
const undefs = new Set();
for (const errMessage of messages) {
const {ruleId, message} = errMessage;
if (!fileRuleTally[ruleId]) fileRuleTally[ruleId] = 0;
fileRuleTally[ruleId]++;
if (ruleId === `no-undef`) {
const undef = message.match(/'(\w+)' is not defined/)[1];
undefs.add(undef);
}
}
if (source) {
let oldSource = source.trimLeft();
const eslintCommentMatch = oldSource.match(/^\/\* eslint (.*) \*\/\n/);
if (eslintCommentMatch) {
// update existing rules
eslintCommentMatch[1]
.split(`, `)
.forEach(r => fileRuleTally[r.split(`:`)[0]] = 1);
oldSource = oldSource.substr(eslintCommentMatch[0].length);
}
const strSort = (a, b) => a.toLowerCase().localeCompare(b.toLowerCase());
const rules = Object.keys(fileRuleTally).sort(strSort).filter(r => r !== `no-undef`);
const supressComment = rules.length ?
`/* eslint ${rules.map(r => `${r}: 0`).join(`, `)} */\n` :
``;
const globalsComment = undefs.size ?
`/* globals ${Array.from(undefs).sort(strSort).join(`, `)} */\n`:
``;
console.info(filePath);
const newSource = supressComment + globalsComment + oldSource;
fs.writeFileSync(filePath, newSource);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.