Javascript: Why assignment to property of function parameter is bad style.

Created on 21 Dec 2016  路  14Comments  路  Source: airbnb/javascript

Node.js web service

(req, res, next) => {
  req.user = Guest;
  return next();
};

Often used, can not be avoided.
How should I do?

needs eslint rule changaddition loosefidocument rules

Most helpful comment

The next release of eslint-config-airbnb-base, and likely eslint-config-airbnb, will include this loosening of no-param-reassign.

All 14 comments

Since express does require this, you currently can only disable the rule entirely, on a per-file basis, or on a per-line/per-function basis.

eslint has an option proposed to cover this: https://github.com/eslint/eslint/issues/6505

Once that's enabled, we'll likely enable it by default to cover req, request, and $scope.

Thank you sir.

(the question's answered, but reopening to track the eslint rule change)

@zhangchitc You are not reassigning the parameter, you are just creating a property on the object.

Because of this, you can allow it today with "no-param-reassign": ["error", { "props": false }],.

Yes, but "props": true is something that should be strictly enforced everywhere, except where a framework requires it, like express. The eslint rule change would be for "props": true, but with exceptions for req, etc.

Just a quick update, this has been merged and will be released in the next version of ESLint (which I believe is tomorrow): https://github.com/eslint/eslint/issues/6505#issuecomment-282325903

Another example is ctx in Koa routing:

router.get('/', (ctx) => {
  ctx.status = 200;
});

The next release of eslint-config-airbnb-base, and likely eslint-config-airbnb, will include this loosening of no-param-reassign.

no-param-reassign as "never reassign parameters" is ok.

no-param-reassign as "never mutate parameters" means eslint-config-airbnb enforces immutability paradigm. It makes a profound design choice for us.

@jbruni Airbnb's config makes many profound design choices for you; that's the point. If you don't agree with them, you can easily override rules and/or fork the guide.

Guys does anybody knows how to set up eslit for showing error if I modify value from param array ?
Here is example:

const checkIsValid = (id, array) => {
  for (let i = 0; i < array.length; i++) {
    const item = array[i];

    if (item.param) {
      item.value = null;
      item.payment = null;
    }
  }

  return array;
};

@kholiavko-roman that鈥檚 pushing the limits of static analysis; you may want to try eslint-plugin-immutable, but it鈥檚 not very reliable due to the way the language works.

@kholiavko-roman Or try TypeScript and something like jonaskello/tslint-immutable.

Thanks for reply. I think I will try eslint-plugin-immutable for the first.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kozhevnikov picture kozhevnikov  路  3Comments

brendanvinson picture brendanvinson  路  4Comments

felixsanz picture felixsanz  路  3Comments

golopot picture golopot  路  3Comments

surfaceowl picture surfaceowl  路  3Comments