简体   繁体   English

解释JSHint在'+'错误之前的坏行

[英]Explanation of JSHint's Bad line breaking before '+' error

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 我知道可以使用laxbreak 选项配置此错误,该选项被描述为

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是自以为是的短信),或者当某些口译人员以这种方式打破时,某些口译员会出现问题。

It's a style guide to avoid statements that could be 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. 这个想法是你在一行结束时清楚表达表达式是在那里结束还是在下一行继续。

Jshint wont flag this as a bad line break if you use the + before the line break as opposed to in the new line. 如果你在换行符之前使用+而不是换行符,那么Jshint不会将此标记为坏换行符。 Like so: 像这样:

window.location.href = String1 +
'#' +
Sting2 +
'=' +
String3;

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++ (eg with JSLint plugin), this can be fixed using the following search & replace: 使用Notepad ++时(例如使用JSLint插件),可以使用以下搜索和替换来修复:

  • Find what: (\\r\\n|\\n|\\r)( *)\\+ 找到: (\\r\\n|\\n|\\r)( *)\\+
  • Replace with: +$1$2 (including the first and last space) 替换为: +$1$2 (包括第一个和最后一个空格)
  • Search Mode: Regular expression 搜索模式: 正则表达式

(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)( *)(\\|\\||&&|==|!=|<=|>=) 找到: (\\r\\n|\\n|\\r)( *)(\\|\\||&&|==|!=|<=|>=)
  • Replace with: $3$1 $2 (including the first and last space) 替换为: $3$1 $2 (包括第一个和最后一个空格)

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

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