kea-typegen 3.0.0 → 3.1.0

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 (43) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/package.json +1 -1
  3. package/dist/src/print/print.d.ts +1 -2
  4. package/dist/src/print/print.js +5 -12
  5. package/dist/src/print/print.js.map +1 -1
  6. package/dist/src/print/printInternalExtraInput.js +1 -4
  7. package/dist/src/print/printInternalExtraInput.js.map +1 -1
  8. package/dist/src/types.d.ts +0 -1
  9. package/dist/src/utils.d.ts +1 -2
  10. package/dist/src/utils.js +29 -41
  11. package/dist/src/utils.js.map +1 -1
  12. package/dist/src/visit/visit.js +1 -2
  13. package/dist/src/visit/visit.js.map +1 -1
  14. package/dist/src/write/writeTypeImports.d.ts +2 -2
  15. package/dist/src/write/writeTypeImports.js +1 -4
  16. package/dist/src/write/writeTypeImports.js.map +1 -1
  17. package/dist/tsconfig.tsbuildinfo +1 -1
  18. package/package.json +1 -1
  19. package/samples/autoImportLogic.ts +4 -4
  20. package/samples/autoImportLogicType.ts +7 -7
  21. package/samples/builderLogicType.ts +3 -3
  22. package/samples/complexLogic.ts +7 -9
  23. package/samples/complexLogicType.ts +5 -6
  24. package/samples/githubConnectLogicType.ts +3 -3
  25. package/samples/githubImportLogicType.ts +3 -3
  26. package/samples/githubLogicType.ts +3 -3
  27. package/samples/githubType.ts +2 -2
  28. package/samples/loadersLogicType.ts +3 -3
  29. package/samples/logic.ts +3 -3
  30. package/samples/logicType.ts +5 -3
  31. package/samples/pluginLogicType.ts +2 -2
  32. package/samples/propsLogic.ts +1 -1
  33. package/samples/propsLogicType.ts +2 -2
  34. package/samples/routerConnectLogicType.ts +3 -3
  35. package/samples/typed-builder/typedFormDemoLogicType.ts +2 -2
  36. package/samples/windowValuesLogicType.ts +2 -2
  37. package/src/__tests__/e2e/__snapshots__/loaders.ts.snap +2 -2
  38. package/src/print/print.ts +4 -12
  39. package/src/print/printInternalExtraInput.ts +1 -5
  40. package/src/types.ts +0 -1
  41. package/src/utils.ts +31 -43
  42. package/src/visit/visit.ts +2 -3
  43. package/src/write/writeTypeImports.ts +9 -18
package/src/utils.ts CHANGED
@@ -4,7 +4,7 @@ import { cloneNode } from '@wessberg/ts-clone-node'
4
4
  import { visitProgram } from './visit/visit'
5
5
  import { parsedLogicToTypeString } from './print/print'
6
6
  import { AppOptions, NameType, ParsedLogic } from './types'
7
- import { factory, NodeBuilderFlags, SyntaxKind } from 'typescript'
7
+ import { factory, isSourceFile, NodeBuilderFlags, SyntaxKind } from 'typescript'
8
8
 
9
9
  export function logicSourceToLogicType(logicSource: string, appOptions?: AppOptions) {
10
10
  const program = programFromSource(logicSource)
@@ -23,6 +23,13 @@ export function programFromSource(sourceCode: string) {
23
23
  return ts.createProgram(['logic.ts'], options, compilerHost)
24
24
  }
25
25
 
26
+ function rejectImportPath(path: string): boolean {
27
+ if (path.includes('/node_modules/typescript/')) {
28
+ return true
29
+ }
30
+ return false
31
+ }
32
+
26
33
  export function isKeaCall(node: ts.Node, checker: ts.TypeChecker) {
27
34
  if (!ts.isIdentifier(node)) {
28
35
  return false
@@ -216,9 +223,7 @@ export function getLogicPathString(appOptions: AppOptions, fileName: string) {
216
223
  }
217
224
 
218
225
  export function getFilenamesForSymbol(symbol: ts.Symbol): string[] | undefined {
219
- return (symbol?.declarations || [])
220
- .map((d) => d.getSourceFile().fileName)
221
- .filter((str) => !str.includes('/node_modules/typescript/lib/lib'))
226
+ return (symbol?.declarations || []).map((d) => d.getSourceFile().fileName).filter((f) => !rejectImportPath(f))
222
227
  }
223
228
 
224
229
  /** gathers onto parsedLogic the TypeReference nodes that are declared in a different sourceFile */
@@ -237,7 +242,6 @@ export function gatherImports(input: ts.Node, checker: ts.TypeChecker, parsedLog
237
242
  typeRootName = (node.typeName.left as any)?.escapedText
238
243
  }
239
244
  }
240
-
241
245
  const symbol = checker.getSymbolAtLocation(node.typeName) || (node.typeName as any).symbol
242
246
  if (symbol) {
243
247
  storeExtractedSymbol(symbol, checker, parsedLogic, typeRootName)
@@ -255,32 +259,23 @@ export function storeExtractedSymbol(
255
259
  typeRootName?: string,
256
260
  ) {
257
261
  const declaration = symbol.getDeclarations()?.[0]
262
+ let typeName = typeRootName
258
263
 
259
- if (declaration && ts.isImportSpecifier(declaration)) {
260
- const importFilename = getFilenameForImportSpecifier(declaration, checker)
261
- if (importFilename) {
262
- addTypeImport(parsedLogic, importFilename, typeRootName || declaration.getText())
263
- } else {
264
- parsedLogic.typeReferencesInLogicInput.add(typeRootName || declaration.getText())
265
- }
266
- return
267
- }
268
-
269
- const files = getFilenamesForSymbol(symbol)
270
- if (files[0]) {
271
- // same file, add to logicType<...>
272
- if (
264
+ if (
265
+ declaration &&
266
+ (ts.isImportSpecifier(declaration) ||
273
267
  ts.isTypeAliasDeclaration(declaration) ||
274
268
  ts.isInterfaceDeclaration(declaration) ||
275
269
  ts.isEnumDeclaration(declaration) ||
276
- ts.isClassDeclaration(declaration)
277
- ) {
278
- if (files[0] === parsedLogic.fileName) {
279
- parsedLogic.typeReferencesInLogicInput.add(typeRootName || declaration.name.getText())
280
- } else {
281
- // but is it exported?
282
- addTypeImport(parsedLogic, files[0], typeRootName || declaration.name.getText())
283
- }
270
+ ts.isClassDeclaration(declaration))
271
+ ) {
272
+ typeName = typeName || declaration.name.getText()
273
+ }
274
+
275
+ if (typeName) {
276
+ const importFilename = getFilenameForNode(declaration, checker)
277
+ if (importFilename && !rejectImportPath(importFilename)) {
278
+ addTypeImport(parsedLogic, importFilename, typeName)
284
279
  }
285
280
  }
286
281
  }
@@ -293,14 +288,17 @@ export function getFilenameForImportDeclaration(checker: ts.TypeChecker, importN
293
288
  }
294
289
  }
295
290
 
296
- export function getFilenameForImportSpecifier(declaration: ts.ImportSpecifier, checker: ts.TypeChecker): string | void {
297
- let importNode: ts.Node = declaration
298
- while (importNode && !ts.isImportDeclaration(importNode)) {
291
+ export function getFilenameForNode(node: ts.Node, checker: ts.TypeChecker): string | void {
292
+ let importNode: ts.Node = node
293
+ while (importNode) {
294
+ if (ts.isImportDeclaration(importNode)) {
295
+ return getFilenameForImportDeclaration(checker, importNode)
296
+ }
297
+ if (isSourceFile(importNode)) {
298
+ return importNode.fileName
299
+ }
299
300
  importNode = importNode.parent
300
301
  }
301
- if (ts.isImportDeclaration(importNode)) {
302
- return getFilenameForImportDeclaration(checker, importNode)
303
- }
304
302
  }
305
303
 
306
304
  function addTypeImport(parsedLogic: ParsedLogic, file: string, typeName: string) {
@@ -310,16 +308,6 @@ function addTypeImport(parsedLogic: ParsedLogic, file: string, typeName: string)
310
308
  parsedLogic.typeReferencesToImportFromFiles[file].add(typeName.split('.')[0])
311
309
  }
312
310
 
313
- export function arrayContainsSet(array: string[], setToContain: Set<string>): boolean {
314
- const arraySet = new Set(array)
315
- for (const str of setToContain) {
316
- if (!arraySet.has(str)) {
317
- return false
318
- }
319
- }
320
- return true
321
- }
322
-
323
311
  export function unPromisify(node: ts.Node): ts.Node {
324
312
  if (ts.isTypeReferenceNode(node) && (node.typeName as any)?.escapedText === 'Promise') {
325
313
  return node.typeArguments?.[0]
@@ -4,7 +4,7 @@ import { AppOptions, ParsedLogic, PluginModule, TypeBuilderModule, VisitKeaPrope
4
4
  import {
5
5
  gatherImports,
6
6
  getFilenameForImportDeclaration,
7
- getFilenameForImportSpecifier,
7
+ getFilenameForNode,
8
8
  getLogicPathString,
9
9
  getTypeNodeForNode,
10
10
  isKeaCall,
@@ -185,7 +185,7 @@ export function visitKeaCalls(
185
185
  const declaration = symbol.getDeclarations()?.[0]
186
186
 
187
187
  if (declaration && ts.isImportSpecifier(declaration)) {
188
- const filename = getFilenameForImportSpecifier(declaration, checker)
188
+ const filename = getFilenameForNode(declaration, checker)
189
189
  logicTypeImported = filename === typeFileName
190
190
  }
191
191
  }
@@ -221,7 +221,6 @@ export function visitKeaCalls(
221
221
  hasKeyInLogic: false,
222
222
  hasPathInLogic: false,
223
223
  typeReferencesToImportFromFiles: {},
224
- typeReferencesInLogicInput: new Set(),
225
224
  extraInput: {},
226
225
  importFromKeaInLogicType: new Set([]),
227
226
  inputBuilderArray: ts.isArrayLiteralExpression(input),
@@ -1,10 +1,10 @@
1
- import { AppOptions, ParsedLogic } from "../types";
2
- import * as ts from "typescript";
3
- import { print, visit } from "recast";
4
- import * as osPath from "path";
5
- import { runThroughPrettier } from "../print/print";
6
- import * as fs from "fs";
7
- import { t, b, visitAllKeaCalls, getAst } from "./utils";
1
+ import { AppOptions, ParsedLogic } from '../types'
2
+ import * as ts from 'typescript'
3
+ import { print, visit } from 'recast'
4
+ import * as osPath from 'path'
5
+ import { runThroughPrettier } from '../print/print'
6
+ import * as fs from 'fs'
7
+ import { t, b, visitAllKeaCalls, getAst } from './utils'
8
8
 
9
9
  export function writeTypeImports(
10
10
  appOptions: AppOptions,
@@ -68,19 +68,10 @@ export function writeTypeImports(
68
68
  })
69
69
  }
70
70
 
71
- // find all kea calls, add `<logicType<a,b>>` type parameters if needed
71
+ // find all kea calls, add `<logicType>` type parameters if needed
72
72
  visitAllKeaCalls(ast, logicsNeedingImports, filename, ({ path, parsedLogic }) => {
73
- const { logicTypeName, typeReferencesInLogicInput } = parsedLogic
74
-
75
73
  path.node.typeParameters = b.tsTypeParameterInstantiation([
76
- b.tsTypeReference(
77
- b.identifier(logicTypeName),
78
- typeReferencesInLogicInput.size > 0 ? b.tsTypeParameterInstantiation(
79
- [...typeReferencesInLogicInput.values()]
80
- .sort()
81
- .map((type) => b.tsTypeReference(b.identifier(type))),
82
- ) : null,
83
- ),
74
+ b.tsTypeReference(b.identifier(parsedLogic.logicTypeName)),
84
75
  ])
85
76
  })
86
77