-
Notifications
You must be signed in to change notification settings - Fork 0
/
codemod-use-props-shortcut.js
46 lines (34 loc) · 1.19 KB
/
codemod-use-props-shortcut.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
const glob = require('fast-glob')
const fs = require('fs')
const is = (thing, line) => thing.test(line)
const get = (regex, line) => line.match(regex)
// const BLOCK = /^([A-Z][a-zA-Z0-9]*)(\s+([A-Z][a-zA-Z0-9]*))?$/;
const PROP = /^([a-z][a-zA-Z0-9]*)(\s+(.+))?$/
const PROPS = /^(!?props(\.[a-zA-Z0-9]+)?)(\s+(.+))?$/
const getProp = line => get(PROP, line)
const getProps = line => get(PROPS, line)
// const isBlock = line => is(BLOCK, line);
const isProp = line => is(PROP, line)
const isProps = line => is(PROPS, line)
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 (!isProp(line)) return line
const [_, prop, _1, value] = getProp(line)
if (!isProps(value)) return line
const [_2, props, propsProp] = getProps(value)
if (propsProp === `.${prop}`) return line.replace(propsProp, '')
return line
})
return next.join('\n')
}
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))
})
})