javascript '+'错误前JSHint的Bad换行解释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15140740/
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
Explanation of JSHint's Bad line breaking before '+' error
提问by James McMahon
Can someone explain to me why JSHint complains about the following,
有人可以向我解释为什么 JSHint 抱怨以下内容,
window.location.href = String1
+ '#'
+ Sting2
+ '='
+ String3;
With the error, Bad line breaking before '+' error
随着错误, Bad line breaking before '+' error
I understand that this error can be configured with the laxbreak
option, which is described as
This option suppresses most of the warnings about possibly unsafe line breakings in your code. It doesn't suppress warnings about comma-first coding style. To suppress those you have to use laxcomma (see below).
此选项会抑制大多数有关代码中可能不安全的换行符的警告。它不会抑制关于逗号优先编码风格的警告。要抑制这些,您必须使用 laxcomma(见下文)。
This explanation is pretty terse and I am curious about why breaking lines this way is considered bad or lax in the first place.
这个解释非常简洁,我很好奇为什么以这种方式打破线首先被认为是不好或松懈的。
Keep in mind I am not trying to start a holy war here, I am just looking for an objective answer about why the JSHint folks think this is bad, whether it is just a style preference they are injecting into their linter (I thought JSLint was the opinionated linter), or if there is something that can go wrong on certain interpreters when line breaking this way.
请记住,我并不是要在这里开始一场圣战,我只是在寻找一个客观的答案,说明为什么 JSHint 的人认为这很糟糕,这是否只是他们注入 linter 的一种风格偏好(我认为 JSLint 是自以为是的 linter),或者如果以这种方式断行时某些解释器可能会出错。
采纳答案by Barney
It's a style guide to avoid statements that couldbe liable to assumptions about automatic semicolon insertion.
它是一种风格指南,可以避免可能会导致自动分号插入假设的语句。
The idea is that you make it clear by the end of a line whether the expression ends there or could be continued on the next line.
这个想法是你在一行的末尾明确表达是在那里结束还是可以在下一行继续。
回答by asulaiman
Jshint wont flag this as a bad line break if you use the + before the line break as opposed to in the new line. Like so:
如果您在换行符之前而不是在新行中使用 +,则 Jshint 不会将此标记为错误的换行符。像这样:
window.location.href = String1 +
'#' +
Sting2 +
'=' +
String3;
回答by Steve Chambers
Not a direct answer to the question but for anyone coming across this from Googling (as I did) who wish to keep the rule but fix the warnings, the following may be useful...
不是对问题的直接回答,但对于希望保留规则但修复警告的谷歌搜索(就像我一样)遇到的任何人来说,以下内容可能有用......
When using Notepad++ (e.g. with JSLint plugin), this can be fixed using the following search & replace:
当使用 Notepad++(例如使用 JSLint 插件)时,可以使用以下搜索和替换来修复:
- Find what:
(\r\n|\n|\r)( *)\+
- Replace with:
?+$1$2?
(including the first and last space) - Search Mode: Regular expression
- 找什么:
(\r\n|\n|\r)( *)\+
- 替换为:(包括第一个和最后一个空格)
?+$1$2?
- 搜索方式:正则表达式
(Only tested on Windows but the regex should also work with Unix or Mac OS line endings.)
(仅在 Windows 上测试过,但正则表达式也应该适用于 Unix 或 Mac OS 行尾。)
To do a similar thing for ||
, &&
, ==
, !=
, <=
or >=
instead of +
, use this:
做类似的事情||
,&&
,==
,!=
,<=
或>=
代替+
,这样做:
- Find what:
(\r\n|\n|\r)( *)(\|\||&&|==|!=|<=|>=)
- Replace with:
?$3$1 $2?
(including the first and last space)
- 找什么:
(\r\n|\n|\r)( *)(\|\||&&|==|!=|<=|>=)
- 替换为:(包括第一个和最后一个空格)
?$3$1 $2?