Skip to content

Commit

Permalink
chore: some more contributor docs and remove passing jscodeshift to t…
Browse files Browse the repository at this point in the history
…ransform hook
  • Loading branch information
thepassle committed Jul 17, 2024
1 parent 53bfeeb commit 7eccc2e
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 15 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ And it will automatically clean up any unneeded dependencies.

## Contributing

If you would like to contribute a codemod for a module in [module replacements](https://github.com/es-tooling/module-replacements), please feel free to create a pull request! If you're interested in contributing a codemod, but don't have much experience with _writing_ codemods, take a look at [codemod.studio](https://codemod.com/studio), which makes it really easy.
If you would like to contribute a codemod for a module in [module replacements](https://github.com/es-tooling/module-replacements), please feel free to create a pull request! Pick any module from [module replacements](https://github.com/es-tooling/module-replacements), collect some before/after examples, and get started.

> If you're interested in contributing a codemod, but don't have much experience with _writing_ codemods, take a look at [codemod.studio](https://codemod.com/studio), which makes it really easy.
To start, you can fork and clone this project. Then execute the following steps:

Expand All @@ -34,3 +36,22 @@ This will scaffold all the needed files to create the codemod, and scaffold some
- `test/fixtures/is-array-buffer/case-1/after.js`: The expected _after_ state of the code after applying your codemod

You can take a look at an existing codemod under the `./codemods/*` folder as a reference implementation; most of them are very small.

### Codemod implementation

A codemod is a function that gets passed an options object, and returns an object. Here's an example:

```js
export default function (options) {
return {
name: "foo-lib",
transform: ({ file }) => {
return file.source.replaceAll("foo", "bar");
},
};
}
```

The codemod's `name` should be equal to the name of the package that you're trying to replace (which should be a package from [module replacements](https://github.com/es-tooling/module-replacements)). So if you're writing a codemod for `is-array-buffer`, the `name` of the codemod should be `is-array-buffer`.

The `transform` function is where you can implement your codemod magic. Feel free to use whatever codemod tool you're comfortable with, [ast-grep](https://github.com/ast-grep/ast-grep), [jscodeshift](https://github.com/facebook/jscodeshift), etc. It gets passed the `file` with a `source` property which is a string containing the contents of the file, which you can use to perform transformations on. Make sure to return the changed file from the `transform` function.
8 changes: 4 additions & 4 deletions biome.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
"organizeImports": {
"enabled": true
},
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
"organizeImports": {
"enabled": true
},
"files": {
"ignore": ["node_modules/*", "test/fixtures/*"]
},
Expand Down
4 changes: 3 additions & 1 deletion codemods/is-array-buffer/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import jscodeshift from 'jscodeshift';

/**
* @typedef {import('../../types.js').Codemod} Codemod
* @typedef {import('../../types.js').CodemodOptions} CodemodOptions
Expand All @@ -10,7 +12,7 @@
export default function (options) {
return {
name: 'is-array-buffer',
transform: ({ file, jscodeshift }) => {
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;
Expand Down
4 changes: 3 additions & 1 deletion codemods/is-boolean-object/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import jscodeshift from 'jscodeshift';

/**
* @typedef {import('../../types.js').Codemod} Codemod
* @typedef {import('../../types.js').CodemodOptions} CodemodOptions
Expand All @@ -10,7 +12,7 @@
export default function (options) {
return {
name: 'is-boolean-object',
transform: ({ file, jscodeshift }) => {
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;
Expand Down
4 changes: 3 additions & 1 deletion codemods/is-whitespace/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import jscodeshift from 'jscodeshift';

/**
* @typedef {import('../../types.js').Codemod} Codemod
* @typedef {import('../../types.js').CodemodOptions} CodemodOptions
Expand All @@ -16,7 +18,7 @@
export default function (options) {
return {
name: 'is-whitespace',
transform: ({ file, jscodeshift }) => {
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);

Expand Down
2 changes: 0 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env node

import fg from 'fast-glob';
import jscodeshift from 'jscodeshift';
import picocolors from 'picocolors';
import { readFile } from 'fs/promises';
import { codemods } from './codemods/index.js';
Expand All @@ -26,7 +25,6 @@ await Promise.all(entries.map(async (entry) => {
source,
filename: entry
},
jscodeshift
});

if (source !== result) {
Expand Down
2 changes: 0 additions & 2 deletions test/codemod.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import jscodeshift from 'jscodeshift';
import { describe, it } from 'node:test';
import fs from 'node:fs';
import assert from 'assert';
Expand Down Expand Up @@ -34,7 +33,6 @@ describe('codemod', () => {
filename,
source: before,
},
jscodeshift,
});
fs.writeFileSync(
`./test/fixtures/${codemod.name}/${fixture}/result.js`,
Expand Down
4 changes: 1 addition & 3 deletions types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type jscodeshift from "jscodeshift";

export interface CodemodOptions {
}

Expand All @@ -14,5 +12,5 @@ export interface Codemod {
* you're trying to replace
*/
name: string;
transform: (options: { file: File, jscodeshift: typeof jscodeshift }) => string | Promise<string>;
transform: (options: { file: File }) => string | Promise<string>;
}

0 comments on commit 7eccc2e

Please sign in to comment.