Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(codemod): improve replace-string-ref accuracy #1310

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ class C extends React1.Component {
return (
<div
ref={(ref) => {
this.refs.refName = ref;
if(ref === null) {
delete this.refs.refName;
} else {
this.refs.refName = ref;
}
}}
/>
);
Expand All @@ -17,7 +21,11 @@ class C1 extends PureComponent1 {
return (
<div
ref={(ref) => {
this.refs.refName = ref;
if(ref === null) {
delete this.refs.refName;
} else {
this.refs.refName = ref;
}
}}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ class C extends React.Component {
return (
<div
ref={(ref) => {
this.refs.refName = ref;
if(ref === null) {
delete this.refs.refName;
} else {
this.refs.refName = ref;
}
}}
/>
);
Expand All @@ -17,7 +21,11 @@ class C1 extends React.PureComponent {
return (
<div
ref={(ref) => {
this.refs.refName = ref;
if(ref === null) {
delete this.refs.refName;
} else {
this.refs.refName = ref;
}
}}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ class C extends Component {
return (
<div
ref={(ref) => {
this.refs.refName = ref;
if(ref === null) {
delete this.refs.refName;
} else {
this.refs.refName = ref;
}
}}
/>
);
Expand All @@ -17,7 +21,11 @@ class C1 extends PureComponent {
return (
<div
ref={(ref) => {
this.refs.refName = ref;
if(ref === null) {
delete this.refs.refName;
} else {
this.refs.refName = ref;
}
}}
/>
);
Expand Down
17 changes: 7 additions & 10 deletions packages/codemods/react/19/replace-string-ref/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@ const buildCallbackRef = (j: JSCodeshift, refName: string) =>
j.arrowFunctionExpression(
[j.jsxIdentifier("ref")],
j.blockStatement([
j.expressionStatement(
j.assignmentExpression(
"=",
j.memberExpression(
j.memberExpression(j.thisExpression(), j.identifier("refs")),
j.identifier(refName),
),
j.identifier("ref"),
),
),
j.template.statement`
if (ref === null) {
delete this.refs.${j.identifier(refName)};
} else {
this.refs.${j.identifier(refName)} = ref;
}
`,
]),
),
),
Expand Down
Loading