kea-typegen 1.2.2 → 1.4.2
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 +77 -33
- package/dist/package.json +18 -17
- package/dist/src/__tests__/e2e/actions.js +4 -4
- package/dist/src/__tests__/e2e/actions.js.map +1 -1
- package/dist/src/__tests__/e2e/connect.js +6 -6
- package/dist/src/__tests__/e2e/connect.js.map +1 -1
- package/dist/src/__tests__/e2e/loaders.js +3 -3
- package/dist/src/__tests__/e2e/loaders.js.map +1 -1
- package/dist/src/__tests__/e2e/reducers.js +4 -4
- package/dist/src/__tests__/e2e/reducers.js.map +1 -1
- package/dist/src/__tests__/e2e/selectors.js +3 -3
- package/dist/src/__tests__/e2e/selectors.js.map +1 -1
- package/dist/src/__tests__/utils.js +7 -7
- package/dist/src/__tests__/utils.js.map +1 -1
- package/dist/src/cli/typegen.js +35 -9
- package/dist/src/cli/typegen.js.map +1 -1
- package/dist/src/print/print.d.ts +5 -5
- package/dist/src/print/print.js +68 -44
- package/dist/src/print/print.js.map +1 -1
- package/dist/src/print/printActionCreators.d.ts +1 -2
- package/dist/src/print/printActionCreators.js +7 -7
- package/dist/src/print/printActionCreators.js.map +1 -1
- package/dist/src/print/printActionKeys.d.ts +1 -2
- package/dist/src/print/printActionKeys.js +4 -4
- package/dist/src/print/printActionKeys.js.map +1 -1
- package/dist/src/print/printActionTypes.d.ts +1 -2
- package/dist/src/print/printActionTypes.js +4 -4
- package/dist/src/print/printActionTypes.js.map +1 -1
- package/dist/src/print/printActions.d.ts +1 -2
- package/dist/src/print/printActions.js +4 -4
- package/dist/src/print/printActions.js.map +1 -1
- package/dist/src/print/printConstants.d.ts +1 -2
- package/dist/src/print/printConstants.js +2 -2
- package/dist/src/print/printConstants.js.map +1 -1
- package/dist/src/print/printDefaults.d.ts +1 -2
- package/dist/src/print/printDefaults.js +3 -3
- package/dist/src/print/printDefaults.js.map +1 -1
- package/dist/src/print/printEvents.d.ts +1 -2
- package/dist/src/print/printEvents.js +3 -3
- package/dist/src/print/printEvents.js.map +1 -1
- package/dist/src/print/printInternalExtraInput.d.ts +1 -2
- package/dist/src/print/printInternalExtraInput.js +8 -8
- package/dist/src/print/printInternalExtraInput.js.map +1 -1
- package/dist/src/print/printInternalReducerActions.d.ts +1 -2
- package/dist/src/print/printInternalReducerActions.js +3 -4
- package/dist/src/print/printInternalReducerActions.js.map +1 -1
- package/dist/src/print/printInternalSelectorTypes.d.ts +1 -2
- package/dist/src/print/printInternalSelectorTypes.js +3 -3
- package/dist/src/print/printInternalSelectorTypes.js.map +1 -1
- package/dist/src/print/printKey.d.ts +1 -2
- package/dist/src/print/printKey.js +2 -2
- package/dist/src/print/printKey.js.map +1 -1
- package/dist/src/print/printListeners.d.ts +1 -2
- package/dist/src/print/printListeners.js +9 -9
- package/dist/src/print/printListeners.js.map +1 -1
- package/dist/src/print/printProps.d.ts +1 -2
- package/dist/src/print/printProps.js +6 -5
- package/dist/src/print/printProps.js.map +1 -1
- package/dist/src/print/printReducer.d.ts +1 -2
- package/dist/src/print/printReducer.js +6 -6
- package/dist/src/print/printReducer.js.map +1 -1
- package/dist/src/print/printReducerOptions.d.ts +1 -2
- package/dist/src/print/printReducerOptions.js +3 -3
- package/dist/src/print/printReducerOptions.js.map +1 -1
- package/dist/src/print/printReducers.d.ts +1 -2
- package/dist/src/print/printReducers.js +7 -7
- package/dist/src/print/printReducers.js.map +1 -1
- package/dist/src/print/printSelector.d.ts +1 -2
- package/dist/src/print/printSelector.js +4 -4
- package/dist/src/print/printSelector.js.map +1 -1
- package/dist/src/print/printSelectors.d.ts +1 -2
- package/dist/src/print/printSelectors.js +6 -6
- package/dist/src/print/printSelectors.js.map +1 -1
- package/dist/src/print/printSharedListeners.d.ts +1 -2
- package/dist/src/print/printSharedListeners.js +11 -11
- package/dist/src/print/printSharedListeners.js.map +1 -1
- package/dist/src/print/printValues.d.ts +1 -2
- package/dist/src/print/printValues.js +3 -3
- package/dist/src/print/printValues.js.map +1 -1
- package/dist/src/typegen.js +5 -5
- package/dist/src/typegen.js.map +1 -1
- package/dist/src/types.d.ts +6 -0
- package/dist/src/utils.js +21 -18
- package/dist/src/utils.js.map +1 -1
- package/dist/src/visit/__tests__/visit.js +2 -2
- package/dist/src/visit/__tests__/visit.js.map +1 -1
- package/dist/src/visit/visit.js +22 -14
- package/dist/src/visit/visit.js.map +1 -1
- package/dist/src/visit/visitActions.d.ts +2 -2
- package/dist/src/visit/visitActions.js +13 -11
- package/dist/src/visit/visitActions.js.map +1 -1
- package/dist/src/visit/visitConnect.js +4 -4
- package/dist/src/visit/visitConnect.js.map +1 -1
- package/dist/src/visit/visitDefaults.js +1 -1
- package/dist/src/visit/visitDefaults.js.map +1 -1
- package/dist/src/visit/visitKey.js +2 -2
- package/dist/src/visit/visitKey.js.map +1 -1
- package/dist/src/visit/visitListeners.js +5 -5
- package/dist/src/visit/visitListeners.js.map +1 -1
- package/dist/src/visit/visitLoaders.d.ts +2 -2
- package/dist/src/visit/visitLoaders.js +27 -27
- package/dist/src/visit/visitLoaders.js.map +1 -1
- package/dist/src/visit/visitProps.js +1 -1
- package/dist/src/visit/visitProps.js.map +1 -1
- package/dist/src/visit/visitReducers.js +4 -4
- package/dist/src/visit/visitReducers.js.map +1 -1
- package/dist/src/visit/visitSelectors.js +8 -8
- package/dist/src/visit/visitSelectors.js.map +1 -1
- package/dist/src/visit/visitSharedListeners.js +5 -5
- package/dist/src/visit/visitSharedListeners.js.map +1 -1
- package/dist/src/visit/visitWindowValues.js +1 -1
- package/dist/src/visit/visitWindowValues.js.map +1 -1
- package/dist/src/write/write.d.ts +4 -0
- package/dist/src/{import/writeLogicTypeImports.js → write/write.js} +56 -11
- package/dist/src/write/write.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/form-plugin/lib/form-plugin/src/typegen.js +16 -15
- package/form-plugin/lib/src/types.d.ts +4 -1
- package/form-plugin/package.json +1 -1
- package/form-plugin/src/typegen.ts +25 -25
- package/form-plugin/tsconfig.json +0 -1
- package/form-plugin/yarn.lock +10 -10
- package/package.json +18 -17
- package/samples/.kearc +3 -0
- package/samples/autoImportLogic.ts +12 -3
- package/samples/autoImportLogicType.ts +7 -2
- package/samples/complexLogic.ts +4 -3
- package/samples/donotimport.ts +1 -0
- package/samples/githubConnectLogic.ts +1 -0
- package/samples/githubImportLogic.ts +1 -0
- package/samples/githubLogic.ts +1 -0
- package/samples/loadersLogic.ts +1 -0
- package/samples/pluginLogic.ts +2 -0
- package/samples/propsLogic.ts +1 -0
- package/samples/propsLogicType.ts +9 -9
- package/samples/windowValuesLogic.ts +1 -0
- package/src/__tests__/utils.ts +5 -5
- package/src/cli/typegen.ts +33 -7
- package/src/print/print.ts +95 -37
- package/src/print/printActionCreators.ts +12 -15
- package/src/print/printActionKeys.ts +5 -6
- package/src/print/printActionTypes.ts +6 -7
- package/src/print/printActions.ts +6 -13
- package/src/print/printConstants.ts +5 -6
- package/src/print/printDefaults.ts +5 -6
- package/src/print/printEvents.ts +8 -6
- package/src/print/printInternalExtraInput.ts +12 -13
- package/src/print/printInternalReducerActions.ts +5 -12
- package/src/print/printInternalSelectorTypes.ts +7 -8
- package/src/print/printKey.ts +2 -2
- package/src/print/printListeners.ts +16 -17
- package/src/print/printProps.ts +7 -7
- package/src/print/printReducer.ts +14 -15
- package/src/print/printReducerOptions.ts +4 -5
- package/src/print/printReducers.ts +13 -14
- package/src/print/printSelector.ts +8 -9
- package/src/print/printSelectors.ts +12 -13
- package/src/print/printSharedListeners.ts +20 -21
- package/src/print/printValues.ts +5 -6
- package/src/typegen.ts +4 -4
- package/src/types.ts +11 -1
- package/src/utils.ts +17 -17
- package/src/visit/visit.ts +11 -3
- package/src/visit/visitActions.ts +12 -10
- package/src/visit/visitListeners.ts +8 -8
- package/src/visit/visitLoaders.ts +49 -43
- package/src/visit/visitReducers.ts +1 -1
- package/src/visit/visitSelectors.ts +3 -3
- package/src/visit/visitSharedListeners.ts +8 -10
- package/src/{import/writeLogicTypeImports.ts → write/write.ts} +115 -16
- package/dist/src/import/writeLogicTypeImports.d.ts +0 -3
- package/dist/src/import/writeLogicTypeImports.js.map +0 -1
package/src/print/print.ts
CHANGED
|
@@ -1,4 +1,16 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
createPrinter,
|
|
3
|
+
createSourceFile,
|
|
4
|
+
EmitHint,
|
|
5
|
+
factory,
|
|
6
|
+
Node,
|
|
7
|
+
NewLineKind,
|
|
8
|
+
Program,
|
|
9
|
+
ScriptKind,
|
|
10
|
+
ScriptTarget,
|
|
11
|
+
SyntaxKind,
|
|
12
|
+
TypeParameterDeclaration,
|
|
13
|
+
} from 'typescript'
|
|
2
14
|
import * as fs from 'fs'
|
|
3
15
|
import * as path from 'path'
|
|
4
16
|
import * as prettier from 'prettier'
|
|
@@ -23,7 +35,7 @@ import { printReducerOptions } from './printReducerOptions'
|
|
|
23
35
|
import { printEvents } from './printEvents'
|
|
24
36
|
import { printSharedListeners } from './printSharedListeners'
|
|
25
37
|
import { printListeners } from './printListeners'
|
|
26
|
-
import {
|
|
38
|
+
import { writePaths, writeTypeImports } from '../write/write'
|
|
27
39
|
import { printInternalExtraInput } from './printInternalExtraInput'
|
|
28
40
|
|
|
29
41
|
export function runThroughPrettier(sourceText: string, filePath: string): string {
|
|
@@ -43,10 +55,10 @@ export function runThroughPrettier(sourceText: string, filePath: string): string
|
|
|
43
55
|
|
|
44
56
|
// returns files to write
|
|
45
57
|
export function printToFiles(
|
|
46
|
-
program:
|
|
58
|
+
program: Program,
|
|
47
59
|
appOptions: AppOptions,
|
|
48
60
|
parsedLogics: ParsedLogic[],
|
|
49
|
-
): { filesToWrite: number; writtenFiles: number;
|
|
61
|
+
): { filesToWrite: number; writtenFiles: number; filesToModify: number } {
|
|
50
62
|
const { log } = appOptions
|
|
51
63
|
|
|
52
64
|
const groupedByFile: Record<string, ParsedLogic[]> = {}
|
|
@@ -56,13 +68,36 @@ export function printToFiles(
|
|
|
56
68
|
}
|
|
57
69
|
groupedByFile[parsedLogic.fileName].push(parsedLogic)
|
|
58
70
|
|
|
59
|
-
// create the
|
|
71
|
+
// create the Nodes and gather referenced types
|
|
60
72
|
printLogicType(parsedLogic, appOptions)
|
|
61
73
|
}
|
|
62
74
|
|
|
75
|
+
// Automatically ignore imports from "node_modules/@types/node", if {types: ["node"]} in tsconfig.json
|
|
76
|
+
const defaultGlobalTypePaths = appOptions.importGlobalTypes
|
|
77
|
+
? []
|
|
78
|
+
: (program.getCompilerOptions().types || []).map(
|
|
79
|
+
(type) =>
|
|
80
|
+
path.join(
|
|
81
|
+
appOptions.packageJsonPath ? path.dirname(appOptions.packageJsonPath) : appOptions.rootPath,
|
|
82
|
+
'node_modules',
|
|
83
|
+
'@types',
|
|
84
|
+
type,
|
|
85
|
+
) + path.sep,
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
// Manually ignored
|
|
89
|
+
const ignoredImportPaths = (appOptions.ignoreImportPaths || []).map((importPath) =>
|
|
90
|
+
path.resolve(appOptions.rootPath, importPath),
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
const doNotImportFromPaths = [...defaultGlobalTypePaths, ...ignoredImportPaths]
|
|
94
|
+
|
|
95
|
+
const shouldIgnore = (absolutePath: string) =>
|
|
96
|
+
!!doNotImportFromPaths.find((badPath) => absolutePath.startsWith(badPath))
|
|
97
|
+
|
|
63
98
|
let writtenFiles = 0
|
|
64
99
|
let filesToWrite = 0
|
|
65
|
-
let
|
|
100
|
+
let filesToModify = 0
|
|
66
101
|
|
|
67
102
|
Object.entries(groupedByFile).forEach(([fileName, parsedLogics]) => {
|
|
68
103
|
const typeFileName = parsedLogics[0].typeFileName
|
|
@@ -88,8 +123,14 @@ export function printToFiles(
|
|
|
88
123
|
if (!relativePath.startsWith('.')) {
|
|
89
124
|
relativePath = `./${relativePath}`
|
|
90
125
|
}
|
|
91
|
-
return
|
|
126
|
+
return {
|
|
127
|
+
list: [...list].sort(),
|
|
128
|
+
fullPath: file,
|
|
129
|
+
relativePath,
|
|
130
|
+
}
|
|
92
131
|
})
|
|
132
|
+
.filter(({ fullPath }) => !shouldIgnore(fullPath))
|
|
133
|
+
.map(({ list, relativePath }) => `import { ${list.join(', ')} } from '${relativePath}'`)
|
|
93
134
|
.join('\n')
|
|
94
135
|
|
|
95
136
|
const finalOutput = [
|
|
@@ -129,19 +170,19 @@ export function printToFiles(
|
|
|
129
170
|
}
|
|
130
171
|
}
|
|
131
172
|
|
|
173
|
+
const parsedLogicNeedsTypeImport = (pl: ParsedLogic) =>
|
|
174
|
+
// reload if logic type not imported
|
|
175
|
+
(pl.logicTypeImported === false ||
|
|
176
|
+
// reload if don't have the right types in arguments
|
|
177
|
+
pl.logicTypeArguments.join(', ') !== [...pl.typeReferencesInLogicInput].sort().join(', ')) &&
|
|
178
|
+
pl.fileName.match(/\.tsx?$/)
|
|
179
|
+
|
|
132
180
|
// write the type into the logic itself
|
|
133
|
-
const logicsNeedingImports = parsedLogics.filter(
|
|
134
|
-
(pl) =>
|
|
135
|
-
// reload if logic type not imported
|
|
136
|
-
(pl.logicTypeImported === false ||
|
|
137
|
-
// reload if don't have the right types in arguments
|
|
138
|
-
pl.logicTypeArguments.join(', ') !== [...pl.typeReferencesInLogicInput].sort().join(', ')) &&
|
|
139
|
-
pl.fileName.match(/\.tsx?$/),
|
|
140
|
-
)
|
|
181
|
+
const logicsNeedingImports = parsedLogics.filter(parsedLogicNeedsTypeImport)
|
|
141
182
|
if (logicsNeedingImports.length > 0) {
|
|
142
183
|
if (appOptions.write && !appOptions.noImport) {
|
|
143
|
-
|
|
144
|
-
|
|
184
|
+
writeTypeImports(appOptions, program, fileName, logicsNeedingImports, parsedLogics)
|
|
185
|
+
filesToModify += logicsNeedingImports.length
|
|
145
186
|
} else {
|
|
146
187
|
log(
|
|
147
188
|
`❌ Will not write ${logicsNeedingImports.length} logic type import${
|
|
@@ -150,28 +191,43 @@ export function printToFiles(
|
|
|
150
191
|
)
|
|
151
192
|
}
|
|
152
193
|
}
|
|
194
|
+
|
|
195
|
+
const parsedLogicNeedsPath = appOptions.writePaths ? (pl: ParsedLogic) => !pl.hasPathInLogic : () => false
|
|
196
|
+
const logicsNeedingPaths = parsedLogics.filter(parsedLogicNeedsPath)
|
|
197
|
+
if (logicsNeedingPaths.length > 0) {
|
|
198
|
+
if (appOptions.write && !appOptions.noImport) {
|
|
199
|
+
writePaths(appOptions, program, fileName, logicsNeedingPaths)
|
|
200
|
+
filesToModify += logicsNeedingPaths.length
|
|
201
|
+
} else {
|
|
202
|
+
log(
|
|
203
|
+
`❌ Will not write ${logicsNeedingPaths.length} logic path${
|
|
204
|
+
logicsNeedingPaths.length === 1 ? '' : 's'
|
|
205
|
+
}`,
|
|
206
|
+
)
|
|
207
|
+
}
|
|
208
|
+
}
|
|
153
209
|
})
|
|
154
210
|
|
|
155
|
-
if (writtenFiles === 0 &&
|
|
211
|
+
if (writtenFiles === 0 && filesToModify === 0) {
|
|
156
212
|
if (appOptions.write) {
|
|
157
213
|
log(`💚 ${parsedLogics.length} logic type${parsedLogics.length === 1 ? '' : 's'} up to date!`)
|
|
158
214
|
log('')
|
|
159
|
-
} else if (filesToWrite > 0 ||
|
|
215
|
+
} else if (filesToWrite > 0 || filesToModify > 0) {
|
|
160
216
|
log(
|
|
161
|
-
`🚨 Run "kea-typegen write" to save ${filesToWrite +
|
|
217
|
+
`🚨 Run "kea-typegen write" to save ${filesToWrite + filesToModify} file${
|
|
162
218
|
filesToWrite === 1 ? '' : 's'
|
|
163
219
|
} to disk`,
|
|
164
220
|
)
|
|
165
221
|
}
|
|
166
222
|
}
|
|
167
223
|
|
|
168
|
-
return { filesToWrite, writtenFiles,
|
|
224
|
+
return { filesToWrite, writtenFiles, filesToModify }
|
|
169
225
|
}
|
|
170
226
|
|
|
171
|
-
export function nodeToString(node:
|
|
172
|
-
const printer =
|
|
173
|
-
const sourceFile =
|
|
174
|
-
return printer.printNode(
|
|
227
|
+
export function nodeToString(node: Node): string {
|
|
228
|
+
const printer = createPrinter({ newLine: NewLineKind.LineFeed })
|
|
229
|
+
const sourceFile = createSourceFile('logic.ts', '', ScriptTarget.Latest, false, ScriptKind.TS)
|
|
230
|
+
return printer.printNode(EmitHint.Unspecified, node, sourceFile)
|
|
175
231
|
}
|
|
176
232
|
|
|
177
233
|
export function parsedLogicToTypeString(parsedLogic: ParsedLogic, appOptions?: AppOptions): string {
|
|
@@ -179,15 +235,15 @@ export function parsedLogicToTypeString(parsedLogic: ParsedLogic, appOptions?: A
|
|
|
179
235
|
return nodeToString(parsedLogic.interfaceDeclaration)
|
|
180
236
|
}
|
|
181
237
|
|
|
182
|
-
export function getLogicTypeArguments(parsedLogic: ParsedLogic):
|
|
238
|
+
export function getLogicTypeArguments(parsedLogic: ParsedLogic): TypeParameterDeclaration[] {
|
|
183
239
|
return [...parsedLogic.typeReferencesInLogicInput]
|
|
184
240
|
.sort()
|
|
185
|
-
.map((text) =>
|
|
241
|
+
.map((text) => factory.createTypeParameterDeclaration(factory.createIdentifier(text), undefined))
|
|
186
242
|
}
|
|
187
243
|
|
|
188
244
|
export function printLogicType(parsedLogic: ParsedLogic, appOptions?: AppOptions): void {
|
|
189
245
|
const printProperty = (name, typeNode) =>
|
|
190
|
-
|
|
246
|
+
factory.createPropertySignature(undefined, factory.createIdentifier(name), undefined, typeNode)
|
|
191
247
|
|
|
192
248
|
const addSelectorTypeHelp = parsedLogic.selectors.filter((s) => s.functionTypes.length > 0).length > 0
|
|
193
249
|
|
|
@@ -203,9 +259,11 @@ export function printLogicType(parsedLogic: ParsedLogic, appOptions?: AppOptions
|
|
|
203
259
|
printProperty('listeners', printListeners(parsedLogic)),
|
|
204
260
|
printProperty(
|
|
205
261
|
'path',
|
|
206
|
-
|
|
262
|
+
factory.createTupleTypeNode(
|
|
263
|
+
parsedLogic.path.map((p) => factory.createLiteralTypeNode(factory.createStringLiteral(p))),
|
|
264
|
+
),
|
|
207
265
|
),
|
|
208
|
-
printProperty('pathString',
|
|
266
|
+
printProperty('pathString', factory.createStringLiteral(parsedLogic.pathString)),
|
|
209
267
|
printProperty('props', printProps(parsedLogic)),
|
|
210
268
|
printProperty('reducer', printReducer(parsedLogic)),
|
|
211
269
|
printProperty('reducerOptions', printReducerOptions(parsedLogic)),
|
|
@@ -214,8 +272,8 @@ export function printLogicType(parsedLogic: ParsedLogic, appOptions?: AppOptions
|
|
|
214
272
|
printProperty('selectors', printSelectors(parsedLogic)),
|
|
215
273
|
printProperty('sharedListeners', printSharedListeners(parsedLogic)),
|
|
216
274
|
printProperty('values', printValues(parsedLogic)),
|
|
217
|
-
printProperty('_isKea',
|
|
218
|
-
printProperty('_isKeaWithKey', parsedLogic.keyType ?
|
|
275
|
+
printProperty('_isKea', factory.createTrue()),
|
|
276
|
+
printProperty('_isKeaWithKey', parsedLogic.keyType ? factory.createTrue() : factory.createFalse()),
|
|
219
277
|
addSelectorTypeHelp
|
|
220
278
|
? printProperty('__keaTypeGenInternalSelectorTypes', printInternalSelectorTypes(parsedLogic))
|
|
221
279
|
: null,
|
|
@@ -229,14 +287,14 @@ export function printLogicType(parsedLogic: ParsedLogic, appOptions?: AppOptions
|
|
|
229
287
|
|
|
230
288
|
const logicTypeArguments = getLogicTypeArguments(parsedLogic)
|
|
231
289
|
|
|
232
|
-
parsedLogic.interfaceDeclaration =
|
|
290
|
+
parsedLogic.interfaceDeclaration = factory.createInterfaceDeclaration(
|
|
233
291
|
undefined,
|
|
234
|
-
[
|
|
235
|
-
|
|
292
|
+
[factory.createModifier(SyntaxKind.ExportKeyword)],
|
|
293
|
+
factory.createIdentifier(`${parsedLogic.logicName}Type`),
|
|
236
294
|
logicTypeArguments,
|
|
237
295
|
[
|
|
238
|
-
|
|
239
|
-
|
|
296
|
+
factory.createHeritageClause(SyntaxKind.ExtendsKeyword, [
|
|
297
|
+
factory.createExpressionWithTypeArguments(factory.createIdentifier('Logic'), undefined),
|
|
240
298
|
]),
|
|
241
299
|
],
|
|
242
300
|
logicProperties,
|
|
@@ -1,39 +1,36 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { factory } from 'typescript'
|
|
2
2
|
import { AppOptions, ParsedLogic } from '../types'
|
|
3
3
|
import { getActionTypeCreator } from '../utils'
|
|
4
4
|
|
|
5
5
|
export function printActionCreators(parsedLogic: ParsedLogic, appOptions?: AppOptions) {
|
|
6
6
|
const getActionType = getActionTypeCreator(parsedLogic)
|
|
7
7
|
|
|
8
|
-
return
|
|
8
|
+
return factory.createTypeLiteralNode(
|
|
9
9
|
parsedLogic.actions.map(({ name, parameters, returnTypeNode }) => {
|
|
10
|
-
return
|
|
10
|
+
return factory.createPropertySignature(
|
|
11
11
|
undefined,
|
|
12
|
-
|
|
12
|
+
factory.createIdentifier(name),
|
|
13
13
|
undefined,
|
|
14
|
-
|
|
14
|
+
factory.createFunctionTypeNode(
|
|
15
15
|
undefined,
|
|
16
16
|
parameters,
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
factory.createParenthesizedType(
|
|
18
|
+
factory.createTypeLiteralNode([
|
|
19
|
+
factory.createPropertySignature(
|
|
20
20
|
undefined,
|
|
21
|
-
|
|
22
|
-
undefined,
|
|
23
|
-
ts.createLiteralTypeNode(ts.createStringLiteral(getActionType(name))),
|
|
21
|
+
factory.createIdentifier('type'),
|
|
24
22
|
undefined,
|
|
23
|
+
factory.createLiteralTypeNode(factory.createStringLiteral(getActionType(name))),
|
|
25
24
|
),
|
|
26
|
-
|
|
25
|
+
factory.createPropertySignature(
|
|
27
26
|
undefined,
|
|
28
|
-
|
|
27
|
+
factory.createIdentifier('payload'),
|
|
29
28
|
undefined,
|
|
30
29
|
returnTypeNode,
|
|
31
|
-
undefined,
|
|
32
30
|
),
|
|
33
31
|
]),
|
|
34
32
|
),
|
|
35
33
|
),
|
|
36
|
-
undefined,
|
|
37
34
|
)
|
|
38
35
|
}),
|
|
39
36
|
)
|
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { factory } from 'typescript'
|
|
2
2
|
import { AppOptions, ParsedLogic } from '../types'
|
|
3
3
|
import { getActionTypeCreator } from '../utils'
|
|
4
4
|
|
|
5
5
|
export function printActionKeys(parsedLogic: ParsedLogic, appOptions?: AppOptions) {
|
|
6
6
|
const getActionType = getActionTypeCreator(parsedLogic)
|
|
7
7
|
|
|
8
|
-
return
|
|
8
|
+
return factory.createTypeLiteralNode(
|
|
9
9
|
parsedLogic.actions.map(({ name, parameters, returnTypeNode }) => {
|
|
10
|
-
return
|
|
10
|
+
return factory.createPropertySignature(
|
|
11
11
|
undefined,
|
|
12
|
-
|
|
13
|
-
undefined,
|
|
14
|
-
ts.createLiteralTypeNode(ts.createStringLiteral(name)),
|
|
12
|
+
factory.createStringLiteral(getActionType(name)),
|
|
15
13
|
undefined,
|
|
14
|
+
factory.createLiteralTypeNode(factory.createStringLiteral(name)),
|
|
16
15
|
)
|
|
17
16
|
}),
|
|
18
17
|
)
|
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { factory } from 'typescript'
|
|
2
2
|
import { AppOptions, ParsedLogic } from '../types'
|
|
3
3
|
import { getActionTypeCreator } from '../utils'
|
|
4
4
|
|
|
5
5
|
export function printActionTypes(parsedLogic: ParsedLogic, appOptions?: AppOptions) {
|
|
6
6
|
const getActionType = getActionTypeCreator(parsedLogic)
|
|
7
7
|
|
|
8
|
-
return
|
|
9
|
-
parsedLogic.actions.map(({ name
|
|
10
|
-
return
|
|
8
|
+
return factory.createTypeLiteralNode(
|
|
9
|
+
parsedLogic.actions.map(({ name }) => {
|
|
10
|
+
return factory.createPropertySignature(
|
|
11
11
|
undefined,
|
|
12
|
-
|
|
13
|
-
undefined,
|
|
14
|
-
ts.createLiteralTypeNode(ts.createStringLiteral(getActionType(name))),
|
|
12
|
+
factory.createIdentifier(name),
|
|
15
13
|
undefined,
|
|
14
|
+
factory.createLiteralTypeNode(factory.createStringLiteral(getActionType(name))),
|
|
16
15
|
)
|
|
17
16
|
}),
|
|
18
17
|
)
|
|
@@ -1,24 +1,17 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { factory, SyntaxKind } from 'typescript'
|
|
2
2
|
import { AppOptions, ParsedLogic } from '../types'
|
|
3
3
|
import { getActionTypeCreator } from '../utils'
|
|
4
4
|
|
|
5
5
|
export function printActions(parsedLogic: ParsedLogic, appOptions?: AppOptions) {
|
|
6
6
|
const getActionType = getActionTypeCreator(parsedLogic)
|
|
7
7
|
|
|
8
|
-
return
|
|
9
|
-
parsedLogic.actions.map(({ name, parameters
|
|
10
|
-
return
|
|
8
|
+
return factory.createTypeLiteralNode(
|
|
9
|
+
parsedLogic.actions.map(({ name, parameters }) => {
|
|
10
|
+
return factory.createPropertySignature(
|
|
11
11
|
undefined,
|
|
12
|
-
|
|
13
|
-
undefined,
|
|
14
|
-
ts.createFunctionTypeNode(
|
|
15
|
-
undefined,
|
|
16
|
-
parameters,
|
|
17
|
-
// Please someone tell me what must I do here?
|
|
18
|
-
// Just ts.createToken(void), as suggested by ts-ast-viewer.com doesn't work
|
|
19
|
-
{ ...ts.createToken(ts.SyntaxKind.VoidKeyword), _typeNodeBrand: true }
|
|
20
|
-
),
|
|
12
|
+
factory.createIdentifier(name),
|
|
21
13
|
undefined,
|
|
14
|
+
factory.createFunctionTypeNode(undefined, parameters, factory.createToken(SyntaxKind.VoidKeyword)),
|
|
22
15
|
)
|
|
23
16
|
}),
|
|
24
17
|
)
|
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { factory } from 'typescript'
|
|
2
2
|
import { ParsedLogic } from '../types'
|
|
3
3
|
|
|
4
4
|
export function printConstants(parsedLogic: ParsedLogic) {
|
|
5
|
-
return
|
|
5
|
+
return factory.createTypeLiteralNode(
|
|
6
6
|
parsedLogic.constants.map((constant) =>
|
|
7
|
-
|
|
7
|
+
factory.createPropertySignature(
|
|
8
8
|
undefined,
|
|
9
|
-
|
|
10
|
-
undefined,
|
|
11
|
-
ts.createLiteralTypeNode(ts.createStringLiteral(constant)),
|
|
9
|
+
factory.createStringLiteral(constant),
|
|
12
10
|
undefined,
|
|
11
|
+
factory.createLiteralTypeNode(factory.createStringLiteral(constant)),
|
|
13
12
|
),
|
|
14
13
|
),
|
|
15
14
|
)
|
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { factory } from 'typescript'
|
|
2
2
|
import { ParsedLogic } from '../types'
|
|
3
|
-
import {cleanDuplicateAnyNodes} from
|
|
3
|
+
import { cleanDuplicateAnyNodes } from '../utils'
|
|
4
4
|
|
|
5
5
|
export function printDefaults(parsedLogic: ParsedLogic) {
|
|
6
|
-
return
|
|
6
|
+
return factory.createTypeLiteralNode(
|
|
7
7
|
cleanDuplicateAnyNodes(parsedLogic.reducers).map((reducer) => {
|
|
8
|
-
return
|
|
8
|
+
return factory.createPropertySignature(
|
|
9
9
|
undefined,
|
|
10
|
-
|
|
10
|
+
factory.createIdentifier(reducer.name),
|
|
11
11
|
undefined,
|
|
12
12
|
reducer.typeNode,
|
|
13
|
-
undefined,
|
|
14
13
|
)
|
|
15
14
|
}),
|
|
16
15
|
)
|
package/src/print/printEvents.ts
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { factory, SyntaxKind } from 'typescript'
|
|
2
2
|
import { ParsedLogic } from '../types'
|
|
3
3
|
|
|
4
4
|
export function printEvents(parsedLogic: ParsedLogic) {
|
|
5
|
-
return
|
|
5
|
+
return factory.createTypeLiteralNode(
|
|
6
6
|
Object.keys(parsedLogic.events).map((name) => {
|
|
7
|
-
return
|
|
7
|
+
return factory.createPropertySignature(
|
|
8
8
|
undefined,
|
|
9
|
-
|
|
10
|
-
undefined,
|
|
11
|
-
ts.createFunctionTypeNode(undefined, [], { ...ts.createToken(ts.SyntaxKind.VoidKeyword), _typeNodeBrand: true }),
|
|
9
|
+
factory.createIdentifier(name),
|
|
12
10
|
undefined,
|
|
11
|
+
factory.createFunctionTypeNode(undefined, [], {
|
|
12
|
+
...factory.createToken(SyntaxKind.VoidKeyword),
|
|
13
|
+
_typeNodeBrand: true,
|
|
14
|
+
}),
|
|
13
15
|
)
|
|
14
16
|
}),
|
|
15
17
|
)
|
|
@@ -1,32 +1,32 @@
|
|
|
1
1
|
import { ParsedLogic } from '../types'
|
|
2
|
-
import
|
|
2
|
+
import { factory } from 'typescript'
|
|
3
3
|
|
|
4
4
|
export function printInternalExtraInput(parsedLogic: ParsedLogic) {
|
|
5
|
-
return
|
|
5
|
+
return factory.createTypeLiteralNode(
|
|
6
6
|
Object.entries(parsedLogic.extraInput).map(([type, { typeNode, withLogicFunction }]) => {
|
|
7
7
|
if (withLogicFunction) {
|
|
8
8
|
const logicTypeArguments = [...parsedLogic.typeReferencesInLogicInput]
|
|
9
9
|
.sort()
|
|
10
|
-
.map((text) =>
|
|
10
|
+
.map((text) => factory.createTypeReferenceNode(factory.createIdentifier(text), undefined))
|
|
11
11
|
|
|
12
|
-
return
|
|
12
|
+
return factory.createPropertySignature(
|
|
13
13
|
undefined,
|
|
14
|
-
|
|
14
|
+
factory.createStringLiteral(type),
|
|
15
15
|
undefined,
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
factory.createUnionTypeNode([
|
|
18
18
|
typeNode,
|
|
19
|
-
|
|
19
|
+
factory.createFunctionTypeNode(
|
|
20
20
|
undefined,
|
|
21
21
|
[
|
|
22
|
-
|
|
22
|
+
factory.createParameterDeclaration(
|
|
23
23
|
undefined,
|
|
24
24
|
undefined,
|
|
25
25
|
undefined,
|
|
26
|
-
|
|
26
|
+
factory.createIdentifier('logic'),
|
|
27
27
|
undefined,
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
factory.createTypeReferenceNode(
|
|
29
|
+
factory.createIdentifier(parsedLogic.logicTypeName),
|
|
30
30
|
logicTypeArguments.length > 0 ? logicTypeArguments : undefined,
|
|
31
31
|
),
|
|
32
32
|
undefined,
|
|
@@ -35,11 +35,10 @@ export function printInternalExtraInput(parsedLogic: ParsedLogic) {
|
|
|
35
35
|
typeNode,
|
|
36
36
|
),
|
|
37
37
|
]),
|
|
38
|
-
undefined,
|
|
39
38
|
)
|
|
40
39
|
}
|
|
41
40
|
|
|
42
|
-
return
|
|
41
|
+
return factory.createPropertySignature(undefined, factory.createStringLiteral(type), undefined, typeNode)
|
|
43
42
|
}),
|
|
44
43
|
)
|
|
45
44
|
}
|
|
@@ -1,17 +1,10 @@
|
|
|
1
1
|
import { ParsedLogic } from '../types'
|
|
2
|
-
import
|
|
2
|
+
import { factory } from 'typescript'
|
|
3
3
|
|
|
4
4
|
export function printInternalReducerActions(parsedLogic: ParsedLogic) {
|
|
5
|
-
return
|
|
6
|
-
Object.entries(parsedLogic.extraActions)
|
|
7
|
-
.
|
|
8
|
-
|
|
9
|
-
undefined,
|
|
10
|
-
ts.createStringLiteral(type),
|
|
11
|
-
undefined,
|
|
12
|
-
typeNode,
|
|
13
|
-
undefined,
|
|
14
|
-
)
|
|
15
|
-
}),
|
|
5
|
+
return factory.createTypeLiteralNode(
|
|
6
|
+
Object.entries(parsedLogic.extraActions).map(([type, typeNode]) => {
|
|
7
|
+
return factory.createPropertySignature(undefined, factory.createStringLiteral(type), undefined, typeNode)
|
|
8
|
+
}),
|
|
16
9
|
)
|
|
17
10
|
}
|
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
import { ParsedLogic } from '../types'
|
|
2
|
-
import
|
|
2
|
+
import { factory } from 'typescript'
|
|
3
3
|
|
|
4
4
|
export function printInternalSelectorTypes(parsedLogic: ParsedLogic) {
|
|
5
|
-
return
|
|
5
|
+
return factory.createTypeLiteralNode(
|
|
6
6
|
parsedLogic.selectors
|
|
7
7
|
.filter((s) => s.functionTypes.length > 0)
|
|
8
8
|
.map((selector) => {
|
|
9
|
-
return
|
|
9
|
+
return factory.createPropertySignature(
|
|
10
10
|
undefined,
|
|
11
|
-
|
|
11
|
+
factory.createIdentifier(selector.name),
|
|
12
12
|
undefined,
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
factory.createFunctionTypeNode(
|
|
15
15
|
undefined,
|
|
16
16
|
selector.functionTypes.map((functionType, index) =>
|
|
17
|
-
|
|
17
|
+
factory.createParameterDeclaration(
|
|
18
18
|
undefined,
|
|
19
19
|
undefined,
|
|
20
20
|
undefined,
|
|
21
|
-
|
|
21
|
+
factory.createIdentifier(functionType.name),
|
|
22
22
|
undefined,
|
|
23
23
|
functionType.type,
|
|
24
24
|
undefined,
|
|
@@ -26,7 +26,6 @@ export function printInternalSelectorTypes(parsedLogic: ParsedLogic) {
|
|
|
26
26
|
),
|
|
27
27
|
selector.typeNode,
|
|
28
28
|
),
|
|
29
|
-
undefined,
|
|
30
29
|
)
|
|
31
30
|
}),
|
|
32
31
|
)
|
package/src/print/printKey.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { factory, SyntaxKind } from 'typescript'
|
|
2
2
|
import { ParsedLogic } from '../types'
|
|
3
3
|
|
|
4
4
|
export function printKey(parsedLogic: ParsedLogic) {
|
|
5
|
-
return parsedLogic.keyType ||
|
|
5
|
+
return parsedLogic.keyType || factory.createKeywordTypeNode(SyntaxKind.UndefinedKeyword)
|
|
6
6
|
}
|
|
@@ -1,47 +1,46 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { factory, SyntaxKind } from 'typescript'
|
|
2
2
|
import { ParsedLogic } from '../types'
|
|
3
3
|
|
|
4
4
|
export function printListeners(parsedLogic: ParsedLogic) {
|
|
5
|
-
return
|
|
5
|
+
return factory.createTypeLiteralNode(
|
|
6
6
|
parsedLogic.listeners.map(({ name, payload, action }) =>
|
|
7
|
-
|
|
7
|
+
factory.createPropertySignature(
|
|
8
8
|
undefined,
|
|
9
|
-
|
|
9
|
+
factory.createStringLiteral(name),
|
|
10
10
|
undefined,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
factory.createArrayTypeNode(
|
|
12
|
+
factory.createParenthesizedType(
|
|
13
|
+
factory.createFunctionTypeNode(
|
|
14
14
|
undefined,
|
|
15
15
|
[
|
|
16
|
-
|
|
16
|
+
factory.createParameterDeclaration(
|
|
17
17
|
undefined,
|
|
18
18
|
undefined,
|
|
19
19
|
undefined,
|
|
20
|
-
|
|
20
|
+
factory.createIdentifier('action'),
|
|
21
21
|
undefined,
|
|
22
22
|
action,
|
|
23
23
|
undefined,
|
|
24
24
|
),
|
|
25
|
-
|
|
25
|
+
factory.createParameterDeclaration(
|
|
26
26
|
undefined,
|
|
27
27
|
undefined,
|
|
28
28
|
undefined,
|
|
29
|
-
|
|
29
|
+
factory.createIdentifier('previousState'),
|
|
30
30
|
undefined,
|
|
31
|
-
|
|
31
|
+
factory.createKeywordTypeNode(SyntaxKind.AnyKeyword),
|
|
32
32
|
undefined,
|
|
33
33
|
),
|
|
34
34
|
],
|
|
35
|
-
|
|
36
|
-
{ ...
|
|
37
|
-
|
|
38
|
-
{ ...
|
|
35
|
+
factory.createUnionTypeNode([
|
|
36
|
+
{ ...factory.createToken(SyntaxKind.VoidKeyword), _typeNodeBrand: true },
|
|
37
|
+
factory.createTypeReferenceNode(factory.createIdentifier('Promise'), [
|
|
38
|
+
{ ...factory.createToken(SyntaxKind.VoidKeyword), _typeNodeBrand: true },
|
|
39
39
|
]),
|
|
40
40
|
]),
|
|
41
41
|
),
|
|
42
42
|
),
|
|
43
43
|
),
|
|
44
|
-
undefined,
|
|
45
44
|
),
|
|
46
45
|
),
|
|
47
46
|
)
|