版本

object-curly-newline

强制在开始和结束大括号之后一致地换行

🔧 可修复

此规则报告的一些问题可以通过 --fix 命令行 选项自动修复。

此规则在 ESLint v8.53.0 中已**弃用**。请在 @stylistic/eslint-plugin-js 中使用相应的规则

许多风格指南要求或禁止在对象括号和其他标记内换行。

规则详情

此规则要求或禁止在{与其后的标记之间以及}与其前面的对象字面量或解构赋值标记之间换行。

选项

此规则具有字符串选项

  • "always" 要求在开始和结束大括号之后换行
  • "never" 禁止在开始和结束大括号之后换行

或对象选项

  • "multiline": true 如果属性内部或属性之间存在换行符,则要求换行。否则,它禁止换行。
  • "minProperties" 如果属性的数量至少为给定的整数,则要求换行。默认情况下,如果对象包含换行符并且属性少于给定的整数,则也会报告错误。但是,如果将consistent选项设置为true,则第二个行为将被禁用。
  • "consistent": true(默认)要求要么两个花括号都直接包含换行符,要么都不包含。请注意,启用此选项还会更改minProperties选项的行为。(有关更多信息,请参见上面的minProperties

您可以为对象字面量、解构赋值以及命名导入和导出指定不同的选项。

{
    "object-curly-newline": ["error", {
        "ObjectExpression": "always",
        "ObjectPattern": { "multiline": true },
        "ImportDeclaration": "never",
        "ExportDeclaration": { "multiline": true, "minProperties": 3 }
    }]
}
  • "ObjectExpression" 用于对象字面量的配置
  • "ObjectPattern" 用于解构赋值的对象模式的配置
  • "ImportDeclaration" 用于命名导入的配置
  • "ExportDeclaration" 用于命名导出的配置

always

使用"always"选项时,此规则的错误代码示例

在代码沙盒中打开
/*eslint object-curly-newline: ["error", "always"]*/

let a = {};
let b = {foo: 1};
let c = {foo: 1, bar: 2};
let d = {foo: 1,
    bar: 2};
let e = {foo() {
    dosomething();
}};

let {} = obj;
let {f} = obj;
let {g, h} = obj;
let {i,
    j} = obj;
let {k = function() {
    dosomething();
}} = obj;

使用"always"选项时,此规则的正确代码示例

在代码沙盒中打开
/*eslint object-curly-newline: ["error", "always"]*/

let a = {
};
let b = {
    foo: 1
};
let c = {
    foo: 1, bar: 2
};
let d = {
    foo: 1,
    bar: 2
};
let e = {
    foo: function() {
        dosomething();
    }
};

let {
} = obj;
let {
    f
} = obj;
let {
    g, h
} = obj;
let {
    i,
    j
} = obj;
let {
    k = function() {
        dosomething();
    }
} = obj;

never

使用"never"选项时,此规则的错误代码示例

在代码沙盒中打开
/*eslint object-curly-newline: ["error", "never"]*/

let a = {
};
let b = {
    foo: 1
};
let c = {
    foo: 1, bar: 2
};
let d = {
    foo: 1,
    bar: 2
};
let e = {
    foo: function() {
        dosomething();
    }
};

let {
} = obj;
let {
    f
} = obj;
let {
    g, h
} = obj;
let {
    i,
    j
} = obj;
let {
    k = function() {
        dosomething();
    }
} = obj;

使用"never"选项时,此规则的正确代码示例

在代码沙盒中打开
/*eslint object-curly-newline: ["error", "never"]*/

let a = {};
let b = {foo: 1};
let c = {foo: 1, bar: 2};
let d = {foo: 1,
    bar: 2};
let e = {foo: function() {
    dosomething();
}};

let {} = obj;
let {f} = obj;
let {g, h} = obj;
let {i,
    j} = obj;
let {k = function() {
    dosomething();
}} = obj;

multiline

使用{ "multiline": true }选项时,此规则的错误代码示例

在代码沙盒中打开
/*eslint object-curly-newline: ["error", { "multiline": true }]*/

let a = {
};
let b = {
    foo: 1
};
let c = {
    foo: 1, bar: 2
};
let d = {foo: 1,
    bar: 2};
let e = {foo: function() {
    dosomething();
}};

let {
} = obj;
let {
    f
} = obj;
let {
    g, h
} = obj;
let {i,
    j} = obj;
let {k = function() {
    dosomething();
}} = obj;

使用{ "multiline": true }选项时,此规则的正确代码示例

在代码沙盒中打开
/*eslint object-curly-newline: ["error", { "multiline": true }]*/

let a = {};
let b = {foo: 1};
let c = {foo: 1, bar: 2};
let d = {
    foo: 1,
    bar: 2
};
let e = {
    foo: function() {
        dosomething();
    }
};

let {} = obj;
let {f} = obj;
let {g, h} = obj;
let {
    i,
    j
} = obj;
let {
    k = function() {
        dosomething();
    }
} = obj;

minProperties

使用{ "minProperties": 2 }选项时,此规则的错误代码示例

在代码沙盒中打开
/*eslint object-curly-newline: ["error", { "minProperties": 2 }]*/

let a = {
};
let b = {
    foo: 1
};
let c = {foo: 1, bar: 2};
let d = {foo: 1,
    bar: 2};
let e = {
    foo: function() {
        dosomething();
    }
};

let {
} = obj;
let {
    f
} = obj;
let {g, h} = obj;
let {i,
    j} = obj;
let {
    k = function() {
        dosomething();
    }
} = obj;

使用{ "minProperties": 2 }选项时,此规则的正确代码示例

在代码沙盒中打开
/*eslint object-curly-newline: ["error", { "minProperties": 2 }]*/

let a = {};
let b = {foo: 1};
let c = {
    foo: 1, bar: 2
};
let d = {
    foo: 1,
    bar: 2
};
let e = {foo: function() {
    dosomething();
}};

let {} = obj;
let {f} = obj;
let {
    g, h
} = obj;
let {
    i,
    j
} = obj;
let {k = function() {
    dosomething();
}} = obj;

consistent

使用默认的{ "consistent": true }选项时,此规则的错误代码示例

在代码沙盒中打开
/*eslint object-curly-newline: ["error", { "consistent": true }]*/

let a = {foo: 1
};
let b = {
    foo: 1};
let c = {foo: 1, bar: 2
};
let d = {
    foo: 1, bar: 2};
let e = {foo: function() {
    dosomething();
    }
};
let f = {
    foo: function() {
    dosomething();}};

let {g
} = obj;
let {
    h} = obj;
let {i, j
} = obj;
let {k, l
} = obj;
let {
    m, n} = obj;
let {
    o, p} = obj;
let {q = function() {
    dosomething();
    }
} = obj;
let {
    r = function() {
        dosomething();
    }} = obj;

使用默认的{ "consistent": true }选项时,此规则的正确代码示例

在代码沙盒中打开
/*eslint object-curly-newline: ["error", { "consistent": true }]*/

let empty1 = {};
let empty2 = {
};
let a = {foo: 1};
let b = {
    foo: 1
};
let c = {
    foo: 1, bar: 2
};
let d = {
    foo: 1,
    bar: 2
};
let e = {foo: function() {dosomething();}};
let f = {
    foo: function() {
        dosomething();
    }
};

let {} = obj;
let {
} = obj;
let {g} = obj;
let {
    h
} = obj;
let {i, j} = obj;
let {
    k, l
} = obj;
let {m,
    n} = obj;
let {
    o,
    p
} = obj;
let {q = function() {dosomething();}} = obj;
let {
    r = function() {
        dosomething();
    }
} = obj;

ObjectExpression 和 ObjectPattern

使用{ "ObjectExpression": "always", "ObjectPattern": "never" }选项时,此规则的错误代码示例

在代码沙盒中打开
/*eslint object-curly-newline: ["error", { "ObjectExpression": "always", "ObjectPattern": "never" }]*/

let a = {};
let b = {foo: 1};
let c = {foo: 1, bar: 2};
let d = {foo: 1,
    bar: 2};
let e = {foo: function() {
    dosomething();
}};

let {
} = obj;
let {
    f
} = obj;
let {
    g, h
} = obj;
let {
    i,
    j
} = obj;
let {
    k = function() {
        dosomething();
    }
} = obj;

使用{ "ObjectExpression": "always", "ObjectPattern": "never" }选项时,此规则的正确代码示例

在代码沙盒中打开
/*eslint object-curly-newline: ["error", { "ObjectExpression": "always", "ObjectPattern": "never" }]*/

let a = {
};
let b = {
    foo: 1
};
let c = {
    foo: 1, bar: 2
};
let d = {
    foo: 1,
    bar: 2
};
let e = {
    foo: function() {
        dosomething();
    }
};

let {} = obj;
let {f} = obj;
let {g, h} = obj;
let {i,
    j} = obj;
let {k = function() {
    dosomething();
}} = obj;

ImportDeclaration 和 ExportDeclaration

使用{ "ImportDeclaration": "always", "ExportDeclaration": "never" }选项时,此规则的错误代码示例

在代码沙盒中打开
/*eslint object-curly-newline: ["error", { "ImportDeclaration": "always", "ExportDeclaration": "never" }]*/

import {foo, bar} from 'foo-bar';
import {foo as f, baz} from 'foo-bar';
import {qux,
    foobar} from 'foo-bar';

export {
   foo,
   bar
};
export {
   foo as f,
   baz
} from 'foo-bar';

使用{ "ImportDeclaration": "always", "ExportDeclaration": "never" }选项时,此规则的正确代码示例

在代码沙盒中打开
/*eslint object-curly-newline: ["error", { "ImportDeclaration": "always", "ExportDeclaration": "never" }]*/

import {
    foo,
    bar
} from 'foo-bar';
import {
    baz, qux
} from 'foo-bar';
import {
    foo as f,
    foobar
} from 'foo-bar';

export { foo, bar } from 'foo-bar';
export { foo as f, baz } from 'foo-bar';

何时不使用它

如果您不想强制在开始和结束大括号之后一致地换行,则可以安全地禁用此规则。

兼容性

版本

此规则在 ESLint v2.12.0 中引入。

资源

更改语言