-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a codemod for transforming imports
- Loading branch information
1 parent
f731cd9
commit d02e1a1
Showing
23 changed files
with
1,197 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
cdmd_dist | ||
pnpm-lock.yaml | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"tabWidth": 2, | ||
"semi": true, | ||
"singleQuote": false, | ||
"arrowParens": "avoid", | ||
"printWidth": 120 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
``` |
4 changes: 4 additions & 0 deletions
4
codemods/tiptap-2-migrate-imports/__testfixtures__/fixture1.input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
1 change: 1 addition & 0 deletions
1
codemods/tiptap-2-migrate-imports/__testfixtures__/fixture1.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import { Table, TableCell, TableHeader, TableRow } from "@tiptap/extension-table"; |
4 changes: 4 additions & 0 deletions
4
codemods/tiptap-2-migrate-imports/__testfixtures__/fixture2.input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
1 change: 1 addition & 0 deletions
1
codemods/tiptap-2-migrate-imports/__testfixtures__/fixture2.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import { Table, TableCell, TableHeader, TableRow } from "@tiptap/extension-table"; |
4 changes: 4 additions & 0 deletions
4
codemods/tiptap-2-migrate-imports/__testfixtures__/fixture3.input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
6 changes: 6 additions & 0 deletions
6
codemods/tiptap-2-migrate-imports/__testfixtures__/fixture3.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
3 changes: 3 additions & 0 deletions
3
codemods/tiptap-2-migrate-imports/__testfixtures__/fixture4.input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
1 change: 1 addition & 0 deletions
1
codemods/tiptap-2-migrate-imports/__testfixtures__/fixture4.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import { BulletList, ListItem, OrderedList } from "@tiptap/extension-list"; |
2 changes: 2 additions & 0 deletions
2
codemods/tiptap-2-migrate-imports/__testfixtures__/fixture5.input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
1 change: 1 addition & 0 deletions
1
codemods/tiptap-2-migrate-imports/__testfixtures__/fixture5.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import { Color, TextStyle } from "@tiptap/extension-text-style"; |
2 changes: 2 additions & 0 deletions
2
codemods/tiptap-2-migrate-imports/__testfixtures__/fixture6.input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
1 change: 1 addition & 0 deletions
1
codemods/tiptap-2-migrate-imports/__testfixtures__/fixture6.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import { History, Placeholder } from "@tiptap/extensions"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
Oops, something went wrong.