Skip to content

Latest commit

 

History

History
96 lines (69 loc) · 1.92 KB

arrow-body-style.md

File metadata and controls

96 lines (69 loc) · 1.92 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

This rule can enforce the use of braces around arrow function body.

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)
  • "except-object" enforces no braces, except when returning object literals

"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;
};

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(); };
let foo = () => {};
let foo = () => { /* do nothing */ };
let foo = () => {
    // do nothing.
};

"except-object"

When the rule is set to "except-object" the following patterns are considered problems:

/*eslint arrow-body-style: ["error", "except-object"]*/
/*eslit-env es6*/

let foo = () => ({ key: 'bar' });

The following patterns are not considered problems:

/*eslint arrow-body-style: ["error", "except-object"]*/
/*eslit-env es6*/

let foo = () => {
  return { key: 'bar' };
};