Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: react/no-unused-prop-types returns no errors when using the spread operator in JSX element #3566

Closed
2 tasks done
david-martyn-ford opened this issue Apr 27, 2023 · 1 comment · Fixed by #3570
Closed
2 tasks done

Comments

@david-martyn-ford
Copy link

david-martyn-ford commented Apr 27, 2023

Is there an existing issue for this?

  • I have searched the existing issues and my issue is unique
  • My issue appears in the command-line and not only in the text editor

Description Overview

When using the spread operator within a JSX component, the rule react/no-unused-prop-types no longer detects any errors.

Demo Code

package.json

{
  "scripts": {
    "lint": "eslint Demo.tsx"
  },
  "private": true,
  "devDependencies": {
    "@types/react": "^18.2.0",
    "@typescript-eslint/parser": "^5.59.1",
    "eslint": "^8.39.0",
    "eslint-plugin-react": "^7.32.2",
    "typescript": "^5.0.4"
  },
  "dependencies": {
    "react": "^18.2.0"
  }
}

.eslintrc.js

module.exports = {
  parser: "@typescript-eslint/parser",
  extends: ["plugin:react/recommended"],
  parserOptions: {
    ecmaVersion: 2018,
    sourceType: "module",
    ecmaFeatures: {
      jsx: true
    }
  },
  settings: {
    react: {
      version: "detect"
    }
  },
  rules: {
    "react/no-unused-prop-types": 2
  }
};

Demo.tsx

import React from "react";

type Props = {
  foo: string;
  bar: string; // <-- unused prop
};

const Demo: React.FC<Props> = ({ foo }) => {
  return <div {...{}}>{foo}</div>;
};

export default Demo;

What is happening?

With spread operator

No error is detected when a prop type is unused within the component a spread operator is used in an element for attributes.

CleanShot 2023-04-28 at 10 48 47@2x

Without spread operator

When removing the spread operator, the unused prop is detected
CleanShot 2023-04-27 at 14 10 33@2x

Steps to reproduce

yarn lint

Expected Behavior

bar should be marked as a linting error
CleanShot 2023-04-27 at 14 08 06@2x

eslint-plugin-react version

v7.32.2

eslint version

v8.39.0

node version

v18.11.0

@ljharb ljharb changed the title [Bug]: react/no-unused-prop-types returns no errors when using the spread operator in JXS element [Bug]: react/no-unused-prop-types returns no errors when using the spread operator in JSX element May 3, 2023
@ljharb
Copy link
Member

ljharb commented May 3, 2023

When you're spreading the props object, the rule intentionally stops warning on things, because if you're doing this (don't) you've basically given up on anyone, human or linter, from being able to figure out what's in that object by reading the code.

This specific case is weird because you're not spreading the props object, so I wouldn't expect this to affect anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants