Skip to content

Instantly share code, notes, and snippets.

@nojvek
Last active February 6, 2018 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nojvek/ba67e6cf842440c349d59bf6ade1c26e to your computer and use it in GitHub Desktop.
Save nojvek/ba67e6cf842440c349d59bf6ade1c26e to your computer and use it in GitHub Desktop.
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