Skip to content
Merged
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
2 changes: 1 addition & 1 deletion codemods/create-element-to-jsx/scripts/error-tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ test("throws on unsupported props literal", () => {
const result = runCodemod("var React = require('react/addons');\nReact.createElement('foo', 1);\n");
const combined = `${result.stdout}\n${result.stderr}`;
assert.match(combined, /Unexpected attribute of type .*Literal/);
assert.match(combined, /Failed to execute codemod|InitializationFailed/);
assert.match(combined, /Failed to execute codemod|InitializationFailed|Workflow failed after|codemod::runtime::javascript/);
});
2 changes: 2 additions & 0 deletions codemods/replace-string-ref/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Replace string refs in React class components with callback refs that assign through `this.refs`.

On unmount, React invokes callback refs with `null`. The generated callback deletes the `this.refs` property in that case (instead of assigning `null`) so behavior stays closer to string refs for checks like `typeof this.refs.refName`, `'refName' in this.refs`, and `Object.keys(this.refs)`.

## Usage

```bash
Expand Down
6 changes: 5 additions & 1 deletion codemods/replace-string-ref/scripts/codemod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ function callbackRefText(refName: string): string {
? `this.refs.${refName}`
: `this.refs[${JSON.stringify(refName)}]`;
return `ref={(ref) => {
${assignmentTarget} = ref;
if (ref === null) {
delete ${assignmentTarget};
} else {
${assignmentTarget} = ref;
}
}}`;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ import React1, { PureComponent as PureComponent1 } from "react";
class C extends React1.Component {
render() {
return (<div ref={(ref) => {
this.refs.refName = ref;
if (ref === null) {
delete this.refs.refName;
} else {
this.refs.refName = ref;
}
}} />);
}
}

class C1 extends PureComponent1 {
render() {
return (<div ref={(ref) => {
this.refs.refName = ref;
if (ref === null) {
delete this.refs.refName;
} else {
this.refs.refName = ref;
}
}} />);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ import React from "react";
class C extends React.Component {
render() {
return (<div ref={(ref) => {
this.refs.refName = ref;
if (ref === null) {
delete this.refs.refName;
} else {
this.refs.refName = ref;
}
}} />);
}
}

class C1 extends React.PureComponent {
render() {
return (<div ref={(ref) => {
this.refs.refName = ref;
if (ref === null) {
delete this.refs.refName;
} else {
this.refs.refName = ref;
}
}} />);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ import { Component, PureComponent } from "react";
class C extends Component {
render() {
return (<div ref={(ref) => {
this.refs.refName = ref;
if (ref === null) {
delete this.refs.refName;
} else {
this.refs.refName = ref;
}
}} />);
}
}

class C1 extends PureComponent {
render() {
return (<div ref={(ref) => {
this.refs.refName = ref;
if (ref === null) {
delete this.refs.refName;
} else {
this.refs.refName = ref;
}
}} />);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from "react";

class C extends React.Component {
render() {
return (<div ref={(ref) => {
if (ref === null) {
delete this.refs.refName;
} else {
this.refs.refName = ref;
}
}} />);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from "react";

class C extends React.Component {
render() {
return <div ref="refName" />;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"string-ref-replacements": [
{
"cardinality": {
"file": "tests/delete-ref-on-unmount/input.tsx",
"refs": "refName"
},
"count": 1
}
]
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import React from "react";
export default class C extends React.Component { render() { return (<div ref={(ref) => {
this.refs.refName = ref;
if (ref === null) {
delete this.refs.refName;
} else {
this.refs.refName = ref;
}
}} />); } }
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import React from "react";
class C extends React.Component { render() { return <><div ref={(ref) => {
this.refs.a = ref;
if (ref === null) {
delete this.refs.a;
} else {
this.refs.a = ref;
}
}} /><span ref={(ref) => {
this.refs.b = ref;
if (ref === null) {
delete this.refs.b;
} else {
this.refs.b = ref;
}
}} /></>; } }
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import * as React1 from "react";
class C extends React1.Component { render() { return (<div ref={(ref) => {
this.refs.refName = ref;
if (ref === null) {
delete this.refs.refName;
} else {
this.refs.refName = ref;
}
}} />); } }
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import React from "react";
class C extends React.Component { render(){ return (<div ref={(ref) => {
this.refs["foo-bar"] = ref;
if (ref === null) {
delete this.refs["foo-bar"];
} else {
this.refs["foo-bar"] = ref;
}
}} />); } }
Loading