Object properties must be on a new line with ESLint

huangapple go评论64阅读模式
英文:

Object properties must be on a new line with ESLint

问题

我想强制执行一个 eslint 规则,以确保每个新属性都位于新行上。谢谢!

英文:

let's say I have the following variable:

const myObject = { property1: 'value1', property2: 'value2', property3: 'value3' };

I want to enforce an eslint rule so that if an object has a minimum of three properties, then each property must go on a new line, such as below:

const myObject = {
  property1: 'value1',
  property2: 'value2',
  property3: 'value3',
};

However, if I use the eslint rule object-curly-newline, it appears that the following is acceptable, and this is what get's automatically formatted when eslint --fix runs:

const myObject = {
  property1: 'value1', property2: 'value2', property3: 'value3',
};

How can I enforce an eslint rule that makes sure that each new property is on a new line? Thanks!

答案1

得分: 3

"object-curly-newline": ["error", { "multiline": true }],
"object-curly-spacing": ["error", "always"]

英文:

Try this:

"object-curly-newline": ["error", { "multiline": true }],
"object-curly-spacing": ["error", "always"]

答案2

得分: 1

要强制 Eslint 规则,以确保对象具有最少数量的属性,每个属性都必须放在新行上,你需要使用 object-curly-newline 规则,以及 multilineminProperties 选项。以下是一个示例配置,其中 ESLint 将强制要求至少有三个属性的对象的每个属性都在新行上:

{
  "rules": {
    "object-curly-newline": ["error", {
      "ObjectExpression": { "multiline": true, "minProperties": 3 }
    }]
  }
}

还可以用于以下情况:

上述规则不仅适用于对象字面量。在文档中还提到了一些其他选项,这些选项还可以用于解构赋值导入和导出属性。以下是一个完整的配置示例,其中 ESLint 将强制执行object-curly-newline规则:

  • 具有至少三个属性的对象字面量
  • 涉及对象的解构赋值
  • 具有至少三个导入属性的导入语句
  • 具有至少三个导出属性的导出语句
{
  "rules": {
    "object-curly-newline": ["error", {
      "ObjectExpression": { "multiline": true, "minProperties": 3 },
      "ObjectPattern": { "multiline": true },
      "ImportDeclaration": { "multiline": true, "minProperties": 3 },
      "ExportDeclaration": { "multiline": true, "minProperties": 3 }
    }]
  }
}

示例
根据上述规则,如果你有如下示例代码:

const obj = { p1: 'v1', p2: 'v2', p3: 'v3' };
const { k1, k2, k3 } = obj;
import { i1, i2, i3 } from './somePath';
export { e1, e2, e3 };

ESLint 应该会为这些情况强制执行上述的object-curly-newline规则,并建议以下修复:

const obj = {
  p1: 'v1',
  p2: 'v2',
  p3: 'v3',
};
const {
  k1,
  k2,
  k3,
} = obj;
import {
  i1,
  i2,
  i3,
} from './somePath';
export {
  e1,
  e2,
  e3,
};

你可以在此处详细了解每个选项的信息。

希望这有所帮助。

英文:

To Enforce an Eslint rule for an object with a minimum number of properties where each property must go on a new line, you need to use the object-curly-newline along with the multiline and minProperties options.

Here is an example configuration in which ESLint will enforce that objects with at least three properties have each property on a new line.

{
  "rules": {
    "object-curly-newline": ["error", {
      "ObjectExpression": { "multiline": true, "minProperties": 3 },
    }]
  }
}

Could be useful too-

The above rule is not limited to object literals only. There are a few more options also mentioned in the documentation where this rule can be enforced for destructuring assignments, and import and export properties as well.

Here is the complete configuration example in which ESLint will enforce the object-curly-newline rule for:

  • Object literals with at least three properties
  • Destructuring assignments that involve objects
  • Import statements with at least three imported properties
  • Export statements with at least three exported properties
{
  "rules": {
    "object-curly-newline": ["error", {
      "ObjectExpression": { "multiline": true, "minProperties": 3 },
      "ObjectPattern": { "multiline": true },
      "ImportDeclaration": { "multiline": true, "minProperties": 3 },
      "ExportDeclaration": { "multiline": true, "minProperties": 3 }
    }]
  }
}

An example

As per the above-written rule, if you have an example code like this-

const obj = { p1: 'v1', p2: 'v2', p3: 'v3' };
const { k1, k2, k3 } = obj;
import { i1, i2, i3 } from './somePath';
export { e1, e2, e3 };

ESLint should enforce the above written object-curly-newline rule for each of these cases and suggest the following fix-

const obj = {
  p1: 'v1',
  p2: 'v2',
  p3: 'v3',
};
const {
  k1,
  k2,
  k3,
} = obj;
import {
  i1,
  i2,
  i3,
} from './somePath';
export {
  e1,
  e2,
  e3,
};

You can read more about each option in detail here.

I hope this helps.

答案3

得分: 0

你正在寻找https://eslint.org/docs/latest/rules/object-curly-newline#multiline与https://eslint.org/docs/latest/rules/object-curly-newline#minproperties混合的配置。

在你的情况下,你需要以下配置:

object-curly-newline: ["error", { "multiline": true, "minProperties": 3 }]
英文:

You are looking for https://eslint.org/docs/latest/rules/object-curly-newline#multiline mixed with https://eslint.org/docs/latest/rules/object-curly-newline#minproperties

In your case you need

object-curly-newline: ["error", { "multiline": true, "minProperties": 3 }]

答案4

得分: 0

You can use object-curly-newline by setting option multiline to true.

For Example in .eslintrc, it would be written as :

{
"rules": {
"object-curly-newline": ["error", {
"multiline": true,
"consistent": true
}]
}
}

Examples:

The above code will throw an error in this type of code line :

const dummyObject = { p1: 'v1', p2: 'v2', p3: 'v3' };

The above code will work correctly when above dummyObject is written as:

const dummyObject = {
p1: 'v1',
p2: 'v2',
p3: 'v3',
};

英文:

You can use object-curly-newline by setting option multiline to true.

For Example in .eslintrc, it would be written as :

{
  "rules": {
    "object-curly-newline": ["error", {
      "multiline": true,
      "consistent": true
    }]
  }
}

Examples:

The above code will throw an error in this type of code line :

const dummyObject = { p1: 'v1', p2: 'v2', p3: 'v3' };

The above code will work correctly when above dummyObject is written as:

const dummyObject = {
  p1: 'v1',
  p2: 'v2',
  p3: 'v3',
};

答案5

得分: 0

我更喜欢在我的项目中使用 prettier 来实现这个目的,而不是使用 eslint 提供的选项。

但话虽如此,这不会直接生效并且可能会与 eslint 冲突。你需要在 .eslintrc.js 文件的规则中添加以下内容:

'object-curly-newline': 0,

请参考本文中的这一部分 - 我认为它将帮助你实现你的目标。

如果你有一个多行对象,想将其合并成一行:

const user = {
  name: "John Doe",
  age: 30,
};

// 你只需要删除 {: 后面的换行:

const user = { name: "John Doe",
  age: 30
};

// 然后运行 Prettier:

const user = { name: "John Doe", age: 30 };

// 如果你想再次换行,可以在 {: 后面加入一个换行:

const user = {
 name: "John Doe", age: 30
};

// 运行 Prettier:

const user = {
  name: "John Doe",
  age: 30,
};

我的理由是,相比于 eslint 的选项,我不需要进行太多的调整。

https://prettier.io/docs/en/rationale.html#multi-line-objects

英文:

I prefer to use prettier for this purpose instead of using the option available by eslint in my projects.

That being said, this will not work out of the box and conflict with eslint. You must add the following to your rules in the .eslintrc.js file:

'object-curly-newline': 0,

Refer this section here in the article - I think it will help you achieve your result.

If you have a multiline object that you’d like to join up into a single line:

const user = {
  name: "John Doe",
  age: 30,
};

// All you need to do is remove the newline after {:

const user = {  name: "John Doe",
  age: 30
};

// then run Prettier:

const user = { name: "John Doe", age: 30 };

// If you’d like to go multiline again, add in a newline after {:

const user = {
 name: "John Doe", age: 30 };

// run Prettier:

const user = {
  name: "John Doe",
  age: 30,
};

The rationale is that I do not need to do much tinkering as that needed in the eslint option.

https://prettier.io/docs/en/rationale.html#multi-line-objects

huangapple
  • 本文由 发表于 2023年3月4日 02:06:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定