Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support import xx from element-ui/lib/xx conversion #94

Merged
merged 1 commit into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions transformations/__tests__/element-plus-upgrade.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ defineInlineTest(
`import ElementUI, { ElMessageBox as MessageBox } from "element-plus";`,
'correctly transform multiple imports from element-plus'
)

defineInlineTest(
transform,
{},
`import locale from "element-ui/lib/locale/lang/zh-cn";`,
`import locale from "element-plus/lib/locale/lang/zh-cn";`,
'correctly transform import from element-plus'
)
6 changes: 2 additions & 4 deletions transformations/element-plus/element-plus-component-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getCntFunc } from '../../src/report'
* @param content
*/
export const transformAST: ASTTransformation = ({ root, j }) => {
const cntFunc = getCntFunc('element-plus-upgrade', subRules)
// find element-ui import
const elementPlusImport = root.find(j.ImportDeclaration, {
source: {
Expand All @@ -22,6 +23,7 @@ export const transformAST: ASTTransformation = ({ root, j }) => {
elementPlusImport.forEach(({ node }) => {
let newSpecifier: (ImportSpecifier | ImportDefaultSpecifier)[] = []
node.specifiers.forEach(importNode => {
cntFunc()
if (importNode.type === 'ImportSpecifier') {
newSpecifier.push(
j.importSpecifier(
Expand All @@ -38,10 +40,6 @@ export const transformAST: ASTTransformation = ({ root, j }) => {
}
})
})

// stats
const cntFunc = getCntFunc('observable', subRules)
cntFunc()
}
}

Expand Down
15 changes: 6 additions & 9 deletions transformations/element-plus/element-plus-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,17 @@ import { getCntFunc } from '../../src/report'
* @param content
*/
export const transformAST: ASTTransformation = ({ root, j }) => {
const cntFunc = getCntFunc('element-plus-import', subRules)
// find element-ui import
const elementPlusImport = root.find(j.ImportDeclaration, {
source: {
value: 'element-ui'
}
const elementPlusImport = root.find(j.ImportDeclaration, node => {
return node.source.value.toString().startsWith('element-ui')
})

if (elementPlusImport.length) {
elementPlusImport.forEach(({ node }) => {
node.source.value = 'element-plus'
node.source.value =
'element-plus' +
node.source.value?.toString().substring('element-ui'.length)
})

// stats
const cntFunc = getCntFunc('observable', subRules)
cntFunc()
}
}
Expand Down