English 中文(简体)
Is there any way to ban certain filetypes from entering my codebase with ESLint/TypeScript ESLint?
原标题:

My company has been working to convert all of our JavaScript (.js or .jsx) files into TypeScript (.ts & .tsx). The problem is, we re a fairly large company and there will be a continuous stream of .js and .jsx files sneaking into the codebase unless we lint against them specifically. Also, ideally there would be a way to force a bypass of this for circumstances in which we need .js or .jsx files.

We use ESLint, typescript-eslint, and prettier, is there an easy way to set a rule banning plain JavaScript files from being committed? It seems like this should be really easy but I can t find any information on it - all searches seem to turn up results on ignoring certain filetypes, rather than banning them.

I m considering skipping using a linter altogether and running something like

# Prevent committing .js and .jsx files
git diff --cached --name-only | grep -E  .jsx?$  > /dev/null
if [[ $? == 0 ]]; then
  echo "Error: JavaScript files (.js/.jsx) are not allowed in this repository."
  exit 1

in a pre-commit hook, or making an entirely new rule in a custom plugin, and then hooking that up to our repository. In either case I feel like others must have solved this problem by now, is there an easier existing solution?

问题回答

You can use a pre-commit hook, which is a script that runs before you commit your changes to the repository. You can use husky to set up a pre-commit hook that checks for any JavaScript files in your staged files and prevents you from committing them. For example, using husky, you can add a script like this to your package.json file:

{
  "husky": {
    "hooks": {
      "pre-commit": "git diff --cached --name-only | grep -E  \.(js|jsx)$  && echo  Error: JavaScript files (.js/.jsx) are not allowed in this repository.  && exit 1 || true"
    }
  }
}

Of course, for this, you need to install husky package as dev dependency.

For example:

npm i husky -D

Instead of a pre-commit hook, I would basically use git s ignore file to hide/ignore all files with the js extension, with the current list of existing (known) files as exceptions. As each file is then converted, the committer can keep trimming down the exception list in your .gitignore file. This way, you will also have a quantifiable/finite goal that you are marching towards and it can provide a sense of completion.

Imo, this is better in the sense that its language agnostic, but my answer is specific to git. Ofc, you can do the same for other source control management softwares like cvs, mercurial, bazaar, svn, etc





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签