kea-typegen 1.2.1 → 1.4.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 +75 -32
- 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 +70 -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 -29
- 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/loadersLogicType.ts +9 -9
- package/samples/logicType.ts +4 -4
- 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__/e2e/__snapshots__/loaders.ts.snap +10 -10
- package/src/__tests__/utils.ts +5 -5
- package/src/cli/typegen.ts +33 -7
- package/src/print/print.ts +97 -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 -45
- 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
|
@@ -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
|
)
|
package/src/print/printProps.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { factory, SyntaxKind } from 'typescript'
|
|
2
2
|
import { ParsedLogic } from '../types'
|
|
3
3
|
|
|
4
4
|
export function printProps(parsedLogic: ParsedLogic) {
|
|
5
|
-
return
|
|
6
|
-
|
|
7
|
-
[
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
]
|
|
5
|
+
return (
|
|
6
|
+
parsedLogic.propsType ||
|
|
7
|
+
factory.createTypeReferenceNode(factory.createIdentifier('Record'), [
|
|
8
|
+
factory.createKeywordTypeNode(SyntaxKind.StringKeyword),
|
|
9
|
+
factory.createKeywordTypeNode(SyntaxKind.UnknownKeyword),
|
|
10
|
+
])
|
|
11
11
|
)
|
|
12
12
|
}
|
|
@@ -1,47 +1,46 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { factory, SyntaxKind } from 'typescript'
|
|
2
2
|
import { ParsedLogic } from '../types'
|
|
3
3
|
import { cleanDuplicateAnyNodes } from '../utils'
|
|
4
4
|
|
|
5
5
|
export function printReducer(parsedLogic: ParsedLogic) {
|
|
6
|
-
return
|
|
6
|
+
return factory.createFunctionTypeNode(
|
|
7
7
|
undefined,
|
|
8
8
|
[
|
|
9
|
-
|
|
9
|
+
factory.createParameterDeclaration(
|
|
10
10
|
undefined,
|
|
11
11
|
undefined,
|
|
12
12
|
undefined,
|
|
13
|
-
|
|
13
|
+
factory.createIdentifier('state'),
|
|
14
14
|
undefined,
|
|
15
|
-
|
|
15
|
+
factory.createKeywordTypeNode(SyntaxKind.AnyKeyword),
|
|
16
16
|
undefined,
|
|
17
17
|
),
|
|
18
|
-
|
|
18
|
+
factory.createParameterDeclaration(
|
|
19
19
|
undefined,
|
|
20
20
|
undefined,
|
|
21
21
|
undefined,
|
|
22
|
-
|
|
22
|
+
factory.createIdentifier('action'),
|
|
23
23
|
undefined,
|
|
24
|
-
|
|
24
|
+
factory.createFunctionTypeNode(undefined, [], factory.createKeywordTypeNode(SyntaxKind.AnyKeyword)),
|
|
25
25
|
undefined,
|
|
26
26
|
),
|
|
27
|
-
|
|
27
|
+
factory.createParameterDeclaration(
|
|
28
28
|
undefined,
|
|
29
29
|
undefined,
|
|
30
30
|
undefined,
|
|
31
|
-
|
|
31
|
+
factory.createIdentifier('fullState'),
|
|
32
32
|
undefined,
|
|
33
|
-
|
|
33
|
+
factory.createKeywordTypeNode(SyntaxKind.AnyKeyword),
|
|
34
34
|
undefined,
|
|
35
35
|
),
|
|
36
36
|
],
|
|
37
|
-
|
|
37
|
+
factory.createTypeLiteralNode(
|
|
38
38
|
cleanDuplicateAnyNodes(parsedLogic.reducers).map((reducer) =>
|
|
39
|
-
|
|
39
|
+
factory.createPropertySignature(
|
|
40
40
|
undefined,
|
|
41
|
-
|
|
41
|
+
factory.createIdentifier(reducer.name),
|
|
42
42
|
undefined,
|
|
43
43
|
reducer.typeNode,
|
|
44
|
-
undefined,
|
|
45
44
|
),
|
|
46
45
|
),
|
|
47
46
|
),
|
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { factory } from 'typescript'
|
|
2
2
|
import { ParsedLogic, ReducerTransform } from '../types'
|
|
3
3
|
import { cleanDuplicateAnyNodes } from '../utils'
|
|
4
4
|
|
|
5
5
|
export function printReducerOptions(parsedLogic: ParsedLogic) {
|
|
6
|
-
return
|
|
6
|
+
return factory.createTypeLiteralNode(
|
|
7
7
|
cleanDuplicateAnyNodes(parsedLogic.reducers)
|
|
8
8
|
.filter((r: ReducerTransform) => r.reducerOptions)
|
|
9
9
|
.map((reducer: ReducerTransform) => {
|
|
10
10
|
if (reducer.reducerOptions) {
|
|
11
|
-
return
|
|
11
|
+
return factory.createPropertySignature(
|
|
12
12
|
undefined,
|
|
13
|
-
|
|
13
|
+
factory.createIdentifier(reducer.name),
|
|
14
14
|
undefined,
|
|
15
15
|
reducer.reducerOptions,
|
|
16
|
-
undefined,
|
|
17
16
|
)
|
|
18
17
|
}
|
|
19
18
|
})
|
|
@@ -1,48 +1,47 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { factory, SyntaxKind } from 'typescript'
|
|
2
2
|
import { ParsedLogic } from '../types'
|
|
3
3
|
import { cleanDuplicateAnyNodes } from '../utils'
|
|
4
4
|
|
|
5
5
|
export function printReducers(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
|
+
factory.createFunctionTypeNode(
|
|
13
13
|
undefined,
|
|
14
14
|
[
|
|
15
|
-
|
|
15
|
+
factory.createParameterDeclaration(
|
|
16
16
|
undefined,
|
|
17
17
|
undefined,
|
|
18
18
|
undefined,
|
|
19
|
-
|
|
19
|
+
factory.createIdentifier('state'),
|
|
20
20
|
undefined,
|
|
21
21
|
reducer.typeNode,
|
|
22
22
|
undefined,
|
|
23
23
|
),
|
|
24
|
-
|
|
24
|
+
factory.createParameterDeclaration(
|
|
25
25
|
undefined,
|
|
26
26
|
undefined,
|
|
27
27
|
undefined,
|
|
28
|
-
|
|
28
|
+
factory.createIdentifier('action'),
|
|
29
29
|
undefined,
|
|
30
|
-
|
|
30
|
+
factory.createKeywordTypeNode(SyntaxKind.AnyKeyword),
|
|
31
31
|
undefined,
|
|
32
32
|
),
|
|
33
|
-
|
|
33
|
+
factory.createParameterDeclaration(
|
|
34
34
|
undefined,
|
|
35
35
|
undefined,
|
|
36
36
|
undefined,
|
|
37
|
-
|
|
37
|
+
factory.createIdentifier('fullState'),
|
|
38
38
|
undefined,
|
|
39
|
-
|
|
39
|
+
factory.createKeywordTypeNode(SyntaxKind.AnyKeyword),
|
|
40
40
|
undefined,
|
|
41
41
|
),
|
|
42
42
|
],
|
|
43
43
|
reducer.typeNode,
|
|
44
44
|
),
|
|
45
|
-
undefined,
|
|
46
45
|
)
|
|
47
46
|
}),
|
|
48
47
|
)
|
|
@@ -1,29 +1,28 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { factory, SyntaxKind } from 'typescript'
|
|
2
2
|
import { ParsedLogic } from '../types'
|
|
3
3
|
import { cleanDuplicateAnyNodes } from '../utils'
|
|
4
4
|
|
|
5
5
|
export function printSelector(parsedLogic: ParsedLogic) {
|
|
6
|
-
return
|
|
6
|
+
return factory.createFunctionTypeNode(
|
|
7
7
|
undefined,
|
|
8
8
|
[
|
|
9
|
-
|
|
9
|
+
factory.createParameterDeclaration(
|
|
10
10
|
undefined,
|
|
11
11
|
undefined,
|
|
12
12
|
undefined,
|
|
13
|
-
|
|
13
|
+
factory.createIdentifier('state'),
|
|
14
14
|
undefined,
|
|
15
|
-
|
|
15
|
+
factory.createKeywordTypeNode(SyntaxKind.AnyKeyword),
|
|
16
16
|
undefined,
|
|
17
17
|
),
|
|
18
18
|
],
|
|
19
|
-
|
|
19
|
+
factory.createTypeLiteralNode(
|
|
20
20
|
cleanDuplicateAnyNodes(parsedLogic.reducers).map((reducer) =>
|
|
21
|
-
|
|
21
|
+
factory.createPropertySignature(
|
|
22
22
|
undefined,
|
|
23
|
-
|
|
23
|
+
factory.createIdentifier(reducer.name),
|
|
24
24
|
undefined,
|
|
25
25
|
reducer.typeNode,
|
|
26
|
-
undefined,
|
|
27
26
|
),
|
|
28
27
|
),
|
|
29
28
|
),
|
|
@@ -1,39 +1,38 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { factory, SyntaxKind } from 'typescript'
|
|
2
2
|
import { ParsedLogic } from '../types'
|
|
3
3
|
import { cleanDuplicateAnyNodes } from '../utils'
|
|
4
4
|
|
|
5
5
|
export function printSelectors(parsedLogic: ParsedLogic) {
|
|
6
|
-
return
|
|
6
|
+
return factory.createTypeLiteralNode(
|
|
7
7
|
cleanDuplicateAnyNodes(parsedLogic.reducers.concat(parsedLogic.selectors)).map((reducer) => {
|
|
8
|
-
return
|
|
8
|
+
return factory.createPropertySignature(
|
|
9
9
|
undefined,
|
|
10
|
-
|
|
10
|
+
factory.createIdentifier(reducer.name),
|
|
11
11
|
undefined,
|
|
12
|
-
|
|
12
|
+
factory.createFunctionTypeNode(
|
|
13
13
|
undefined,
|
|
14
14
|
[
|
|
15
|
-
|
|
15
|
+
factory.createParameterDeclaration(
|
|
16
16
|
undefined,
|
|
17
17
|
undefined,
|
|
18
18
|
undefined,
|
|
19
|
-
|
|
19
|
+
factory.createIdentifier('state'),
|
|
20
20
|
undefined,
|
|
21
|
-
|
|
21
|
+
factory.createKeywordTypeNode(SyntaxKind.AnyKeyword),
|
|
22
22
|
undefined,
|
|
23
23
|
),
|
|
24
|
-
|
|
24
|
+
factory.createParameterDeclaration(
|
|
25
25
|
undefined,
|
|
26
26
|
undefined,
|
|
27
27
|
undefined,
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
factory.createIdentifier('props'),
|
|
29
|
+
factory.createToken(SyntaxKind.QuestionToken),
|
|
30
|
+
factory.createKeywordTypeNode(SyntaxKind.AnyKeyword),
|
|
31
31
|
undefined,
|
|
32
32
|
),
|
|
33
33
|
],
|
|
34
34
|
reducer.typeNode,
|
|
35
35
|
),
|
|
36
|
-
undefined,
|
|
37
36
|
)
|
|
38
37
|
}),
|
|
39
38
|
)
|
|
@@ -1,61 +1,60 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { factory, SyntaxKind } from 'typescript'
|
|
2
2
|
import { ParsedLogic } from '../types'
|
|
3
3
|
|
|
4
4
|
export function printSharedListeners(parsedLogic: ParsedLogic) {
|
|
5
|
-
return
|
|
5
|
+
return factory.createTypeLiteralNode(
|
|
6
6
|
parsedLogic.sharedListeners.map(({ name, payload, action }) =>
|
|
7
|
-
|
|
7
|
+
factory.createPropertySignature(
|
|
8
8
|
undefined,
|
|
9
|
-
|
|
9
|
+
factory.createStringLiteral(name),
|
|
10
10
|
undefined,
|
|
11
|
-
|
|
11
|
+
factory.createFunctionTypeNode(
|
|
12
12
|
undefined,
|
|
13
13
|
[
|
|
14
|
-
|
|
14
|
+
factory.createParameterDeclaration(
|
|
15
15
|
undefined,
|
|
16
16
|
undefined,
|
|
17
17
|
undefined,
|
|
18
|
-
|
|
18
|
+
factory.createIdentifier('payload'),
|
|
19
19
|
undefined,
|
|
20
|
-
payload ||
|
|
20
|
+
payload || factory.createKeywordTypeNode(SyntaxKind.AnyKeyword),
|
|
21
21
|
undefined,
|
|
22
22
|
),
|
|
23
|
-
|
|
23
|
+
factory.createParameterDeclaration(
|
|
24
24
|
undefined,
|
|
25
25
|
undefined,
|
|
26
26
|
undefined,
|
|
27
|
-
|
|
27
|
+
factory.createIdentifier('breakpoint'),
|
|
28
28
|
undefined,
|
|
29
|
-
|
|
29
|
+
factory.createTypeReferenceNode(factory.createIdentifier('BreakPointFunction'), undefined),
|
|
30
30
|
undefined,
|
|
31
31
|
),
|
|
32
|
-
|
|
32
|
+
factory.createParameterDeclaration(
|
|
33
33
|
undefined,
|
|
34
34
|
undefined,
|
|
35
35
|
undefined,
|
|
36
|
-
|
|
36
|
+
factory.createIdentifier('action'),
|
|
37
37
|
undefined,
|
|
38
38
|
action,
|
|
39
39
|
undefined,
|
|
40
40
|
),
|
|
41
|
-
|
|
41
|
+
factory.createParameterDeclaration(
|
|
42
42
|
undefined,
|
|
43
43
|
undefined,
|
|
44
44
|
undefined,
|
|
45
|
-
|
|
45
|
+
factory.createIdentifier('previousState'),
|
|
46
46
|
undefined,
|
|
47
|
-
|
|
47
|
+
factory.createKeywordTypeNode(SyntaxKind.AnyKeyword),
|
|
48
48
|
undefined,
|
|
49
49
|
),
|
|
50
50
|
],
|
|
51
|
-
|
|
52
|
-
{ ...
|
|
53
|
-
|
|
54
|
-
{ ...
|
|
51
|
+
factory.createUnionTypeNode([
|
|
52
|
+
{ ...factory.createToken(SyntaxKind.VoidKeyword), _typeNodeBrand: true },
|
|
53
|
+
factory.createTypeReferenceNode(factory.createIdentifier('Promise'), [
|
|
54
|
+
{ ...factory.createToken(SyntaxKind.VoidKeyword), _typeNodeBrand: true },
|
|
55
55
|
]),
|
|
56
56
|
]),
|
|
57
57
|
),
|
|
58
|
-
undefined,
|
|
59
58
|
),
|
|
60
59
|
),
|
|
61
60
|
)
|
package/src/print/printValues.ts
CHANGED
|
@@ -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 printValues(parsedLogic: ParsedLogic) {
|
|
6
|
-
return
|
|
6
|
+
return factory.createTypeLiteralNode(
|
|
7
7
|
cleanDuplicateAnyNodes(parsedLogic.reducers.concat(parsedLogic.selectors)).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/typegen.ts
CHANGED
|
@@ -105,7 +105,7 @@ export function runTypeGen(appOptions: AppOptions) {
|
|
|
105
105
|
function goThroughAllTheFiles(
|
|
106
106
|
program,
|
|
107
107
|
appOptions,
|
|
108
|
-
): { filesToWrite: number; writtenFiles: number;
|
|
108
|
+
): { filesToWrite: number; writtenFiles: number; filesToModify: number } {
|
|
109
109
|
const parsedLogics = visitProgram(program, appOptions)
|
|
110
110
|
if (appOptions.verbose) {
|
|
111
111
|
log(`🗒️ ${parsedLogics.length} logic${parsedLogics.length === 1 ? '' : 's'} found!`)
|
|
@@ -115,7 +115,7 @@ export function runTypeGen(appOptions: AppOptions) {
|
|
|
115
115
|
|
|
116
116
|
// running "kea-typegen check" and would write files?
|
|
117
117
|
// exit with 1
|
|
118
|
-
if (!appOptions.write && !appOptions.watch && (response.filesToWrite > 0 || response.
|
|
118
|
+
if (!appOptions.write && !appOptions.watch && (response.filesToWrite > 0 || response.filesToModify > 0)) {
|
|
119
119
|
process.exit(1)
|
|
120
120
|
}
|
|
121
121
|
|
|
@@ -126,9 +126,9 @@ export function runTypeGen(appOptions: AppOptions) {
|
|
|
126
126
|
if (appOptions.write) {
|
|
127
127
|
let round = 0
|
|
128
128
|
while ((round += 1)) {
|
|
129
|
-
const { writtenFiles,
|
|
129
|
+
const { writtenFiles, filesToModify } = goThroughAllTheFiles(program, appOptions)
|
|
130
130
|
|
|
131
|
-
if (writtenFiles === 0 &&
|
|
131
|
+
if (writtenFiles === 0 && filesToModify === 0) {
|
|
132
132
|
log(`👋 Finished writing files! Exiting.`)
|
|
133
133
|
process.exit(0)
|
|
134
134
|
}
|
package/src/types.ts
CHANGED
|
@@ -35,6 +35,8 @@ export interface ParsedLogic {
|
|
|
35
35
|
logicTypeImported: boolean
|
|
36
36
|
path: string[]
|
|
37
37
|
pathString: string
|
|
38
|
+
hasPathInLogic: boolean
|
|
39
|
+
hasKeyInLogic: boolean
|
|
38
40
|
logicTypeArguments: string[]
|
|
39
41
|
constants: string[]
|
|
40
42
|
events: Record<string, boolean>
|
|
@@ -50,11 +52,12 @@ export interface ParsedLogic {
|
|
|
50
52
|
typeReferencesToImportFromFiles: Record<string, Set<string>>
|
|
51
53
|
typeReferencesInLogicInput: Set<string>
|
|
52
54
|
interfaceDeclaration?: ts.InterfaceDeclaration
|
|
53
|
-
extraInput: Record<string, { typeNode: ts.TypeNode
|
|
55
|
+
extraInput: Record<string, { typeNode: ts.TypeNode; withLogicFunction: boolean }>
|
|
54
56
|
}
|
|
55
57
|
|
|
56
58
|
export interface AppOptions {
|
|
57
59
|
tsConfigPath?: string
|
|
60
|
+
packageJsonPath?: string
|
|
58
61
|
sourceFilePath?: string
|
|
59
62
|
rootPath?: string
|
|
60
63
|
typesPath?: string
|
|
@@ -62,7 +65,14 @@ export interface AppOptions {
|
|
|
62
65
|
watch?: boolean
|
|
63
66
|
quiet?: boolean
|
|
64
67
|
verbose?: boolean
|
|
68
|
+
/** Do not write imports inside logic.ts files */
|
|
65
69
|
noImport?: boolean
|
|
70
|
+
/** Add import statements in logicType.ts files for global types (e.g. @types/node) */
|
|
71
|
+
importGlobalTypes?: boolean
|
|
72
|
+
/** List of paths we will never import from inside logicType.ts files */
|
|
73
|
+
ignoreImportPaths?: string[]
|
|
74
|
+
/** Write paths inside logic.ts files */
|
|
75
|
+
writePaths?: boolean
|
|
66
76
|
|
|
67
77
|
log: (message: string) => void
|
|
68
78
|
}
|
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 { NodeBuilderFlags } from 'typescript'
|
|
7
|
+
import { factory, NodeBuilderFlags, SyntaxKind } from 'typescript'
|
|
8
8
|
|
|
9
9
|
export function logicSourceToLogicType(logicSource: string, appOptions?: AppOptions) {
|
|
10
10
|
const program = programFromSource(logicSource)
|
|
@@ -54,19 +54,19 @@ export function getTypeNodeForNode(node: ts.Node, checker: ts.TypeChecker): ts.T
|
|
|
54
54
|
if (ts.isParenthesizedTypeNode(typeNode)) {
|
|
55
55
|
typeNode = typeNode.type
|
|
56
56
|
}
|
|
57
|
-
} else if (node?.kind ===
|
|
58
|
-
typeNode =
|
|
57
|
+
} else if (node?.kind === SyntaxKind.TrueKeyword || node?.kind === SyntaxKind.FalseKeyword) {
|
|
58
|
+
typeNode = factory.createKeywordTypeNode(SyntaxKind.BooleanKeyword)
|
|
59
59
|
} else if (ts.isStringLiteralLike(node)) {
|
|
60
|
-
typeNode =
|
|
60
|
+
typeNode = factory.createKeywordTypeNode(SyntaxKind.StringKeyword)
|
|
61
61
|
} else if (ts.isNumericLiteral(node)) {
|
|
62
|
-
typeNode =
|
|
62
|
+
typeNode = factory.createKeywordTypeNode(SyntaxKind.NumberKeyword)
|
|
63
63
|
} else if (ts.isArrayLiteralExpression(node) && node.elements.length === 0) {
|
|
64
|
-
typeNode =
|
|
64
|
+
typeNode = factory.createArrayTypeNode(factory.createKeywordTypeNode(SyntaxKind.AnyKeyword))
|
|
65
65
|
} else {
|
|
66
66
|
typeNode = checker.typeToTypeNode(checker.getTypeAtLocation(node), undefined, undefined)
|
|
67
67
|
}
|
|
68
68
|
} else {
|
|
69
|
-
typeNode =
|
|
69
|
+
typeNode = factory.createKeywordTypeNode(SyntaxKind.AnyKeyword)
|
|
70
70
|
}
|
|
71
71
|
return typeNode
|
|
72
72
|
}
|
|
@@ -82,13 +82,13 @@ export function getAndGatherTypeNodeForDefaultValue(
|
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
export function getParameterDeclaration(param: ts.ParameterDeclaration) {
|
|
85
|
-
return
|
|
85
|
+
return factory.createParameterDeclaration(
|
|
86
86
|
undefined,
|
|
87
87
|
undefined,
|
|
88
88
|
undefined,
|
|
89
|
-
|
|
90
|
-
param.initializer || param.questionToken ?
|
|
91
|
-
cloneNode(param.type ||
|
|
89
|
+
factory.createIdentifier(param.name.getText()),
|
|
90
|
+
param.initializer || param.questionToken ? factory.createToken(SyntaxKind.QuestionToken) : undefined,
|
|
91
|
+
cloneNode(param.type || factory.createKeywordTypeNode(SyntaxKind.AnyKeyword)),
|
|
92
92
|
undefined,
|
|
93
93
|
)
|
|
94
94
|
}
|
|
@@ -216,7 +216,7 @@ export function getLogicPathString(appOptions: AppOptions, fileName: string) {
|
|
|
216
216
|
}
|
|
217
217
|
|
|
218
218
|
export function getFilenamesForSymbol(symbol: ts.Symbol): string[] | undefined {
|
|
219
|
-
return symbol?.declarations
|
|
219
|
+
return (symbol?.declarations || [])
|
|
220
220
|
.map((d) => d.getSourceFile().fileName)
|
|
221
221
|
.filter((str) => !str.includes('/node_modules/typescript/lib/lib'))
|
|
222
222
|
}
|
|
@@ -230,7 +230,7 @@ export function gatherImports(input: ts.Node, checker: ts.TypeChecker, parsedLog
|
|
|
230
230
|
let node = requestedNode
|
|
231
231
|
if (ts.isTypeReferenceNode(node)) {
|
|
232
232
|
let typeRootName: string | undefined
|
|
233
|
-
if (node.typeName?.kind ===
|
|
233
|
+
if (node.typeName?.kind === SyntaxKind.FirstNode) {
|
|
234
234
|
try {
|
|
235
235
|
typeRootName = node.typeName.getFirstToken().getText()
|
|
236
236
|
} catch (e) {
|
|
@@ -254,9 +254,9 @@ export function storeExtractedSymbol(
|
|
|
254
254
|
parsedLogic: ParsedLogic,
|
|
255
255
|
typeRootName?: string,
|
|
256
256
|
) {
|
|
257
|
-
const declaration = symbol.getDeclarations()[0]
|
|
257
|
+
const declaration = symbol.getDeclarations()?.[0]
|
|
258
258
|
|
|
259
|
-
if (ts.isImportSpecifier(declaration)) {
|
|
259
|
+
if (declaration && ts.isImportSpecifier(declaration)) {
|
|
260
260
|
const importFilename = getFilenameForImportSpecifier(declaration, checker)
|
|
261
261
|
if (importFilename) {
|
|
262
262
|
addTypeImport(parsedLogic, importFilename, typeRootName || declaration.getText())
|
|
@@ -334,8 +334,8 @@ export function isAnyUnknown(node?: ts.Node): boolean {
|
|
|
334
334
|
const unPromised = unPromisify(node)
|
|
335
335
|
return (
|
|
336
336
|
!unPromised ||
|
|
337
|
-
unPromised.kind ===
|
|
338
|
-
unPromised.kind ===
|
|
337
|
+
unPromised.kind === SyntaxKind.AnyKeyword ||
|
|
338
|
+
unPromised.kind === SyntaxKind.UnknownKeyword ||
|
|
339
339
|
(ts.isTypeLiteralNode(unPromised) && unPromised.members.length === 0)
|
|
340
340
|
)
|
|
341
341
|
}
|
package/src/visit/visit.ts
CHANGED
|
@@ -179,9 +179,9 @@ export function visitKeaCalls(
|
|
|
179
179
|
// only if symbol resolves we mark the logic type as imported
|
|
180
180
|
const symbol = checker.getSymbolAtLocation(keaTypeArgument.typeName)
|
|
181
181
|
if (symbol) {
|
|
182
|
-
const declaration = symbol.getDeclarations()[0]
|
|
182
|
+
const declaration = symbol.getDeclarations()?.[0]
|
|
183
183
|
|
|
184
|
-
if (ts.isImportSpecifier(declaration)) {
|
|
184
|
+
if (declaration && ts.isImportSpecifier(declaration)) {
|
|
185
185
|
const filename = getFilenameForImportSpecifier(declaration, checker)
|
|
186
186
|
logicTypeImported = filename === typeFileName
|
|
187
187
|
}
|
|
@@ -214,9 +214,11 @@ export function visitKeaCalls(
|
|
|
214
214
|
propsType: undefined,
|
|
215
215
|
path: pathString.split('.'),
|
|
216
216
|
pathString: pathString,
|
|
217
|
+
hasKeyInLogic: false,
|
|
218
|
+
hasPathInLogic: false,
|
|
217
219
|
typeReferencesToImportFromFiles: {},
|
|
218
220
|
typeReferencesInLogicInput: new Set(),
|
|
219
|
-
extraInput: {}
|
|
221
|
+
extraInput: {},
|
|
220
222
|
}
|
|
221
223
|
|
|
222
224
|
const input = (node.parent as ts.CallExpression).arguments[0] as ts.ObjectLiteralExpression
|
|
@@ -232,6 +234,12 @@ export function visitKeaCalls(
|
|
|
232
234
|
}
|
|
233
235
|
|
|
234
236
|
const name = symbol.getName()
|
|
237
|
+
if (name === 'path') {
|
|
238
|
+
parsedLogic.hasPathInLogic = true
|
|
239
|
+
}
|
|
240
|
+
if (name === 'key') {
|
|
241
|
+
parsedLogic.hasKeyInLogic = true
|
|
242
|
+
}
|
|
235
243
|
|
|
236
244
|
let type = checker.getTypeOfSymbolAtLocation(symbol, symbol.valueDeclaration)
|
|
237
245
|
let typeNode = type ? checker.typeToTypeNode(type, undefined, undefined) : null
|