jsonc/sort-keys | eslint-plugin-jsonc (original) (raw)

require object keys to be sorted

📖 Rule Details ​

This rule checks property definitions of object and verifies that all properties are sorted alphabetically or specified order.

â„šī¸ To fully sort long files, you may need to run ESLint's --fix option multiple times. The rule sorts keys incrementally, and ESLint limits the number of fixes to 10 per run.

🔧 Options ​

json5

{
    "jsonc/sort-keys": ["error",
        // For example, a definition for package.json
        {
            "pathPattern": "^$", // Hits the root properties
            "order": [
                "name",
                "version",
                "private",
                "publishConfig"
                // ...
            ]
        },
        {
            "pathPattern": "^(?:dev|peer|optional|bundled)?[Dd]ependencies$",
            "order": { "type": "asc" }
        }
        // ...
    ]
}

json5

{
    "jsonc/sort-keys": ["error",
        // For example, a definition for JSON Schema
        {
            "pathPattern": ".*", // Hits the all properties
            "hasProperties": ["type"],
            "order": [
                "type",
                "properties",
                "items",
                "required",
                "minItems",
                "additionalProperties",
                "additionalItems"
                // ...
            ]
        }
        // ...
    ]
}

The option receives multiple objects with the following properties:

You can also define options in the same format as the sort-keys rule.

json5

{
    "jsonc/sort-keys": ["error",
        "asc",
        {
            "caseSensitive": true,
            "natural": false,
            "minKeys": 2,
            "allowLineSeparatedGroups": false
        }
    ]
}

See here for details.

🚀 Version ​

This rule was introduced in eslint-plugin-jsonc v0.1.0

🔍 Implementation ​