Last active
December 13, 2019 03:20
-
-
Save caaatisgood/649990de3e4755393074cbfbfd917ba6 to your computer and use it in GitHub Desktop.
Disable eslint rules in a breeze.
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
const fs = require('fs') | |
const input = '' | |
const _log = ({ type, value }) => { | |
console.log(`[ eslater ${type} ]` + (value ? ` - ${value}` : '')) | |
} | |
const read = () => { | |
return input | |
} | |
const getFileRulesMap = (input) => { | |
return input.split('\n\n').reduce((fileRulesMap, group) => { | |
const [file, ...rules] = group.split('\n') | |
const rulesMap = rules.reduce((map, rule) => { | |
map[rule.split(' ').pop()] = '' | |
return map | |
}, {}) | |
fileRulesMap[file] = Object.keys(rulesMap) | |
return fileRulesMap | |
}, {}) | |
} | |
const getDisableComment = (rule) => `/* eslint-disable ${rule} */` | |
const eslintDisable = (fileRulesMap) => { | |
let writtenCount = 0 | |
Object.entries(fileRulesMap).forEach(([filePath, rules]) => { | |
let rawContent | |
try { | |
rawContent = fs.readFileSync(filePath) | |
} catch (err) { | |
_log({ type: 'failed to read', value: err }) | |
} | |
if (rawContent === undefined) { | |
return | |
} | |
const newContent = `${rules.map(getDisableComment).join('\n')}\n${rawContent}` | |
try { | |
fs.writeFileSync(filePath, newContent) | |
process.stdout.write('.') | |
writtenCount++ | |
} catch (err) { | |
_log({ type: 'failed to write', value: err }) | |
} | |
}) | |
if (writtenCount > 0) { | |
console.log('') | |
} | |
return writtenCount | |
} | |
const eslater = () => { | |
_log({ type: 'starts' }) | |
_log({ type: 'reading' }) | |
const input = read() | |
_log({ type: 'parsing' }) | |
const fileRulesMap = getFileRulesMap(input) | |
_log({ type: 'writing' }) | |
const writtenCount = eslintDisable(fileRulesMap) | |
_log({ type: 'done', value: `${writtenCount} files written` }) | |
} | |
eslater() | |
/** | |
* example format of `input`: | |
/Users/somebody/my-app/src/Layout.js | |
53:63 error ["DEFAULT"] is better written in dot notation dot-notation | |
193:5 error Identifier 'with_border' is not in camel case camelcase | |
194:5 error Identifier 'with_background' is not in camel case camelcase | |
/Users/somebody/my-app/src/Selector.js | |
75:3 error propType "options" is not required, but has no corresponding defaultProps declaration react/require-default-props | |
83:3 error propType "defaultOption" is not required, but has no corresponding defaultProps declaration react/require-default-props | |
/Users/somebody/my-app/src/About.js | |
67:58 error ["Oceania"] is better written in dot notation dot-notation | |
73:58 error ["Africa"] is better written in dot notation dot-notation | |
* | |
* comments that used to disable eslint rules will be prepended right after you "eslater" | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment