Hoist assignment out of `return` statement

JavaScript pattern

This rule hoists the assignments out of return. Because an assignment, = is easy to confuse with a comparison, ==, The best practice is not to use any assignments in return statements.

This does not apply when assignment is wrapped in parentheses.


Apply with the Grit CLI
grit apply no_return_assign

Hoist = from return statement

BEFORE
function doSomething() {
  var x = getXResult();
  var y = getYResult();
  return (a = x + y);
}
AFTER
function doSomething() {
  var x = getXResult();
  var y = getYResult();
  a = x + y;
  return a;
}

Hoist += from return statement

BEFORE
function doSomething() {
  return (a += 5);
}
AFTER
function doSomething() {
  a += 5;
  return a;
}

Hoist -= from return statement

BEFORE
function doSomething(y) {
  return (y -= 5);
}
AFTER
function doSomething(y) {
  y -= 5;
  return y;
}

Hoist *= from return statement

BEFORE
function doSomething() {
  return (a *= 5);
}
AFTER
function doSomething() {
  a *= 5;
  return a;
}

Hoist /= from return statement

BEFORE
function doSomething() {
  return (x /= 5);
}
AFTER
function doSomething() {
  x /= 5;
  return x;
}

Hoist %= from return statement

BEFORE
function doSomething() {
  return (b %= 2);
}
AFTER
function doSomething() {
  b %= 2;
  return b;
}

Hoist **= from return statement

BEFORE
function doSomething(x, y) {
  return (x **= y);
}
AFTER
function doSomething(x, y) {
  x **= y;
  return x;
}

Hoist &= from return statement

BEFORE
function doSomething() {
  return (x &= a);
}
AFTER
function doSomething() {
  x &= a;
  return x;
}

Hoist |= from return statement

BEFORE
function doSomething() {
  return (x |= b);
}
AFTER
function doSomething() {
  x |= b;
  return x;
}

Hoist ^= from return statement

BEFORE
function doSomething() {
  return (x ^= z);
}
AFTER
function doSomething() {
  x ^= z;
  return x;
}

Do not hoist == from return statement

JAVASCRIPT
function doSomething() {
  return a == b * 0.5;
}

Do not hoist === from return statement

JAVASCRIPT
function doSomething() {
  return a === b + 5;
}