Javascript 为什么会引入新的 JSLint 错误“使用空格,而不是制表符”和“不安全字符”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13913189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why were the new JSLint errors "use spaces, not tabs" and "unsafe character" introduced?
提问by Konstantin Dinev
I have been validating my JavaScript using JSLint for about 2 years now and once in a while there are rules that change. In general when JSLint introduces a new rule, there is a checkbox to ignore this rule when parsing, or if you choose to not ignore it then to make your code compliant to it.
我已经使用 JSLint 验证我的 JavaScript 大约 2 年了,有时规则会发生变化。通常,当 JSLint 引入新规则时,解析时会有一个复选框忽略此规则,或者如果您选择不忽略它,则使您的代码符合它。
As I was running my JSLint validation today, however, I run into these two new errors:
然而,当我今天运行 JSLint 验证时,我遇到了这两个新错误:
Use spaces, not tabs.
使用空格,而不是制表符。
This is not the "mixing of tabs and spaces" error. I am using only tabs. This is a recently modified version of "mixing of tabs and spaces" which now disallows tabs in general.
这不是“混合制表符和空格”错误。我只使用标签。这是“混合制表符和空格”的最近修改版本,现在一般不允许使用制表符。
And:
和:
Unsafe character.
*/
Unsafe character.
_const: {
不安全的性格。
*/
不安全的性格。
_const:{
There are no new options to ignore. I cannot understand what is unsafe about closing a block comment, why it considers _const: { as unsafe when I have nomen: true, (dangling _ in identifiers) or why should I be suddenly switching from spaces to tabs, when I still have the configuration about indentation of 4 spaces being a tab.
没有可以忽略的新选项。我无法理解关闭块注释的不安全之处,为什么它认为 _const: { 不安全,当我有nomen: true,(在标识符中悬挂 _)或者为什么我应该突然从空格切换到制表符,当我仍然有关于缩进的配置时4 个空格是制表符。
Does anyone have an idea why those were introduced to at least how to make JSLint ignore these new rules?
有没有人知道为什么至少介绍了如何让 JSLint 忽略这些新规则?
Update:The Messy White Spaceoption works around the issue but it would cause other unexpected behavior:
更新:该Messy White Space选项可以解决该问题,但会导致其他意外行为:
if (condition) {
// ^-- there is a space but it won't indicate an error
回答by James Allardice
Well it looks like Douglas Crockford just made a whole lot more people switch to JSHint. Have a look at this commit.
好吧,看起来 Douglas Crockford 刚刚让更多人转向 JSHint。看看这个提交。
The "Mixed spaces and tabs" error has been removed, and a new "Use spaces, not tabs" error has been added in its place. Aside from that, there's one tiny change in that diff that shows the cause of this. The following line (comment added):
“混合空格和制表符”错误已被删除,并在其位置添加了新的“使用空格,而不是制表符”错误。除此之外,该差异中还有一个微小的变化显示了造成这种情况的原因。以下行(已添加注释):
at = source_row.search(/ \t/);
// ^ Space
has been replaced with this:
已被替换为:
at = source_row.search(/\t/);
// ^ No space!
Following that search there's an ifstatement. If the condition evaluates to true, the "Use spaces, not tabs" warning is issued. Here's that statement:
在搜索之后有一个if声明。如果条件评估为true,则会发出“使用空格,而不是制表符”警告。这是声明:
if (at >= 0) {
warn_at('use_spaces', line, at + 1);
}
I hopethat this is just a little oversight by Crockford. As you can see, JSLint is now going to raise this warning if you use a tab character anywhere. Unfortuately, his commit messages are completely useless, and the documentation doesn't appear to have been updated, so I can't do anything other than speculate as to the reasons behind this change.
我希望这只是 Crockford 的一个小疏忽。如您所见,如果您在任何地方使用制表符,JSLint 现在将引发此警告。不幸的是,他的提交信息完全没有用,而且文档似乎也没有更新,所以除了推测这一变化背后的原因之外,我无能为力。
I suggest you abandon JSLint and switch to JSHintright now.
我建议你放弃的JSLint和开关JSHint现在。
回答by mathius1
You can suppress the error by clicking the "messy white space" option.
您可以通过单击“混乱的空白”选项来抑制错误。
回答by Kat
To answer whyJSLint now gives an error about tabs, http://www.jslint.com/help.htmlgives this justification:
为了回答为什么JSLint 现在给出关于选项卡的错误,http: //www.jslint.com/help.html给出了这个理由:
Tabs and spaces should not be mixed. We should pick just one in order to avoid the problems that come from having both. Personal preference is an extremely unreliable criteria. Neither offers a powerful advantage over the other. Fifty years ago, tab had the advantage of consuming less memory, but Moore's Law has eliminated that advantage. Space has one clear advantage over tab: there is no reliable standard for how many spaces a tab represents, but it is universally accepted that a space occupies a space. So use spaces. You can edit with tabs if you must, but make sure it is spaces again before you commit. Maybe someday we will finally get a universal standard for tabs, but until that day comes, the better choice is spaces.
制表符和空格不应混用。我们应该只选择一个,以避免两者兼而有之。个人喜好是一个极其不可靠的标准。两者都没有提供比另一个强大的优势。五十年前,tab 具有消耗更少内存的优势,但摩尔定律已经消除了这一优势。空格比制表符有一个明显的优势:制表符代表多少个空格没有可靠的标准,但普遍认为一个空格占一个空格。所以使用空格。如果必须,您可以使用制表符进行编辑,但在提交之前再次确保它是空格。也许有一天我们会最终获得制表符的通用标准,但在那一天到来之前,更好的选择是空格。
Essentially he wants everybody to come to a consensus on whether to use tabs or spaces to prevent them from ever being mixed. He's decided that the consistency of the width of a space makes it the superior choice, so we should all use that. Clearly some people will disagree with this line of thinking (myself included), but that is the reason why JSLint throws that error.
从本质上讲,他希望每个人都能就是否使用制表符或空格达成共识,以防止它们混合在一起。他认为空间宽度的一致性使其成为最佳选择,所以我们都应该使用它。显然有些人会不同意这种思路(包括我自己),但这就是 JSLint 抛出该错误的原因。
回答by TyMayn
Depending on your editor/IDE you can adjust how TAB works.
根据您的编辑器/IDE,您可以调整 TAB 的工作方式。
For instance, I use Sublime Text. Near the bottom right corner there is a Tab Size : 4.
例如,我使用 Sublime Text。在右下角附近有一个标签大小:4。
I clicked on it and set it 'Indent Using Spaces'.
我点击它并将其设置为“使用空格缩进”。
This updated all my Tabs to use spaces and JSLint errors disapeared. I try to use as few options as possible with JSLint as I want my code to be well structured.
这更新了我所有的标签以使用空格和 JSLint 错误消失。我尝试在 JSLint 中使用尽可能少的选项,因为我希望我的代码结构良好。
I also use JSFormat, which will tab based on my editors settings, so whenever I'm done I run my JSFormat, then JSLint. No errors = happy boy!
我还使用 JSFormat,它会根据我的编辑器设置进行选项卡,所以每当我完成时,我都会运行我的 JSFormat,然后运行 JSLint。没有错误=快乐的孩子!
Hope it helps.
希望能帮助到你。

