Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
XantreDev committed Sep 23, 2023
1 parent 1b40561 commit 3537998
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/rare-fishes-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-fast-hoc": patch
---

Fix `wrapIntoProxy` type issue
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ yarn-error.log*

**/lib
/packages/react-fast-hoc/README.md
**/*.tsbuildinfo
4 changes: 2 additions & 2 deletions packages/react-fast-hoc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"types": "./lib/index.d.ts",
"module": "./lib/esm/index.mjs",
"scripts": {
"test": "vitest run",
"test": "vitest run && tsc --project tsconfig.node.json",
"test:watch": "vitest watch",
"typecheck": "tsc --noEmit --diagnostics",
"typecheck": "tsc --noEmit",
"lint": "pnpm typecheck",
"build:collect-readme": "cpx ../../README.md .",
"build": "pnpm build:collect-readme & rollup -c"
Expand Down
2 changes: 1 addition & 1 deletion packages/react-fast-hoc/src/handlers/rewriteCall.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type React from "react";

type ComponentArgs = [props: object, ref: React.Ref<unknown>];
type ComponentArgs = [props: Record<any, unknown>, ref: React.Ref<unknown>];

export class RewriteCall implements ProxyHandler<Function> {
constructor(
Expand Down
33 changes: 30 additions & 3 deletions packages/react-fast-hoc/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { cleanup, render } from "@testing-library/react";
import { Objects } from "hotscript";
import React, { createElement, forwardRef, memo } from "react";
import { Function } from "ts-toolbelt";
import { afterEach, describe, expect, test, vi } from "vitest";
import { createTransformProps } from ".";
import { afterEach, describe, expect, expectTypeOf, test, vi } from "vitest";
import { createTransformProps, transformProps, wrapIntoProxy } from ".";

const identityProps = <T>(props: T) => props;

Expand Down Expand Up @@ -131,7 +131,7 @@ describe("transformProps", () => {
// test("works with unloaded lazy", async () => {
// const Cmp = vi.fn(Component);
// const Lazy = React.lazy(() => Promise.resolve({ default: Cmp }));

// console.log(Lazy._payload._result.toString());
// console.log(Lazy._init.toString());
// render(
Expand All @@ -150,3 +150,30 @@ describe("transformProps", () => {
// });
// });
});

describe.skip("type tests", () => {
expectTypeOf(() =>
transformProps(
{} as React.ForwardRefExoticComponent<{
ref?: React.Ref<number>;
}>,
identityProps
)
)
.returns.parameter(0)
.toEqualTypeOf<{
ref?: React.Ref<number>;
}>();

expectTypeOf(() =>
wrapIntoProxy({})(
{} as React.ForwardRefExoticComponent<{
ref?: React.Ref<number>;
}>
)
)
.returns.parameter(0)
.toEqualTypeOf<{
ref?: React.Ref<number>;
}>();
});
2 changes: 2 additions & 0 deletions packages/react-fast-hoc/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export * from "./createHoc";
export * from "./createTransformProps";
export * from "./handlers";
export { isRef } from "./internals";
export * from "./transformProps";
export type * from "./type";
export * from "./wrapIntoHoc";

18 changes: 10 additions & 8 deletions packages/react-fast-hoc/src/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import type { ReactNode, Ref } from "react";
import type { HocTransformer, MimicToNewComponentHandler } from "./handlers";
import { isClassComponent, toFunctional, type Get } from "./toFunctional";

export const isRef = <T = unknown>(maybeRef: unknown): maybeRef is Ref<T> =>
maybeRef === null ||
typeof maybeRef === "function" ||
(!!maybeRef && typeof maybeRef === "object" && "current" in maybeRef);

export const wrapPropsTransformer =
<T extends object, R extends object>(transformer: (arg: T) => R) =>
(args: [Omit<T, "ref">, Get<T, "ref">]) => {
Expand All @@ -10,23 +15,20 @@ export const wrapPropsTransformer =
// so wrapping it into props transform hoc has overhead
const props = Object.assign(Object.create(null), _props);

const isRealRef =
ref === null ||
typeof ref === "function" ||
(ref && typeof ref === "object" && "current" in ref);
if (isRealRef) {
const hasRef = isRef(ref);
if (hasRef) {
(props as any).ref = ref;
}

type RealProps = T & { ref: Get<T, "ref"> };

const resultProps = transformer(props as RealProps);
const resultRef = isRealRef && "ref" in resultProps && resultProps["ref"];
if (isRealRef) {
const resultRef = "ref" in resultProps && resultProps["ref"];
if ("ref" in resultProps) {
delete (resultProps as R & { ref?: unknown }).ref;
}

return [resultProps, isRealRef && resultRef ? resultRef : ref] as const;
return [resultProps, hasRef && isRef(resultRef) ? resultRef : ref] as const;
};

const REACT_MEMO_TYPE = Symbol.for("react.memo");
Expand Down
2 changes: 1 addition & 1 deletion packages/react-fast-hoc/src/wrapIntoHoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { PropsBase, WrappedComponent } from "./type";
*/
export const wrapIntoProxy =
(proxy: ProxyHandler<Function>) =>
<T extends React.ComponentType>(Component: T) =>
<T extends React.ComponentType<any>>(Component: T) =>
wrapComponentIntoHoc(
Component,
proxy as HocTransformer,
Expand Down
10 changes: 9 additions & 1 deletion packages/react-fast-hoc/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
{
"extends": "tsconfig/react-library.json",
"include": ["./src"],
"exclude": ["dist", "lib", "build", "node_modules"]
"exclude": [
"**/*.test.*",
"**/*.spec.*",
"__tests__",
"dist",
"lib",
"build",
"node_modules"
]
}
11 changes: 11 additions & 0 deletions packages/react-fast-hoc/tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"module": "ESNext",
"noEmit": true,
"moduleResolution": "Node",
"rootDir": ".",
"allowSyntheticDefaultImports": true
},
"include": ["./src", "./typeTests"],
"extends": "tsconfig/react-library.json"
}

0 comments on commit 3537998

Please sign in to comment.