简体   繁体   English

数组和对象中的eslint的自定义缩进规则

[英]Custom indentation rule for eslint in arrays and objects

I could not find any standard rules for such indentation in arrays and objects. 我在数组和对象中找不到任何这种缩进的标准规则。 I need aliment by identificators ( ignoring brackets ). 我需要识别者的食物(忽略括号)。 For our code style this approach is more convinient. 对于我们的代码风格,这种方法更方便。

    // default indent is 4 spaces
    webix.ui({
        view: "popup",
        id: "group_name_edit",
        position: "center",
        body: {
            padding: 20,
            rows: [
                input,
              { cols: [    //       <-------- sample line
                  {},
                  { view: "button",
                    label: "Ok",
                    width: 150,
                  },
                ] 
              }
            ]
        },
    });

You could combine the indent and object-curly-newline rules: 您可以组合indentobject-curly-newline规则:

  rules: {
    "indent": ["error", 4, { "SwitchCase": 1 }],
    "object-curly-newline": ["error", { "minProperties": 1 }]
  },

I copied your sample and applied auto-fix and this is the result: 我复制了您的示例并应用了自动修复,结果如下:

webix.ui({
    view: "popup",
    id: "group_name_edit",
    position: "center",
    body: {
        padding: 20,
        rows: [
            input,
            {
                cols: [    
                    {},
                    {
                        view: "button",
                        label: "Ok",
                        width: 150,
                    },
                ] 
            }
        ]
    },
});

If you also want to enforce line breaks in arrays (which is not neccessary based on the given sample, as there already are line breaks), you could also add array-bracket-newline: ["error", "always"] to the rules. 如果你还想强制数组中的换行符(根据给定的样本不是必需的,因为已经有换行符),你也可以添加array-bracket-newline: ["error", "always"]到规则。

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

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