Skip to content

Latest commit

 

History

History
129 lines (98 loc) · 3.17 KB

alpha-object-expression.md

File metadata and controls

129 lines (98 loc) · 3.17 KB

enforce alphabetization of object expressions (alpha-object-expression)

🔧 The --fix option on the command line can automatically fix some of the problems reported by this rule.

If a line is more than 10 lines away from where it should be, you'll have to run this rule with --fix more than once because of an eslint limitation.

Rule Details

We want object expressions to always be alphabetized.

Options

This rule has an object option:

  • favorShorthand: true (default) enforces shorthand properties be grouped at the top of an object declaration per the Airbnb JavaScript Style Guide > Objects.
  • ignoreAllCapitalized: false (default) ignores objects that have keys that are all capitalized, such as an enumeration.

favorShorthand

👎 Examples of incorrect code for this rule with the default { "favorShorthand": true } option:

// eslint alpha-object-expression: ["error", { "favorShorthand": true }]
var x = 1;
var obj = {
    a: 2,
    x,
}

👍 Examples of correct code for this rule with the default { "favorShorthand": true } option:

// eslint alpha-object-expression: ["error", { "favorShorthand": true }]
var x = 1;
var obj = {
    x,
    a: 2,
}

👍 Examples of correct code for this rule with the { "favorShorthand": false } option:

// eslint alpha-object-expression: ["error", { "favorShorthand": false }]
var x = 1;
var obj = {
    a: 2,
    x,
}
// eslint alpha-object-expression: ["error", { "favorShorthand": false }]
var x = 1;
var obj = {
    x,
    a: 2,
}

ignoreAllCapitalized

👎 Examples of incorrect code for this rule with the default { "ignoreAllCapitalized": false } option:

// eslint alpha-object-expression: ["error", { "ignoreAllCapitalized": false }]
var myEnum = {
    YES: 'yes',
    NO: 'no'
}

👍 Examples of correct code for this rule with the default { "ignoreAllCapitalized": false } option:

// eslint alpha-object-expression: ["error", { "ignoreAllCapitalized": false }]
var myEnum = {
    NO: 'no'
    YES: 'yes',
}
// eslint alpha-object-expression: ["error", { "ignoreAllCapitalized": false }]
var myEnum = {
    b: true,
    NO: 'no'
    YES: 'yes',
}

👎 Examples of incorrect code for this rule with the { "ignoreAllCapitalized": true } option:

// eslint alpha-object-expression: ["error", { "ignoreAllCapitalized": true }]
var myEnum = {
    cat: true,
    YES: 'yes',
    NO: 'no'
}

👍 Examples of correct code for this rule with the { "ignoreAllCapitalized": true } option:

// eslint alpha-object-expression: ["error", { "ignoreAllCapitalized": true }]
var myEnum = {
    YES: 'yes',
    NO: 'no'
}
// eslint alpha-object-expression: ["error", { "ignoreAllCapitalized": true }]
var myEnum = {
    NO: 'no'
    YES: 'yes',
}

When Not To Use It

If you don't care about the order of lines in your object expressions

Resouces