简体   繁体   English

缺少分号时,是否可以检查带有节点的JavaScript文件并获得错误?

[英]Is it possible to check JavaScript files with node and get errors when a semicolon is missing?

I use the following command before sending JS files to my server: 在将JS文件发送到服务器之前,我使用以下命令:

node --check <filename>.js

That way I can be sure there are no gross errors before forwarding the file to the server (ie a missing ) generates an error). 这样,我可以确定在将文件转发到服务器之前(即失踪)产生错误),不会有重大错误。

What I noticed, though, is that node.js does not give me an error if I do not put a semicolon at the end of a line. 但是,我注意到的是,如果我没有在行尾添加分号,那么node.js不会给我一个错误。 I would like that to happen, but after looking at the command line options, I just cannot see such a feature. 我希望这种情况发生,但是在查看了命令行选项之后,我只是看不到这样的功能。

Is there a way to do that with node.js ? 有没有办法用node.js做到这一点?

What I noticed, though, is that node.js does not give me an error if I do not put a semicolon at the end of a line. 但是,我注意到的是,如果我没有在行尾添加分号,那么node.js不会给我一个错误。

That's because JavaScript has an error-correction mechanism called automatic semicolon insertion (see this question's answers for more about ASI), where the parser will insert semicolons into the token stream for you if they're missing in many cases. 这是因为JavaScript具有一种称为自动分号插入的错误纠正机制(有关ASI的更多信息,请参见此问题的答案),在这种情况下,解析器会在很多情况下将分号插入令牌流中,以供您使用。 So Node itself isn't going to give you this information. 因此,Node本身不会为您提供此信息。

I would recommend using a proper lint tool instead, such as ESLint , JSHint , or any of several others. 我会建议使用合适的工具皮棉代替,如ESLintJSHint ,或任何其他几个人的。

Its actually not an error if you don't put in a semicolon in javascript. 如果您未在JavaScript中输入分号,则实际上不是错误。 I've seen both kinds of developers some of them prefer it and some of them don't. 我已经看到两种开发人员中的一些都喜欢它,而有些则不喜欢。

I agree with the answer above, you can use linters and they run with IDEs pretty smoothly. 我同意上面的答案,您可以使用linters,并且它们可以与IDE一起非常平稳地运行。

You can correct yourself as you go rather than writing lines of code and at the end, correcting all your syntax errors (which obviously doesn't scale and becomes irritating after a while). 您可以在进行过程中纠正自己,而不用编写代码,最后,纠正所有语法错误(显然,这种错误不会扩展,过一会儿就会变得烦人)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM