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.
Files changed (172) hide show
  1. package/CHANGELOG.md +77 -33
  2. package/dist/package.json +18 -17
  3. package/dist/src/__tests__/e2e/actions.js +4 -4
  4. package/dist/src/__tests__/e2e/actions.js.map +1 -1
  5. package/dist/src/__tests__/e2e/connect.js +6 -6
  6. package/dist/src/__tests__/e2e/connect.js.map +1 -1
  7. package/dist/src/__tests__/e2e/loaders.js +3 -3
  8. package/dist/src/__tests__/e2e/loaders.js.map +1 -1
  9. package/dist/src/__tests__/e2e/reducers.js +4 -4
  10. package/dist/src/__tests__/e2e/reducers.js.map +1 -1
  11. package/dist/src/__tests__/e2e/selectors.js +3 -3
  12. package/dist/src/__tests__/e2e/selectors.js.map +1 -1
  13. package/dist/src/__tests__/utils.js +7 -7
  14. package/dist/src/__tests__/utils.js.map +1 -1
  15. package/dist/src/cli/typegen.js +35 -9
  16. package/dist/src/cli/typegen.js.map +1 -1
  17. package/dist/src/print/print.d.ts +5 -5
  18. package/dist/src/print/print.js +68 -44
  19. package/dist/src/print/print.js.map +1 -1
  20. package/dist/src/print/printActionCreators.d.ts +1 -2
  21. package/dist/src/print/printActionCreators.js +7 -7
  22. package/dist/src/print/printActionCreators.js.map +1 -1
  23. package/dist/src/print/printActionKeys.d.ts +1 -2
  24. package/dist/src/print/printActionKeys.js +4 -4
  25. package/dist/src/print/printActionKeys.js.map +1 -1
  26. package/dist/src/print/printActionTypes.d.ts +1 -2
  27. package/dist/src/print/printActionTypes.js +4 -4
  28. package/dist/src/print/printActionTypes.js.map +1 -1
  29. package/dist/src/print/printActions.d.ts +1 -2
  30. package/dist/src/print/printActions.js +4 -4
  31. package/dist/src/print/printActions.js.map +1 -1
  32. package/dist/src/print/printConstants.d.ts +1 -2
  33. package/dist/src/print/printConstants.js +2 -2
  34. package/dist/src/print/printConstants.js.map +1 -1
  35. package/dist/src/print/printDefaults.d.ts +1 -2
  36. package/dist/src/print/printDefaults.js +3 -3
  37. package/dist/src/print/printDefaults.js.map +1 -1
  38. package/dist/src/print/printEvents.d.ts +1 -2
  39. package/dist/src/print/printEvents.js +3 -3
  40. package/dist/src/print/printEvents.js.map +1 -1
  41. package/dist/src/print/printInternalExtraInput.d.ts +1 -2
  42. package/dist/src/print/printInternalExtraInput.js +8 -8
  43. package/dist/src/print/printInternalExtraInput.js.map +1 -1
  44. package/dist/src/print/printInternalReducerActions.d.ts +1 -2
  45. package/dist/src/print/printInternalReducerActions.js +3 -4
  46. package/dist/src/print/printInternalReducerActions.js.map +1 -1
  47. package/dist/src/print/printInternalSelectorTypes.d.ts +1 -2
  48. package/dist/src/print/printInternalSelectorTypes.js +3 -3
  49. package/dist/src/print/printInternalSelectorTypes.js.map +1 -1
  50. package/dist/src/print/printKey.d.ts +1 -2
  51. package/dist/src/print/printKey.js +2 -2
  52. package/dist/src/print/printKey.js.map +1 -1
  53. package/dist/src/print/printListeners.d.ts +1 -2
  54. package/dist/src/print/printListeners.js +9 -9
  55. package/dist/src/print/printListeners.js.map +1 -1
  56. package/dist/src/print/printProps.d.ts +1 -2
  57. package/dist/src/print/printProps.js +6 -5
  58. package/dist/src/print/printProps.js.map +1 -1
  59. package/dist/src/print/printReducer.d.ts +1 -2
  60. package/dist/src/print/printReducer.js +6 -6
  61. package/dist/src/print/printReducer.js.map +1 -1
  62. package/dist/src/print/printReducerOptions.d.ts +1 -2
  63. package/dist/src/print/printReducerOptions.js +3 -3
  64. package/dist/src/print/printReducerOptions.js.map +1 -1
  65. package/dist/src/print/printReducers.d.ts +1 -2
  66. package/dist/src/print/printReducers.js +7 -7
  67. package/dist/src/print/printReducers.js.map +1 -1
  68. package/dist/src/print/printSelector.d.ts +1 -2
  69. package/dist/src/print/printSelector.js +4 -4
  70. package/dist/src/print/printSelector.js.map +1 -1
  71. package/dist/src/print/printSelectors.d.ts +1 -2
  72. package/dist/src/print/printSelectors.js +6 -6
  73. package/dist/src/print/printSelectors.js.map +1 -1
  74. package/dist/src/print/printSharedListeners.d.ts +1 -2
  75. package/dist/src/print/printSharedListeners.js +11 -11
  76. package/dist/src/print/printSharedListeners.js.map +1 -1
  77. package/dist/src/print/printValues.d.ts +1 -2
  78. package/dist/src/print/printValues.js +3 -3
  79. package/dist/src/print/printValues.js.map +1 -1
  80. package/dist/src/typegen.js +5 -5
  81. package/dist/src/typegen.js.map +1 -1
  82. package/dist/src/types.d.ts +6 -0
  83. package/dist/src/utils.js +21 -18
  84. package/dist/src/utils.js.map +1 -1
  85. package/dist/src/visit/__tests__/visit.js +2 -2
  86. package/dist/src/visit/__tests__/visit.js.map +1 -1
  87. package/dist/src/visit/visit.js +22 -14
  88. package/dist/src/visit/visit.js.map +1 -1
  89. package/dist/src/visit/visitActions.d.ts +2 -2
  90. package/dist/src/visit/visitActions.js +13 -11
  91. package/dist/src/visit/visitActions.js.map +1 -1
  92. package/dist/src/visit/visitConnect.js +4 -4
  93. package/dist/src/visit/visitConnect.js.map +1 -1
  94. package/dist/src/visit/visitDefaults.js +1 -1
  95. package/dist/src/visit/visitDefaults.js.map +1 -1
  96. package/dist/src/visit/visitKey.js +2 -2
  97. package/dist/src/visit/visitKey.js.map +1 -1
  98. package/dist/src/visit/visitListeners.js +5 -5
  99. package/dist/src/visit/visitListeners.js.map +1 -1
  100. package/dist/src/visit/visitLoaders.d.ts +2 -2
  101. package/dist/src/visit/visitLoaders.js +27 -27
  102. package/dist/src/visit/visitLoaders.js.map +1 -1
  103. package/dist/src/visit/visitProps.js +1 -1
  104. package/dist/src/visit/visitProps.js.map +1 -1
  105. package/dist/src/visit/visitReducers.js +4 -4
  106. package/dist/src/visit/visitReducers.js.map +1 -1
  107. package/dist/src/visit/visitSelectors.js +8 -8
  108. package/dist/src/visit/visitSelectors.js.map +1 -1
  109. package/dist/src/visit/visitSharedListeners.js +5 -5
  110. package/dist/src/visit/visitSharedListeners.js.map +1 -1
  111. package/dist/src/visit/visitWindowValues.js +1 -1
  112. package/dist/src/visit/visitWindowValues.js.map +1 -1
  113. package/dist/src/write/write.d.ts +4 -0
  114. package/dist/src/{import/writeLogicTypeImports.js → write/write.js} +56 -11
  115. package/dist/src/write/write.js.map +1 -0
  116. package/dist/tsconfig.tsbuildinfo +1 -1
  117. package/form-plugin/lib/form-plugin/src/typegen.js +16 -15
  118. package/form-plugin/lib/src/types.d.ts +4 -1
  119. package/form-plugin/package.json +1 -1
  120. package/form-plugin/src/typegen.ts +25 -25
  121. package/form-plugin/tsconfig.json +0 -1
  122. package/form-plugin/yarn.lock +10 -10
  123. package/package.json +18 -17
  124. package/samples/.kearc +3 -0
  125. package/samples/autoImportLogic.ts +12 -3
  126. package/samples/autoImportLogicType.ts +7 -2
  127. package/samples/complexLogic.ts +4 -3
  128. package/samples/donotimport.ts +1 -0
  129. package/samples/githubConnectLogic.ts +1 -0
  130. package/samples/githubImportLogic.ts +1 -0
  131. package/samples/githubLogic.ts +1 -0
  132. package/samples/loadersLogic.ts +1 -0
  133. package/samples/pluginLogic.ts +2 -0
  134. package/samples/propsLogic.ts +1 -0
  135. package/samples/propsLogicType.ts +9 -9
  136. package/samples/windowValuesLogic.ts +1 -0
  137. package/src/__tests__/utils.ts +5 -5
  138. package/src/cli/typegen.ts +33 -7
  139. package/src/print/print.ts +95 -37
  140. package/src/print/printActionCreators.ts +12 -15
  141. package/src/print/printActionKeys.ts +5 -6
  142. package/src/print/printActionTypes.ts +6 -7
  143. package/src/print/printActions.ts +6 -13
  144. package/src/print/printConstants.ts +5 -6
  145. package/src/print/printDefaults.ts +5 -6
  146. package/src/print/printEvents.ts +8 -6
  147. package/src/print/printInternalExtraInput.ts +12 -13
  148. package/src/print/printInternalReducerActions.ts +5 -12
  149. package/src/print/printInternalSelectorTypes.ts +7 -8
  150. package/src/print/printKey.ts +2 -2
  151. package/src/print/printListeners.ts +16 -17
  152. package/src/print/printProps.ts +7 -7
  153. package/src/print/printReducer.ts +14 -15
  154. package/src/print/printReducerOptions.ts +4 -5
  155. package/src/print/printReducers.ts +13 -14
  156. package/src/print/printSelector.ts +8 -9
  157. package/src/print/printSelectors.ts +12 -13
  158. package/src/print/printSharedListeners.ts +20 -21
  159. package/src/print/printValues.ts +5 -6
  160. package/src/typegen.ts +4 -4
  161. package/src/types.ts +11 -1
  162. package/src/utils.ts +17 -17
  163. package/src/visit/visit.ts +11 -3
  164. package/src/visit/visitActions.ts +12 -10
  165. package/src/visit/visitListeners.ts +8 -8
  166. package/src/visit/visitLoaders.ts +49 -43
  167. package/src/visit/visitReducers.ts +1 -1
  168. package/src/visit/visitSelectors.ts +3 -3
  169. package/src/visit/visitSharedListeners.ts +8 -10
  170. package/src/{import/writeLogicTypeImports.ts → write/write.ts} +115 -16
  171. package/dist/src/import/writeLogicTypeImports.d.ts +0 -3
  172. package/dist/src/import/writeLogicTypeImports.js.map +0 -1
@@ -1,4 +1,16 @@
1
- import * as ts from 'typescript'
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 { writeLogicTypeImports } from '../import/writeLogicTypeImports'
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: ts.Program,
58
+ program: Program,
47
59
  appOptions: AppOptions,
48
60
  parsedLogics: ParsedLogic[],
49
- ): { filesToWrite: number; writtenFiles: number; importsToModify: 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 ts.Nodes and gather referenced types
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 importsToModify = 0
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 `import { ${[...list].sort().join(', ')} } from '${relativePath}'`
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
- writeLogicTypeImports(appOptions, program, fileName, logicsNeedingImports, parsedLogics)
144
- importsToModify = logicsNeedingImports.length
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 && importsToModify === 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 || importsToModify > 0) {
215
+ } else if (filesToWrite > 0 || filesToModify > 0) {
160
216
  log(
161
- `🚨 Run "kea-typegen write" to save ${filesToWrite + importsToModify} file${
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, importsToModify }
224
+ return { filesToWrite, writtenFiles, filesToModify }
169
225
  }
170
226
 
171
- export function nodeToString(node: ts.Node): string {
172
- const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed })
173
- const sourceFile = ts.createSourceFile('logic.ts', '', ts.ScriptTarget.Latest, false, ts.ScriptKind.TS)
174
- return printer.printNode(ts.EmitHint.Unspecified, node, sourceFile)
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): ts.TypeParameterDeclaration[] {
238
+ export function getLogicTypeArguments(parsedLogic: ParsedLogic): TypeParameterDeclaration[] {
183
239
  return [...parsedLogic.typeReferencesInLogicInput]
184
240
  .sort()
185
- .map((text) => ts.createTypeParameterDeclaration(ts.createIdentifier(text), undefined))
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
- ts.createPropertySignature(undefined, ts.createIdentifier(name), undefined, typeNode, undefined)
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
- ts.createTupleTypeNode(parsedLogic.path.map((p) => ts.createLiteralTypeNode(ts.createStringLiteral(p)))),
262
+ factory.createTupleTypeNode(
263
+ parsedLogic.path.map((p) => factory.createLiteralTypeNode(factory.createStringLiteral(p))),
264
+ ),
207
265
  ),
208
- printProperty('pathString', ts.createStringLiteral(parsedLogic.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', ts.createTrue()),
218
- printProperty('_isKeaWithKey', parsedLogic.keyType ? ts.createTrue() : ts.createFalse()),
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 = ts.createInterfaceDeclaration(
290
+ parsedLogic.interfaceDeclaration = factory.createInterfaceDeclaration(
233
291
  undefined,
234
- [ts.createModifier(ts.SyntaxKind.ExportKeyword)],
235
- ts.createIdentifier(`${parsedLogic.logicName}Type`),
292
+ [factory.createModifier(SyntaxKind.ExportKeyword)],
293
+ factory.createIdentifier(`${parsedLogic.logicName}Type`),
236
294
  logicTypeArguments,
237
295
  [
238
- ts.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [
239
- ts.createExpressionWithTypeArguments(undefined, ts.createIdentifier('Logic')),
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 * as ts from 'typescript'
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 ts.createTypeLiteralNode(
8
+ return factory.createTypeLiteralNode(
9
9
  parsedLogic.actions.map(({ name, parameters, returnTypeNode }) => {
10
- return ts.createPropertySignature(
10
+ return factory.createPropertySignature(
11
11
  undefined,
12
- ts.createIdentifier(name),
12
+ factory.createIdentifier(name),
13
13
  undefined,
14
- ts.createFunctionTypeNode(
14
+ factory.createFunctionTypeNode(
15
15
  undefined,
16
16
  parameters,
17
- ts.createParenthesizedType(
18
- ts.createTypeLiteralNode([
19
- ts.createPropertySignature(
17
+ factory.createParenthesizedType(
18
+ factory.createTypeLiteralNode([
19
+ factory.createPropertySignature(
20
20
  undefined,
21
- ts.createIdentifier('type'),
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
- ts.createPropertySignature(
25
+ factory.createPropertySignature(
27
26
  undefined,
28
- ts.createIdentifier('payload'),
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 * as ts from 'typescript'
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 ts.createTypeLiteralNode(
8
+ return factory.createTypeLiteralNode(
9
9
  parsedLogic.actions.map(({ name, parameters, returnTypeNode }) => {
10
- return ts.createPropertySignature(
10
+ return factory.createPropertySignature(
11
11
  undefined,
12
- ts.createStringLiteral(getActionType(name)),
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 * as ts from 'typescript'
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 ts.createTypeLiteralNode(
9
- parsedLogic.actions.map(({ name, parameters, returnTypeNode }) => {
10
- return ts.createPropertySignature(
8
+ return factory.createTypeLiteralNode(
9
+ parsedLogic.actions.map(({ name }) => {
10
+ return factory.createPropertySignature(
11
11
  undefined,
12
- ts.createIdentifier(name),
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 * as ts from 'typescript'
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 ts.createTypeLiteralNode(
9
- parsedLogic.actions.map(({ name, parameters, returnTypeNode }) => {
10
- return ts.createPropertySignature(
8
+ return factory.createTypeLiteralNode(
9
+ parsedLogic.actions.map(({ name, parameters }) => {
10
+ return factory.createPropertySignature(
11
11
  undefined,
12
- ts.createIdentifier(name),
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 * as ts from 'typescript'
1
+ import { factory } from 'typescript'
2
2
  import { ParsedLogic } from '../types'
3
3
 
4
4
  export function printConstants(parsedLogic: ParsedLogic) {
5
- return ts.createTypeLiteralNode(
5
+ return factory.createTypeLiteralNode(
6
6
  parsedLogic.constants.map((constant) =>
7
- ts.createPropertySignature(
7
+ factory.createPropertySignature(
8
8
  undefined,
9
- ts.createStringLiteral(constant),
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 * as ts from 'typescript'
1
+ import { factory } from 'typescript'
2
2
  import { ParsedLogic } from '../types'
3
- import {cleanDuplicateAnyNodes} from "../utils";
3
+ import { cleanDuplicateAnyNodes } from '../utils'
4
4
 
5
5
  export function printDefaults(parsedLogic: ParsedLogic) {
6
- return ts.createTypeLiteralNode(
6
+ return factory.createTypeLiteralNode(
7
7
  cleanDuplicateAnyNodes(parsedLogic.reducers).map((reducer) => {
8
- return ts.createPropertySignature(
8
+ return factory.createPropertySignature(
9
9
  undefined,
10
- ts.createIdentifier(reducer.name),
10
+ factory.createIdentifier(reducer.name),
11
11
  undefined,
12
12
  reducer.typeNode,
13
- undefined,
14
13
  )
15
14
  }),
16
15
  )
@@ -1,15 +1,17 @@
1
- import * as ts from 'typescript'
1
+ import { factory, SyntaxKind } from 'typescript'
2
2
  import { ParsedLogic } from '../types'
3
3
 
4
4
  export function printEvents(parsedLogic: ParsedLogic) {
5
- return ts.createTypeLiteralNode(
5
+ return factory.createTypeLiteralNode(
6
6
  Object.keys(parsedLogic.events).map((name) => {
7
- return ts.createPropertySignature(
7
+ return factory.createPropertySignature(
8
8
  undefined,
9
- ts.createIdentifier(name),
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 * as ts from 'typescript'
2
+ import { factory } from 'typescript'
3
3
 
4
4
  export function printInternalExtraInput(parsedLogic: ParsedLogic) {
5
- return ts.createTypeLiteralNode(
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) => ts.createTypeReferenceNode(ts.createIdentifier(text), undefined))
10
+ .map((text) => factory.createTypeReferenceNode(factory.createIdentifier(text), undefined))
11
11
 
12
- return ts.createPropertySignature(
12
+ return factory.createPropertySignature(
13
13
  undefined,
14
- ts.createStringLiteral(type),
14
+ factory.createStringLiteral(type),
15
15
  undefined,
16
16
 
17
- ts.createUnionTypeNode([
17
+ factory.createUnionTypeNode([
18
18
  typeNode,
19
- ts.createFunctionTypeNode(
19
+ factory.createFunctionTypeNode(
20
20
  undefined,
21
21
  [
22
- ts.createParameter(
22
+ factory.createParameterDeclaration(
23
23
  undefined,
24
24
  undefined,
25
25
  undefined,
26
- ts.createIdentifier('logic'),
26
+ factory.createIdentifier('logic'),
27
27
  undefined,
28
- ts.createTypeReferenceNode(
29
- ts.createIdentifier(parsedLogic.logicTypeName),
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 ts.createPropertySignature(undefined, ts.createStringLiteral(type), undefined, typeNode, undefined)
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 * as ts from 'typescript'
2
+ import { factory } from 'typescript'
3
3
 
4
4
  export function printInternalReducerActions(parsedLogic: ParsedLogic) {
5
- return ts.createTypeLiteralNode(
6
- Object.entries(parsedLogic.extraActions)
7
- .map(([type, typeNode]) => {
8
- return ts.createPropertySignature(
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 * as ts from 'typescript'
2
+ import { factory } from 'typescript'
3
3
 
4
4
  export function printInternalSelectorTypes(parsedLogic: ParsedLogic) {
5
- return ts.createTypeLiteralNode(
5
+ return factory.createTypeLiteralNode(
6
6
  parsedLogic.selectors
7
7
  .filter((s) => s.functionTypes.length > 0)
8
8
  .map((selector) => {
9
- return ts.createPropertySignature(
9
+ return factory.createPropertySignature(
10
10
  undefined,
11
- ts.createIdentifier(selector.name),
11
+ factory.createIdentifier(selector.name),
12
12
  undefined,
13
13
 
14
- ts.createFunctionTypeNode(
14
+ factory.createFunctionTypeNode(
15
15
  undefined,
16
16
  selector.functionTypes.map((functionType, index) =>
17
- ts.createParameter(
17
+ factory.createParameterDeclaration(
18
18
  undefined,
19
19
  undefined,
20
20
  undefined,
21
- ts.createIdentifier(functionType.name),
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
  )
@@ -1,6 +1,6 @@
1
- import * as ts from 'typescript'
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 || ts.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword)
5
+ return parsedLogic.keyType || factory.createKeywordTypeNode(SyntaxKind.UndefinedKeyword)
6
6
  }
@@ -1,47 +1,46 @@
1
- import * as ts from 'typescript'
1
+ import { factory, SyntaxKind } from 'typescript'
2
2
  import { ParsedLogic } from '../types'
3
3
 
4
4
  export function printListeners(parsedLogic: ParsedLogic) {
5
- return ts.createTypeLiteralNode(
5
+ return factory.createTypeLiteralNode(
6
6
  parsedLogic.listeners.map(({ name, payload, action }) =>
7
- ts.createPropertySignature(
7
+ factory.createPropertySignature(
8
8
  undefined,
9
- ts.createStringLiteral(name),
9
+ factory.createStringLiteral(name),
10
10
  undefined,
11
- ts.createArrayTypeNode(
12
- ts.createParenthesizedType(
13
- ts.createFunctionTypeNode(
11
+ factory.createArrayTypeNode(
12
+ factory.createParenthesizedType(
13
+ factory.createFunctionTypeNode(
14
14
  undefined,
15
15
  [
16
- ts.createParameter(
16
+ factory.createParameterDeclaration(
17
17
  undefined,
18
18
  undefined,
19
19
  undefined,
20
- ts.createIdentifier('action'),
20
+ factory.createIdentifier('action'),
21
21
  undefined,
22
22
  action,
23
23
  undefined,
24
24
  ),
25
- ts.createParameter(
25
+ factory.createParameterDeclaration(
26
26
  undefined,
27
27
  undefined,
28
28
  undefined,
29
- ts.createIdentifier('previousState'),
29
+ factory.createIdentifier('previousState'),
30
30
  undefined,
31
- ts.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword),
31
+ factory.createKeywordTypeNode(SyntaxKind.AnyKeyword),
32
32
  undefined,
33
33
  ),
34
34
  ],
35
- ts.createUnionTypeNode([
36
- { ...ts.createToken(ts.SyntaxKind.VoidKeyword), _typeNodeBrand: true },
37
- ts.createTypeReferenceNode(ts.createIdentifier('Promise'), [
38
- { ...ts.createToken(ts.SyntaxKind.VoidKeyword), _typeNodeBrand: true },
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
  )