Program a test to check for libraries in the "package.json" type "@types/..." that are in the wrong place. Is it possible?

Asked

Viewed 99 times

2

I’ve taken several times package.json libraries "@types/..." in "dependencies" instead of "devDependencies".

It is that they were installed without adding the parameter -D during the yarn add. Mistakes happen and it’s completely normal.

Some devs end up installing the dependency incorrectly and it gets annoying to keep requiring them over and over again to take this care.

VS Code here does not display any warning message when it is like this.

Then it would be possible to program a test in VS Code to check for dependencies "@types/..." who are in "dependencies" but they should be "devDependencies" using Typescript?

And if there are libraries that are in the wrong location, display an alert message on the PROBLEMS console of the VS Code type:

A biblioteca "@types/..." está no local incorreto no "package.json"! Ela deveria estar em "devDependencies" em vez de "dependencies".
A biblioteca "@types/..." está no local incorreto no "package.json"! Ela deveria estar em "devDependencies" em vez de "dependencies".

As the warning message needs to be in English, maybe it could look like this:

The library "@types / ..." is in the wrong location in "package.json"! It should be in "devDependencies" instead of "dependencies".
The library "@types / ..." is in the wrong location in "package.json"! It should be in "devDependencies" instead of "dependencies".

(I’m taking a chance on English! Forgive me! )

If devs do not take this care there can be multiple libraries on the application deploy without need. The deploy gets even slower because of this!

Is it possible to program this? Or does this already exist?

It makes sense to program this in VS Code in order to help devs take this kind of care?

  • 4

    I honestly think this has nothing to do with the purpose of Eslint. As the name says, "Ecmascript Lint". And that, at least in my view, does not seem to me to be the responsibility of the Eslint, since this is not about language Ecmascript (alias Javascript) itself, but yes from ambience development. What seems most appropriate to me is to create a script postinstall that runs any script to do this check. Another possibility is to delegate this to something like an IC, but then it seems to me a certain exaggeration.

  • @Luizfelipe Really you’re right! Thank you! Eslint is not for checking JSON files. Eslint is a static code analysis tool to identify problematic patterns found in Javascript code. I will edit the question statement by removing the term "Eslint". All right?

2 answers

3


If you have a script test in his package.json, can create another call pretest, which will always be run automatically before of test, or posttest, to be executed afterward of test (also automatically).

So whenever (before or after) a test is run, you can do this check.

In the package.json, something like that:

{
  "scripts": {
    "test": "<seu-script-de-testes>",
    "pretest": "node check-deps.js"
  },
  // ...
}

Note that we use pretest, which will execute the script check-deps.js before of the tests.

And in the check-deps.js, can do something like this:

const { readFileSync } = require('fs');
const { join } = require('path');

const pkgPath = join(process.cwd(), 'package.json');
const { dependencies } = JSON.parse(readFileSync(pkgPath, 'utf8'));

if (typeof dependencies !== 'object') {
  process.exit(0);
}

let warned = false;
for (const depName of Object.keys(dependencies)) {
  if (depName.startsWith('@types/')) {
    if (!warned) {
      warned = true;
    }

    console.error(
      `Dependência "${depName}" deveria estar no campo \`devDependencies\`, e não \`dependencies\`.`
    );
  }
}

process.exit(warned ? 1 : 0);

If you want to do something more connected to Vscode, you can try create an extension.

0

We believe that there is no concrete possibility to do this, because let us think about the following case. Our library reference archive is package.json, right? And in it, it contains various properties like:

author:...
license:...
version:...

So, logically, the right one would be something like, locate the file package.json, access the file, search dependencies if there is any @types, if yes, check if there is devDependencies, if available, launch a Warning telling it to install the typesetting library in development mode. I guess I’d have to do something like streams, that we can access and etc...

  • Vagnerwentz thank you for the reply. I get it. O package.json is a JSON file easy to read and manipulate data with any programming language. The question is this: It is possible to program this test on top of VS Code? It would be like: "How to create my custom tests in VS Code". Perhaps it is even possible to use Typescript itself to create this custom test. I will then search this subject on the websites abroad. (Updated at @Luizfelipe’s suggestion)

  • @Deividsondamasio I understand, I believe yes, it is possible to do, but from my opinion, I believe that this makes the programmer more "lazy". Because in fact, programmer who is using Typescript for example, he knows that libraries @types should be in development mode. But maybe looking deeper, find a better answer. But super interesting your question. I’ll run back and see if I can find something similar.

  • Vagnerwentz understand. But I don’t want to let the "lazy" dev. I just want to display an error message on the console PROBLEMS of VS Code in order to show that there is a fix to be made on package.json. Do you understand? I’m also searching here. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.