Skip to content

Commit 837d050

Browse files
feat: update error components import statements (#179)
1 parent 03f9f16 commit 837d050

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { testCodemod } from '@sourcegraph/codemod-toolkit-ts'
2+
3+
import { errorMessageMove } from '../errorMessageMove'
4+
5+
testCodemod('errorMessageMove', errorMessageMove, [
6+
{
7+
label: 'default',
8+
initialSource: `
9+
import { ErrorMessage } from '@sourcegraph/branded/src/components/alerts'
10+
11+
export const Test = <ErrorMessage className="btn">Hey</ErrorMessage>
12+
`,
13+
expectedSource: `
14+
import { ErrorMessage } from '@sourcegraph/wildcard'
15+
16+
export const Test = <ErrorMessage className="btn">Hey</ErrorMessage>
17+
`,
18+
},
19+
])
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { addOrUpdateSourcegraphWildcardImportIfNeeded } from '@sourcegraph/codemod-toolkit-packages'
2+
import { getImportDeclarationByModuleSpecifier, JsxTagElement, runTransform } from '@sourcegraph/codemod-toolkit-ts'
3+
4+
const COMPONENTS_TO_MOVE = new Set(['ErrorMessage', 'ErrorAlert'])
5+
6+
/**
7+
* Update imports for COMPONENTS_TO_MOVE to `@sourcegraph/wildcard`. See tests for more examples.
8+
*/
9+
export const errorMessageMove = runTransform(() => {
10+
const jsxTagElementsToUpdate = new Set<string>()
11+
12+
function processElement(element: JsxTagElement): void {
13+
const tagName = element.getTagNameNode().getText()
14+
15+
if (COMPONENTS_TO_MOVE.has(tagName)) {
16+
jsxTagElementsToUpdate.add(tagName)
17+
}
18+
}
19+
20+
return {
21+
JsxOpeningElement: processElement,
22+
JsxSelfClosingElement: processElement,
23+
SourceFileExit(sourceFile) {
24+
if (jsxTagElementsToUpdate.size === 0) {
25+
return
26+
}
27+
const declaration = getImportDeclarationByModuleSpecifier(
28+
sourceFile,
29+
'@sourcegraph/branded/src/components/alerts'
30+
)
31+
declaration?.remove()
32+
33+
addOrUpdateSourcegraphWildcardImportIfNeeded({
34+
sourceFile,
35+
importStructure: {
36+
namedImports: [...jsxTagElementsToUpdate],
37+
},
38+
})
39+
40+
sourceFile.fixUnusedIdentifiers()
41+
},
42+
}
43+
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './errorMessageMove'

0 commit comments

Comments
 (0)