首页
学习
活动
专区
工具
TVP
发布

operator-linebreak

在命令行上的--fix选项可以自动修复一些被这条规则反映的问题。

当一条语句太长而不适合单行时,通常会在分隔表达式的运算符旁边插入换行符。想到的第一种方式是按照英文标点符号将操作员放在行的末尾。

var fullHeight = borderTop +
                 innerHeight +
                 borderBottom;

一些开发人员发现,将行号放置在行首会使代码更具可读性。

var fullHeight = borderTop
               + innerHeight
               + borderBottom;

规则细节

此规则为运营商实施一致的换行样式。

选项

此规则有一个选项,可以是字符串选项或对象选项。

字符串选项:

  • "after" 需要将换行符置于操作员之后
  • "before" 需要将换行符放在操作员面前
  • "none" 不允许在运营商的任何一方进行换行

对象选项:

  • "overrides" 覆盖用于指定operatorsThe默认配置的全局设置是 "after", { "overrides": { "?": "before", ":": "before" } }的afterExamples 不正确代码此规则与默认"after"选项:
  • /*eslint operator-linebreak: ["error", "after"]*/ foo = 1 + 2; foo = 1 + 2; foo = 5; if (someCondition || otherCondition) { } answer = everything ? 42 : foo;Examples of correct code for this rule with the default "after" option:/*eslint operator-linebreak: ["error", "after"]*/ foo = 1 + 2; foo = 1 + 2; foo = 5; if (someCondition || otherCondition) { } answer = everything ? 42 : foo;beforeExamples of incorrect code for this rule with the "before" option:/*eslint operator-linebreak: ["error", "before"]*/ foo = 1 + 2; foo = 5; if (someCondition || otherCondition) { } answer = everything ? 42 : foo;
  • 之前的这个规则的代码不正确"before"选项:
  • /*eslint operator-linebreak: ["error", "before"]*/ foo = 1 + 2; foo = 1 + 2; foo = 5; if (someCondition || otherCondition) { } answer = everything ? 42 : foo;none
  • 此规则的代码不正确,代码如下"none"选项:
  • /*eslint operator-linebreak: ["error", "none"]*/ foo = 1 + 2; foo = 1 + 2; if (someCondition || otherCondition) { } if (someCondition || otherCondition) { } answer = everything ? 42 : foo; answer = everything ? 42 : foo;
  • 此规则的代码不正确,带有"none"选项的代码如下:
  • /*eslint operator-linebreak: ["error", "none"]*/ foo = 1 + 2; foo = 5; if (someCondition || otherCondition) { } answer = everything ? 42 : foo;overrides
  • 此规则附加正确代码的示例,其中包含以下{ "overrides": { "+=": "before" } }选项:
  • /*eslint operator-linebreak: ["error", "after", { "overrides": { "+=": "before" } }]*/ var thing += 'thing';
  • 这个规则的附加正确代码示例包含以下{ "overrides": { "?": "ignore", ":": "ignore" } }选项:
  • /*eslint operator-linebreak: ["error", "after", { "overrides": { "?": "ignore", ":": "ignore" } }]*/ answer = everything ? 42 : foo; answer = everything ? 42 : foo;
  • When Not To Use It
  • If your project will not be using a common operator line break style, turn this rule off.Related Rules
  • comma-style

版本

该规则在 ESLint 0.19.0中引入。

资源

扫码关注腾讯云开发者

领取腾讯云代金券