Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
XantreDev committed Oct 10, 2023
1 parent d2cc312 commit 8680658
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 deletions.
17 changes: 17 additions & 0 deletions .changeset/giant-papayas-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
"react-fast-hoc": minor
---

Made displayName editable for each transformed component:

```tsx
const _ = transformProps(A, (props) => props, {
displayNameTransform: {
value: (name) => name + "C",
type: "rewrite-dynamic",
},
});
expect(_.displayName).toBe("AC");
_.displayName = "D";
expect(_.displayName).toBe("D");
```
30 changes: 23 additions & 7 deletions packages/react-fast-hoc/src/handlers/hocTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const calculateDisplayNameTransform = (

// Using classes to save memory
export class HocTransformer implements ProxyHandler<Function> {
_displayNameField = Symbol("displayName");
constructor(
private transformer:
| null
Expand All @@ -38,15 +39,30 @@ export class HocTransformer implements ProxyHandler<Function> {
}

get(target: Function, p: string | symbol, receiver: any) {
if (process.env.NODE_ENV === "production") {
if (
process.env.NODE_ENV === "production" ||
p !== "displayName" ||
!this.displayNameTransform
) {
return Reflect.get(target, p, receiver);
}
if (p !== "displayName" || !this.displayNameTransform) {
return Reflect.get(target, p, receiver);
if (!(this._displayNameField in target)) {
// @ts-expect-error
target[this._displayNameField] = calculateDisplayNameTransform(
getComponentName(target),
this.displayNameTransform
);
}
return calculateDisplayNameTransform(
getComponentName(target),
this.displayNameTransform
);

// @ts-expect-error
return target[this._displayNameField];
}
set(target: Function, p: string | symbol, value: any) {
if (process.env.NODE_ENV !== "production" && p === "displayName") {
// @ts-expect-error
target[this._displayNameField] = value;
return true;
}
return Reflect.set(target, p, value);
}
}
7 changes: 0 additions & 7 deletions packages/react-fast-hoc/src/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ const REACT_MEMO_TYPE = Symbol.for("react.memo");
const REACT_FORWARD_REF_TYPE = Symbol.for("react.forward_ref");
const REACT_LAZY_TYPE = Symbol.for("react.lazy");

// const enum LazyStatus {
// Uninitialized = -1,
// Pending = 0,
// Resolved = 1,
// Rejected = 2,
// }

type RealComponentType<TProps extends object, IRef = unknown> =
| {
$$typeof: typeof REACT_FORWARD_REF_TYPE;
Expand Down
22 changes: 22 additions & 0 deletions packages/react-fast-hoc/src/rename.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,26 @@ describe("renaming works", () => {
}).displayName
).toBe("AC");
});

test("that component is imperatively renameable", () => {
const _ = transformProps(A, (props) => props, {
displayNameTransform: {
value: (name) => name + "C",
type: "rewrite-dynamic",
},
});
expect(_.displayName).toBe("AC");
_.displayName = "D";
expect(_.displayName).toBe("D");
const __ = transformProps(_, (props) => props, {
displayNameTransform: {
type: "rewrite",
value: "E",
},
});
expect(__.displayName).toBe("E");
__.displayName = "bebe";
expect(__.displayName).toBe("bebe");
expect(_.displayName).toBe("D");
});
});

0 comments on commit 8680658

Please sign in to comment.