Skip to content

Commit ddc118b

Browse files
authored
feat: implements locale file name lower case (#95)
1 parent b4d2dac commit ddc118b

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { defineInlineTest } from 'jscodeshift/src/testUtils'
2+
3+
const transform = require('../element-plus/locale-lang-file-lower-case')
4+
5+
defineInlineTest(
6+
transform,
7+
{},
8+
`
9+
import locale1 from 'element-plus/lib/locale/lang/zh-CN'
10+
import locale2 from 'element-plus/lib/locale/lang/tr-TR'
11+
import locale3 from 'element-plus/lib/locale/lang/en'
12+
`,
13+
`
14+
import locale1 from "element-plus/lib/locale/lang/zh-cn"
15+
import locale2 from "element-plus/lib/locale/lang/tr-tr"
16+
import locale3 from 'element-plus/lib/locale/lang/en'
17+
`,
18+
'element-ui locale lang file lower case'
19+
)

transformations/element-plus-upgrade.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import wrap from '../src/wrapAstTransformation'
22
import type { ASTTransformation } from '../src/wrapAstTransformation'
33
import { transformAST as elementPlusImport } from './element-plus/element-plus-import'
44
import { transformAST as elementPlusComponentName } from './element-plus/element-plus-component-name'
5+
import { transformAST as langLowerCase } from './element-plus/locale-lang-file-lower-case'
56
import { getCntFunc } from '../src/report'
67

78
export const transformAST: ASTTransformation = context => {
@@ -11,6 +12,7 @@ export const transformAST: ASTTransformation = context => {
1112
)
1213
elementPlusImport(context)
1314
elementPlusComponentName(context)
15+
langLowerCase(context)
1416
const afterCount = Object.keys(subRules).reduce(
1517
(sum, key) => sum + subRules[key],
1618
0
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import wrap from '../../src/wrapAstTransformation'
2+
import type { ASTTransformation } from '../../src/wrapAstTransformation'
3+
4+
export const transformAST: ASTTransformation = ({ root, j }) => {
5+
const importValuePrefix = 'element-plus/lib/locale/lang/'
6+
// find element-ui lang import
7+
const langImport = root.find(j.ImportDeclaration, node => {
8+
return (
9+
node.source.type === 'StringLiteral' &&
10+
node.source.value?.startsWith(importValuePrefix)
11+
)
12+
})
13+
14+
if (langImport.length) {
15+
langImport.replaceWith(({ node }) => {
16+
const value = node.source.value as string
17+
const lang = value.replace(importValuePrefix, '')
18+
// if lang contains upper-case,converts to lower-case
19+
if (/[A-Z]+/.test(lang)) {
20+
node.source.value = importValuePrefix + lang.toLowerCase()
21+
}
22+
return node
23+
})
24+
}
25+
}
26+
27+
export default wrap(transformAST)
28+
export const parser = 'babylon'

0 commit comments

Comments
 (0)