Rule: member-ordering

Enforces member ordering.

Rationale

A consistent ordering for class members can make classes easier to read, navigate, and edit.

A common opposite practice to member-ordering is to keep related groups of classes together. Instead of creating classes with multiple separate groups, consider splitting class responsibilities apart across multiple single-responsibility classes.

Notes:
Has Fixer

Config

One argument, which is an object, must be provided. It should contain an order property. The order property should have a value of one of the following strings:

  • fields-first
  • instance-sandwich
  • statics-first

Alternatively, the value for order maybe be an array consisting of the following strings:

  • public-static-field
  • public-static-method
  • protected-static-field
  • protected-static-method
  • private-static-field
  • private-static-method
  • public-instance-field
  • protected-instance-field
  • private-instance-field
  • public-constructor
  • protected-constructor
  • private-constructor
  • public-instance-method
  • protected-instance-method
  • private-instance-method

You can also omit the access modifier to refer to “public-“, “protected-“, and “private-“ all at once; for example, “static-field”.

You can also make your own categories by using an object instead of a string:

{
    "name": "static non-private",
    "kinds": [
        "public-static-field",
        "protected-static-field",
        "public-static-method",
        "protected-static-method"
    ]
}

The ‘alphabetize’ option will enforce that members within the same category should be alphabetically sorted by name.

Config examples
"member-ordering": [true, {"order": "fields-first"}]
"member-ordering": [
  true,
  {
    "order": [
      "public-static-field",
      "public-instance-field",
      "public-constructor",
      "private-static-field",
      "private-instance-field",
      "private-constructor",
      "public-instance-method",
      "protected-instance-method",
      "private-instance-method"
    ]
  }
]
"member-ordering": [
  true,
  {
    "order": [
      {
        "name": "static non-private",
        "kinds": [
          "public-static-field",
          "protected-static-field",
          "public-static-method",
          "protected-static-method"
        ]
      },
      "constructor"
    ]
  }
]
Schema
{
  "type": "object",
  "properties": {
    "order": {
      "oneOf": [
        {
          "type": "string",
          "enum": [
            "fields-first",
            "instance-sandwich",
            "statics-first"
          ]
        },
        {
          "type": "array",
          "items": {
            "type": "string",
            "enum": [
              "public-static-field",
              "public-static-method",
              "protected-static-field",
              "protected-static-method",
              "private-static-field",
              "private-static-method",
              "public-instance-field",
              "protected-instance-field",
              "private-instance-field",
              "public-constructor",
              "protected-constructor",
              "private-constructor",
              "public-instance-method",
              "protected-instance-method",
              "private-instance-method"
            ]
          },
          "maxLength": 13
        }
      ]
    }
  },
  "additionalProperties": false
}