Skip to content

Commit

Permalink
fix: duplicate react props added (#2057)
Browse files Browse the repository at this point in the history
## PR Checklist

- [x] Addresses an existing open issue: fixes #2019
- [x] That issue was marked as [`status: accepting
prs`](https://github.com/JoshuaKGoldberg/TypeStat/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22)
- [x] Steps in
[CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/TypeStat/blob/main/.github/CONTRIBUTING.md)
were taken

## Overview

I think the checks are little bit late but I didn't find better place.
  • Loading branch information
rubiesonthesky authored Jan 3, 2025
1 parent a5f7aea commit dc9f408
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const visitReactComponentNode = (
request: FileMutationsRequest,
): Mutation | undefined => {
// If the node is a class declaration, don't bother with prop types if it already declares a React.Component template
if (ts.isClassDeclaration(node)) {
if (ts.isClassLike(node)) {
const extendsType = getClassExtendsType(node);

if (
Expand All @@ -48,6 +48,12 @@ const visitReactComponentNode = (
) {
return undefined;
}
} else if (
node.parameters.at(0)?.getChildCount() &&
node.parameters[0].getChildCount() > 1
) {
// if function already has type annotation, skip it
return undefined;
}

// Try to find a static `propTypes` member to indicate the interface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ const visitReactComponentNode = (
node: ReactComponentNode,
request: FileMutationsRequest,
) => {
if (
!ts.isClassLike(node) &&
node.parameters.at(0)?.getChildCount() &&
node.parameters[0].getChildCount() > 1
) {
// if function already has type annotation, skip it
return undefined;
}

// Make sure a node doesn't yet exist to declare the node's props type
const propsNode = getComponentPropsNode(request, node);
if (propsNode !== undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,58 @@ interface LaterAssignedComponentProps {
string: PropTypes.string,
stringRequired: PropTypes.string.isRequired,
};

interface GreetingProps {
name?: string;
}


class Greeting extends React.Component<GreetingProps> {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}

Greeting.propTypes = {
name: PropTypes.string,
};

interface HelloWorldComponentProps {
name?: string;
}


function HelloWorldComponent({ name }: HelloWorldComponentProps) {
return <div>Hello, {name}</div>;
}

HelloWorldComponent.propTypes = {
name: PropTypes.string,
};

interface HeadingProps {
text?: string;
}


function Heading({ text }: HeadingProps) {
return <h1>{text}</h1>;
}
Heading.propTypes = {
text: PropTypes.string,
};
Heading.defaultProps = {
text: "Hello, world!",
};

const LegendImage = function (props: any) {
return (
<img
{...props}
style={{ display: "none", marginTop: "4px" }}
onLoad={(e) => (e.currentTarget.style.display = "block")}
onError={(e) => (e.currentTarget.style.display = "none")}
/>
);
};
})();
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,43 @@ import PropTypes from "prop-types";
string: PropTypes.string,
stringRequired: PropTypes.string.isRequired,
};

class Greeting extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}

Greeting.propTypes = {
name: PropTypes.string,
};

function HelloWorldComponent({ name }) {
return <div>Hello, {name}</div>;
}

HelloWorldComponent.propTypes = {
name: PropTypes.string,
};

function Heading({ text }) {
return <h1>{text}</h1>;
}
Heading.propTypes = {
text: PropTypes.string,
};
Heading.defaultProps = {
text: "Hello, world!",
};

const LegendImage = function (props: any) {
return (
<img
{...props}
style={{ display: "none", marginTop: "4px" }}
onLoad={(e) => (e.currentTarget.style.display = "block")}
onError={(e) => (e.currentTarget.style.display = "none")}
/>
);
};
})();

0 comments on commit dc9f408

Please sign in to comment.