Skip to content

Commit ce001f4

Browse files
committed
Fix wrong lookup for State interface
1 parent 7ada5fc commit ce001f4

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/analysis/head.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function resolveAlias(path: NodePath<TSType>): NodePath<TSType | TSInterfaceBody
7979
for (const body of path_.get("body")) {
8080
if (body.isTSTypeAliasDeclaration() && body.node.id.name === name) {
8181
return body.get("typeAnnotation");
82-
} else if (body.isTSInterfaceDeclaration()) {
82+
} else if (body.isTSInterfaceDeclaration() && body.node.id.name === name) {
8383
return body.get("body");
8484
}
8585
}

src/index.test.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -629,13 +629,14 @@ describe("react-declassify", () => {
629629
expect(transform(input)).toBe(output);
630630
});
631631

632-
it("transforms state types", () => {
632+
it("transforms state types (type alias)", () => {
633633
const input = dedent`\
634+
type Props = {};
634635
type State = {
635636
foo: number,
636637
bar: number,
637638
};
638-
class C extends React.Component<{}, State> {
639+
class C extends React.Component<Props, State> {
639640
state = {
640641
foo: 1,
641642
bar: 2,
@@ -646,12 +647,46 @@ describe("react-declassify", () => {
646647
}
647648
`;
648649
const output = dedent`\
650+
type Props = {};
649651
type State = {
650652
foo: number,
651653
bar: number,
652654
};
653655
654-
const C: React.FC<{}> = () => {
656+
const C: React.FC<Props> = () => {
657+
const [foo, setFoo] = React.useState<number>(1);
658+
const [bar, setBar] = React.useState<number>(2);
659+
return <button onClick={() => setBar(3)}>{foo}</button>;
660+
};
661+
`;
662+
expect(transform(input, { ts: true })).toBe(output);
663+
});
664+
665+
it("transforms state types (interface)", () => {
666+
const input = dedent`\
667+
interface Props {}
668+
interface State {
669+
foo: number,
670+
bar: number,
671+
}
672+
class C extends React.Component<Props, State> {
673+
state = {
674+
foo: 1,
675+
bar: 2,
676+
};
677+
render() {
678+
return <button onClick={() => this.setState({ bar: 3 })}>{this.state.foo}</button>;
679+
}
680+
}
681+
`;
682+
const output = dedent`\
683+
interface Props {}
684+
interface State {
685+
foo: number,
686+
bar: number,
687+
}
688+
689+
const C: React.FC<Props> = () => {
655690
const [foo, setFoo] = React.useState<number>(1);
656691
const [bar, setBar] = React.useState<number>(2);
657692
return <button onClick={() => setBar(3)}>{foo}</button>;

0 commit comments

Comments
 (0)