|
| 1 | +--- |
| 2 | +id: utils |
| 3 | +title: codeshift/utils |
| 4 | +slug: /utils |
| 5 | +--- |
| 6 | + |
| 7 | +CodeshiftCommunity provides a set of utilities to help perform common codemod operations. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +`@codeshift/utils` is pre-bundled with every codemod that is published to the [community folder](https://github.com/CodeshiftCommunity/CodeshiftCommunity/tree/main/community), |
| 12 | +so there's no need to install it manually. |
| 13 | + |
| 14 | +However, it is also available for use outside of this project and compatible with jscodeshift. |
| 15 | + |
| 16 | +`npm install --save-dev @codeshift/utils` or `yarn add -D @codeshift/utils` |
| 17 | + |
| 18 | +## Imports |
| 19 | + |
| 20 | +### `hasImportDeclaration(j, source, importPath)` |
| 21 | + |
| 22 | +Finds an import declaration by source name |
| 23 | + |
| 24 | +**Returns** |
| 25 | + |
| 26 | +`boolean` |
| 27 | + |
| 28 | +**Example** |
| 29 | + |
| 30 | +```jsx |
| 31 | +// src/App.js |
| 32 | +import React from 'react'; |
| 33 | +``` |
| 34 | + |
| 35 | +```js |
| 36 | +import { hasImportDeclaration } from '@codeshift/utils'; |
| 37 | + |
| 38 | +hasImportDeclaration(j, source, 'react'); // True |
| 39 | +``` |
| 40 | + |
| 41 | +### `getImportDeclaration(j, source, importPath)` |
| 42 | + |
| 43 | +Returns an import declaration by source name |
| 44 | + |
| 45 | +**Returns** |
| 46 | + |
| 47 | +`Collection`: Collection containing 1 or more imports |
| 48 | + |
| 49 | +**Example** |
| 50 | + |
| 51 | +```jsx |
| 52 | +// src/App.js |
| 53 | +import React from 'react'; |
| 54 | +``` |
| 55 | + |
| 56 | +```js |
| 57 | +import { getImportDeclaration } from '@codeshift/utils'; |
| 58 | + |
| 59 | +getImportDeclaration(j, source, 'react'); |
| 60 | +``` |
| 61 | + |
| 62 | +### `getDefaultImportSpecifier(j, source, specifier)` |
| 63 | + |
| 64 | +Finds a default import specifier by name |
| 65 | + |
| 66 | +**Returns** |
| 67 | + |
| 68 | +`Collection`: Collection containing all matched default import specifiers |
| 69 | + |
| 70 | +**Example** |
| 71 | + |
| 72 | +```jsx |
| 73 | +// src/App.js |
| 74 | +import React from 'react'; |
| 75 | +``` |
| 76 | + |
| 77 | +```js |
| 78 | +import { getDefaultImportSpecifier } from '@codeshift/utils'; |
| 79 | + |
| 80 | +getDefaultImportSpecifier(j, source, 'React'); // Collection containing 'React' |
| 81 | +``` |
| 82 | + |
| 83 | +### `getImportSpecifier(j, source, specifier)` |
| 84 | + |
| 85 | +Finds an import specifier by name |
| 86 | + |
| 87 | +**Returns** |
| 88 | + |
| 89 | +`Collection`: Collection containing all matched import specifiers |
| 90 | + |
| 91 | +**Example** |
| 92 | + |
| 93 | +```jsx |
| 94 | +// src/App.js |
| 95 | +import React, { useEffect } from 'react'; |
| 96 | +``` |
| 97 | + |
| 98 | +```js |
| 99 | +import { getImportSpecifier } from '@codeshift/utils'; |
| 100 | + |
| 101 | +getImportSpecifier(j, source, 'useEffect'); // Collection containing 'useEffect' |
| 102 | +``` |
| 103 | + |
| 104 | +## JSX |
| 105 | + |
| 106 | +### `getJSXAttributesByName(j, source, attributeName)` |
| 107 | + |
| 108 | +Finds a JSX attributeName (aka prop) by name |
| 109 | + |
| 110 | +**Returns** |
| 111 | + |
| 112 | +`Collection`: Collection containing all matched jsx attributes |
| 113 | + |
| 114 | +**Example** |
| 115 | + |
| 116 | +```jsx |
| 117 | +// src/App.js |
| 118 | +import React from 'react'; |
| 119 | + |
| 120 | +const App = () => <Button primary>Say hello</Button>; |
| 121 | +``` |
| 122 | + |
| 123 | +```js |
| 124 | +import { getJSXAttributesByName } from '@codeshift/utils'; |
| 125 | + |
| 126 | +getJSXAttributesByName(j, source, 'primary'); // Collection containing 'primary' |
| 127 | +``` |
| 128 | + |
| 129 | +## Comments |
| 130 | + |
| 131 | +### `addCommentBefore(j, source, message)` |
| 132 | + |
| 133 | +Appends a comment before the provided node |
| 134 | + |
| 135 | +**Returns** |
| 136 | + |
| 137 | +`void` |
| 138 | + |
| 139 | +**Example** |
| 140 | + |
| 141 | +```jsx |
| 142 | +// src/App.js |
| 143 | +import React from 'react'; |
| 144 | + |
| 145 | +const App = () => <Button primary>Say hello</Button>; |
| 146 | +``` |
| 147 | + |
| 148 | +```js |
| 149 | +import { addCommentBefore } from '@codeshift/utils'; |
| 150 | + |
| 151 | +addCommentBefore( |
| 152 | + j, |
| 153 | + path.find(j.ImportDeclaration), |
| 154 | + 'This should be removed in favour of mylib', |
| 155 | +); |
| 156 | +``` |
| 157 | + |
| 158 | +```js |
| 159 | +// src/App.js |
| 160 | +// TODO: (Codemod) This should be removed in favour of mylib |
| 161 | +import React from 'react'; |
| 162 | + |
| 163 | +const App = () => <Button primary>Say hello</Button>; |
| 164 | +``` |
0 commit comments