-
Notifications
You must be signed in to change notification settings - Fork 1
/
react-declare-state.ts
164 lines (138 loc) · 5.58 KB
/
react-declare-state.ts
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { FileInfo, API, ExpressionStatement, AssignmentExpression, ObjectExpression, Property, Identifier, MemberExpression, ASTPath, ClassExpression, ExportDefaultDeclaration, ClassDeclaration, objectExpression } from 'jscodeshift'
import _ from 'lodash'
export const parser = 'ts'
function searchUp(path: ASTPath, type: string) {
while (path != null && path.node.type != type) {
path = path.parentPath
}
return path
}
function searchUpParent(path: ASTPath, type: string) {
while (path != null && (!path.parentPath || path.parentPath.node.type != type)) {
path = path.parentPath
}
return path
}
export default function transformer(file: FileInfo, api: API) {
const j = api.jscodeshift;
let src = file.source
// Find React classes
src = j(src)
.find(j.ClassDeclaration)
.forEach(path => {
// console.log(path.node)
const className = (path.node as ClassDeclaration).id!.name
// Get superclass
const superClass = path.node.superClass
if (!superClass) {
return
}
if (superClass.type == "Identifier") {
// console.log(superClass)
if (superClass.name != "Component") {
return
}
// (classPath.node as ClassExpression | ClassDeclaration).superClass = `${superClass.name}<${className}Props>` as any
}
else if (superClass.type == "MemberExpression" && superClass.property.type == "Identifier") {
// console.log(superClass)
if (superClass.property.name != "Component") {
return
}
// ((classPath.node as ClassExpression | ClassDeclaration).superClass! as any).property = `${superClass.property.name}<${className}Props>`
}
else {
throw new Error("SUPERCLASS")
}
// Find state calls
const states: string[] = []
j(path)
.find(j.MemberExpression)
.filter(p => p.node.object.type == "MemberExpression"
&& p.node.object.object.type == "ThisExpression"
&& p.node.object.property.type == "Identifier"
&& p.node.object.property.name == "state"
&& p.node.property.type == "Identifier")
.forEach(p => {
states.push((p.node.property as Identifier).name)
})
if (states.length == 0) {
return
}
if (!path.node.superTypeParameters) {
return
}
if (path.node.superTypeParameters.params.length == 2) {
return
}
path.node.superTypeParameters.params.push(`${className}State` as any)
// Add interface
const ifcode = `interface ${className}State {${_.uniq(states).map(state => `\n${state}: any`)}
}`
// Find Program
const topPath = searchUpParent(path, "Program")
topPath.insertBefore(ifcode)
// // Add interface
// const ifcode = `interface ${className}Props {${propTypes.map(prop => `\n${prop.comment ? ` /** ${prop.comment} */\n` : " "}${prop.name}${prop.optional ? "?" : ""}: ${prop.type}`)}
// }`
// // Find Program
// const topPath = searchUpParent(path, "Program")
// topPath.insertBefore(ifcode)
// // Get superclass
// const superClass = (classPath.node as ClassExpression | ClassDeclaration).superClass!
// if (superClass.type == "Identifier") {
// (classPath.node as ClassExpression | ClassDeclaration).superClass = `${superClass.name}<${className}Props>` as any
// }
// else if (superClass.type == "MemberExpression" && superClass.property.type == "Identifier") {
// ((classPath.node as ClassExpression | ClassDeclaration).superClass! as any).property = `${superClass.property.name}<${className}Props>`
// }
// else {
// throw new Error("SUPERCLASS")
// }
// // Remove initClass function
// path.replace()
// // Remove initClass call
// j(searchUp(path, "Program"))
// .find(j.CallExpression)
// .filter(path => path.node.callee.type == "MemberExpression"
// && path.node.callee.property.type == "Identifier"
// && path.node.callee.property.name == "initClass"
// && path.node.callee.object.type == "Identifier"
// && path.node.callee.object.name == className)
// .replaceWith(null)
// // Remove let
// j(searchUp(path, "Program"))
// .find(j.VariableDeclaration)
// .filter(p => p.node.declarations[0].type == "VariableDeclarator" && p.node.declarations[0].id.type == "Identifier" && p.node.declarations[0].id.name == className)
// .replaceWith(null)
})
.toSource();
return src
}
// const killLets: string[] = []
// // Simplify complex export
// src = j(src)
// .find(j.ExportDefaultDeclaration)
// .forEach(p => {
// // console.log(p.node.declaration)
// const comments = p.node.comments
// const paths = j(p).find(j.ClassExpression).paths()
// if (paths.length == 1 && classNames.includes(paths[0].node.id!.name)) {
// // console.log(paths[0].node)
// killLets.push(paths[0].node.id!.name)
// p.node.declaration = paths[0].node
// p.node.comments = comments
// }
// })
// .toSource()
// // Remove let
// for (const className of killLets) {
// src = j(src)
// .find(j.VariableDeclaration)
// .filter(p => p.node.declarations[0].type == "VariableDeclarator" && p.node.declarations[0].id.type == "Identifier" && p.node.declarations[0].id.name == className)
// // .forEach(p => {
// // console.log(p.node.id)
// // })
// .replaceWith(null)
// .toSource()
// }