Skip to content

Commit

Permalink
Unseal this.refs
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Apr 24, 2024
1 parent 33b6cde commit 4d37e63
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ class ParentComponent extends React.Component {
return (
<div
ref={(current) => {
if (process.env.NODE_ENV !== 'production') {
if (Object.isSealed(this.refs)) {
this.refs = {};
}
}

this.refs["refComponent"] = current;
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,49 @@ class ParentComponent extends React.Component {
render() {
return (
<div ref={current => {
if (process.env.NODE_ENV !== 'production') {
if (Object.isSealed(this.refs)) {
this.refs = {};
}
}

this.refs['P'] = current;
}} id="P">
<div ref={current => {
if (process.env.NODE_ENV !== 'production') {
if (Object.isSealed(this.refs)) {
this.refs = {};
}
}

this.refs['P_P1'] = current;
}} id="P_P1">
<span ref={current => {
if (process.env.NODE_ENV !== 'production') {
if (Object.isSealed(this.refs)) {
this.refs = {};
}
}

this.refs['P_P1_C1'] = current;
}} id="P_P1_C1" />
<span ref={current => {
if (process.env.NODE_ENV !== 'production') {
if (Object.isSealed(this.refs)) {
this.refs = {};
}
}

this.refs['P_P1_C2'] = current;
}} id="P_P1_C2" />
</div>
<div ref={current => {
if (process.env.NODE_ENV !== 'production') {
if (Object.isSealed(this.refs)) {
this.refs = {};
}
}

this.refs['P_OneOff'] = current;
}} id="P_OneOff" />
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import * as React from "react";

<div ref={current => {
if (process.env.NODE_ENV !== 'production') {
if (Object.isSealed(this.refs)) {
this.refs = {};
}
}

this.refs['bad'] = current;
}} />;
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,49 @@ class ParentComponent extends React.Component {
render() {
return (
<div ref={current => {
if (process.env.NODE_ENV !== 'production') {
if (Object.isSealed(this.refs)) {
this.refs = {};
}
}

this.refs['P'] = current;
}} id="P">
<div ref={current => {
if (process.env.NODE_ENV !== 'production') {
if (Object.isSealed(this.refs)) {
this.refs = {};
}
}

this.refs['P_P1'] = current;
}} id="P_P1">
<span ref={current => {
if (process.env.NODE_ENV !== 'production') {
if (Object.isSealed(this.refs)) {
this.refs = {};
}
}

this.refs['P_P1_C1'] = current;
}} id="P_P1_C1" />
<span ref={current => {
if (process.env.NODE_ENV !== 'production') {
if (Object.isSealed(this.refs)) {
this.refs = {};
}
}

this.refs['P_P1_C2'] = current;
}} id="P_P1_C2" />
</div>
<div ref={current => {
if (process.env.NODE_ENV !== 'production') {
if (Object.isSealed(this.refs)) {
this.refs = {};
}
}

this.refs['P_OneOff'] = current;
}} id="P_OneOff" />
</div>
Expand Down
47 changes: 46 additions & 1 deletion transforms/string-refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,56 @@ export default (file, api, options) => {
// Maybe JSCodeShift has such a helper as well?
const currentIdentifierName = "current";
valuePath.replace(
// {(current) => { this.refs[valuePath.node.value] = current }}
// {(current) => {}}
j.jsxExpressionContainer(
j.arrowFunctionExpression(
[j.identifier(currentIdentifierName)],
j.blockStatement([
// if (process.env.NODE_ENV !== 'production')
j.ifStatement(
j.binaryExpression(
"!==",
j.memberExpression(
j.memberExpression(
j.identifier("process"),
j.identifier("env")
),
j.identifier("NODE_ENV")
),
j.stringLiteral("production")
),
j.blockStatement([
// if (Object.isSealed(this.refs))
j.ifStatement(
j.callExpression(
j.memberExpression(
j.identifier("Object"),
j.identifier("isSealed")
),
[
j.memberExpression(
j.thisExpression(),
j.identifier("refs")
),
]
),
j.blockStatement([
// this.refs = {}
j.expressionStatement(
j.assignmentExpression(
"=",
j.memberExpression(
j.thisExpression(),
j.identifier("refs")
),
j.objectExpression([])
)
),
])
),
])
),
// this.refs[valuePath.node.value] = current
j.expressionStatement(
j.assignmentExpression(
"=",
Expand Down

0 comments on commit 4d37e63

Please sign in to comment.