I want to write a couple of scripts to automatically detect missing imports and import them based upon a root directory. Is it better to write this script as a codemod script or as an eslint rule with the fix option?
Codemods are meant for migrations while linting is there permanently to nag/warn your developers about some mistake they have potentially made while developing. Both can be used together.
For your case, I think there are two approaches you can take:
Write a lint rule that detects the problem and a codemod to fix existing occurences of the problem. The lint rule ensures that developers won't miss that out in future.
Write a lint rule that detects the problem along with a --fix
option to automatically fix the problem.
I would lean towards approach two because it's more futureproof. You might want to just use this no-unresolved
ESLint rule directly rather than writing your own. In any case, the fix/codemod is not trivial and can be a performance hit if your project has many directories and files.