@eventra_dev/eventra-cli 0.0.4 → 0.0.5

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.
@@ -0,0 +1,110 @@
1
+ import {
2
+ SourceFile,
3
+ SyntaxKind,
4
+ } from "ts-morph";
5
+
6
+ import { ComponentWrapper } from "../../types";
7
+
8
+ export function scanComponentWrappers(
9
+ source: SourceFile,
10
+ wrappers: ComponentWrapper[]
11
+ ) {
12
+ const events = new Set<string>();
13
+
14
+ const elements = [
15
+ ...source.getDescendantsOfKind(
16
+ SyntaxKind.JsxOpeningElement
17
+ ),
18
+ ...source.getDescendantsOfKind(
19
+ SyntaxKind.JsxSelfClosingElement
20
+ )
21
+ ];
22
+
23
+ for (const el of elements) {
24
+ const name = el
25
+ .getTagNameNode()
26
+ .getText()
27
+ .toLowerCase();
28
+
29
+ for (const wrapper of wrappers) {
30
+ if (
31
+ name !==
32
+ wrapper.name.toLowerCase()
33
+ )
34
+ continue;
35
+
36
+ const attrs =
37
+ el.getAttributes();
38
+
39
+ for (const attr of attrs) {
40
+ const attrNode =
41
+ attr.asKind(
42
+ SyntaxKind.JsxAttribute
43
+ );
44
+
45
+ if (!attrNode) continue;
46
+
47
+ const key =
48
+ attrNode
49
+ .getNameNode()
50
+ .getText()
51
+ .toLowerCase();
52
+
53
+ if (
54
+ key !==
55
+ wrapper.prop.toLowerCase()
56
+ )
57
+ continue;
58
+
59
+ const init =
60
+ attrNode.getInitializer();
61
+
62
+ if (!init) continue;
63
+
64
+ // event="signup"
65
+ if (
66
+ init.getKind() ===
67
+ SyntaxKind.StringLiteral
68
+ ) {
69
+ const value =
70
+ init.asKindOrThrow(
71
+ SyntaxKind.StringLiteral
72
+ );
73
+
74
+ events.add(
75
+ value.getLiteralText()
76
+ );
77
+ }
78
+
79
+ // event={"signup"}
80
+ if (
81
+ init.getKind() ===
82
+ SyntaxKind.JsxExpression
83
+ ) {
84
+ const expr =
85
+ init
86
+ .asKindOrThrow(
87
+ SyntaxKind.JsxExpression
88
+ )
89
+ .getExpression();
90
+
91
+ if (
92
+ expr?.getKind() ===
93
+ SyntaxKind.StringLiteral
94
+ ) {
95
+ const value =
96
+ expr.asKindOrThrow(
97
+ SyntaxKind.StringLiteral
98
+ );
99
+
100
+ events.add(
101
+ value.getLiteralText()
102
+ );
103
+ }
104
+ }
105
+ }
106
+ }
107
+ }
108
+
109
+ return events;
110
+ }
@@ -0,0 +1,67 @@
1
+ import {
2
+ SourceFile,
3
+ SyntaxKind,
4
+ } from "ts-morph";
5
+
6
+ import { extractEvent } from "../extract";
7
+ import { FunctionWrapper } from "../../types";
8
+
9
+ export function scanFunctionWrappers(
10
+ source: SourceFile,
11
+ wrappers: FunctionWrapper[]
12
+ ) {
13
+ const events =
14
+ new Set<string>();
15
+
16
+ const calls =
17
+ source.getDescendantsOfKind(
18
+ SyntaxKind.CallExpression
19
+ );
20
+
21
+ for (const call of calls) {
22
+ const expression =
23
+ call.getExpression();
24
+
25
+ let name: string | null = null;
26
+
27
+ // trackFeature()
28
+ if (
29
+ expression.getKind() ===
30
+ SyntaxKind.Identifier
31
+ ) {
32
+ name =
33
+ expression.getText();
34
+ }
35
+
36
+ // analytics.trackFeature()
37
+ if (
38
+ expression.getKind() ===
39
+ SyntaxKind.PropertyAccessExpression
40
+ ) {
41
+ const prop =
42
+ expression.asKindOrThrow(
43
+ SyntaxKind.PropertyAccessExpression
44
+ );
45
+
46
+ name = prop.getName();
47
+ }
48
+
49
+ if (!name) continue;
50
+
51
+ for (const wrapper of wrappers) {
52
+ if (name !== wrapper.name)
53
+ continue;
54
+
55
+ const event =
56
+ extractEvent(
57
+ call,
58
+ wrapper.path
59
+ );
60
+
61
+ if (event)
62
+ events.add(event);
63
+ }
64
+ }
65
+
66
+ return events;
67
+ }
@@ -0,0 +1,84 @@
1
+ import {
2
+ SourceFile,
3
+ SyntaxKind
4
+ } from "ts-morph";
5
+
6
+ export function scanTrack(
7
+ source: SourceFile
8
+ ) {
9
+ const events =
10
+ new Set<string>();
11
+
12
+ const calls =
13
+ source.getDescendantsOfKind(
14
+ SyntaxKind.CallExpression
15
+ );
16
+
17
+ for (const call of calls) {
18
+ const expr =
19
+ call.getExpression();
20
+
21
+ let isTrack = false;
22
+
23
+ // track()
24
+ if (
25
+ expr.getKind() ===
26
+ SyntaxKind.Identifier
27
+ ) {
28
+ isTrack =
29
+ expr.getText() === "track";
30
+ }
31
+
32
+ // analytics.track()
33
+ if (
34
+ expr.getKind() ===
35
+ SyntaxKind.PropertyAccessExpression
36
+ ) {
37
+ const prop =
38
+ expr.asKindOrThrow(
39
+ SyntaxKind.PropertyAccessExpression
40
+ );
41
+
42
+ isTrack =
43
+ prop.getName() === "track";
44
+ }
45
+
46
+ if (!isTrack) continue;
47
+
48
+ const arg =
49
+ call.getArguments()[0];
50
+
51
+ if (!arg) continue;
52
+
53
+ if (
54
+ arg.getKind() ===
55
+ SyntaxKind.StringLiteral
56
+ ) {
57
+ const value =
58
+ arg.asKindOrThrow(
59
+ SyntaxKind.StringLiteral
60
+ );
61
+
62
+ events.add(
63
+ value.getLiteralText()
64
+ );
65
+ }
66
+
67
+ // template literal
68
+ if (
69
+ arg.getKind() ===
70
+ SyntaxKind.NoSubstitutionTemplateLiteral
71
+ ) {
72
+ const value =
73
+ arg.asKindOrThrow(
74
+ SyntaxKind.NoSubstitutionTemplateLiteral
75
+ );
76
+
77
+ events.add(
78
+ value.getLiteralText()
79
+ );
80
+ }
81
+ }
82
+
83
+ return events;
84
+ }
package/tsconfig.json CHANGED
@@ -2,7 +2,9 @@
2
2
  "compilerOptions": {
3
3
  "target": "ES2022",
4
4
  "module": "CommonJS",
5
+ "rootDir": "src",
5
6
  "outDir": "dist",
7
+ "resolveJsonModule": true,
6
8
  "esModuleInterop": true,
7
9
  "strict": true,
8
10
  "skipLibCheck": true,