Skip to content

Commit b4d2dac

Browse files
authored
feat: support transform import xx from element-ui/lib/.. (#94)
1 parent 9b55ed4 commit b4d2dac

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

transformations/__tests__/element-plus-upgrade.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,11 @@ defineInlineTest(
2424
`import ElementUI, { ElMessageBox as MessageBox } from "element-plus";`,
2525
'correctly transform multiple imports from element-plus'
2626
)
27+
28+
defineInlineTest(
29+
transform,
30+
{},
31+
`import locale from "element-ui/lib/locale/lang/zh-cn";`,
32+
`import locale from "element-plus/lib/locale/lang/zh-cn";`,
33+
'correctly transform import from element-plus'
34+
)

transformations/element-plus/element-plus-component-name.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { getCntFunc } from '../../src/report'
1111
* @param content
1212
*/
1313
export const transformAST: ASTTransformation = ({ root, j }) => {
14+
const cntFunc = getCntFunc('element-plus-upgrade', subRules)
1415
// find element-ui import
1516
const elementPlusImport = root.find(j.ImportDeclaration, {
1617
source: {
@@ -22,6 +23,7 @@ export const transformAST: ASTTransformation = ({ root, j }) => {
2223
elementPlusImport.forEach(({ node }) => {
2324
let newSpecifier: (ImportSpecifier | ImportDefaultSpecifier)[] = []
2425
node.specifiers.forEach(importNode => {
26+
cntFunc()
2527
if (importNode.type === 'ImportSpecifier') {
2628
newSpecifier.push(
2729
j.importSpecifier(
@@ -38,10 +40,6 @@ export const transformAST: ASTTransformation = ({ root, j }) => {
3840
}
3941
})
4042
})
41-
42-
// stats
43-
const cntFunc = getCntFunc('observable', subRules)
44-
cntFunc()
4543
}
4644
}
4745

transformations/element-plus/element-plus-import.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,17 @@ import { getCntFunc } from '../../src/report'
77
* @param content
88
*/
99
export const transformAST: ASTTransformation = ({ root, j }) => {
10+
const cntFunc = getCntFunc('element-plus-import', subRules)
1011
// find element-ui import
11-
const elementPlusImport = root.find(j.ImportDeclaration, {
12-
source: {
13-
value: 'element-ui'
14-
}
12+
const elementPlusImport = root.find(j.ImportDeclaration, node => {
13+
return node.source.value.toString().startsWith('element-ui')
1514
})
16-
1715
if (elementPlusImport.length) {
1816
elementPlusImport.forEach(({ node }) => {
19-
node.source.value = 'element-plus'
17+
node.source.value =
18+
'element-plus' +
19+
node.source.value?.toString().substring('element-ui'.length)
2020
})
21-
22-
// stats
23-
const cntFunc = getCntFunc('observable', subRules)
2421
cntFunc()
2522
}
2623
}

0 commit comments

Comments
 (0)