Is it possible to ban a list of words with ESlint or anything else when pre-commit?

这一生的挚爱 提交于 2021-01-28 08:47:48

问题


I am using husky to deal with the pre-commit thing.

So here I want that the newly written program should not contain a list of words, like dangerouslySetInnerHTML, etc.

I know there is a rule no-danger in eslint-plugin-react, but it only prevents that one word. How can I write a list of words in a file and use it as a filter?


回答1:


So I finally solved this problem myself.

The solution lies in taking advantage of git hook and the precommit, which is one of those.

A good resource of achieving this is here:

Pre-commit Git Hook that Prevents Commits with Undesired Words

And with Husky, which is "Git Hooks Made Easy", we first write a script, which is exactly like what has been showed in the blog above. Then we can add the hook itself in the script of package.json, which is like this:

"script": {
  "precommit": "./pre-commit.sh"
}

This means everytime you commit, the pre-commit script will be run first. And thus you can filter the words that are undesired.




回答2:


This is possible, You should be able to write a new Eslint rule that takes in an array of prohibited words then run Eslint in a pre-commit script. I've done this using a git pre-commit script (not sure about husky, sorry). For a starting point I would look at eslint-plugin-jasmine. The no focused test rule sets a list of prohibited words (ddescribe, iit, fdescribe, and fit) and checks code for these words. You could do the same thing but change the prohibited array to contain the words you want to ban.



来源:https://stackoverflow.com/questions/44643324/is-it-possible-to-ban-a-list-of-words-with-eslint-or-anything-else-when-pre-comm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!