├── .editorconfig ├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── index.d.ts ├── jest.config.js ├── manualRun.ts ├── package.json ├── src ├── __snapshots__ │ └── index.test.ts.snap ├── index.test.ts └── index.ts ├── tsconfig.json └── yarn.lock /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: http://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | # Unix-style newlines with a newline ending every file 7 | [*] 8 | end_of_line = lf 9 | insert_final_newline = true 10 | indent_style = space 11 | indent_size = 2 12 | charset = utf-8 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (http://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | .idea 61 | .history 62 | cjs 63 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | src/__snapshots__/ 2 | src/index.test.ts 3 | .editorconfig 4 | .gitignore 5 | manualRun.ts 6 | yarn.lock 7 | *.log 8 | /jest.config.js 9 | /index.d.ts 10 | /cjs/ 11 | !/cjs/src/index.js 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Andrii Los 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # import-move-codemod 2 | ![npm](https://img.shields.io/npm/v/import-move-codemod) 3 | 4 | Codemod to move imports from one module to another. Super useful in huge monorepos or huge migrations of 3rd party packages. 5 | 6 | ## Usage 7 | 8 | Inside your project root run 9 | For `npm` 10 | ``` 11 | npm i import-move-codemod --save-dev 12 | ``` 13 | or for `yarn` 14 | ``` 15 | yarn add import-move-codemod --dev 16 | ``` 17 | or for `pnpm` 18 | ``` 19 | pnpm add import-move-codemod --D 20 | ``` 21 | 22 | Before the run you need to create a file with a config, let say `codemod.json` 23 | ```json 24 | { 25 | "module": { 26 | "from": "one", 27 | "to": "two" 28 | }, 29 | "specifiers": ["One"] 30 | } 31 | ``` 32 | 33 | Then for example to run codemod against all the files in `/src` run 34 | ``` 35 | npx @codemod/cli -p import-move-codemod -o import-move-codemod=@codemod.json --printer prettier /src 36 | ``` 37 | 38 | 39 | 40 | 41 | ## What refactorings are available 42 | 43 | ### Move of one or multiple named imports from one module to another 44 | 45 | ``` 46 | { 47 | module: { 48 | from: "one", 49 | to: "two" 50 | }, 51 | specifiers: ["One"] 52 | } 53 | ``` 54 | 55 | ```ts 56 | import { One } from 'one' 57 | 58 | // ↓ ↓ ↓ ↓ ↓ ↓ 59 | 60 | import { One } from 'two'; 61 | ``` 62 | 63 | ``` 64 | { 65 | module: { 66 | from: "one", 67 | to: "two" 68 | }, 69 | specifiers: ["One", "Two"] 70 | } 71 | ``` 72 | 73 | ```ts 74 | import { One, Two } from 'one' 75 | 76 | // ↓ ↓ ↓ ↓ ↓ ↓ 77 | 78 | import { One, Two } from 'two'; 79 | ``` 80 | 81 | ```ts 82 | import { One as OneA, Two as TwoA } from 'one' 83 | 84 | // ↓ ↓ ↓ ↓ ↓ ↓ 85 | 86 | import { One as OneA, Two as TwoA } from 'two'; 87 | 88 | ``` 89 | 90 | ``` 91 | { 92 | module: { 93 | from: "one", 94 | to: "two" 95 | }, 96 | specifiers: ["Two"] 97 | } 98 | ``` 99 | 100 | ```ts 101 | import { One, Two } from 'one' 102 | 103 | // ↓ ↓ ↓ ↓ ↓ ↓ 104 | 105 | import { Two } from 'two'; 106 | import { One } from 'one'; 107 | ``` 108 | 109 | ### Move named imports and default in one go 110 | 111 | ``` 112 | { 113 | module: { 114 | from: "one", 115 | to: "two" 116 | }, 117 | specifiers: ["default", "One", "Two"] 118 | } 119 | ``` 120 | 121 | 122 | ```ts 123 | import ADefault, { One, Two, Three } from 'one' 124 | 125 | // ↓ ↓ ↓ ↓ ↓ ↓ 126 | 127 | import ADefault, { One, Two } from 'two'; 128 | import { Three } from 'one'; 129 | ``` 130 | 131 | ``` 132 | { 133 | module: { 134 | from: "one", 135 | to: "two" 136 | }, 137 | specifiers: ["default", "Two"] 138 | } 139 | ``` 140 | 141 | ```ts 142 | import One, { Two } from 'one' 143 | 144 | // ↓ ↓ ↓ ↓ ↓ ↓ 145 | 146 | import One, { Two } from 'two'; 147 | ``` 148 | 149 | ### Move everything i.e. module rename 150 | 151 | ``` 152 | { 153 | module: { 154 | from: "one", 155 | to: "two" 156 | }, 157 | specifiers: ["*"] 158 | } 159 | ``` 160 | 161 | ```ts 162 | import ADefault, { One, Two, Three } from 'one' 163 | 164 | // ↓ ↓ ↓ ↓ ↓ ↓ 165 | 166 | import ADefault, { One, Two, Three } from 'two'; 167 | 168 | ``` 169 | 170 | ### Move named imports and rename them 171 | 172 | ``` 173 | { 174 | module: { 175 | from: "one", 176 | to: "two" 177 | }, 178 | specifiers: { 179 | One: "OneR", 180 | Two: "TwoR" 181 | } 182 | } 183 | ``` 184 | 185 | ```ts 186 | import ADefault, { One, Two } from 'one' 187 | 188 | // ↓ ↓ ↓ ↓ ↓ ↓ 189 | 190 | import { OneR, TwoR } from 'two'; 191 | import ADefault from 'one'; 192 | ``` 193 | 194 | ```ts 195 | import { One, Two } from 'one' 196 | 197 | // ↓ ↓ ↓ ↓ ↓ ↓ 198 | 199 | import { OneR, TwoR } from 'two'; 200 | ``` 201 | 202 | ### Move default import to named 203 | ``` 204 | { 205 | module: { 206 | from: "one", 207 | to: "two" 208 | }, 209 | specifiers: { default: "One" } 210 | } 211 | ``` 212 | 213 | ```ts 214 | import One, { Two } from 'one' 215 | 216 | // ↓ ↓ ↓ ↓ ↓ ↓ 217 | 218 | import { One } from 'two'; 219 | import { Two } from 'one'; 220 | ``` 221 | 222 | ```ts 223 | import One from 'one' 224 | 225 | // ↓ ↓ ↓ ↓ ↓ ↓ 226 | 227 | import { One } from 'two'; 228 | ``` 229 | 230 | ### Move only default import 231 | 232 | ``` 233 | { 234 | module: { 235 | from: "one", 236 | to: "two" 237 | }, 238 | specifiers: ["default"] 239 | } 240 | ``` 241 | 242 | ```ts 243 | import ADefault, { One, Two, Three } from 'one' 244 | 245 | // ↓ ↓ ↓ ↓ ↓ ↓ 246 | 247 | import ADefault from 'two'; 248 | import { One, Two, Three } from 'one'; 249 | ``` 250 | 251 | ```ts 252 | import One from 'one' 253 | 254 | // ↓ ↓ ↓ ↓ ↓ ↓ 255 | 256 | import One from 'two'; 257 | ``` 258 | 259 | ### Make default import from named 260 | 261 | ``` 262 | { 263 | module: { 264 | from: "one", 265 | to: "two" 266 | }, 267 | specifiers: { 268 | One: "default" 269 | } 270 | } 271 | ``` 272 | 273 | ```ts 274 | import { One } from 'one' 275 | 276 | // ↓ ↓ ↓ ↓ ↓ ↓ 277 | 278 | import One from 'two'; 279 | ``` 280 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | declare module "@babel/plugin-syntax-typescript"; 2 | declare module "babel-plugin-tester"; 3 | declare module "@babel/helper-plugin-utils"; 4 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | preset: "ts-jest", 3 | testEnvironment: "node", 4 | rootDir: "src", 5 | }; 6 | -------------------------------------------------------------------------------- /manualRun.ts: -------------------------------------------------------------------------------- 1 | import { format } from "prettier"; 2 | 3 | import { transform } from "@babel/core"; 4 | import { Config } from "./src"; 5 | 6 | const check = ` 7 | import One, { Two } from 'one' 8 | `; 9 | 10 | const options = { 11 | module: { 12 | from: "one", 13 | to: "two", 14 | }, 15 | specifiers: { default: "Three" }, 16 | } as Config; 17 | 18 | const result = 19 | transform(check, { 20 | plugins: [["./src/index.ts", options]], 21 | })?.code ?? ""; 22 | 23 | console.log( 24 | format(result, { semi: false, singleQuote: true, parser: "typescript" }) 25 | ); 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "import-move-codemod", 3 | "version": "0.0.3", 4 | "description": "This babel plugin should be used as codemod for bulk import refactorings a.k.a module move refactorings", 5 | "repository": "https://github.com/RIP21/import-move-codemod", 6 | "license": "MIT", 7 | "author": "https://github.com/RIP21/", 8 | "main": "cjs/src/index.js", 9 | "keywords": [ 10 | "babel-plugin", 11 | "babel-codemod", 12 | "codemod", 13 | "import", 14 | "rename", 15 | "move", 16 | "typescript", 17 | "javascript", 18 | "refactoring" 19 | ], 20 | "devDependencies": { 21 | "@types/babel__core": "^7.1.9", 22 | "@types/prettier": "^2.0.2", 23 | "babel-plugin-tester": "8.0.1", 24 | "jest": "^26.1.0", 25 | "np": "^6.3.2", 26 | "prettier": "^2.0.5", 27 | "ts-jest": "^26.1.2", 28 | "ts-node": "^8.10.2", 29 | "typescript": "^3.9.6" 30 | }, 31 | "scripts": { 32 | "test": "jest", 33 | "start": "ts-node manualRun.ts", 34 | "tsc": "tsc", 35 | "release": "tsc && np" 36 | }, 37 | "dependencies": { 38 | "@babel/core": "^7.10.5", 39 | "@babel/helper-plugin-utils": "^7.10.4", 40 | "@babel/plugin-syntax-typescript": "^7.10.4" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/__snapshots__/index.test.ts.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`import-rename-codemod Should move default import and named imports: Should move default import and named imports 1`] = ` 4 | 5 | import ADefault, { One, Two, Three } from 'one' 6 | 7 | ↓ ↓ ↓ ↓ ↓ ↓ 8 | 9 | import ADefault, { One, Two } from 'two'; 10 | import { Three } from 'one'; 11 | 12 | 13 | `; 14 | 15 | exports[`import-rename-codemod Should move default to default: Should move default to default 1`] = ` 16 | 17 | import One from 'one' 18 | 19 | ↓ ↓ ↓ ↓ ↓ ↓ 20 | 21 | import One from 'two'; 22 | 23 | 24 | `; 25 | 26 | exports[`import-rename-codemod Should move default to named and move others along: Should move default to named and move others along 1`] = ` 27 | 28 | import One, { Two } from 'one' 29 | 30 | ↓ ↓ ↓ ↓ ↓ ↓ 31 | 32 | import { One, Two } from 'two'; 33 | 34 | 35 | `; 36 | 37 | exports[`import-rename-codemod Should move default to named while keeping others that are left: Should move default to named while keeping others that are left 1`] = ` 38 | 39 | import One, { Two } from 'one' 40 | 41 | ↓ ↓ ↓ ↓ ↓ ↓ 42 | 43 | import { One } from 'two'; 44 | import { Two } from 'one'; 45 | 46 | 47 | `; 48 | 49 | exports[`import-rename-codemod Should move default to named: Should move default to named 1`] = ` 50 | 51 | import One from 'one' 52 | 53 | ↓ ↓ ↓ ↓ ↓ ↓ 54 | 55 | import { One } from 'two'; 56 | 57 | 58 | `; 59 | 60 | exports[`import-rename-codemod Should move only default import: Should move only default import 1`] = ` 61 | 62 | import ADefault, { One, Two, Three } from 'one' 63 | 64 | ↓ ↓ ↓ ↓ ↓ ↓ 65 | 66 | import ADefault from 'two'; 67 | import { One, Two, Three } from 'one'; 68 | 69 | 70 | `; 71 | 72 | exports[`import-rename-codemod Should not create useless default import if 'from' library import is present but has no required named import: Should not create useless default import if 'from' library import is present but has no required named import 1`] = ` 73 | 74 | import SomeImport from 'one' 75 | 76 | ↓ ↓ ↓ ↓ ↓ ↓ 77 | 78 | import SomeImport from 'one'; 79 | 80 | 81 | `; 82 | 83 | exports[`import-rename-codemod Should not produce useless import if no match when default and named specifiers: Should not produce useless import if no match when default and named specifiers 1`] = ` 84 | 85 | import { Three } from 'one' 86 | 87 | ↓ ↓ ↓ ↓ ↓ ↓ 88 | 89 | import { Three } from 'one'; 90 | 91 | 92 | `; 93 | 94 | exports[`import-rename-codemod Should not produce useless import if no match: Should not produce useless import if no match 1`] = ` 95 | 96 | import One, { Two } from 'one' 97 | 98 | ↓ ↓ ↓ ↓ ↓ ↓ 99 | 100 | import One, { Two } from 'one'; 101 | 102 | 103 | `; 104 | 105 | exports[`import-rename-codemod When named to default should change import: When named to default should change import 1`] = ` 106 | 107 | import { One } from 'one' 108 | 109 | ↓ ↓ ↓ ↓ ↓ ↓ 110 | 111 | import One from 'two'; 112 | 113 | 114 | `; 115 | 116 | exports[`import-rename-codemod When named to named and imports should be renamed and default should be preserved: When named to named and imports should be renamed and default should be preserved 1`] = ` 117 | 118 | import ADefault, { One, Two } from 'one' 119 | 120 | ↓ ↓ ↓ ↓ ↓ ↓ 121 | 122 | import { OneR, TwoR } from 'two'; 123 | import ADefault from 'one'; 124 | 125 | 126 | `; 127 | 128 | exports[`import-rename-codemod When named to named and imports should be renamed: When named to named and imports should be renamed 1`] = ` 129 | 130 | import { One, Two } from 'one' 131 | 132 | ↓ ↓ ↓ ↓ ↓ ↓ 133 | 134 | import { OneR, TwoR } from 'two'; 135 | 136 | 137 | `; 138 | 139 | exports[`import-rename-codemod When named to named import and multiple imported and all moved it should keep and change import: When named to named import and multiple imported and all moved it should keep and change import 1`] = ` 140 | 141 | import { One, Two } from 'one' 142 | 143 | ↓ ↓ ↓ ↓ ↓ ↓ 144 | 145 | import { One, Two } from 'two'; 146 | 147 | 148 | `; 149 | 150 | exports[`import-rename-codemod When named to named import and multiple imported and only one identifier moved it should keep one import and create new import: When named to named import and multiple imported and only one identifier moved it should keep one import and create new import 1`] = ` 151 | 152 | import { One, Two } from 'one' 153 | 154 | ↓ ↓ ↓ ↓ ↓ ↓ 155 | 156 | import { Two } from 'two'; 157 | import { One } from 'one'; 158 | 159 | 160 | `; 161 | 162 | exports[`import-rename-codemod When named to named import and only one imported should change current import: When named to named import and only one imported should change current import 1`] = ` 163 | 164 | import { One } from 'one' 165 | 166 | ↓ ↓ ↓ ↓ ↓ ↓ 167 | 168 | import { One } from 'two'; 169 | 170 | 171 | `; 172 | 173 | exports[`import-rename-codemod When named to named with 'as' import and multiple imported and all moved it should keep and change import: When named to named with 'as' import and multiple imported and all moved it should keep and change import 1`] = ` 174 | 175 | import { One as OneA, Two as TwoA } from 'one' 176 | 177 | ↓ ↓ ↓ ↓ ↓ ↓ 178 | 179 | import { One as OneA, Two as TwoA } from 'two'; 180 | 181 | 182 | `; 183 | 184 | exports[`import-rename-codemod When wildcard should move all imports: When wildcard should move all imports 1`] = ` 185 | 186 | import ADefault, { One, Two, Three } from 'one' 187 | 188 | ↓ ↓ ↓ ↓ ↓ ↓ 189 | 190 | import ADefault, { One, Two, Three } from 'two'; 191 | 192 | 193 | `; 194 | -------------------------------------------------------------------------------- /src/index.test.ts: -------------------------------------------------------------------------------- 1 | import pluginTester from "babel-plugin-tester"; 2 | import transform, { Config } from "./index"; 3 | 4 | const { format } = require("prettier"); 5 | 6 | pluginTester({ 7 | pluginName: "import-rename-codemod", 8 | plugin: transform, 9 | snapshot: true, 10 | formatResult: (output: any) => 11 | format(output, { 12 | semi: true, 13 | singleQuote: true, 14 | parser: "babel", 15 | }), 16 | tests: [ 17 | { 18 | title: 19 | "When named to named import and only one imported should change current import", 20 | code: ` 21 | import { One } from 'one' 22 | `, 23 | pluginOptions: { 24 | module: { 25 | from: "one", 26 | to: "two", 27 | }, 28 | specifiers: ["One"], 29 | } as Config, 30 | }, 31 | { 32 | title: 33 | "When named to named import and multiple imported and only one identifier moved it should keep one import and create new import", 34 | code: ` 35 | import { One, Two } from 'one' 36 | `, 37 | pluginOptions: { 38 | module: { 39 | from: "one", 40 | to: "two", 41 | }, 42 | specifiers: ["Two"], 43 | } as Config, 44 | }, 45 | { 46 | title: 47 | "When named to named import and multiple imported and all moved it should keep and change import", 48 | code: ` 49 | import { One, Two } from 'one' 50 | `, 51 | pluginOptions: { 52 | module: { 53 | from: "one", 54 | to: "two", 55 | }, 56 | specifiers: ["One", "Two"], 57 | } as Config, 58 | }, 59 | { 60 | title: 61 | "When named to named with 'as' import and multiple imported and all moved it should keep and change import", 62 | code: ` 63 | import { One as OneA, Two as TwoA } from 'one' 64 | `, 65 | pluginOptions: { 66 | module: { 67 | from: "one", 68 | to: "two", 69 | }, 70 | specifiers: ["One", "Two"], 71 | } as Config, 72 | }, 73 | { 74 | title: "When named to named and imports should be renamed", 75 | code: ` 76 | import { One, Two } from 'one' 77 | `, 78 | pluginOptions: { 79 | module: { 80 | from: "one", 81 | to: "two", 82 | }, 83 | specifiers: { 84 | One: "OneR", 85 | Two: "TwoR", 86 | }, 87 | } as Config, 88 | }, 89 | { 90 | title: 91 | "When named to named and imports should be renamed and default should be preserved", 92 | code: ` 93 | import ADefault, { One, Two } from 'one' 94 | `, 95 | pluginOptions: { 96 | module: { 97 | from: "one", 98 | to: "two", 99 | }, 100 | specifiers: { 101 | One: "OneR", 102 | Two: "TwoR", 103 | }, 104 | } as Config, 105 | }, 106 | { 107 | title: "When named to default should change import", 108 | code: ` 109 | import { One } from 'one' 110 | `, 111 | pluginOptions: { 112 | module: { 113 | from: "one", 114 | to: "two", 115 | }, 116 | specifiers: { 117 | One: "default", 118 | }, 119 | } as Config, 120 | }, 121 | { 122 | title: "When wildcard should move all imports", 123 | code: ` 124 | import ADefault, { One, Two, Three } from 'one' 125 | `, 126 | pluginOptions: { 127 | module: { 128 | from: "one", 129 | to: "two", 130 | }, 131 | specifiers: ["*"], 132 | } as Config, 133 | }, 134 | { 135 | title: "Should move only default import", 136 | code: ` 137 | import ADefault, { One, Two, Three } from 'one' 138 | `, 139 | pluginOptions: { 140 | module: { 141 | from: "one", 142 | to: "two", 143 | }, 144 | specifiers: ["default"], 145 | } as Config, 146 | }, 147 | { 148 | title: "Should move default import and named imports", 149 | code: ` 150 | import ADefault, { One, Two, Three } from 'one' 151 | `, 152 | pluginOptions: { 153 | module: { 154 | from: "one", 155 | to: "two", 156 | }, 157 | specifiers: ["default", "One", "Two"], 158 | } as Config, 159 | }, 160 | 161 | { 162 | title: 163 | "Should not produce useless import if no match when default and named specifiers", 164 | code: ` 165 | import { Three } from 'one' 166 | `, 167 | pluginOptions: { 168 | module: { 169 | from: "one", 170 | to: "two", 171 | }, 172 | specifiers: ["default", "One", "Two"], 173 | } as Config, 174 | }, 175 | { 176 | title: "Should move default to named", 177 | code: ` 178 | import One from 'one' 179 | `, 180 | pluginOptions: { 181 | module: { 182 | from: "one", 183 | to: "two", 184 | }, 185 | specifiers: { default: "One" }, 186 | } as Config, 187 | }, 188 | { 189 | title: "Should move default to named while keeping others that are left", 190 | code: ` 191 | import One, { Two } from 'one' 192 | `, 193 | pluginOptions: { 194 | module: { 195 | from: "one", 196 | to: "two", 197 | }, 198 | specifiers: { default: "One" }, 199 | } as Config, 200 | }, 201 | { 202 | title: "Should not produce useless import if no match", 203 | code: ` 204 | import One, { Two } from 'one' 205 | `, 206 | pluginOptions: { 207 | module: { 208 | from: "one", 209 | to: "two", 210 | }, 211 | specifiers: { default: "Three" }, 212 | } as Config, 213 | }, 214 | { 215 | title: 216 | "Should not create useless default import if 'from' library import is present but has no required named import", 217 | code: ` 218 | import SomeImport from 'one' 219 | `, 220 | pluginOptions: { 221 | module: { 222 | from: "one", 223 | to: "two", 224 | }, 225 | specifiers: { One: "default" }, 226 | } as Config, 227 | }, 228 | { 229 | title: "Should move default to named and move others along", 230 | code: ` 231 | import One, { Two } from 'one' 232 | `, 233 | pluginOptions: { 234 | module: { 235 | from: "one", 236 | to: "two", 237 | }, 238 | specifiers: { default: "One", Two: "Two" }, 239 | } as Config, 240 | }, 241 | { 242 | title: "Should move default to default", 243 | code: ` 244 | import One from 'one' 245 | `, 246 | pluginOptions: { 247 | module: { 248 | from: "one", 249 | to: "two", 250 | }, 251 | specifiers: ["default"], 252 | } as Config, 253 | }, 254 | // { 255 | // title: 256 | // "Should fail when asked to create impossible two or more defaults from named imports", 257 | // code: ` 258 | // import One from 'one' 259 | // `, 260 | // pluginOptions: { 261 | // module: { 262 | // from: "one", 263 | // to: "two", 264 | // }, 265 | // specifiers: { One: "default", Two: "default" }, 266 | // } as Config, 267 | // error: 268 | // "It's impossible to make two named imports into two defaults! You should only have one 'default' value in your specifiers mapping", 269 | // }, 270 | // { 271 | // title: "Should fail when provided with wrong config shape", 272 | // code: ` 273 | // import One from 'one' 274 | // `, 275 | // pluginOptions: { 276 | // module: { 277 | // from: "one", 278 | // to: "two", 279 | // }, 280 | // specifiers: "Wrong", 281 | // }, 282 | // error: 283 | // "Wrong specifiers format provided! It should be non-empty object or array.", 284 | // }, 285 | ], 286 | }); 287 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import { declare } from "@babel/helper-plugin-utils"; 2 | import { types as t, PluginObj } from "@babel/core"; 3 | import typescript from "@babel/plugin-syntax-typescript"; 4 | import { Program, ImportSpecifier } from "@babel/types"; 5 | 6 | type StringMap = { [key: string]: string }; 7 | 8 | export type Config = { 9 | module: { 10 | from: string; 11 | to: string; 12 | }; 13 | specifiers: string[] | StringMap; 14 | }; 15 | 16 | const getFrom = (config: Config) => { 17 | return config.module.from; 18 | }; 19 | 20 | const getTo = (config: Config) => { 21 | return config.module.to; 22 | }; 23 | 24 | const getSpecifiersList = (config: Config) => { 25 | return Array.isArray(config.specifiers) 26 | ? config.specifiers 27 | : Object.keys(config.specifiers); 28 | }; 29 | 30 | const shouldRenameSpecifiers = (config: Config) => { 31 | return !config.specifiers.length; 32 | }; 33 | 34 | const shouldChangeNamedToDefault = (config: Config) => { 35 | return ( 36 | !Array.isArray(config.specifiers) && 37 | Object.values(config.specifiers).filter((it) => it.includes("default")) 38 | .length === 1 39 | ); 40 | }; 41 | 42 | const shouldMoveDefaultToNamed = (config: Config) => { 43 | return ( 44 | !Array.isArray(config.specifiers) && 45 | Object.keys(config.specifiers).filter((it) => it.includes("default")) 46 | .length === 1 47 | ); 48 | }; 49 | 50 | const getShouldMoveDefaultToNamedName = (config: Config) => { 51 | return !Array.isArray(config.specifiers) && config.specifiers.default; 52 | }; 53 | 54 | const shouldMoveAll = (config: Config) => { 55 | return Array.isArray(config.specifiers) && config.specifiers[0] === "*"; 56 | }; 57 | 58 | const getNamedIdentifierToDefault = (config: Config) => { 59 | return Object.entries(config.specifiers).find( 60 | ([_, value]) => value === "default" 61 | )?.[0]; 62 | }; 63 | 64 | export default declare((_: any, options: Config) => { 65 | // if ( 66 | // options.specifiers?.length === 0 || 67 | // typeof options.specifiers !== "object" 68 | // ) { 69 | // throw new Error( 70 | // "Wrong specifiers format provided! It should be non-empty object or array." 71 | // ); 72 | // } 73 | // if ( 74 | // typeof options.specifiers === "object" && 75 | // Object.values(options.specifiers).filter((it) => it === "default").length > 76 | // 1 77 | // ) { 78 | // throw new Error( 79 | // "It's impossible to make two or more named imports into two or more defaults in one import statement! You should only have one 'default' value in your specifiers mapping" 80 | // ); 81 | // } 82 | 83 | return { 84 | name: "import-move-codemod", 85 | inherits: () => typescript(_, { ...options, isTSX: true }), 86 | visitor: { 87 | Program(path) { 88 | const from = getFrom(options); 89 | const isFromImportIsPresent = !!path.node.body.find((arg) => { 90 | if (t.isImportDeclaration(arg)) { 91 | return arg.source.value === from; 92 | } 93 | return; 94 | }); 95 | if (isFromImportIsPresent) { 96 | return; 97 | } 98 | }, 99 | ImportDeclaration(path) { 100 | const { node } = path; 101 | const from = getFrom(options); 102 | const to = getTo(options); 103 | const specifiers = getSpecifiersList(options); 104 | const program = path.parent as Program; 105 | 106 | if (t.isImportDeclaration(node) && node.source.value === from) { 107 | if (shouldMoveAll(options)) { 108 | path.node.source.value = to; 109 | return; 110 | } 111 | const newImportDeclaration = t.importDeclaration( 112 | [], 113 | t.stringLiteral(to) 114 | ); 115 | // { One: "default" } case 116 | if (shouldChangeNamedToDefault(options)) { 117 | if ( 118 | path.node.specifiers.find( 119 | (it) => 120 | (it as ImportSpecifier)?.imported?.name === 121 | getNamedIdentifierToDefault(options) 122 | ) 123 | ) { 124 | newImportDeclaration.specifiers = [ 125 | t.importDefaultSpecifier( 126 | t.identifier(getNamedIdentifierToDefault(options)) 127 | ), 128 | ]; 129 | 130 | const filterMovedToDefaultNamedImport = (it: any) => 131 | t.isImportSpecifier(it) && 132 | !(it.imported.name === getNamedIdentifierToDefault(options)); 133 | 134 | path.node.specifiers = path.node.specifiers.filter( 135 | filterMovedToDefaultNamedImport 136 | ); 137 | } 138 | } 139 | const namedSpecifiersToMove = node.specifiers.filter( 140 | (it) => 141 | t.isImportSpecifier(it) && specifiers.includes(it.imported.name) 142 | ) as ImportSpecifier[]; 143 | const thereIsSpecifiersToMove = namedSpecifiersToMove.length !== 0; 144 | // ['One', 'Two'] case 145 | if (thereIsSpecifiersToMove) { 146 | if (shouldRenameSpecifiers(options)) { 147 | const namesMapping = options.specifiers as StringMap; 148 | const renamedNamedImports = namedSpecifiersToMove.map< 149 | ImportSpecifier 150 | >((it: ImportSpecifier) => ({ 151 | ...it, 152 | local: { 153 | ...it.local, 154 | name: namesMapping[it.local.name], 155 | }, 156 | imported: { 157 | ...it.imported, 158 | name: namesMapping[it.imported.name], 159 | }, 160 | })); 161 | newImportDeclaration.specifiers = newImportDeclaration.specifiers.concat( 162 | renamedNamedImports as typeof newImportDeclaration.specifiers 163 | ); 164 | path.node.specifiers = path.node.specifiers.filter( 165 | (it) => !namedSpecifiersToMove.includes(it as ImportSpecifier) 166 | ); 167 | } else { 168 | newImportDeclaration.specifiers = newImportDeclaration.specifiers.concat( 169 | namedSpecifiersToMove as typeof newImportDeclaration.specifiers 170 | ); 171 | } 172 | } 173 | const shouldMoveDefault = specifiers.includes("default"); 174 | // ['default'] case 175 | if (shouldMoveDefault && !shouldMoveDefaultToNamed(options)) { 176 | const aDefaultSpecifier = node.specifiers.find((it) => 177 | t.isImportDefaultSpecifier(it) 178 | ); 179 | aDefaultSpecifier && 180 | newImportDeclaration.specifiers.unshift(aDefaultSpecifier); 181 | } 182 | // { "default" : "One" } case 183 | if (shouldMoveDefaultToNamed(options)) { 184 | const aDefaultSpecifier = node.specifiers.find( 185 | (it) => 186 | t.isImportDefaultSpecifier(it) && 187 | getShouldMoveDefaultToNamedName(options) === it.local.name 188 | ); 189 | if (aDefaultSpecifier) { 190 | const newNamedSpecifier = t.importSpecifier( 191 | aDefaultSpecifier.local, 192 | aDefaultSpecifier.local 193 | ); 194 | newImportDeclaration.specifiers.unshift(newNamedSpecifier); 195 | path.node.specifiers = path.node.specifiers.filter( 196 | (it) => !t.isImportDefaultSpecifier(it) 197 | ); 198 | } 199 | } 200 | if (newImportDeclaration.specifiers.length !== 0) { 201 | program.body.unshift(newImportDeclaration); 202 | // In case of regular named imports we filter them out from "from" import statement 203 | path.node.specifiers = path.node.specifiers.filter( 204 | (it) => !newImportDeclaration.specifiers.includes(it) 205 | ); 206 | // If it appears to have no import specifiers left after filtering, we're getting rid of it. 207 | if (path.node.specifiers.length === 0) { 208 | path.remove(); 209 | } 210 | } 211 | } 212 | }, 213 | }, 214 | } as PluginObj; 215 | }); 216 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | /* Visit https://aka.ms/tsconfig.json to read more about this file */ 4 | 5 | /* Basic Options */ 6 | "incremental": true /* Enable incremental compilation */, 7 | "target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */, 8 | "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */, 9 | // "lib": [], /* Specify library files to be included in the compilation. */ 10 | // "allowJs": true, /* Allow javascript files to be compiled. */ 11 | // "checkJs": true, /* Report errors in .js files. */ 12 | // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ 13 | // "declaration": true, /* Generates corresponding '.d.ts' file. */ 14 | // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ 15 | "sourceMap": true /* Generates corresponding '.map' file. */, 16 | // "outFile": "./", /* Concatenate and emit output to single file. */ 17 | "outDir": "./cjs" /* Redirect output structure to the directory. */, 18 | "rootDir": "./" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */, 19 | // "composite": true, /* Enable project compilation */ 20 | // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ 21 | // "removeComments": true, /* Do not emit comments to output. */ 22 | "noEmit": false, /* Do not emit outputs. */ 23 | // "importHelpers": true, /* Import emit helpers from 'tslib'. */ 24 | // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ 25 | // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ 26 | 27 | /* Strict Type-Checking Options */ 28 | "strict": true /* Enable all strict type-checking options. */, 29 | // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ 30 | // "strictNullChecks": true, /* Enable strict null checks. */ 31 | // "strictFunctionTypes": true, /* Enable strict checking of function types. */ 32 | // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ 33 | // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ 34 | // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ 35 | // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ 36 | 37 | /* Additional Checks */ 38 | "noUnusedLocals": true /* Report errors on unused locals. */, 39 | "noUnusedParameters": true /* Report errors on unused parameters. */, 40 | "noImplicitReturns": true /* Report error when not all code paths in function return a value. */, 41 | "noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */, 42 | 43 | /* Module Resolution Options */ 44 | // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ 45 | // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ 46 | // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ 47 | // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ 48 | // "typeRoots": [], /* List of folders to include type definitions from. */ 49 | // "types": [], /* Type declaration files to be included in compilation. */ 50 | // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ 51 | "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */, 52 | // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ 53 | // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ 54 | 55 | /* Source Map Options */ 56 | // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ 57 | // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ 58 | // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ 59 | // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ 60 | 61 | /* Experimental Options */ 62 | // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ 63 | // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ 64 | 65 | /* Advanced Options */ 66 | "skipLibCheck": true /* Skip type checking of declaration files. */, 67 | "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ 68 | }, 69 | "include": ["src/**/*", "index.d.ts", "manualRun.ts"], 70 | "ts-node": { 71 | "files": true 72 | } 73 | } 74 | --------------------------------------------------------------------------------