简体   繁体   English

禁用或至少检测自动分号插入

[英]Disable, or at least detect, automatic semicolon insertion

I always code in strict mode hoping to be shielded(or at least forcibly told to change my code) from problems with the Javascript language like using deprecated methods or misinterpretable syntax. 我总是以strict mode编写代码,希望屏蔽(或者至少强行告诉他更改我的代码)来解决Javascript语言的问题,例如使用弃用的方法或错误解释的语法。

However I hit this problem today and I was wondering whether there was any way to disable semicolon insertion in the browser or otherwise have similar-to-strict-mode 'compile'-time errors? 但是我今天遇到了这个问题,我想知道是否有任何方法可以在浏览器中禁用分号插入,或者是否有类似于严格模式的'编译'时错误?

JS[H/L]int doesn't happen to be able to pick up where JS interpreters would insert semicolons and flag them for us to mitigate would it? JS [H / L] int似乎没有能够接受JS解释器插入分号并标记它们以便我们减轻它的位置吗?


EDIT 编辑

JShint and JSLint both error if a new line is present before a semicolon is found after a the return keyword. 如果在return关键字后面找到分号之前存在新行,则JShintJSLint都会出错。 However, I don't know about the other caveats regarding automatic insertion and whether they are each detected too. 但是,我不知道有关自动插入的其他警告以及它们是否也被检测到。
Regardless, if an answer actually solves the 'disabling' part, that would be more relevant. 无论如何,如果答案实际上解决了“禁用”部分,那将更具相关性。

The expression after return keyword MUST ALWAYS start on the same line that the keyword is, this is not interpreter related, it's defined by the ECMAScript standard, it's a bad part of the language but if you respect the rules of writing the JS code described by Douglas Crockford then you'll not encounter this again. return关键字之后的表达式必须始终与关键字在同一行上开始,这与解释器无关,它是由ECMAScript标准定义的,它是语言的一个不好的部分但是如果你尊重编写JS代码所描述的规则Douglas Crockford然后你再也不会遇到这个了。

From "JavaScript: The Good Parts" by Douglas Crockford (Appendix A.3 Awful Parts): 来自Douglas Crockford的“JavaScript:The Good Parts”(附录A.3可怕的部分):

JavaScript has a mechanism that tries to correct faulty programs by automatically inserting semicolons. JavaScript有一种机制,试图通过自动插入分号来纠正错误的程序。 Do not depend on this. 不要依赖于此。 It can mask more serious errors. 它可以掩盖更严重的错误。

It sometimes inserts semicolons in places where they are not welcome. 它有时会在不受欢迎的地方插入分号。 Consider the consequences of semicolon insertion on the return statement. 考虑分号插入对return语句的影响。 If a return statement returns a value, that value expression must begin on the same line as the return: 如果return语句返回一个值,那么该值表达式必须与返回的同一行开始:

return
{
    status: true
};

This appears to return an object containing a status member. 这似乎返回包含状态成员的对象。 Unfortunately, semicolon insertion turns it into a statement that returns undefined. 不幸的是,分号插入将其转换为返回undefined的语句。 There is no warning that semicolon insertion caused the misinterpretation of the program. 没有任何警告说分号插入导致了对程序的误解。 The problem can be avoided if the { is placed at the end of the previous line and not at the beginning of the next line: 如果{放在上一行的末尾而不是下一行的开头,则可以避免此问题:

return {
    status: true
};

Also see the code conventions for JavaScript by Douglas Crockford: http://javascript.crockford.com/code.html 另请参阅Douglas Crockford的JavaScript代码约定: http//javascript.crockford.com/code.html

Just thought I'd help anyone who comes across this question. 我以为我会帮助遇到这个问题的人。
I've started using Google's Closure Comiler to minify JS for a project and it gave me a few handy warnings. 我已经开始使用Google的Closure Comiler来缩小JS的项目,它给了我一些方便的警告。

   $> java -jar google_closure_compiler.jar  --language_in=ECMASCRIPT5_STRICT --js_output_file 'min.js' 'file1.js' 'file2.js'
   file1.js:152: WARNING - unreachable code
               getSomething(function() { return this.doThingo(''); });
               ^

   file2.js:203: WARNING - Suspicious code. The result of the 'add' operator is not being used.
               ' to ' + (typeof obj);
               ^

   0 error(s), 2 warning(s)

The offending code blocks were as follows (the comments describe what the code was missing that the compiler alerted me to): 有问题的代码块如下(注释描述了编译器提醒我的代码丢失的内容):

file1.js:150 file1.js:150

    return generateSomeObject(bunch, of, arguments)
        //the period before the function call was missing
        getSomething(function() { return this.doThingo(''); });
}

file2.js:201 file2.js:201

if (someCondition)
    //trailing plus was missing from this line
    throw 'Could not set ' + last + ' of ' + (typeof root)
        ' to ' + (typeof obj);
return old;

I don't know whether it will recognise all mistakes that the browser tidies up (so I probably won't mark this as an answer), but it does a lot more than YUI's minifier was doing for me (which was ignoring all of these cases). 我不知道它是否会识别浏览器整理的所有错误(所以我可能不会将此标记为答案),但它确实比YUI的缩小器为我做的更多(这忽略了所有这些例)。
Also, compared to YUI, the compiler is able to take in multiple file inputs and so can give me line numbers per file for errors, and doesn't error on/redeact the debugger keyword). 此外,与YUI相比,编译器能够接收多个文件输入,因此可以为每个文件提供错误的行号,并且不会出错/重新生成debugger关键字)。
Hopefully it'll help you too. 希望它也会对你有所帮助。

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

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