Is there a way to “compile” or detect typos in Javascript (node)?

北城以北 提交于 2021-01-20 11:54:15

问题


I know this is difficult because of how Javascript works and "everything" may be possible.

But I'm tired that errors frequently occur and are not noticed until I run my code and everything crashes or returns errors because I wrote a typo in a variable or on a function.

My fear is that a crash is the best I can hope for, some errors may not be noticeable immediately and send a "valid" undefined value that causes some subtle errors that may sneak into production.

Is there any tool that helps you see that you are calling a function that is not found or using a variable that was not declared, or something like that?

Something that helps me "compile" and check that the code is ok before I test my functions.


回答1:


You can't compile JavaScript but you have options:

  • ESLint - A linter is a tool that provides you code insights, and sometimes even quick fixes. ESLint is the most common and flexible , and it has integration with the most popular editors. If you want to stay with pure JavaScript, this is the only option.
  • Flow - Static type checker for JavaScript. Flow, unlike TypeScript, extends JavaScript without being a separate language. If you don't modify your JavaScript code, the Flow checker can still provide some insight (if I'm not mistaken), but the standard modus operandi is to decorate your JavaScript with type annotations.
  • TypeScript - TypeScript is a separate language, that proposes to be a super-set of JavaScript. It's harder than Flow to start using on an existing code-base even though it's not impossible. If you follow discussions in here, Reddit and HackerNews, you'll notice TypeScript is much more popular than Flow.
  • Using a good editor. Really, I think people sometimes underestimate the importance of the editor in the quality of the code. - WebStorm is the unquestionably winner. The code insight and specially the refactoring options are unbeatable. VS Code has still a lot to catch up but is the best free option now, IMHO. VS Code has this neat feature that it uses TypeScript behind the hood to type-check your code even if you're not using TypeScript. I think VS Code and its extensions will eventually catch up with WebStorm down the road.

My personal suggestion is: Go with TypeScript, if possible. Otherwise, go with modern JS (ES201*) with ESLint and lots of unit-tests. Not using TypeScript makes unit-tests even more important. I wouldn't use Flow because its development and support may stop when Facebook realize they won't win Microsoft in that regard. For big projects, use WebStorm. For smaller ones, use WebStorm or VS Code.




回答2:


In my NodeJS project, ESLint found typos that neither JSLint nor JSHint did. It also works for browser javascript.

To install in NodeJS, follow the directions, e.g. at a terminal:

npm install eslint --save-dev

You can initialize a preferences file with:

./node_modules/.bin/eslint --init

or put a file .eslintrc.js in your app's root folder with these preferences:

module.exports = {
  "env": {
    "browser": false,
    "commonjs": true,
    "es2021": true,
    "node": true
  },
  "extends": "eslint:recommended",
  "parserOptions": {
    "ecmaVersion": 12
  },
  "rules": {
  }
};

Checking typos is part of this default configuration:

Then it's part of the configuration (see documentation):

Disallow Undeclared Variables (no-undef) The "extends": "eslint:recommended" property in a configuration file enables this rule.

Then run ESLint with:

./node_modules/.bin/eslint *.js

I added it as a step before sending the code to the production server. You can also add it as a git pre-commit hook, or a step before publishing to npm.



来源:https://stackoverflow.com/questions/46291810/is-there-a-way-to-compile-or-detect-typos-in-javascript-node

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