Skip to content

Commit

Permalink
[Fix] prop-types: className missing in prop validation false negative
Browse files Browse the repository at this point in the history
  • Loading branch information
akulsr0 authored and ljharb committed Jun 1, 2024
1 parent a944aa5 commit 9ac517f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/util/propTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,8 @@ module.exports = function propTypesInstructions(context, components, utils) {
// So we should construct an optional children prop
this.shouldSpecifyOptionalChildrenProps = true;

this.shouldSpecifyClassNameProp = true;

const rightMostName = getRightMostTypeName(node.typeName);
const importedName = localToImportedMap[rightMostName];
const idx = genericTypeParamIndexWherePropsArePresent[
Expand Down Expand Up @@ -835,6 +837,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 9ac517f

Please sign in to comment.