Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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