简体   繁体   English

关于JSHint 2.5.0中的尾随空格我该怎么办?

[英]What should I do about trailing whitespace in JSHint 2.5.0?

According to this documentation trailing whitespace seems to have been removed. 根据这个文档,尾随空格似乎已被删除。 How can I enforce whitespace checking while not using a feature that's been deprecated like "white": true ? 如何在不使用已被弃用的功能(如"white": true强制执行空白检查"white": true

As other answers have stated, this functionality has been removed in JSHint 2.5.0, along with various other legacy features that were basically just leftovers from the original JSLint fork. 正如其他答案所述,JSHint 2.5.0中已删除了此功能,以及其他各种遗留功能,这些功能基本上只是原始JSLint分支的剩余部分。

Whitespace linting is still a valuable tool though. 虽然空白掉毛仍然是一个有价值的工具。 It can be really annoying when diffs are polluted with endless whitespace changes because of some erroneous text editor setting that the committer forgot to switch off. 当差异被无限的空白变化污染时,由于一些错误的文本编辑器设置,提交者忘记关闭,这可能会非常烦人。 The tool best suited to this job now in my opinion is JSCS which provides validation of a wide range of style choices. 在我看来,最适合这项工作的工具是JSCS ,它提供了各种风格选择的验证。

This separation leaves JSHint to analyse the code itself for potential problems, and JSCS to analyse the way the code is presented. 这种分离使JSHint分析代码本身的潜在问题,并使JSCS分析代码的呈现方式。 I use both alongside each other in most projects now. 我现在在大多数项目中都使用它们。 The JSCS rule used to prevent trailing white space is disallowTrailingWhitespace . 用于防止尾随空格的JSCS规则是disallowTrailingWhitespace

As this excerpt: 正如这段摘录:

JSHint will not error if you have these options in your config or your files, it will simply ignore Them. 如果你的配置或文件中有这些选项,JSHint不会出错,它只会忽略它们。

So you It can still use, but if you set the option to true . 所以你仍然可以使用,但如果你将选项设置为true

jshint 2.5.0 jshint 2.5.0

The following options were removed: nomen, onevar, passfail, white, gcl, smarttabs, trailing. 删除了以下选项:nomen,onevar,passfail,white,gcl,smarttabs,trailing。 In addition to that, indent no longer provides warnings about indentation levels. 除此之外,缩进不再提供有关缩进级别的警告。 You can still use it to set your tab-width but it will be used only for character locations in other warnings. 您仍然可以使用它来设置标签宽度,但它仅用于其他警告中的字符位置。 JSHint won't error if you have these options in your config or your files; 如果你的配置或文件中有这些选项,JSHint不会出错; it will simply ignore them. 它会简单地忽略它们。

Jshint 2.5.0: https://github.com/jshint/jshint/releases/tag/2.5.0 Jshint 2.5.0: https//github.com/jshint/jshint/releases/tag/2.5.0

Jshint 2.5.1: https://github.com/jshint/jshint/releases/tag/2.5.1 Jshint 2.5.1: https//github.com/jshint/jshint/releases/tag/2.5.1

Alternative solution: 替代方案:

An alternative solution is to use notepad++ , whenever you finish editing a code you can use the following: 另一种解决方案是使用notepad ++ ,只要完成编辑代码,就可以使用以下代码:

Edit > Blank Operations > Trim Trailing Space

The result is something like this: 结果是这样的:

[SPACE]if (something) {[SPACE] to [SPACE]if (something) { [SPACE]if (something) {[SPACE] to [SPACE]if (something) {

Note: [SPACE] is only one example (replace by whitespace) 注意: [SPACE]只是一个例子(由空格替换)

You can also configure notepad++ to replace tabs with spaces when you type, configure like this: 您还可以配置notepad ++以在键入时用空格替换制表符,如下所示进行配置:

Settings > Preferences > Tab Settings and select "normal" (or other), tabs will be replaced with 4 spaces when you press tab in an edition. Settings > Preferences > Tab Settings并选择“正常”(或其他),当您按下版本中的选项卡时,选项卡将替换为4个空格。

This way you need not worry about this update "jshint" and can use it without fear of blanks. 这样你就不必担心这个更新“jshint”并且可以使用它而不用担心空白。

What to use in production code: 在生产代码中使用什么:

I recommend keeping two versions of your code, the "development version" and the "production version". 我建议保留两个版本的代码,“开发版本”和“生产版本”。

The development version should be the whole original code commented and unsing only by the team collaborating with the project, a good tool to keep the project (in case of open-source) would be the https://github.com 开发版本应该是整个原始代码评论和仅仅由与项目协作的团队取消,一个很好的工具来保持项目(在开源的情况下)将是https://github.com

The production version should be compacted your original code (without comments and unnecessary spaces), a tool for this would be: http://jscompress.com/ 生产版本应该压缩你的原始代码(没有评论和不必要的空格),这个工具将是: http//jscompress.com/

I suggest you use multistr which will catch whitespace where it really hurts. 我建议你使用multistr来捕捉它真正受伤的空格。

multistr multistr
This option suppresses warnings about multi-line strings. 此选项禁止有关多行字符串的警告。 Multi-line strings can be dangerous in JavaScript because all hell breaks loose if you accidentally put a whitespace in between the escape character () and a new line. 多线字符串在JavaScript中可能是危险的,因为如果你不小心在转义字符()和新行之间放置了一个空格,那么所有地狱都会破裂。

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

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