kea-typegen 3.7.1 → 3.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +5 -0
- package/README.md +33 -0
- package/dist/package.json +3 -1
- package/dist/src/__tests__/inline.d.ts +1 -0
- package/dist/src/__tests__/inline.js +606 -0
- package/dist/src/__tests__/inline.js.map +1 -0
- package/dist/src/cache.d.ts +1 -0
- package/dist/src/cache.js +19 -0
- package/dist/src/cache.js.map +1 -1
- package/dist/src/cli/typegen.js +9 -0
- package/dist/src/cli/typegen.js.map +1 -1
- package/dist/src/print/print.d.ts +6 -0
- package/dist/src/print/print.js +61 -42
- package/dist/src/print/print.js.map +1 -1
- package/dist/src/types.d.ts +9 -0
- package/dist/src/utils.d.ts +6 -0
- package/dist/src/utils.js +82 -1
- package/dist/src/utils.js.map +1 -1
- package/dist/src/visit/visit.js +7 -0
- package/dist/src/visit/visit.js.map +1 -1
- package/dist/src/visit/visitConnect.js +40 -0
- package/dist/src/visit/visitConnect.js.map +1 -1
- package/dist/src/visit/visitSelectors.js +14 -1
- package/dist/src/visit/visitSelectors.js.map +1 -1
- package/dist/src/write/writeInlineLogicTypes.d.ts +8 -0
- package/dist/src/write/writeInlineLogicTypes.js +259 -0
- package/dist/src/write/writeInlineLogicTypes.js.map +1 -0
- package/dist/src/write/writeTypeImports.d.ts +8 -0
- package/dist/src/write/writeTypeImports.js +3 -0
- package/dist/src/write/writeTypeImports.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -1
- package/samples/manualTypedLogic.ts +20 -0
- package/samples-inline/counterLogic.ts +37 -0
- package/samples-inline/manualLogic.ts +20 -0
- package/samples-inline/profileLogic.ts +31 -0
- package/samples-inline/searchLogic.ts +32 -0
- package/samples-inline/tsconfig.json +17 -0
- package/samples-inline/types.ts +5 -0
- package/src/__tests__/inline.ts +747 -0
- package/src/cache.ts +19 -0
- package/src/cli/typegen.ts +10 -0
- package/src/print/print.ts +106 -59
- package/src/types.ts +15 -0
- package/src/utils.ts +124 -1
- package/src/visit/visit.ts +15 -0
- package/src/visit/visitConnect.ts +50 -0
- package/src/visit/visitSelectors.ts +26 -1
- package/src/write/writeInlineLogicTypes.ts +354 -0
- package/src/write/writeTypeImports.ts +4 -4
- package/tsconfig.json +1 -0
|
@@ -0,0 +1,747 @@
|
|
|
1
|
+
import * as fs from 'fs'
|
|
2
|
+
import * as os from 'os'
|
|
3
|
+
import * as path from 'path'
|
|
4
|
+
import * as ts from 'typescript'
|
|
5
|
+
import { AppOptions } from '../types'
|
|
6
|
+
import { programFromSource } from '../utils'
|
|
7
|
+
import { visitProgram } from '../visit/visit'
|
|
8
|
+
import { printToFiles } from '../print/print'
|
|
9
|
+
|
|
10
|
+
describe('manually typed logics', () => {
|
|
11
|
+
test('skips logics typed with MakeLogicType', () => {
|
|
12
|
+
const logicSource = `
|
|
13
|
+
import { kea, MakeLogicType } from 'kea'
|
|
14
|
+
|
|
15
|
+
interface Values { name: string }
|
|
16
|
+
interface Actions { updateName: (name: string) => { name: string } }
|
|
17
|
+
|
|
18
|
+
const logic = kea<MakeLogicType<Values, Actions>>({
|
|
19
|
+
actions: () => ({
|
|
20
|
+
updateName: (name: string) => ({ name }),
|
|
21
|
+
}),
|
|
22
|
+
})
|
|
23
|
+
`
|
|
24
|
+
expect(visitProgram(programFromSource(logicSource)).length).toBe(0)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test('skips logics typed with a custom local type, even when named like a generated one', () => {
|
|
28
|
+
const logicSource = `
|
|
29
|
+
import { kea, MakeLogicType } from 'kea'
|
|
30
|
+
|
|
31
|
+
export type logicType = MakeLogicType<{ name: string }, { updateName: (name: string) => { name: string } }>
|
|
32
|
+
|
|
33
|
+
const logic = kea<logicType>({
|
|
34
|
+
actions: () => ({
|
|
35
|
+
updateName: (name: string) => ({ name }),
|
|
36
|
+
}),
|
|
37
|
+
})
|
|
38
|
+
`
|
|
39
|
+
expect(visitProgram(programFromSource(logicSource)).length).toBe(0)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
test('does not skip local logic types carrying the kea-typegen marker', () => {
|
|
43
|
+
const logicSource = `
|
|
44
|
+
import { kea, MakeLogicType } from 'kea'
|
|
45
|
+
|
|
46
|
+
// Generated by kea-typegen. DO NOT EDIT THIS BLOCK MANUALLY.
|
|
47
|
+
export type logicType = MakeLogicType<{ name: string }, { updateName: (name: string) => { name: string } }>
|
|
48
|
+
|
|
49
|
+
const logic = kea<logicType>({
|
|
50
|
+
actions: () => ({
|
|
51
|
+
updateName: (name: string) => ({ name }),
|
|
52
|
+
}),
|
|
53
|
+
})
|
|
54
|
+
`
|
|
55
|
+
const parsedLogics = visitProgram(programFromSource(logicSource))
|
|
56
|
+
expect(parsedLogics.length).toBe(1)
|
|
57
|
+
expect(parsedLogics[0].actions.map((a) => a.name)).toEqual(['updateName'])
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
test('does not skip generated blocks where the marker sits on the values interface', () => {
|
|
61
|
+
const logicSource = `
|
|
62
|
+
import { kea, MakeLogicType } from 'kea'
|
|
63
|
+
|
|
64
|
+
// Generated by kea-typegen. DO NOT EDIT THIS BLOCK MANUALLY.
|
|
65
|
+
export interface logicValues { name: string }
|
|
66
|
+
|
|
67
|
+
export interface logicActions { updateName: (name: string) => { name: string } }
|
|
68
|
+
|
|
69
|
+
export type logicType = MakeLogicType<logicValues, logicActions>
|
|
70
|
+
|
|
71
|
+
const logic = kea<logicType>({
|
|
72
|
+
actions: () => ({
|
|
73
|
+
updateName: (name: string) => ({ name }),
|
|
74
|
+
}),
|
|
75
|
+
})
|
|
76
|
+
`
|
|
77
|
+
const parsedLogics = visitProgram(programFromSource(logicSource))
|
|
78
|
+
expect(parsedLogics.length).toBe(1)
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
test('does not skip logics with a generated type that does not resolve yet', () => {
|
|
82
|
+
const logicSource = `
|
|
83
|
+
import { kea } from 'kea'
|
|
84
|
+
|
|
85
|
+
const logic = kea<logicType>({
|
|
86
|
+
actions: () => ({
|
|
87
|
+
updateName: (name: string) => ({ name }),
|
|
88
|
+
}),
|
|
89
|
+
})
|
|
90
|
+
`
|
|
91
|
+
expect(visitProgram(programFromSource(logicSource)).length).toBe(1)
|
|
92
|
+
})
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
describe('inline mode', () => {
|
|
96
|
+
const createProgram = (fileNames: string[]) =>
|
|
97
|
+
ts.createProgram(fileNames, {
|
|
98
|
+
module: ts.ModuleKind.CommonJS,
|
|
99
|
+
moduleResolution: ts.ModuleResolutionKind.NodeJs,
|
|
100
|
+
target: ts.ScriptTarget.ES2020,
|
|
101
|
+
skipLibCheck: true,
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
test('writes and updates MakeLogicType blocks above the logic', async () => {
|
|
105
|
+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'kea-typegen-inline-'))
|
|
106
|
+
|
|
107
|
+
try {
|
|
108
|
+
const logicDir = path.join(tempDir, 'src')
|
|
109
|
+
const logicPath = path.join(logicDir, 'logic.ts')
|
|
110
|
+
const logicTypePath = path.join(logicDir, 'logicType.ts')
|
|
111
|
+
const keaDtsPath = path.join(tempDir, 'node_modules', 'kea', 'index.d.ts')
|
|
112
|
+
|
|
113
|
+
fs.mkdirSync(path.dirname(keaDtsPath), { recursive: true })
|
|
114
|
+
fs.mkdirSync(logicDir, { recursive: true })
|
|
115
|
+
|
|
116
|
+
fs.writeFileSync(
|
|
117
|
+
keaDtsPath,
|
|
118
|
+
[
|
|
119
|
+
'export function kea<T = any>(input: any): T',
|
|
120
|
+
'export interface MakeLogicType<V = any, A = any, P = any> {}',
|
|
121
|
+
'',
|
|
122
|
+
].join('\n'),
|
|
123
|
+
)
|
|
124
|
+
fs.writeFileSync(
|
|
125
|
+
logicPath,
|
|
126
|
+
[
|
|
127
|
+
"import { kea } from 'kea'",
|
|
128
|
+
'',
|
|
129
|
+
'export const logic = kea({',
|
|
130
|
+
' props: {} as { id: number },',
|
|
131
|
+
' actions: () => ({',
|
|
132
|
+
' setValue: (value: string) => ({ value }),',
|
|
133
|
+
' }),',
|
|
134
|
+
' reducers: () => ({',
|
|
135
|
+
" value: ['' as string, { setValue: (_, { value }) => value }],",
|
|
136
|
+
' }),',
|
|
137
|
+
'})',
|
|
138
|
+
'',
|
|
139
|
+
].join('\n'),
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
const appOptions: AppOptions = {
|
|
143
|
+
rootPath: logicDir,
|
|
144
|
+
typesPath: logicDir,
|
|
145
|
+
packageJsonPath: path.join(tempDir, 'package.json'),
|
|
146
|
+
inline: true,
|
|
147
|
+
write: true,
|
|
148
|
+
log: () => {},
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const program = createProgram([logicPath])
|
|
152
|
+
const parsedLogics = visitProgram(program, appOptions)
|
|
153
|
+
expect(parsedLogics.length).toBe(1)
|
|
154
|
+
|
|
155
|
+
const response = await printToFiles(program, appOptions, parsedLogics)
|
|
156
|
+
expect(response.writtenFiles).toBe(1)
|
|
157
|
+
|
|
158
|
+
const writtenLogic = fs.readFileSync(logicPath, 'utf8')
|
|
159
|
+
|
|
160
|
+
expect(writtenLogic).toContain("import { MakeLogicType, kea } from 'kea'")
|
|
161
|
+
expect(writtenLogic).toContain('// Generated by kea-typegen. DO NOT EDIT THIS BLOCK MANUALLY.')
|
|
162
|
+
expect(writtenLogic).toContain('export interface logicValues {')
|
|
163
|
+
expect(writtenLogic).toContain('export interface logicActions {')
|
|
164
|
+
expect(writtenLogic).toContain('export interface logicProps {')
|
|
165
|
+
expect(writtenLogic).toContain('export type logicType = MakeLogicType<logicValues, logicActions, logicProps>')
|
|
166
|
+
expect(writtenLogic).toContain('export const logic = kea<logicType>({')
|
|
167
|
+
expect(writtenLogic).toContain('value: string')
|
|
168
|
+
expect(writtenLogic).toContain('setValue: (value: string) =>')
|
|
169
|
+
expect(fs.existsSync(logicTypePath)).toBe(false)
|
|
170
|
+
|
|
171
|
+
// a second pass over the written file changes nothing
|
|
172
|
+
const secondProgram = createProgram([logicPath])
|
|
173
|
+
const secondParsedLogics = visitProgram(secondProgram, appOptions)
|
|
174
|
+
expect(secondParsedLogics.length).toBe(1)
|
|
175
|
+
|
|
176
|
+
const secondResponse = await printToFiles(secondProgram, appOptions, secondParsedLogics)
|
|
177
|
+
expect(secondResponse).toEqual({ filesToWrite: 0, writtenFiles: 0, filesToModify: 0 })
|
|
178
|
+
expect(fs.readFileSync(logicPath, 'utf8')).toBe(writtenLogic)
|
|
179
|
+
|
|
180
|
+
// adding an action updates the existing block in place
|
|
181
|
+
fs.writeFileSync(
|
|
182
|
+
logicPath,
|
|
183
|
+
fs
|
|
184
|
+
.readFileSync(logicPath, 'utf8')
|
|
185
|
+
.replace('setValue: (value: string) => ({ value }),', 'setValue: (value: string) => ({ value }),\n reset: true,'),
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
const thirdProgram = createProgram([logicPath])
|
|
189
|
+
const thirdResponse = await printToFiles(thirdProgram, appOptions, visitProgram(thirdProgram, appOptions))
|
|
190
|
+
expect(thirdResponse.writtenFiles).toBe(1)
|
|
191
|
+
|
|
192
|
+
const updatedLogic = fs.readFileSync(logicPath, 'utf8')
|
|
193
|
+
expect(updatedLogic).toContain('reset: ')
|
|
194
|
+
expect(updatedLogic.match(/export type logicType = MakeLogicType</g)?.length).toBe(1)
|
|
195
|
+
expect(updatedLogic.match(/export interface logicValues \{/g)?.length).toBe(1)
|
|
196
|
+
expect(updatedLogic.match(/export interface logicActions \{/g)?.length).toBe(1)
|
|
197
|
+
// one marker per generated interface (values, actions, props)
|
|
198
|
+
expect(updatedLogic.match(/DO NOT EDIT THIS BLOCK MANUALLY/g)?.length).toBe(3)
|
|
199
|
+
} finally {
|
|
200
|
+
fs.rmSync(tempDir, { recursive: true, force: true })
|
|
201
|
+
}
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
test('replaces the old logicType.ts import when switching to inline mode', async () => {
|
|
205
|
+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'kea-typegen-inline-migrate-'))
|
|
206
|
+
|
|
207
|
+
try {
|
|
208
|
+
const logicDir = path.join(tempDir, 'src')
|
|
209
|
+
const logicPath = path.join(logicDir, 'logic.ts')
|
|
210
|
+
const logicTypePath = path.join(logicDir, 'logicType.ts')
|
|
211
|
+
const keaDtsPath = path.join(tempDir, 'node_modules', 'kea', 'index.d.ts')
|
|
212
|
+
|
|
213
|
+
fs.mkdirSync(path.dirname(keaDtsPath), { recursive: true })
|
|
214
|
+
fs.mkdirSync(logicDir, { recursive: true })
|
|
215
|
+
|
|
216
|
+
fs.writeFileSync(
|
|
217
|
+
keaDtsPath,
|
|
218
|
+
[
|
|
219
|
+
'export function kea<T = any>(input: any): T',
|
|
220
|
+
'export interface MakeLogicType<V = any, A = any, P = any> {}',
|
|
221
|
+
'',
|
|
222
|
+
].join('\n'),
|
|
223
|
+
)
|
|
224
|
+
fs.writeFileSync(logicTypePath, 'export interface logicType {}\n')
|
|
225
|
+
fs.writeFileSync(
|
|
226
|
+
logicPath,
|
|
227
|
+
[
|
|
228
|
+
"import { kea } from 'kea'",
|
|
229
|
+
"import type { logicType } from './logicType'",
|
|
230
|
+
'',
|
|
231
|
+
'export const logic = kea<logicType>({',
|
|
232
|
+
' actions: () => ({',
|
|
233
|
+
' setValue: (value: string) => ({ value }),',
|
|
234
|
+
' }),',
|
|
235
|
+
'})',
|
|
236
|
+
'',
|
|
237
|
+
].join('\n'),
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
const appOptions: AppOptions = {
|
|
241
|
+
rootPath: logicDir,
|
|
242
|
+
typesPath: logicDir,
|
|
243
|
+
packageJsonPath: path.join(tempDir, 'package.json'),
|
|
244
|
+
inline: true,
|
|
245
|
+
write: true,
|
|
246
|
+
delete: true,
|
|
247
|
+
log: () => {},
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const program = createProgram([logicPath])
|
|
251
|
+
const response = await printToFiles(program, appOptions, visitProgram(program, appOptions))
|
|
252
|
+
expect(response.writtenFiles).toBe(1)
|
|
253
|
+
|
|
254
|
+
const writtenLogic = fs.readFileSync(logicPath, 'utf8')
|
|
255
|
+
|
|
256
|
+
expect(writtenLogic).not.toContain("from './logicType'")
|
|
257
|
+
expect(writtenLogic).toContain("import { MakeLogicType, kea } from 'kea'")
|
|
258
|
+
expect(writtenLogic).toContain('export interface logicActions {')
|
|
259
|
+
// no values or props on this logic, so no interfaces are generated for them
|
|
260
|
+
expect(writtenLogic).toContain('export type logicType = MakeLogicType<{}, logicActions>')
|
|
261
|
+
expect(writtenLogic).toContain('export const logic = kea<logicType>({')
|
|
262
|
+
expect(fs.existsSync(logicTypePath)).toBe(false)
|
|
263
|
+
} finally {
|
|
264
|
+
fs.rmSync(tempDir, { recursive: true, force: true })
|
|
265
|
+
}
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
test('leaves a reformatted but semantically identical block alone', async () => {
|
|
269
|
+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'kea-typegen-inline-reformat-'))
|
|
270
|
+
|
|
271
|
+
try {
|
|
272
|
+
const logicDir = path.join(tempDir, 'src')
|
|
273
|
+
const logicPath = path.join(logicDir, 'logic.ts')
|
|
274
|
+
const keaDtsPath = path.join(tempDir, 'node_modules', 'kea', 'index.d.ts')
|
|
275
|
+
|
|
276
|
+
fs.mkdirSync(path.dirname(keaDtsPath), { recursive: true })
|
|
277
|
+
fs.mkdirSync(logicDir, { recursive: true })
|
|
278
|
+
|
|
279
|
+
fs.writeFileSync(
|
|
280
|
+
keaDtsPath,
|
|
281
|
+
[
|
|
282
|
+
'export function kea<T = any>(input: any): T',
|
|
283
|
+
'export interface MakeLogicType<V = any, A = any, P = any> {}',
|
|
284
|
+
'',
|
|
285
|
+
].join('\n'),
|
|
286
|
+
)
|
|
287
|
+
fs.writeFileSync(
|
|
288
|
+
logicPath,
|
|
289
|
+
[
|
|
290
|
+
"import { MakeLogicType, kea } from 'kea'",
|
|
291
|
+
'',
|
|
292
|
+
'export const logic = kea({',
|
|
293
|
+
' actions: () => ({',
|
|
294
|
+
' setValue: (value: string | number) => ({ value }),',
|
|
295
|
+
' }),',
|
|
296
|
+
'})',
|
|
297
|
+
'',
|
|
298
|
+
].join('\n'),
|
|
299
|
+
)
|
|
300
|
+
|
|
301
|
+
const appOptions: AppOptions = {
|
|
302
|
+
rootPath: logicDir,
|
|
303
|
+
typesPath: logicDir,
|
|
304
|
+
packageJsonPath: path.join(tempDir, 'package.json'),
|
|
305
|
+
inline: true,
|
|
306
|
+
write: true,
|
|
307
|
+
log: () => {},
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const program = createProgram([logicPath])
|
|
311
|
+
await printToFiles(program, appOptions, visitProgram(program, appOptions))
|
|
312
|
+
|
|
313
|
+
// simulate a formatter pass: strip semicolons and reflow unions with a leading pipe
|
|
314
|
+
const formatted = fs
|
|
315
|
+
.readFileSync(logicPath, 'utf8')
|
|
316
|
+
.replace(/;$/gm, '')
|
|
317
|
+
.replace(/value: string \| number\) =>/g, 'value:\n | string\n | number\n ) =>')
|
|
318
|
+
fs.writeFileSync(logicPath, formatted)
|
|
319
|
+
|
|
320
|
+
const secondProgram = createProgram([logicPath])
|
|
321
|
+
const secondResponse = await printToFiles(secondProgram, appOptions, visitProgram(secondProgram, appOptions))
|
|
322
|
+
expect(secondResponse).toEqual({ filesToWrite: 0, writtenFiles: 0, filesToModify: 0 })
|
|
323
|
+
expect(fs.readFileSync(logicPath, 'utf8')).toBe(formatted)
|
|
324
|
+
} finally {
|
|
325
|
+
fs.rmSync(tempDir, { recursive: true, force: true })
|
|
326
|
+
}
|
|
327
|
+
})
|
|
328
|
+
|
|
329
|
+
test('keeps leading comments attached to the logic when inserting the block', async () => {
|
|
330
|
+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'kea-typegen-inline-comments-'))
|
|
331
|
+
|
|
332
|
+
try {
|
|
333
|
+
const logicDir = path.join(tempDir, 'src')
|
|
334
|
+
const logicPath = path.join(logicDir, 'logic.ts')
|
|
335
|
+
const keaDtsPath = path.join(tempDir, 'node_modules', 'kea', 'index.d.ts')
|
|
336
|
+
|
|
337
|
+
fs.mkdirSync(path.dirname(keaDtsPath), { recursive: true })
|
|
338
|
+
fs.mkdirSync(logicDir, { recursive: true })
|
|
339
|
+
|
|
340
|
+
fs.writeFileSync(
|
|
341
|
+
keaDtsPath,
|
|
342
|
+
[
|
|
343
|
+
'export function kea<T = any>(input: any): T',
|
|
344
|
+
'export interface MakeLogicType<V = any, A = any, P = any> {}',
|
|
345
|
+
'',
|
|
346
|
+
].join('\n'),
|
|
347
|
+
)
|
|
348
|
+
fs.writeFileSync(
|
|
349
|
+
logicPath,
|
|
350
|
+
[
|
|
351
|
+
"import { kea } from 'kea'",
|
|
352
|
+
'',
|
|
353
|
+
'// this comment describes the logic and must stay attached to it',
|
|
354
|
+
'export const logic = kea({',
|
|
355
|
+
' actions: () => ({',
|
|
356
|
+
' setValue: (value: string) => ({ value }),',
|
|
357
|
+
' }),',
|
|
358
|
+
'})',
|
|
359
|
+
'',
|
|
360
|
+
].join('\n'),
|
|
361
|
+
)
|
|
362
|
+
|
|
363
|
+
const appOptions: AppOptions = {
|
|
364
|
+
rootPath: logicDir,
|
|
365
|
+
typesPath: logicDir,
|
|
366
|
+
packageJsonPath: path.join(tempDir, 'package.json'),
|
|
367
|
+
inline: true,
|
|
368
|
+
write: true,
|
|
369
|
+
log: () => {},
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
const program = createProgram([logicPath])
|
|
373
|
+
const response = await printToFiles(program, appOptions, visitProgram(program, appOptions))
|
|
374
|
+
expect(response.writtenFiles).toBe(1)
|
|
375
|
+
|
|
376
|
+
const writtenLogic = fs.readFileSync(logicPath, 'utf8')
|
|
377
|
+
expect(writtenLogic).toContain(
|
|
378
|
+
[
|
|
379
|
+
'// this comment describes the logic and must stay attached to it',
|
|
380
|
+
'export const logic = kea<logicType>({',
|
|
381
|
+
].join('\n'),
|
|
382
|
+
)
|
|
383
|
+
// the generated block sits above the comment
|
|
384
|
+
expect(writtenLogic.indexOf('DO NOT EDIT THIS BLOCK MANUALLY')).toBeLessThan(
|
|
385
|
+
writtenLogic.indexOf('// this comment describes the logic'),
|
|
386
|
+
)
|
|
387
|
+
} finally {
|
|
388
|
+
fs.rmSync(tempDir, { recursive: true, force: true })
|
|
389
|
+
}
|
|
390
|
+
})
|
|
391
|
+
|
|
392
|
+
test('annotates untyped selector combiner parameters', async () => {
|
|
393
|
+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'kea-typegen-inline-selectors-'))
|
|
394
|
+
|
|
395
|
+
try {
|
|
396
|
+
const logicDir = path.join(tempDir, 'src')
|
|
397
|
+
const logicPath = path.join(logicDir, 'logic.ts')
|
|
398
|
+
const keaDtsPath = path.join(tempDir, 'node_modules', 'kea', 'index.d.ts')
|
|
399
|
+
|
|
400
|
+
fs.mkdirSync(path.dirname(keaDtsPath), { recursive: true })
|
|
401
|
+
fs.mkdirSync(logicDir, { recursive: true })
|
|
402
|
+
|
|
403
|
+
fs.writeFileSync(
|
|
404
|
+
keaDtsPath,
|
|
405
|
+
[
|
|
406
|
+
'export function kea<T = any>(input: any): T',
|
|
407
|
+
'export interface MakeLogicType<V = any, A = any, P = any> {}',
|
|
408
|
+
'',
|
|
409
|
+
].join('\n'),
|
|
410
|
+
)
|
|
411
|
+
fs.writeFileSync(
|
|
412
|
+
logicPath,
|
|
413
|
+
[
|
|
414
|
+
"import { kea } from 'kea'",
|
|
415
|
+
'',
|
|
416
|
+
'export const logic = kea({',
|
|
417
|
+
' actions: () => ({',
|
|
418
|
+
' setCounter: (counter: number) => ({ counter }),',
|
|
419
|
+
' }),',
|
|
420
|
+
' reducers: () => ({',
|
|
421
|
+
' counter: [0 as number, { setCounter: (_, { counter }) => counter }],',
|
|
422
|
+
' }),',
|
|
423
|
+
' selectors: () => ({',
|
|
424
|
+
' doubleCounter: [(s): [() => number] => [s.counter], (counter) => counter * 2],',
|
|
425
|
+
' typedCounter: [(s): [() => number] => [s.counter], (counter: number) => counter * 3],',
|
|
426
|
+
' }),',
|
|
427
|
+
'})',
|
|
428
|
+
'',
|
|
429
|
+
].join('\n'),
|
|
430
|
+
)
|
|
431
|
+
|
|
432
|
+
const appOptions: AppOptions = {
|
|
433
|
+
rootPath: logicDir,
|
|
434
|
+
typesPath: logicDir,
|
|
435
|
+
packageJsonPath: path.join(tempDir, 'package.json'),
|
|
436
|
+
inline: true,
|
|
437
|
+
write: true,
|
|
438
|
+
log: () => {},
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
const program = createProgram([logicPath])
|
|
442
|
+
const response = await printToFiles(program, appOptions, visitProgram(program, appOptions))
|
|
443
|
+
expect(response.writtenFiles).toBe(1)
|
|
444
|
+
|
|
445
|
+
const writtenLogic = fs.readFileSync(logicPath, 'utf8')
|
|
446
|
+
expect(writtenLogic).toContain('(counter: number) => counter * 2')
|
|
447
|
+
// already annotated params stay untouched
|
|
448
|
+
expect(writtenLogic).toContain('(counter: number) => counter * 3')
|
|
449
|
+
|
|
450
|
+
// a second pass changes nothing
|
|
451
|
+
const secondProgram = createProgram([logicPath])
|
|
452
|
+
const secondResponse = await printToFiles(secondProgram, appOptions, visitProgram(secondProgram, appOptions))
|
|
453
|
+
expect(secondResponse).toEqual({ filesToWrite: 0, writtenFiles: 0, filesToModify: 0 })
|
|
454
|
+
} finally {
|
|
455
|
+
fs.rmSync(tempDir, { recursive: true, force: true })
|
|
456
|
+
}
|
|
457
|
+
})
|
|
458
|
+
})
|
|
459
|
+
|
|
460
|
+
describe('mixed mode with inlinePaths', () => {
|
|
461
|
+
const createProgram = (fileNames: string[]) =>
|
|
462
|
+
ts.createProgram(fileNames, {
|
|
463
|
+
module: ts.ModuleKind.CommonJS,
|
|
464
|
+
moduleResolution: ts.ModuleResolutionKind.NodeJs,
|
|
465
|
+
target: ts.ScriptTarget.ES2020,
|
|
466
|
+
skipLibCheck: true,
|
|
467
|
+
})
|
|
468
|
+
|
|
469
|
+
const writeKeaDts = (tempDir: string) => {
|
|
470
|
+
const keaDtsPath = path.join(tempDir, 'node_modules', 'kea', 'index.d.ts')
|
|
471
|
+
fs.mkdirSync(path.dirname(keaDtsPath), { recursive: true })
|
|
472
|
+
fs.writeFileSync(
|
|
473
|
+
keaDtsPath,
|
|
474
|
+
[
|
|
475
|
+
'export function kea<T = any>(input: any): T',
|
|
476
|
+
'export interface MakeLogicType<V = any, A = any, P = any> {}',
|
|
477
|
+
'',
|
|
478
|
+
].join('\n'),
|
|
479
|
+
)
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
const simpleLogicSource = [
|
|
483
|
+
"import { kea } from 'kea'",
|
|
484
|
+
'',
|
|
485
|
+
'export const logic = kea({',
|
|
486
|
+
' actions: () => ({',
|
|
487
|
+
' setValue: (value: string) => ({ value }),',
|
|
488
|
+
' }),',
|
|
489
|
+
'})',
|
|
490
|
+
'',
|
|
491
|
+
].join('\n')
|
|
492
|
+
|
|
493
|
+
test('files under inlinePaths go inline, everything else keeps logicType.ts', async () => {
|
|
494
|
+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'kea-typegen-inline-paths-'))
|
|
495
|
+
|
|
496
|
+
try {
|
|
497
|
+
const rootDir = path.join(tempDir, 'src')
|
|
498
|
+
const inlineDir = path.join(rootDir, 'scenes', 'inlined')
|
|
499
|
+
const classicDir = path.join(rootDir, 'scenes', 'classic')
|
|
500
|
+
const inlineLogicPath = path.join(inlineDir, 'inlinedLogic.ts')
|
|
501
|
+
const classicLogicPath = path.join(classicDir, 'classicLogic.ts')
|
|
502
|
+
|
|
503
|
+
writeKeaDts(tempDir)
|
|
504
|
+
fs.mkdirSync(inlineDir, { recursive: true })
|
|
505
|
+
fs.mkdirSync(classicDir, { recursive: true })
|
|
506
|
+
fs.writeFileSync(inlineLogicPath, simpleLogicSource.replace('export const logic', 'export const inlinedLogic'))
|
|
507
|
+
fs.writeFileSync(classicLogicPath, simpleLogicSource.replace('export const logic', 'export const classicLogic'))
|
|
508
|
+
|
|
509
|
+
const appOptions: AppOptions = {
|
|
510
|
+
rootPath: rootDir,
|
|
511
|
+
typesPath: rootDir,
|
|
512
|
+
packageJsonPath: path.join(tempDir, 'package.json'),
|
|
513
|
+
inlinePaths: [path.join(rootDir, 'scenes', 'inlined')],
|
|
514
|
+
write: true,
|
|
515
|
+
delete: true,
|
|
516
|
+
log: () => {},
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
const program = createProgram([inlineLogicPath, classicLogicPath])
|
|
520
|
+
await printToFiles(program, appOptions, visitProgram(program, appOptions))
|
|
521
|
+
|
|
522
|
+
const writtenInlineLogic = fs.readFileSync(inlineLogicPath, 'utf8')
|
|
523
|
+
expect(writtenInlineLogic).toContain('// Generated by kea-typegen. DO NOT EDIT THIS BLOCK MANUALLY.')
|
|
524
|
+
expect(writtenInlineLogic).toContain('export type inlinedLogicType = MakeLogicType<{}, inlinedLogicActions>')
|
|
525
|
+
expect(fs.existsSync(path.join(inlineDir, 'inlinedLogicType.ts'))).toBe(false)
|
|
526
|
+
|
|
527
|
+
const writtenClassicLogic = fs.readFileSync(classicLogicPath, 'utf8')
|
|
528
|
+
expect(writtenClassicLogic).not.toContain('MakeLogicType')
|
|
529
|
+
expect(fs.existsSync(path.join(classicDir, 'classicLogicType.ts'))).toBe(true)
|
|
530
|
+
} finally {
|
|
531
|
+
fs.rmSync(tempDir, { recursive: true, force: true })
|
|
532
|
+
}
|
|
533
|
+
})
|
|
534
|
+
|
|
535
|
+
test('relative inlinePaths resolve against rootPath', async () => {
|
|
536
|
+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'kea-typegen-inline-paths-relative-'))
|
|
537
|
+
|
|
538
|
+
try {
|
|
539
|
+
const rootDir = path.join(tempDir, 'src')
|
|
540
|
+
const inlineDir = path.join(rootDir, 'scenes', 'inlined')
|
|
541
|
+
const inlineLogicPath = path.join(inlineDir, 'inlinedLogic.ts')
|
|
542
|
+
|
|
543
|
+
writeKeaDts(tempDir)
|
|
544
|
+
fs.mkdirSync(inlineDir, { recursive: true })
|
|
545
|
+
fs.writeFileSync(inlineLogicPath, simpleLogicSource.replace('export const logic', 'export const inlinedLogic'))
|
|
546
|
+
|
|
547
|
+
const appOptions: AppOptions = {
|
|
548
|
+
rootPath: rootDir,
|
|
549
|
+
typesPath: rootDir,
|
|
550
|
+
packageJsonPath: path.join(tempDir, 'package.json'),
|
|
551
|
+
inlinePaths: ['./scenes/inlined'],
|
|
552
|
+
write: true,
|
|
553
|
+
log: () => {},
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
const program = createProgram([inlineLogicPath])
|
|
557
|
+
await printToFiles(program, appOptions, visitProgram(program, appOptions))
|
|
558
|
+
|
|
559
|
+
expect(fs.readFileSync(inlineLogicPath, 'utf8')).toContain(
|
|
560
|
+
'// Generated by kea-typegen. DO NOT EDIT THIS BLOCK MANUALLY.',
|
|
561
|
+
)
|
|
562
|
+
expect(fs.existsSync(path.join(inlineDir, 'inlinedLogicType.ts'))).toBe(false)
|
|
563
|
+
} finally {
|
|
564
|
+
fs.rmSync(tempDir, { recursive: true, force: true })
|
|
565
|
+
}
|
|
566
|
+
})
|
|
567
|
+
|
|
568
|
+
test('connected actions from an inline (MakeLogicType) logic land in the consuming logic type', async () => {
|
|
569
|
+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'kea-typegen-inline-connect-'))
|
|
570
|
+
|
|
571
|
+
try {
|
|
572
|
+
const rootDir = path.join(tempDir, 'src')
|
|
573
|
+
const inlineLogicPath = path.join(rootDir, 'inlinedLogic.ts')
|
|
574
|
+
const consumerLogicPath = path.join(rootDir, 'consumerLogic.ts')
|
|
575
|
+
const keaDtsPath = path.join(tempDir, 'node_modules', 'kea', 'index.d.ts')
|
|
576
|
+
|
|
577
|
+
fs.mkdirSync(path.dirname(keaDtsPath), { recursive: true })
|
|
578
|
+
fs.mkdirSync(rootDir, { recursive: true })
|
|
579
|
+
|
|
580
|
+
// minimal kea with a mapped-type MakeLogicType, mirroring the real shape
|
|
581
|
+
fs.writeFileSync(
|
|
582
|
+
keaDtsPath,
|
|
583
|
+
[
|
|
584
|
+
'export function kea<T = any>(input: any): T',
|
|
585
|
+
'export type AnyFunction = (...args: any) => any',
|
|
586
|
+
'export interface MakeLogicType<V = any, A = any, P = any> {',
|
|
587
|
+
' actionCreators: {',
|
|
588
|
+
' [K in keyof A]: A[K] extends AnyFunction',
|
|
589
|
+
' ? (...args: Parameters<A[K]>) => { type: string; payload: ReturnType<A[K]> }',
|
|
590
|
+
' : never',
|
|
591
|
+
' }',
|
|
592
|
+
' values: V',
|
|
593
|
+
'}',
|
|
594
|
+
'',
|
|
595
|
+
].join('\n'),
|
|
596
|
+
)
|
|
597
|
+
fs.writeFileSync(
|
|
598
|
+
inlineLogicPath,
|
|
599
|
+
[
|
|
600
|
+
"import { MakeLogicType, kea } from 'kea'",
|
|
601
|
+
'',
|
|
602
|
+
'// Generated by kea-typegen. DO NOT EDIT THIS BLOCK MANUALLY.',
|
|
603
|
+
'export interface inlinedLogicValues {',
|
|
604
|
+
' value: string',
|
|
605
|
+
'}',
|
|
606
|
+
'',
|
|
607
|
+
'export interface inlinedLogicActions {',
|
|
608
|
+
' setValue: (value: string) => {',
|
|
609
|
+
' value: string',
|
|
610
|
+
' }',
|
|
611
|
+
'}',
|
|
612
|
+
'',
|
|
613
|
+
'export type inlinedLogicType = MakeLogicType<inlinedLogicValues, inlinedLogicActions>',
|
|
614
|
+
'',
|
|
615
|
+
'export const inlinedLogic = kea<inlinedLogicType>({',
|
|
616
|
+
' actions: () => ({',
|
|
617
|
+
' setValue: (value: string) => ({ value }),',
|
|
618
|
+
' }),',
|
|
619
|
+
' reducers: () => ({',
|
|
620
|
+
" value: ['' as string, { setValue: (_, { value }) => value }],",
|
|
621
|
+
' }),',
|
|
622
|
+
'})',
|
|
623
|
+
'',
|
|
624
|
+
].join('\n'),
|
|
625
|
+
)
|
|
626
|
+
fs.writeFileSync(
|
|
627
|
+
consumerLogicPath,
|
|
628
|
+
[
|
|
629
|
+
"import { kea } from 'kea'",
|
|
630
|
+
'',
|
|
631
|
+
"import { inlinedLogic } from './inlinedLogic'",
|
|
632
|
+
'',
|
|
633
|
+
'export const consumerLogic = kea({',
|
|
634
|
+
' connect: {',
|
|
635
|
+
" actions: [inlinedLogic, ['setValue']],",
|
|
636
|
+
" values: [inlinedLogic, ['value']],",
|
|
637
|
+
' },',
|
|
638
|
+
'})',
|
|
639
|
+
'',
|
|
640
|
+
].join('\n'),
|
|
641
|
+
)
|
|
642
|
+
const inlineConsumerDir = path.join(rootDir, 'inlineConsumer')
|
|
643
|
+
const inlineConsumerLogicPath = path.join(inlineConsumerDir, 'inlineConsumerLogic.ts')
|
|
644
|
+
fs.mkdirSync(inlineConsumerDir, { recursive: true })
|
|
645
|
+
fs.writeFileSync(
|
|
646
|
+
inlineConsumerLogicPath,
|
|
647
|
+
[
|
|
648
|
+
"import { kea } from 'kea'",
|
|
649
|
+
'',
|
|
650
|
+
"import { inlinedLogic } from '../inlinedLogic'",
|
|
651
|
+
'',
|
|
652
|
+
'export const inlineConsumerLogic = kea({',
|
|
653
|
+
' connect: {',
|
|
654
|
+
" actions: [inlinedLogic, ['setValue']],",
|
|
655
|
+
" values: [inlinedLogic, ['value']],",
|
|
656
|
+
' },',
|
|
657
|
+
'})',
|
|
658
|
+
'',
|
|
659
|
+
].join('\n'),
|
|
660
|
+
)
|
|
661
|
+
|
|
662
|
+
const appOptions: AppOptions = {
|
|
663
|
+
rootPath: rootDir,
|
|
664
|
+
typesPath: rootDir,
|
|
665
|
+
packageJsonPath: path.join(tempDir, 'package.json'),
|
|
666
|
+
inlinePaths: [inlineConsumerDir],
|
|
667
|
+
write: true,
|
|
668
|
+
log: () => {},
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
const program = createProgram([inlineLogicPath, consumerLogicPath, inlineConsumerLogicPath])
|
|
672
|
+
await printToFiles(program, appOptions, visitProgram(program, appOptions))
|
|
673
|
+
|
|
674
|
+
// consumerLogic is classic-mode, so it gets a logicType.ts that must include the connected action
|
|
675
|
+
const consumerType = fs.readFileSync(path.join(rootDir, 'consumerLogicType.ts'), 'utf8')
|
|
676
|
+
expect(consumerType).toContain('setValue: (value: string) => void')
|
|
677
|
+
expect(consumerType).toContain('value: string')
|
|
678
|
+
|
|
679
|
+
// the inline consumer's block marks connected values/actions with their source logic
|
|
680
|
+
const inlineConsumer = fs.readFileSync(inlineConsumerLogicPath, 'utf8')
|
|
681
|
+
expect(inlineConsumer).toMatch(/value: string.* \/\/ inlinedLogic/)
|
|
682
|
+
expect(inlineConsumer).toMatch(/\};? \/\/ inlinedLogic/)
|
|
683
|
+
|
|
684
|
+
// a second pass over the annotated block changes nothing
|
|
685
|
+
const secondProgram = createProgram([inlineLogicPath, consumerLogicPath, inlineConsumerLogicPath])
|
|
686
|
+
const secondResponse = await printToFiles(secondProgram, appOptions, visitProgram(secondProgram, appOptions))
|
|
687
|
+
expect(secondResponse).toEqual({ filesToWrite: 0, writtenFiles: 0, filesToModify: 0 })
|
|
688
|
+
} finally {
|
|
689
|
+
fs.rmSync(tempDir, { recursive: true, force: true })
|
|
690
|
+
}
|
|
691
|
+
})
|
|
692
|
+
|
|
693
|
+
test('files that already carry an inline block stay inline without any inline options', async () => {
|
|
694
|
+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'kea-typegen-inline-sticky-'))
|
|
695
|
+
|
|
696
|
+
try {
|
|
697
|
+
const rootDir = path.join(tempDir, 'src')
|
|
698
|
+
const logicPath = path.join(rootDir, 'stickyLogic.ts')
|
|
699
|
+
|
|
700
|
+
writeKeaDts(tempDir)
|
|
701
|
+
fs.mkdirSync(rootDir, { recursive: true })
|
|
702
|
+
fs.writeFileSync(
|
|
703
|
+
logicPath,
|
|
704
|
+
[
|
|
705
|
+
"import { MakeLogicType, kea } from 'kea'",
|
|
706
|
+
'',
|
|
707
|
+
'// Generated by kea-typegen. DO NOT EDIT THIS BLOCK MANUALLY.',
|
|
708
|
+
'export interface stickyLogicActions {',
|
|
709
|
+
' setValue: (value: string) => {',
|
|
710
|
+
' value: string',
|
|
711
|
+
' }',
|
|
712
|
+
'}',
|
|
713
|
+
'',
|
|
714
|
+
'export type stickyLogicType = MakeLogicType<{}, stickyLogicActions>',
|
|
715
|
+
'',
|
|
716
|
+
'export const stickyLogic = kea<stickyLogicType>({',
|
|
717
|
+
' actions: () => ({',
|
|
718
|
+
' setValue: (value: string) => ({ value }),',
|
|
719
|
+
' setOther: (other: number) => ({ other }),',
|
|
720
|
+
' }),',
|
|
721
|
+
'})',
|
|
722
|
+
'',
|
|
723
|
+
].join('\n'),
|
|
724
|
+
)
|
|
725
|
+
|
|
726
|
+
// no `inline`, no `inlinePaths` - the existing block alone keeps the file inline
|
|
727
|
+
const appOptions: AppOptions = {
|
|
728
|
+
rootPath: rootDir,
|
|
729
|
+
typesPath: rootDir,
|
|
730
|
+
packageJsonPath: path.join(tempDir, 'package.json'),
|
|
731
|
+
write: true,
|
|
732
|
+
log: () => {},
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
const program = createProgram([logicPath])
|
|
736
|
+
const response = await printToFiles(program, appOptions, visitProgram(program, appOptions))
|
|
737
|
+
expect(response.writtenFiles).toBe(1)
|
|
738
|
+
|
|
739
|
+
const writtenLogic = fs.readFileSync(logicPath, 'utf8')
|
|
740
|
+
expect(writtenLogic).toContain('setOther: (other: number) =>')
|
|
741
|
+
expect(writtenLogic.match(/DO NOT EDIT THIS BLOCK MANUALLY/g)?.length).toBe(1)
|
|
742
|
+
expect(fs.existsSync(path.join(rootDir, 'stickyLogicType.ts'))).toBe(false)
|
|
743
|
+
} finally {
|
|
744
|
+
fs.rmSync(tempDir, { recursive: true, force: true })
|
|
745
|
+
}
|
|
746
|
+
})
|
|
747
|
+
})
|