-
Notifications
You must be signed in to change notification settings - Fork 0
/
codemod-use-camelCase-props.js
77 lines (67 loc) · 1.81 KB
/
codemod-use-camelCase-props.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const glob = require('fast-glob')
const fs = require('fs')
const toCamelCase = require('to-camel-case')
const is = (thing, line) => thing.test(line)
const get = (regex, line) => line.match(regex)
const PROP = /^([a-z][a-zA-Z0-9]*)(\s+(.+))?$/
const PROP_SLOT = /^([a-z][a-zA-Z0-9]*)(\s+(\<[a-zA-Z0-9]*)(\s+(.+)))?$/
function transform(rtext) {
const text = rtext.replace(/\r\n/g, '\n')
const lines = text.split('\n').map(line => line.trim())
const next = lines
.map((line, i) => {
if (is(PROP_SLOT, line)) {
const [_, prop, _1, slot, _2, value] = get(PROP_SLOT, line)
if (!slot && !value) return null
return `${prop} ${slot} ${maybeMakeHyphenated(prop, value)}`
} else if (is(PROP, line)) {
const [_, prop, _1, value] = get(PROP, line)
if (!value) return null
return `${prop} ${maybeMakeHyphenated(prop, value)}`
} else {
return line
}
})
.filter(line => line !== null)
return next.join('\n')
}
const MAYBE_HYPHENATED_STYLE_PROPS = [
'alignContent',
'alignItems',
'alignSelf',
'backgroundBlendMode',
'backgroundClip',
'backgroudOrigin',
'backgroundRepeat',
'boxSizing',
'clear',
'cursor',
'flexBasis',
'flexDirection',
'flexFlow',
'flexWrap',
'float',
'fontStretch',
'justifyContent',
'objectFit',
'overflowWrap',
'textAlign',
'textDecorationLine',
'textTransform',
'whiteSpace',
'wordBreak',
]
const maybeMakeHyphenated = (name, value) =>
(MAYBE_HYPHENATED_STYLE_PROPS.includes(name) && /-/.test(value)) ||
/(flex|space)-/.test(value)
? toCamelCase(value)
: value
glob(['src/**/*.view'], {
bashNative: ['linux'],
cwd: process.cwd(),
}).then(list => {
list.forEach(file => {
const content = fs.readFileSync(file, 'utf-8')
fs.writeFileSync(file, transform(content))
})
})