Skip to content

Commit

Permalink
docs(README.md): add example
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 committed Apr 22, 2020
1 parent d39c9bc commit 94ef8de
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,22 @@
Codemods for everyday work with React. All support
Flow, TypeScript, and plain JS.

These codemods are intended to be called from IDE extensions, calling them
Most of these codemods (except for `convertSimpleClassComponentsToFunctions`) are intended to be called from IDE extensions, calling them
from the `jscodeshift` CLI wouldn't be worth the effort.

# Table of Contents

<!-- toc -->

- [`wrapWithJSXElement`](#wrapwithjsxelement)
- [`wrapWithChildFunctionElement`](#wrapwithchildfunctionelement)
- [`addProp`](#addprop)
- [`renderConditionally`](#renderconditionally)
- [`wrapWithTernaryConditional`](#wrapwithternaryconditional)
- [`convertSimpleClassComponentsToFunctions`](#convertsimpleclasscomponentstofunctions)

<!-- tocstop -->

# `wrapWithJSXElement`

A codemod that wraps selected JSX elements inside a parent JSX element.
Expand Down Expand Up @@ -292,3 +305,39 @@ const Foo = () => (
# `convertSimpleClassComponentsToFunctions`

Converts `React.Component` subclasses with only a `render` method (no lifecycle methods, constructors, or class properties other than `propTypes`, `contextTypes`, `defaultProps`, and no `state` type parameter) into functional components.

## Example

### Before

```ts
import * as React from 'react'
import PropTypes from 'prop-types'
export default class Foo extends React.Component<Props> {
static propTypes = {
title: PropTypes.string.isRequired,
}
render(): React.ReactNode | null {
return <div>{this.props.title}</div>
}
}
```

### Command

```
jscodeshift -t path/to/react-codemorphs/convertSimpleClassComponentsToFunctions.js <file>
```

### After

```ts
import * as React from 'react'
import PropTypes from 'prop-types'
export default function Foo(props: Props): React.ReactNode | null {
return <div>{props.title}</div>
}
Foo.propTypes = {
title: PropTypes.string.isRequired,
}
```
8 changes: 8 additions & 0 deletions test/convertSimpleClassComponentsToFunctions/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ export const options = {}

export const input = `
import * as React from 'react'
import PropTypes from 'prop-types'
export default class Foo extends React.Component<Props> {
static propTypes = {
title: PropTypes.string.isRequired,
}
render(): React.ReactNode | null {
return <div>{this.props.title}</div>
}
Expand All @@ -14,7 +18,11 @@ export default class Foo extends React.Component<Props> {

export const expected = `
import * as React from 'react'
import PropTypes from 'prop-types'
export default function Foo(props: Props): React.ReactNode | null {
return <div>{props.title}</div>
}
Foo.propTypes = {
title: PropTypes.string.isRequired,
}
`

0 comments on commit 94ef8de

Please sign in to comment.