Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting linting error - error Unexpected empty object pattern no-empty-pattern

I am getting this linting error for the below code- "error Unexpected empty object pattern no-empty-pattern". Does anyone know how to solve this issue? Its a tsx file.

const stateToProps = ({}, { data = [], filters = {}, staticFilters = 
  [{}] }) => {
  const allFilters = staticFilters ? Object.assign({}, filters, ...staticFilters) : filters;
  const newData = getFilteredRows(allFilters, data);
  return {
      data: newData,
      unfilteredData: data,
  };
};
//called like this
export const NodeList = connect<{}, {}, CustomNodeTableProps>(stateToProps)(CustomNodeTable);
like image 278
Neha Gupta Avatar asked Aug 22 '19 12:08

Neha Gupta


2 Answers

I ran this code through the ESLint demo app after transpiling the TypeScript; the linter error is coming from {} in the first line.

const stateToProps = ({}, // rest of the function

no-empty-pattern is meant to catch code that appears to be using destructuring, but does not assign any variables. In the code above, stateToProps accepts a first argument, but no matter what that argument is, it is destructured as an empty object {} and nothing is assigned.

If that first argument matters, you could change the code to descructure it:

const stateToProps = ({importantThing},

If not, indicate that we don't care:

const stateToProps = (_,

Or, set a default.

const stateToProps = (importantThing = {},

Each of these changes resolve the ESLint warning.

like image 121
Jake Worth Avatar answered Oct 20 '22 11:10

Jake Worth


In no-empty-pattern situations like this I tend to disable the lint rule for that particular line by adding the following comment.

JavaScript:

  // eslint-disable-next-line no-empty-pattern
  const stateToProps = ({}, otherParams, // rest of the function
    ... 

TypeScript:

  // eslint-disable-next-line @typescript-eslint/no-empty-pattern
  const stateToProps = ({}, otherParams, // rest of the function
    ... 

The reasoning for this is first, that I DO NOT want turn off the no-empty-pattern globally, and second I also DO NOT want to collect plenty of lint warnings, as this would blur the important warnings and errors.

like image 43
Nils Heumer Avatar answered Oct 20 '22 11:10

Nils Heumer