Skip to content

Instantly share code, notes, and snippets.

@VityaSchel
Created July 16, 2021 15:41
Show Gist options
  • Save VityaSchel/9579e24bea75fa8c1a533c4707d6eac5 to your computer and use it in GitHub Desktop.
Save VityaSchel/9579e24bea75fa8c1a533c4707d6eac5 to your computer and use it in GitHub Desktop.
Autofix for eslint because of React 17 new JSX transform: no-unsed-vars, import React from 'react' React is defined but never used
import fs from 'fs/promises'
// eslint.json must contain all files from project
// eslint ./ -o eslint.json -f json
// Only works with es6 imports
// Only works with react import on separate line
// Removes react from any line in file but only once
// Does not remove prop types
// How to run: cd your_project_dir && eslint ./ -o eslint.json -f json && node index.mjs
// Modify this simple script as you wish
let eslintJSON = await fs.readFile('eslint.json')
let json = JSON.parse(eslintJSON)
for (let file of json) {
if(file.warningCount > 0) {
for (let msg of file.messages) {
if(msg.ruleId === 'no-unused-vars') {
if(msg.message === '\'React\' is defined but never used.') {
let script = await fs.readFile(file.filePath, 'utf8')
let lines = script.split('\n')
lines.splice(lines.indexOf('import React from \'react\''), 1)
while(lines[0] === '')
lines.shift()
let newScript = lines.join('\n')
fs.writeFile(file.filePath, newScript)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment