Search code examples
eslinttypescript-eslinteslintrceslint-config-airbnb

How to forbid edition of arguments of a function, expect Array.reduce accumulator


I did not find a documentation in the whole eslint website, and I do not know if there is a plugin to forbid arguments editions.

Here is an example of code which should be:

Incorrect:

const incorrectUpdate = (data) => {
  data.field = data.field === 'Hello' ? 'Stack' : 'Overflow';
  return data;
};

Correct:

const validUpdate = (data) => {
  return { ...data, field: data.field === 'Hello' ? 'Stack' : 'Overflow' };
};

Please can you send me the eslint config?

Can you left Array.reduce to continue accumulator edition ?


Solution

  • Activate no-param-reassign.

    For Array.reduce, use every time the same variable name: "acc".

    Here is the config, in .eslintrc:

    "no-param-reassign": [
      "warn",
      {
        "props": true,
        "ignorePropertyModificationsFor": ["acc"]
      }
    ],