kea-typegen 3.8.2 → 3.8.3
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/dist/package.json +1 -1
- package/dist/src/__tests__/inline.js +39 -5
- package/dist/src/__tests__/inline.js.map +1 -1
- package/dist/src/print/print.js +8 -3
- package/dist/src/print/print.js.map +1 -1
- package/dist/src/utils.js +1 -1
- package/dist/src/utils.js.map +1 -1
- package/dist/src/write/writeInlineLogicTypes.js +63 -7
- package/dist/src/write/writeInlineLogicTypes.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/__tests__/inline.ts +104 -19
- package/src/print/print.ts +11 -3
- package/src/utils.ts +2 -2
- package/src/write/writeInlineLogicTypes.ts +76 -7
|
@@ -4,6 +4,11 @@ import * as osPath from 'path'
|
|
|
4
4
|
import { factory, SyntaxKind } from 'typescript'
|
|
5
5
|
import { AppOptions, ParsedLogic } from '../types'
|
|
6
6
|
import { buildTypeImportEntries, nodeToString, runThroughPrettier } from '../print/print'
|
|
7
|
+
import { printInternalExtraInput } from '../print/printInternalExtraInput'
|
|
8
|
+
import { printInternalReducerActions } from '../print/printInternalReducerActions'
|
|
9
|
+
import { printInternalSelectorTypes } from '../print/printInternalSelectorTypes'
|
|
10
|
+
import { printKey } from '../print/printKey'
|
|
11
|
+
import { printSharedListeners } from '../print/printSharedListeners'
|
|
7
12
|
import { cleanDuplicateAnyNodes, getInlineBlockStatements, isGeneratedInlineBlock } from '../utils'
|
|
8
13
|
import { applyTextEdits, getImportInsertPosition, getTypeArgumentInsertEnd, TextEdit } from './writeTypeImports'
|
|
9
14
|
|
|
@@ -39,7 +44,9 @@ export function printInlineLogicType(parsedLogic: ParsedLogic): string {
|
|
|
39
44
|
|
|
40
45
|
// mark connected values/actions with the logic they come from, e.g. `user: UserType | null // userLogic`
|
|
41
46
|
const withSourceLogicComment = <T extends ts.TypeElement>(member: T, sourceLogic: string | undefined): T =>
|
|
42
|
-
sourceLogic
|
|
47
|
+
sourceLogic
|
|
48
|
+
? ts.addSyntheticTrailingComment(member, SyntaxKind.SingleLineCommentTrivia, ` ${sourceLogic}`, false)
|
|
49
|
+
: member
|
|
43
50
|
|
|
44
51
|
// connected entries first, grouped by source logic (groups alphabetical, entries alphabetical
|
|
45
52
|
// within each group), then the logic's own entries alphabetically
|
|
@@ -92,6 +99,39 @@ export function printInlineLogicType(parsedLogic: ParsedLogic): string {
|
|
|
92
99
|
}
|
|
93
100
|
}
|
|
94
101
|
|
|
102
|
+
const metaMembers: ts.TypeElement[] = []
|
|
103
|
+
const addMetaProperty = (name: string, typeNode: ts.TypeNode): void => {
|
|
104
|
+
metaMembers.push(
|
|
105
|
+
factory.createPropertySignature(undefined, factory.createIdentifier(name), undefined, typeNode),
|
|
106
|
+
)
|
|
107
|
+
}
|
|
108
|
+
if (parsedLogic.keyType) {
|
|
109
|
+
addMetaProperty('key', printKey(parsedLogic))
|
|
110
|
+
}
|
|
111
|
+
if (parsedLogic.sharedListeners.length > 0) {
|
|
112
|
+
addMetaProperty('sharedListeners', printSharedListeners(parsedLogic))
|
|
113
|
+
}
|
|
114
|
+
if (parsedLogic.selectors.some((selector) => (selector.functionTypes?.length ?? 0) > 0)) {
|
|
115
|
+
addMetaProperty('__keaTypeGenInternalSelectorTypes', printInternalSelectorTypes(parsedLogic))
|
|
116
|
+
}
|
|
117
|
+
if (Object.keys(parsedLogic.extraActions).length > 0) {
|
|
118
|
+
addMetaProperty('__keaTypeGenInternalReducerActions', printInternalReducerActions(parsedLogic))
|
|
119
|
+
}
|
|
120
|
+
if (Object.keys(parsedLogic.extraInput).length > 0) {
|
|
121
|
+
addMetaProperty('__keaTypeGenInternalExtraInput', printInternalExtraInput(parsedLogic))
|
|
122
|
+
}
|
|
123
|
+
if (metaMembers.length > 0) {
|
|
124
|
+
if (!parsedLogic.propsType) {
|
|
125
|
+
typeArguments.push(
|
|
126
|
+
factory.createTypeReferenceNode(factory.createIdentifier('Record'), [
|
|
127
|
+
factory.createKeywordTypeNode(SyntaxKind.StringKeyword),
|
|
128
|
+
factory.createKeywordTypeNode(SyntaxKind.AnyKeyword),
|
|
129
|
+
]),
|
|
130
|
+
)
|
|
131
|
+
}
|
|
132
|
+
addSection('Meta', metaMembers)
|
|
133
|
+
}
|
|
134
|
+
|
|
95
135
|
const typeAlias = factory.createTypeAliasDeclaration(
|
|
96
136
|
[factory.createModifier(SyntaxKind.ExportKeyword)],
|
|
97
137
|
factory.createIdentifier(parsedLogic.logicTypeName),
|
|
@@ -206,6 +246,12 @@ export async function writeInlineLogicTypes(
|
|
|
206
246
|
newImportLines.push(`import type { MakeLogicType } from 'kea'`)
|
|
207
247
|
}
|
|
208
248
|
}
|
|
249
|
+
if (
|
|
250
|
+
managedLogics.some((parsedLogic) => parsedLogic.sharedListeners.length > 0) &&
|
|
251
|
+
!importsNamedKeaType(importDeclarations, 'BreakPointFunction')
|
|
252
|
+
) {
|
|
253
|
+
newImportLines.push(`import type { BreakPointFunction } from 'kea'`)
|
|
254
|
+
}
|
|
209
255
|
|
|
210
256
|
// remove the now obsolete `import type { logicType } from './logicType'`, if any
|
|
211
257
|
const typeFileNameBase = parsedLogics[0].typeFileName.replace(/\.[tj]sx?$/, '')
|
|
@@ -292,13 +338,32 @@ export async function writeInlineLogicTypes(
|
|
|
292
338
|
* block - semicolons, trailing commas, quotes, line breaks - does not make us rewrite it forever.
|
|
293
339
|
*/
|
|
294
340
|
function sameGeneratedCode(existingCode: string, generatedCode: string): boolean {
|
|
295
|
-
const normalize = (code: string): string =>
|
|
296
|
-
|
|
341
|
+
const normalize = (code: string): string => {
|
|
342
|
+
const sourceFile = ts.createSourceFile(
|
|
343
|
+
'inlineLogicType.ts',
|
|
344
|
+
code,
|
|
345
|
+
ts.ScriptTarget.Latest,
|
|
346
|
+
true,
|
|
347
|
+
ts.ScriptKind.TS,
|
|
348
|
+
)
|
|
349
|
+
const transformer: ts.TransformerFactory<ts.SourceFile> = (context) => {
|
|
350
|
+
const visit: ts.Visitor = (node) =>
|
|
351
|
+
ts.isParenthesizedTypeNode(node)
|
|
352
|
+
? ts.visitNode(node.type, visit)
|
|
353
|
+
: ts.visitEachChild(node, visit, context)
|
|
354
|
+
return (root) => ts.visitNode(root, visit) as ts.SourceFile
|
|
355
|
+
}
|
|
356
|
+
const transformed = ts.transform(sourceFile, [transformer])
|
|
357
|
+
const normalized = ts
|
|
358
|
+
.createPrinter({ removeComments: true })
|
|
359
|
+
.printFile(transformed.transformed[0])
|
|
297
360
|
.replace(/"/g, "'")
|
|
298
|
-
.replace(/[;,]/g, '')
|
|
299
361
|
.replace(/\s+/g, '')
|
|
300
|
-
|
|
301
|
-
.replace(/([
|
|
362
|
+
.replace(/,(?=[}\])])/g, '')
|
|
363
|
+
.replace(/'([A-Za-z_$][\w$]*)':/g, '$1:')
|
|
364
|
+
transformed.dispose()
|
|
365
|
+
return normalized
|
|
366
|
+
}
|
|
302
367
|
return normalize(existingCode) === normalize(generatedCode)
|
|
303
368
|
}
|
|
304
369
|
|
|
@@ -318,6 +383,10 @@ function getTopLevelStatement(node: ts.Node): ts.Node {
|
|
|
318
383
|
}
|
|
319
384
|
|
|
320
385
|
function importsMakeLogicType(importDeclarations: ts.ImportDeclaration[]): boolean {
|
|
386
|
+
return importsNamedKeaType(importDeclarations, 'MakeLogicType')
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function importsNamedKeaType(importDeclarations: ts.ImportDeclaration[], name: string): boolean {
|
|
321
390
|
return importDeclarations.some(
|
|
322
391
|
(declaration) =>
|
|
323
392
|
ts.isStringLiteralLike(declaration.moduleSpecifier) &&
|
|
@@ -325,7 +394,7 @@ function importsMakeLogicType(importDeclarations: ts.ImportDeclaration[]): boole
|
|
|
325
394
|
declaration.importClause?.namedBindings &&
|
|
326
395
|
ts.isNamedImports(declaration.importClause.namedBindings) &&
|
|
327
396
|
declaration.importClause.namedBindings.elements.some(
|
|
328
|
-
(element) => element.name.text ===
|
|
397
|
+
(element) => element.name.text === name && !element.propertyName,
|
|
329
398
|
),
|
|
330
399
|
)
|
|
331
400
|
}
|