Skip to content

Commit

Permalink
feat: add a codemod for transforming imports
Browse files Browse the repository at this point in the history
  • Loading branch information
nperez0111 committed Jan 29, 2025
1 parent f731cd9 commit d02e1a1
Show file tree
Hide file tree
Showing 23 changed files with 1,197 additions and 2 deletions.
9 changes: 9 additions & 0 deletions codemods/tiptap-2-migrate-imports/.codemodrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "https://codemod-utils.s3.us-west-1.amazonaws.com/configuration_schema.json",
"name": "tiptap-2-migrate-imports",
"version": "1.0.0",
"engine": "jscodeshift",
"private": false,
"arguments": [],
"meta": {}
}
4 changes: 4 additions & 0 deletions codemods/tiptap-2-migrate-imports/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
cdmd_dist
pnpm-lock.yaml
package-lock.json
7 changes: 7 additions & 0 deletions codemods/tiptap-2-migrate-imports/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"tabWidth": 2,
"semi": true,
"singleQuote": false,
"arrowParens": "avoid",
"printWidth": 120
}
120 changes: 120 additions & 0 deletions codemods/tiptap-2-migrate-imports/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@

This is a [codemod](https://codemod.com) created with [```codemod init```](https://docs.codemod.com/deploying-codemods/cli#codemod-init).

## Using this codemod

You can run this codemod with the following command:

```bash
npx codemod tiptap-2-migrate-imports
```

### Before

```ts
import Table from '@tiptap/extension-table';
import TableRow from '@tiptap/extension-table-row';
import TableCell from '@tiptap/extension-table-cell';
import TableHeader from '@tiptap/extension-table-header';
```

### After

```ts
import {
Table,
TableRow,
TableCell,
TableHeader,
} from '@tiptap/extension-table';
```

,

### Before

```ts
import { Table } from '@tiptap/extension-table';
import { TableRow } from '@tiptap/extension-table-row';
import { TableCell } from '@tiptap/extension-table-cell';
import { TableHeader } from '@tiptap/extension-table-header';
```

### After

```ts
import {
Table,
TableRow,
TableCell,
TableHeader,
} from '@tiptap/extension-table';
```

,

### Before

```ts
import Table1 from '@tiptap/extension-table';
import TableR from '@tiptap/extension-table-row';
import TableCel from '@tiptap/extension-table-cell';
import TableHead from '@tiptap/extension-table-header';
```

### After

```ts
import {
Table as Table1,
TableRow as TableR,
TableCell as TableCel,
TableHeader as TableHead,
} from '@tiptap/extension-table';
```

,

### Before

```ts
import BulletList from '@tiptap/extension-bullet-list';
import OrderedList from '@tiptap/extension-ordered-list';
import ListItem from '@tiptap/extension-list-item';
```

### After

```ts
import { BulletList, OrderedList, ListItem } from '@tiptap/extension-list';
```

,

### Before

```ts
import TextStyle from '@tiptap/extension-text-style';
import { Color } from '@tiptap/extension-color';
```

### After

```ts
import { TextStyle, Color } from '@tiptap/extension-text-style';
```

,

### Before

```ts
import { PlaceHolder } from '@tiptap/extension-placeholder';
import History from '@tiptap/extension-history';
```

### After

```ts
import { PlaceHolder, History } from '@tiptap/extensions';
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Table from "@tiptap/extension-table";
import TableCell from "@tiptap/extension-table-cell";
import TableHeader from "@tiptap/extension-table-header";
import TableRow from "@tiptap/extension-table-row";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { Table, TableCell, TableHeader, TableRow } from "@tiptap/extension-table";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Table } from "@tiptap/extension-table";
import { TableCell } from "@tiptap/extension-table-cell";
import { TableHeader } from "@tiptap/extension-table-header";
import { TableRow } from "@tiptap/extension-table-row";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { Table, TableCell, TableHeader, TableRow } from "@tiptap/extension-table";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Table1 from "@tiptap/extension-table";
import TableCel from "@tiptap/extension-table-cell";
import TableHead from "@tiptap/extension-table-header";
import TableR from "@tiptap/extension-table-row";
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {
Table as Table1,
TableCell as TableCel,
TableHeader as TableHead,
TableRow as TableR,
} from "@tiptap/extension-table";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import BulletList from "@tiptap/extension-bullet-list";
import ListItem from "@tiptap/extension-list-item";
import OrderedList from "@tiptap/extension-ordered-list";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { BulletList, ListItem, OrderedList } from "@tiptap/extension-list";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { Color } from "@tiptap/extension-color";
import TextStyle from "@tiptap/extension-text-style";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { Color, TextStyle } from "@tiptap/extension-text-style";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import History from "@tiptap/extension-history";
import { Placeholder } from "@tiptap/extension-placeholder";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { History, Placeholder } from "@tiptap/extensions";
22 changes: 22 additions & 0 deletions codemods/tiptap-2-migrate-imports/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "tiptap-2-migrate-imports",
"devDependencies": {
"@types/node": "20.9.0",
"typescript": "^5.2.2",
"vitest": "^1.0.1",
"@codemod.com/codemod-utils": "*",
"jscodeshift": "^0.15.1",
"@types/jscodeshift": "^0.11.10"
},
"scripts": {
"test": "vitest run",
"test:watch": "vitest watch"
},
"files": [
"README.md",
".codemodrc.json",
"/dist/index.cjs"
],
"type": "module",
"author": "nperez0111"
}
100 changes: 100 additions & 0 deletions codemods/tiptap-2-migrate-imports/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* eslint-disable semi */
export default function transform(file: any, api: any) {
const j = api.jscodeshift as typeof import("jscodeshift");
const root = j(file.source);
let dirtyFlag = false;

// Define the mapping of old module paths to the new consolidated module path
const moduleMapping = {
// extension-table
"@tiptap/extension-table": "Table",
"@tiptap/extension-table-row": "TableRow",
"@tiptap/extension-table-cell": "TableCell",
"@tiptap/extension-table-header": "TableHeader",
// extension-list
"@tiptap/extension-list-item": "ListItem",
"@tiptap/extension-bullet-list": "BulletList",
"@tiptap/extension-ordered-list": "OrderedList",
"@tiptap/extension-task-item": "TaskItem",
"@tiptap/extension-task-list": "TaskList",
"@tiptap/extension-list-keymap": "ListKeymap",
// extension-text-style
"@tiptap/extension-text-style": "TextStyle",
"@tiptap/extension-color": "Color",
"@tiptap/extension-font-family": "FontFamily",
// extensions
"@tiptap/extension-character-count": "CharacterCount",
"@tiptap/extension-dropcursor": "Dropcursor",
"@tiptap/extension-gapcursor": "Gapcursor",
"@tiptap/extension-focus": "Focus",
"@tiptap/extension-history": "History",
"@tiptap/extension-placeholder": "Placeholder",
};

const importMapping = {
// extension-table
"@tiptap/extension-table": "@tiptap/extension-table",
"@tiptap/extension-table-row": "@tiptap/extension-table",
"@tiptap/extension-table-cell": "@tiptap/extension-table",
"@tiptap/extension-table-header": "@tiptap/extension-table",
// extension-list
"@tiptap/extension-list-item": "@tiptap/extension-list",
"@tiptap/extension-bullet-list": "@tiptap/extension-list",
"@tiptap/extension-ordered-list": "@tiptap/extension-list",
"@tiptap/extension-task-item": "@tiptap/extension-list",
"@tiptap/extension-task-list": "@tiptap/extension-list",
"@tiptap/extension-list-keymap": "@tiptap/extension-list",
// extension-text-style
"@tiptap/extension-text-style": "@tiptap/extension-text-style",
"@tiptap/extension-color": "@tiptap/extension-text-style",
"@tiptap/extension-font-family": "@tiptap/extension-text-style",
// extensions
"@tiptap/extension-character-count": "@tiptap/extensions",
"@tiptap/extension-dropcursor": "@tiptap/extensions",
"@tiptap/extension-gapcursor": "@tiptap/extensions",
"@tiptap/extension-focus": "@tiptap/extensions",
"@tiptap/extension-history": "@tiptap/extensions",
"@tiptap/extension-placeholder": "@tiptap/extensions",
};

// Collect all import specifiers that need to be consolidated
const importSpecifiers = {} as Record<(typeof importMapping)[keyof typeof importMapping], Array<any> | undefined>;

// Find all relevant import declarations
root.find(j.ImportDeclaration).forEach(path => {
const sourceValue = path.node.source.value as keyof typeof moduleMapping;
if (moduleMapping[sourceValue]) {
path.node.specifiers?.forEach(specifier => {
if (j.ImportDefaultSpecifier.check(specifier) || j.ImportSpecifier.check(specifier)) {
const importedName = j.ImportDefaultSpecifier.check(specifier)
? moduleMapping[sourceValue]
: specifier.imported.name;
const localName = specifier.local?.name;
if (!localName) {
return;
}
if (!importSpecifiers[importMapping[sourceValue]]) {
importSpecifiers[importMapping[sourceValue]] = [];
}
importSpecifiers[importMapping[sourceValue]]?.push(
j.importSpecifier(j.identifier(importedName), localName !== importedName ? j.identifier(localName) : null),
);
}
});
j(path).remove();
dirtyFlag = true;
}
});

// If there are import specifiers to consolidate, create a new import declaration
Object.entries(importSpecifiers).forEach(([destinationModule, specifiers]) => {
if (Array.isArray(specifiers) && specifiers.length > 0) {
const newImportDeclaration = j.importDeclaration(specifiers, j.literal(destinationModule));
root.get().node.program.body.unshift(newImportDeclaration);
}
});

return dirtyFlag ? root.toSource() : undefined;
}

export const parser = "tsx";
Loading

0 comments on commit d02e1a1

Please sign in to comment.