Search code examples
javascriptfunctionobjectdestructuring

Default parentheses upon object destructuring


I've recently run into a problem that I can't fix.

I'm writing some code where I have to access data with the help of an input range field. I want to write a getData function which looks like this:

function getData() {
  let currentValue = slider.value;
  let index = currentValue - 1;
  return { pageViews, monthlyCost, leftPercentage } = viewsData[index];
}

this works BUT everytime I save my file visual studio code adds a parenthesis around the destructuring assigment like this:

return ({ pageViews, monthlyCost, leftPercentage } = viewsData[index]);

and after that the code doesn't work

can somebody help me with fixing this behavior in my code editor? thanks (:


Solution

  • I believe you have the prettier extension install in your IDE. To prevent this automatic behaviour for that specific line of code, you can use prettier-ignore and exclude the next line from formatting.

    function getData() {
      let currentValue = slider.value;
      let index = currentValue - 1;
    
      // prettier-ignore
      return { pageViews, monthlyCost, leftPercentage } = viewsData[index];
    }