Skip to content

Commit 469e13d

Browse files
authored
feat: add transformation for element-ui (#84)
1 parent f5502fd commit 469e13d

File tree

7 files changed

+163
-0
lines changed

7 files changed

+163
-0
lines changed

src/__tests__/packageTransformation.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,33 @@ let output =
8585
expect(pack.process(JSON.parse(input))).toStrictEqual(JSON.parse(output))
8686
})
8787
})
88+
89+
describe('upgrade element-ui', () => {
90+
let input =
91+
`{
92+
"dependencies": {
93+
"vue": "^2.6.10",
94+
"vue-i18n": "^8.14.0",
95+
"vue-router": "^3.1.2",
96+
"vuex": "^3.1.1",
97+
"@vue/composition-api": "^1.0.0-rc.12",
98+
"element-ui": "^1.0.0"
99+
}
100+
}
101+
`
102+
let output =
103+
`{
104+
"dependencies": {
105+
"vue": "^3.1.1",
106+
"vue-i18n": "^9.1.6",
107+
"vue-router": "^4.0.8",
108+
"vuex": "^4.0.1",
109+
"element-plus": "^1.0.2-beta.55"
110+
}
111+
}
112+
`
113+
it('upgread element-ui to element-plus', () => {
114+
expect(pack.process(JSON.parse(input))).toStrictEqual(JSON.parse(output))
115+
})
116+
})
117+

src/packageTransformation.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ export function process(packageObj: any): any {
8686
if (packageObj?.eslintConfig?.parserOptions?.parser != undefined) {
8787
packageObj.eslintConfig.parserOptions.parser = '@babel/eslint-parser'
8888
}
89+
if (packageObj?.dependencies['element-ui'] != undefined) {
90+
delete packageObj.dependencies['element-ui']
91+
packageObj.dependencies['element-plus'] = '^1.0.2-beta.55'
92+
}
8993
return packageObj
9094
}
9195
/**
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { defineInlineTest } from 'jscodeshift/src/testUtils'
2+
const transform = require('../element-plus-upgrade')
3+
4+
defineInlineTest(
5+
transform,
6+
{},
7+
`import ElementUI from "element-ui";`,
8+
`import ElementUI from "element-plus";`,
9+
'correctly transform default import from element-plus'
10+
)
11+
12+
defineInlineTest(
13+
transform,
14+
{},
15+
`import { MessageBox } from "element-ui";`,
16+
`import { ElMessageBox as MessageBox } from "element-plus";`,
17+
'correctly transform component import from element-plus'
18+
)
19+
20+
defineInlineTest(
21+
transform,
22+
{},
23+
`import ElementUI, { MessageBox } from "element-ui";`,
24+
`import ElementUI, { ElMessageBox as MessageBox } from "element-plus";`,
25+
'correctly transform multiple imports from element-plus'
26+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import wrap from '../src/wrapAstTransformation'
2+
import type { ASTTransformation } from '../src/wrapAstTransformation'
3+
import { transformAST as elementPlusImport } from './element-plus/element-plus-import'
4+
import { transformAST as elementPlusComponentName } from './element-plus/element-plus-component-name'
5+
import { getCntFunc } from '../src/report'
6+
7+
export const transformAST: ASTTransformation = context => {
8+
const beforeCount = Object.keys(subRules).reduce(
9+
(sum, key) => sum + subRules[key],
10+
0
11+
)
12+
elementPlusImport(context)
13+
elementPlusComponentName(context)
14+
const afterCount = Object.keys(subRules).reduce(
15+
(sum, key) => sum + subRules[key],
16+
0
17+
)
18+
const change = afterCount - beforeCount
19+
const cntFunc = getCntFunc('element-ui-upgrade', global.outputReport)
20+
cntFunc(change)
21+
}
22+
23+
export default wrap(transformAST)
24+
export const parser = 'babylon'
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import wrap from '../../src/wrapAstTransformation'
2+
import type { ASTTransformation } from '../../src/wrapAstTransformation'
3+
import type {
4+
ImportSpecifier,
5+
ImportDefaultSpecifier
6+
} from 'ast-types/gen/nodes'
7+
import { getCntFunc } from '../../src/report'
8+
9+
/**
10+
* Process component names that have been changed in element plus
11+
* @param content
12+
*/
13+
export const transformAST: ASTTransformation = ({ root, j }) => {
14+
// find element-ui import
15+
const elementPlusImport = root.find(j.ImportDeclaration, {
16+
source: {
17+
value: 'element-plus'
18+
}
19+
})
20+
21+
if (elementPlusImport.length) {
22+
elementPlusImport.forEach(({ node }) => {
23+
let newSpecifier: (ImportSpecifier | ImportDefaultSpecifier)[] = []
24+
node.specifiers.forEach(importNode => {
25+
if (importNode.type === 'ImportSpecifier') {
26+
newSpecifier.push(
27+
j.importSpecifier(
28+
j.identifier('El' + importNode.local?.name),
29+
importNode.local
30+
)
31+
)
32+
node.specifiers = newSpecifier
33+
} else {
34+
newSpecifier.push(
35+
j.importDefaultSpecifier(j.identifier(importNode.local!.name))
36+
)
37+
node.specifiers = newSpecifier
38+
}
39+
})
40+
})
41+
42+
// stats
43+
const cntFunc = getCntFunc('observable', subRules)
44+
cntFunc()
45+
}
46+
}
47+
48+
export default wrap(transformAST)
49+
export const parser = 'babylon'
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import wrap from '../../src/wrapAstTransformation'
2+
import type { ASTTransformation } from '../../src/wrapAstTransformation'
3+
import { getCntFunc } from '../../src/report'
4+
5+
/**
6+
* Upgrade element-ui to element-plus
7+
* @param content
8+
*/
9+
export const transformAST: ASTTransformation = ({ root, j }) => {
10+
// find element-ui import
11+
const elementPlusImport = root.find(j.ImportDeclaration, {
12+
source: {
13+
value: 'element-ui'
14+
}
15+
})
16+
17+
if (elementPlusImport.length) {
18+
elementPlusImport.forEach(({ node }) => {
19+
node.source.value = 'element-plus'
20+
})
21+
22+
// stats
23+
const cntFunc = getCntFunc('observable', subRules)
24+
cntFunc()
25+
}
26+
}
27+
28+
export default wrap(transformAST)
29+
export const parser = 'babylon'

transformations/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const transformationMap: {
2626
'render-to-resolveComponent': require('./render-to-resolveComponent'),
2727
'vue-i18n-v9': require('./vue-i18n-v9'),
2828
'vuex-create-logger': require('./vuex-create-logger'),
29+
'element-plus-import': require('./element-plus-upgrade'),
2930

3031
// atomic ones
3132
'remove-contextual-h-from-render': require('./remove-contextual-h-from-render'),

0 commit comments

Comments
 (0)