在 ESlint 中,如果有对象位于不同行,则使所有对象道具位于不同行

问题描述 投票:0回答:1

正在努力解决 eslint 的配置问题,基本上我想要实现的是在解构语句中,如果任何属性位于新行上,那么所有属性都应该位于单独的行上,导入和解构对象相同,例如

// is okay
import { sub1, sub2 } from 'package';
const { item1, item2 } from variable;
// is okay
import {
  sub1,
  sub2,
} from 'package';
const {
  item1,
  item2,
} from variable;

// not okay
import {
  sub1, sub2,
} from 'package';
const {
  item1, item2,
} = variable;

现在我已经看到了类似的问题,并且从已接受的答案中,这是我的 eslint 的一部分

'object-property-newline': [
  'error',
  {
    allowAllPropertiesOnSameLine: false,
  }
],

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

基本上,这些配置最多只是让我将花括号放在单独的行上,但道具本身只是排列在同一行中,任何对所有导入、对象定义和解构都有效的帮助都将受到高度赞赏

javascript typescript eslint
1个回答
0
投票

EsLint 中(仍然🤦)没有针对此类情况的内置规则。 您可以通过一些额外的插件+内置规则来实现您想要的。

这是我的配置(我可能忘记了一些所需的规则)。 而且在这里,导入和导出中的多行工作效果很差(仍然🤦),因为这还需要单独的插件(仍然🤦),我没有在这里添加。

const eslint = {
  plugins: [
    'newline-destructuring',
    'sort-destructure-keys',
    'sort-keys-fix',
  ],
  rules: {

    /* --- newline / wrapping / spacing --- */

    // Enforce consistent spacing inside braces
    'object-curly-spacing': ['error', 'always'],

    // Enforce consistent spacing inside array brackets
    'array-bracket-spacing': ['error', 'always'],

    // Enforce consistent spacing inside computed property brackets
    'computed-property-spacing': ['error', 'always'],

    // Trailing commas - it's better to use it if you want to use sorting
    'comma-dangle': ['error', {
      'arrays': 'always-multiline',
      'objects': 'always-multiline',
      'imports': 'always-multiline',
      'exports': 'always-multiline',
      'functions': 'always-multiline',
    }],

    // Enforce linebreaks after opening and before closing array brackets
    'array-bracket-newline': [ 'error', { minItems: 3 } ],

    // Enforce line breaks after each array element
    'array-element-newline': [ 'error', { minItems: 3 } ],

    // Enforce consistent line breaks inside function parentheses
    'function-paren-newline': [ 'error', { minItems: 4 } ],

    // Eslint plugin (cuz still no built-in 🤦) for enforcing newlines in object destructuring assignment past a certain number of properties.
    'newline-destructuring/newline': [ 'error', {
      items: 2,
      itemsWithRest: 2,
      maxLength: 20,
    } ],

    // Enforce consistent line breaks after opening and before closing braces
    'object-curly-newline': [ 'error', {
      ObjectExpression: {
        multiline: true,
        minProperties: 2,
      },
      ObjectPattern: {
        multiline: true,
        minProperties: 2,
      },
      ImportDeclaration: {
        multiline: true,
        minProperties: 2,
      },
      ExportDeclaration: {
        multiline: true,
        minProperties: 2,
      },
    } ],

    // makes it possible to ensure, as some style guides require, that property specifications appear on separate lines for better readability.
    'object-property-newline': [ 'error', { allowMultiplePropertiesPerLine: false } ],

    // Method and property shorthand syntax for object literals
    'object-shorthand': 2,

    // Enforce consistent brace style for all control statements
    'curly': 'error',

    // Enforce consistent column alignment for objects destructing
    'indent': ['error', 2],

    /* --- sorting --- */

    // Eslint plugin (cuz still no built-in 🤦) - require object destructure key to be sorted
    'sort-destructure-keys/sort-destructure-keys': [ 2, { caseSensitive: true, } ],

    // There is no point in using it 🤦
    'sort-keys': 'off',

    // Eslint plugin (cuz still no built-in 🤦) - Fork of eslint rule that sorts keys in objects (https://eslint.org/docs/rules/sort-keys) with autofix enabled
    'sort-keys-fix/sort-keys-fix': 'error',

  }
}
© www.soinside.com 2019 - 2024. All rights reserved.