# @monteway/codeshift

> Helpers for writing codemods

Latest version **0.4.2** (published 2023-04-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install @monteway/codeshift
pnpm add @monteway/codeshift
yarn add @monteway/codeshift
bun add @monteway/codeshift
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.2 |
| Published | 2023-04-27 |
| First published | 2022-12-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | ^18.12.1 \|\| ^16.16 |
| Dependencies | 2 |
| Unpacked size | 8.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Monterail |
| Maintainers | jedrzejginter |
| Keywords | monterail, monteway |

## Links

- npm: https://www.npmjs.com/package/@monteway/codeshift
- Repository: https://github.com/monterail/monteway-js
- Homepage: https://github.com/monterail/monteway-js#readme
- Issues: https://github.com/monterail/monteway-js/issues
- npm.io page: https://npm.io/package/@monteway/codeshift

## Dependencies (2)

- [tslib](https://npm.io/package/tslib.md) ^2.5.0
- [jscodeshift](https://npm.io/package/jscodeshift.md) ^0.14.0

## Recent versions

- 0.4.2 (latest) — 2023-04-27
- 0.4.1-rc.1 (next) — 2023-04-27
- 0.4.1 — 2023-04-27
- 0.4.0 — 2023-03-31
- 0.4.0-rc.2 — 2023-03-31
- 0.4.0-rc.1 — 2023-03-30
- 0.3.2 — 2023-03-16
- 0.3.2-rc.1 — 2023-03-16
- 0.3.1 — 2022-12-12
- 0.3.0 — 2022-12-02
- 0.3.0-rc.1 — 2022-12-02

## README

# @monteway/codeshift

Internal [jscodeshift](https://github.com/facebook/jscodeshift) helpers used in [@monteway/app](../app/readme.md) for easier codemod implementation.

## Install

```bash
npm i -D @monteway/codeshift
```

## Usage

### `appendImports`

Adds new import inside imports group. You can specify where the import should go, by selecting an existing import inside source file and set if the new one should go before of after it.

1. Let's have `index.tsx` file with following content:

   ```js
   import ReactDOM from 'react-dom';

   // some more content that is irrelevant
   ```

2. Now write a codemod file `codeshift.mjs` that will be run using jscodeshift. \
   In this example we want to add a React default import before `react-dom` import.

   ```js
   import { appendImports } from '@monteway/codeshift';

   export default function codemod(file, api, options) {
     const jscodeshift = api.jscodeshift;

     let source = jscodeshift(file.source);

     source = appendImports(
       jscodeshift,
       source,
       'BEFORE_FIRST',
       /^react-dom$/,
       `import React from 'react';`,
     );

     return source.toSource();
   }
   ```

3. The output of running `codeshift.mjs` for `index.tsx` content would be:

   ```diff
   + import React from 'react';
   import ReactDOM from 'react-dom';

   // some more content that is irrelevant
   ```

### `wrapDefaultExport`

Finds a default export and wraps it with a call experssion.

1. Let's have a `index.tsx` file with the following content:

   ```ts
   import React from 'react';

   function Component() {
     return <>Hello, world!</>;
   }

   export default Component;
   ```

2. Now write a codemod file `codeshift.mjs` that will be run using jscodeshift. \
   In this example we want to wrap the `Component` with `memo`.

   ```js
   import { wrapDefaultExport } from '@monteway/codeshift';

   export default function codemod(file, api, options) {
     const jscodeshift = api.jscodeshift;

     let source = jscodeshift(file.source);

     source = wrapDefaultExport(jscodeshift, source, 'memo');

     return source.toSource();
   }
   ```

3. The output of running `codeshift.mjs` for `index.tsx` content would be:

   ```diff
   import React from 'react';

   function Component() {
     return <>Hello, world!</>;
   }

   - export default Component;
   + export default memo(Component);
   ```

   > Notice that for this code to run we would need to add import for `memo`. We could solve it by using
   > `React.memo` instead of `memo`, since `React` is already imported, but right now,
   > the `wrapDefaultExport` does not accept anything other than identifier node,
   > so we could not use `React.memo`, since this a member experssion, not an identifier.

---
_Source: https://npm.io/package/@monteway/codeshift · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
