Skip to content

Latest commit

 

History

History
116 lines (89 loc) · 2.43 KB

arrow-body-style.md

File metadata and controls

116 lines (89 loc) · 2.43 KB

Require braces in arrow function body (arrow-body-style)

Arrow functions can omit braces when there is a single statement in the body. This rule enforces the consistent use of braces in arrow functions.

Rule Details

Arrow functions have two syntactic forms for their function bodies. They may be defined with a block body (denoted by curly braces) () => { ... } or with a single expression () => ..., whose value is implicitly returned.

This rule may be used to standardize your usage and thus avoid syntactic ambiguity.

Options

The rule takes one option, a string, which can be:

  • "always" enforces braces around the function body
  • "as-needed" enforces no braces where they can be omitted (default)
  • "never" enforces no braces around the function body (constrains arrow functions to the role of returning an expression)

"always"

"arrow-body-style": ["error", "always"]

When the rule is set to "always" the following patterns are considered problems:

/*eslint arrow-body-style: ["error", "always"]*/
/*eslint-env es6*/
let foo = () => 0;

The following patterns are not considered problems:

let foo = () => {
    return 0;
};
let foo = (retv, name) => {
    retv[name] = true;
    return retv;
};

"as-needed"

When the rule is set to "as-needed" the following patterns are considered problems:

/*eslint arrow-body-style: ["error", "as-needed"]*/
/*eslint-env es6*/

let foo = () => {
    return 0;
};
let foo = () => {
    return {
       bar: {
            foo: 1,
            bar: 2,
        }
    };
};

The following patterns are not considered problems:

/*eslint arrow-body-style: ["error", "as-needed"]*/
/*eslint-env es6*/

let foo = () => 0;
let foo = (retv, name) => {
    retv[name] = true;
    return retv;
};
let foo = () => ({
    bar: {
        foo: 1,
        bar: 2,
    }
});
let foo = () => { bar(); };
let foo = () => {};
let foo = () => { /* do nothing */ };
let foo = () => {
    // do nothing.
};

"never"

When the rule is set to "never" the following patterns are considered problems:

/*eslint arrow-body-style: ["error", "never"]*/
/*eslint-env es6*/

let foo = () => {
    return 0;
};
let foo = (retv, name) => {
    retv[name] = true;
    return retv;
};

The following patterns are not considered problems:

/*eslint arrow-body-style: ["error", "as-needed"]*/
/*eslint-env es6*/

let foo = () => 0;
let foo = () => ({ foo: 0 });