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: implements locale file name lower case #95

Merged
merged 1 commit into from
Jul 23, 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
19 changes: 19 additions & 0 deletions transformations/__tests__/locale-lang-file-lower-case.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { defineInlineTest } from 'jscodeshift/src/testUtils'

const transform = require('../element-plus/locale-lang-file-lower-case')

defineInlineTest(
transform,
{},
`
import locale1 from 'element-plus/lib/locale/lang/zh-CN'
import locale2 from 'element-plus/lib/locale/lang/tr-TR'
import locale3 from 'element-plus/lib/locale/lang/en'
`,
`
import locale1 from "element-plus/lib/locale/lang/zh-cn"
import locale2 from "element-plus/lib/locale/lang/tr-tr"
import locale3 from 'element-plus/lib/locale/lang/en'
`,
'element-ui locale lang file lower case'
)
2 changes: 2 additions & 0 deletions transformations/element-plus-upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import wrap from '../src/wrapAstTransformation'
import type { ASTTransformation } from '../src/wrapAstTransformation'
import { transformAST as elementPlusImport } from './element-plus/element-plus-import'
import { transformAST as elementPlusComponentName } from './element-plus/element-plus-component-name'
import { transformAST as langLowerCase } from './element-plus/locale-lang-file-lower-case'
import { getCntFunc } from '../src/report'

export const transformAST: ASTTransformation = context => {
Expand All @@ -11,6 +12,7 @@ export const transformAST: ASTTransformation = context => {
)
elementPlusImport(context)
elementPlusComponentName(context)
langLowerCase(context)
const afterCount = Object.keys(subRules).reduce(
(sum, key) => sum + subRules[key],
0
Expand Down
28 changes: 28 additions & 0 deletions transformations/element-plus/locale-lang-file-lower-case.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import wrap from '../../src/wrapAstTransformation'
import type { ASTTransformation } from '../../src/wrapAstTransformation'

export const transformAST: ASTTransformation = ({ root, j }) => {
const importValuePrefix = 'element-plus/lib/locale/lang/'
// find element-ui lang import
const langImport = root.find(j.ImportDeclaration, node => {
return (
node.source.type === 'StringLiteral' &&
node.source.value?.startsWith(importValuePrefix)
)
})

if (langImport.length) {
langImport.replaceWith(({ node }) => {
const value = node.source.value as string
const lang = value.replace(importValuePrefix, '')
// if lang contains upper-case,converts to lower-case
if (/[A-Z]+/.test(lang)) {
node.source.value = importValuePrefix + lang.toLowerCase()
}
return node
})
}
}

export default wrap(transformAST)
export const parser = 'babylon'