Skip to content

Commit

Permalink
[Fix] prop-types: fix className missing in prop validation false …
Browse files Browse the repository at this point in the history
…negative
  • Loading branch information
akulsr0 authored and ljharb committed May 6, 2024
1 parent cef8123 commit b747450
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

## Unreleased

### Fixed

* [`prop-types`]: fix `className` missing in prop validation false negative ([#3749] @akulsr0)

[#3749]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3749

## [7.34.3] - 2024.06.18

### Fixed
Expand Down
19 changes: 19 additions & 0 deletions lib/util/propTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,17 @@ module.exports = function propTypesInstructions(context, components, utils) {
this.shouldSpecifyOptionalChildrenProps = true;

const rightMostName = getRightMostTypeName(node.typeName);
if (
leftMostName === 'React'
&& (
rightMostName === 'HTMLAttributes'
|| rightMostName === 'HTMLElement'
|| rightMostName === 'HTMLProps'
)
) {
this.shouldSpecifyClassNameProp = true;
}

const importedName = localToImportedMap[rightMostName];
const idx = genericTypeParamIndexWherePropsArePresent[
leftMostName !== rightMostName ? rightMostName : importedName
Expand Down Expand Up @@ -835,6 +846,14 @@ module.exports = function propTypesInstructions(context, components, utils) {
isRequired: false,
};
}
if (this.shouldSpecifyClassNameProp) {
this.declaredPropTypes.className = {
fullName: 'className',
name: 'className',
isRequired: false,
};
}

this.foundDeclaredPropertiesList.forEach((tsInterfaceBody) => {
if (tsInterfaceBody && (tsInterfaceBody.type === 'TSPropertySignature' || tsInterfaceBody.type === 'TSMethodSignature')) {
let accessor = 'name';
Expand Down
23 changes: 23 additions & 0 deletions tests/lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -4195,6 +4195,29 @@ ruleTester.run('prop-types', rule, {
);
`,
features: ['types'],
},
{
code: `
import React from "react"
export function Heading({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) {
return <div className={cn("font-semibold text-lg", className)} {...props} />
}
`,
features: ['types'],
},
{
code: `
import React from 'react';
type TDelIconProps = React.HTMLProps<HTMLDivElement>;
const DelIcon: React.FC<TDelIconProps> = ({className, ...rest}) => (
<div className={classNames('del flex-center', className)} {...rest}>
<i className="iconfont icon-del f12" />
</div>
);
`,
features: ['types'],
}
)),

Expand Down

0 comments on commit b747450

Please sign in to comment.