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 2a267d6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
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 2a267d6

Please sign in to comment.