Created
April 1, 2021 10:08
-
-
Save mparramont/53b4e4d10004098e0e70c333299b5126 to your computer and use it in GitHub Desktop.
Disable all existing ESlint errors with eslint-disable-line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// First, run eslint -f json -o warnings.json | |
// Then, run this file | |
const fs = require('fs') | |
const json = require('./warnings.json') | |
json.forEach(({ filePath, messages, source }) => { | |
// if there is no source we have nothing that needs to be eslint-ignore'd | |
if (!source) { | |
return | |
} | |
const data = source.split('\n') | |
// if the source has multiple lines which need to be eslint-ignored our offset changes per addition | |
// offset is 1 because line numbers start at 1 but index numbers in an array start at 0 | |
let offset = 1 | |
// group errors/warnings by line because we want to have one eslint disable comment with all the rules to disable | |
const groupedMessages = messages.reduce((acc, next) => { | |
const prevMessages = acc[next.line] ? acc[next.line] : [] | |
// some lines may have the same rule twice | |
const duplicateRuleForLine = prevMessages.find( | |
(message) => message.ruleId === next.ruleId | |
) | |
// ignore jsx and graphql lint rules | |
const applicableRule = | |
next.ruleId && | |
!next.ruleId.includes('jsx') && | |
!next.ruleId.includes('graphql') | |
// ignore the eslint-ignore addition for duplicates and non applicable rules | |
if (duplicateRuleForLine || !applicableRule) { | |
return acc | |
} | |
return { | |
...acc, | |
[next.line]: [...prevMessages, next] | |
} | |
}, {}) | |
Object.entries(groupedMessages).forEach(([line, messages]) => { | |
// grouped ignores | |
const ignore = `// eslint-disable-next-line ${messages | |
.map(({ ruleId }) => ruleId) | |
.join(',')}` | |
data.splice(line - offset, 0, ignore) | |
data.splice(line - offset, 0, '// TODO fix') | |
offset -= 2 | |
}) | |
const updated = data.join('\n') | |
fs.writeFile(filePath, updated, (err) => { | |
if (err) return console.log(err) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment