@eventra_dev/eventra-cli 0.0.5 → 0.0.7

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.
@@ -2,6 +2,7 @@ import {
2
2
  CallExpression,
3
3
  Node,
4
4
  ObjectLiteralExpression,
5
+ SyntaxKind,
5
6
  } from "ts-morph";
6
7
 
7
8
  export function extractEvent(
@@ -15,37 +16,59 @@ export function extractEvent(
15
16
 
16
17
  if (!node) return null;
17
18
 
18
- for (let i = 1; i < parts.length; i++) {
19
- if (
20
- Node.isObjectLiteralExpression(node)
21
- ) {
22
- const obj: ObjectLiteralExpression =
23
- node;
19
+ node = unwrap(node);
24
20
 
25
- const prop =
26
- obj.getProperty(parts[i]);
21
+ for (let i = 1; i < parts.length; i++) {
22
+ if (!Node.isObjectLiteralExpression(node)) {
23
+ return null;
24
+ }
27
25
 
28
- if (!prop) return null;
26
+ const obj: ObjectLiteralExpression =
27
+ node;
29
28
 
30
- if (
31
- Node.isPropertyAssignment(prop)
32
- ) {
33
- const initializer =
34
- prop.getInitializer();
29
+ const prop =
30
+ obj.getProperty(parts[i]);
35
31
 
36
- if (!initializer) return null;
32
+ if (!prop) return null;
37
33
 
38
- node = initializer;
39
- }
34
+ if (!Node.isPropertyAssignment(prop)) {
35
+ return null;
40
36
  }
37
+
38
+ const initializer =
39
+ prop.getInitializer();
40
+
41
+ if (!initializer) return null;
42
+
43
+ node = unwrap(initializer);
44
+ }
45
+
46
+ if (Node.isStringLiteral(node)) {
47
+ return node.getLiteralText();
41
48
  }
42
49
 
43
50
  if (
44
- node &&
45
- Node.isStringLiteral(node)
51
+ node.getKind() ===
52
+ SyntaxKind.NoSubstitutionTemplateLiteral
46
53
  ) {
47
- return node.getLiteralText();
54
+ return node
55
+ .getText()
56
+ .replace(/`/g, "");
48
57
  }
49
58
 
50
59
  return null;
51
60
  }
61
+
62
+ function unwrap(node: Node): Node {
63
+ let current = node;
64
+
65
+ while (
66
+ Node.isParenthesizedExpression(
67
+ current
68
+ )
69
+ ) {
70
+ current = current.getExpression();
71
+ }
72
+
73
+ return current;
74
+ }
@@ -11,9 +11,15 @@ export function parseVue(
11
11
  /<script[\s\S]*?>([\s\S]*?)<\/script>/
12
12
  );
13
13
 
14
+ return `
15
+ ${script?.[1] ?? ""}
16
+
17
+ function __vue_template__() {
14
18
  return (
15
- (template?.[1] ?? "") +
16
- "\n" +
17
- (script?.[1] ?? "")
19
+ <>
20
+ ${template?.[1] ?? ""}
21
+ </>
18
22
  );
19
23
  }
24
+ `;
25
+ }
@@ -61,7 +61,6 @@ export function scanComponentWrappers(
61
61
 
62
62
  if (!init) continue;
63
63
 
64
- // event="signup"
65
64
  if (
66
65
  init.getKind() ===
67
66
  SyntaxKind.StringLiteral
@@ -76,7 +75,6 @@ export function scanComponentWrappers(
76
75
  );
77
76
  }
78
77
 
79
- // event={"signup"}
80
78
  if (
81
79
  init.getKind() ===
82
80
  SyntaxKind.JsxExpression
@@ -1,6 +1,9 @@
1
1
  import {
2
2
  SourceFile,
3
3
  SyntaxKind,
4
+ Node,
5
+ CallExpression,
6
+ PropertyAccessExpression,
4
7
  } from "ts-morph";
5
8
 
6
9
  import { extractEvent } from "../extract";
@@ -10,8 +13,7 @@ export function scanFunctionWrappers(
10
13
  source: SourceFile,
11
14
  wrappers: FunctionWrapper[]
12
15
  ) {
13
- const events =
14
- new Set<string>();
16
+ const events = new Set<string>();
15
17
 
16
18
  const calls =
17
19
  source.getDescendantsOfKind(
@@ -19,43 +21,19 @@ export function scanFunctionWrappers(
19
21
  );
20
22
 
21
23
  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
- }
24
+ const name =
25
+ getFunctionName(call);
48
26
 
49
27
  if (!name) continue;
50
28
 
51
29
  for (const wrapper of wrappers) {
52
- if (name !== wrapper.name)
30
+ if (wrapper.name !== name)
53
31
  continue;
54
32
 
55
33
  const event =
56
- extractEvent(
34
+ extractEventFromArgs(
57
35
  call,
58
- wrapper.path
36
+ wrapper.event
59
37
  );
60
38
 
61
39
  if (event)
@@ -65,3 +43,98 @@ export function scanFunctionWrappers(
65
43
 
66
44
  return events;
67
45
  }
46
+
47
+ function getFunctionName(
48
+ call: CallExpression
49
+ ): string | null {
50
+ const expression =
51
+ call.getExpression();
52
+
53
+ // trackFeature()
54
+ if (
55
+ Node.isIdentifier(
56
+ expression
57
+ )
58
+ ) {
59
+ return expression.getText();
60
+ }
61
+
62
+ // analytics.trackFeature()
63
+ if (
64
+ Node.isPropertyAccessExpression(
65
+ expression
66
+ )
67
+ ) {
68
+ return getDeepName(
69
+ expression
70
+ );
71
+ }
72
+
73
+ return null;
74
+ }
75
+
76
+ function getDeepName(
77
+ node: PropertyAccessExpression
78
+ ): string {
79
+ let current:
80
+ | Node
81
+ | undefined = node;
82
+
83
+ let name = "";
84
+
85
+ while (
86
+ Node.isPropertyAccessExpression(
87
+ current
88
+ )
89
+ ) {
90
+ name =
91
+ current.getName();
92
+
93
+ current =
94
+ current.getExpression();
95
+ }
96
+
97
+ return name;
98
+ }
99
+
100
+
101
+ function extractEventFromArgs(
102
+ call: CallExpression,
103
+ event?: string
104
+ ): string | null {
105
+ const args = call.getArguments();
106
+
107
+ for (let i = 0; i < args.length; i++) {
108
+ const arg = args[i];
109
+
110
+ // string case
111
+ if (!event) {
112
+ if (Node.isStringLiteral(arg)) {
113
+ return arg.getLiteralText();
114
+ }
115
+
116
+ // template literal
117
+ if (
118
+ arg.getKind() ===
119
+ SyntaxKind.NoSubstitutionTemplateLiteral
120
+ ) {
121
+ return arg
122
+ .getText()
123
+ .replace(/`/g, "");
124
+ }
125
+ }
126
+
127
+ // object case
128
+ if (event) {
129
+ const result =
130
+ extractEvent(
131
+ call,
132
+ `${i}.${event}`
133
+ );
134
+
135
+ if (result) return result;
136
+ }
137
+ }
138
+
139
+ return null;
140
+ }
@@ -0,0 +1,78 @@
1
+ /* eslint-disable */
2
+
3
+ declare function track(event: string): void;
4
+
5
+ const express = (...args: any[]) => ({
6
+ get: (...args: any[]) => {},
7
+ post: (...args: any[]) => {},
8
+ put: (...args: any[]) => {},
9
+ delete: (...args: any[]) => {},
10
+ use: (...args: any[]) => {},
11
+ listen: (...args: any[]) => {}
12
+ });
13
+
14
+ const app = express();
15
+
16
+ // ===== direct =====
17
+ track("express_event");
18
+
19
+ // ===== middleware =====
20
+ app.use(() => {
21
+ track("middleware_event");
22
+ });
23
+
24
+ // ===== get =====
25
+ app.get("/", () => {
26
+ track("get_event");
27
+ });
28
+
29
+ // ===== post =====
30
+ app.post("/users", () => {
31
+ track("post_event");
32
+ });
33
+
34
+ // ===== put =====
35
+ app.put("/users", () => {
36
+ track("put_event");
37
+ });
38
+
39
+ // ===== delete =====
40
+ app.delete("/users", () => {
41
+ track("delete_event");
42
+ });
43
+
44
+ // ===== nested =====
45
+ app.get("/nested", () => {
46
+ if (true) {
47
+ track("nested_event");
48
+ }
49
+ });
50
+
51
+ // ===== async =====
52
+ app.get("/async", async () => {
53
+ track("async_event");
54
+ });
55
+
56
+ // ===== arrow =====
57
+ const handler = () => {
58
+ track("arrow_event");
59
+ };
60
+
61
+ app.get("/arrow", handler);
62
+
63
+ // ===== function =====
64
+ function service() {
65
+ track("function_event");
66
+ }
67
+
68
+ // ===== class =====
69
+ class Service {
70
+ run() {
71
+ track("class_event");
72
+ }
73
+ }
74
+
75
+ // ===== listen =====
76
+ app.listen(3000, () => {
77
+ track("listen_event");
78
+ });
@@ -0,0 +1,70 @@
1
+ /* eslint-disable */
2
+
3
+ declare function track(event: string): void;
4
+
5
+ function Injectable(): any {
6
+ return () => {};
7
+ }
8
+
9
+ function Controller(): any {
10
+ return () => {};
11
+ }
12
+
13
+ function Get(): any {
14
+ return () => {};
15
+ }
16
+
17
+ function Post(): any {
18
+ return () => {};
19
+ }
20
+
21
+ // ===== service =====
22
+ @Injectable()
23
+ export class TestService {
24
+ run() {
25
+ track("nest_event");
26
+ }
27
+
28
+ async asyncRun() {
29
+ track("async_event");
30
+ }
31
+
32
+ arrow = () => {
33
+ track("arrow_event");
34
+ };
35
+ }
36
+
37
+ // ===== controller =====
38
+ @Controller()
39
+ export class TestController {
40
+ @Get()
41
+ get() {
42
+ track("get_event");
43
+ }
44
+
45
+ @Post()
46
+ post() {
47
+ track("post_event");
48
+ }
49
+
50
+ nested() {
51
+ if (true) {
52
+ track("nested_event");
53
+ }
54
+ }
55
+ }
56
+
57
+ // ===== function =====
58
+ function service() {
59
+ track("function_event");
60
+ }
61
+
62
+ // ===== class =====
63
+ class AnotherService {
64
+ run() {
65
+ track("class_event");
66
+ }
67
+ }
68
+
69
+ // ===== direct =====
70
+ track("direct_event");
@@ -0,0 +1,63 @@
1
+ /* eslint-disable */
2
+
3
+ declare function track(event: string): void;
4
+
5
+ const eventName = "variable_event";
6
+
7
+ // ===== direct =====
8
+ track("node_event");
9
+
10
+ // ===== multiline =====
11
+ track(
12
+ "multiline_event"
13
+ );
14
+
15
+ // ===== template string =====
16
+ track(`template_event`);
17
+
18
+ // ===== conditional =====
19
+ if (true) {
20
+ track("conditional_event");
21
+ }
22
+
23
+ // ===== ternary =====
24
+ true
25
+ ? track("ternary_a")
26
+ : track("ternary_b");
27
+
28
+ // ===== variable =====
29
+ track(eventName);
30
+
31
+ // ===== function =====
32
+ function run() {
33
+ track("function_event");
34
+ }
35
+
36
+ // ===== arrow =====
37
+ const handler = () => {
38
+ track("arrow_event");
39
+ };
40
+
41
+ // ===== class =====
42
+ class Service {
43
+ run() {
44
+ track("class_event");
45
+ }
46
+ }
47
+
48
+ // ===== nested =====
49
+ function outer() {
50
+ function inner() {
51
+ track("nested_event");
52
+ }
53
+ }
54
+
55
+ // ===== async =====
56
+ async function asyncRun() {
57
+ track("async_event");
58
+ }
59
+
60
+ // ===== promise =====
61
+ Promise.resolve().then(() => {
62
+ track("promise_event");
63
+ });
@@ -0,0 +1,101 @@
1
+ /* eslint-disable */
2
+
3
+ declare function track(event: string): void;
4
+ declare function trackFeature(event: string): void;
5
+
6
+ declare const analytics: any;
7
+ declare const Button: any;
8
+ declare const TrackedButton: any;
9
+ declare const MyButton: any;
10
+
11
+ const eventName = "variable_event";
12
+
13
+ export default function Page() {
14
+ // ===== direct =====
15
+ track("next_event");
16
+
17
+ // ===== multiline =====
18
+ track(
19
+ "multiline_event"
20
+ );
21
+
22
+ // ===== template string =====
23
+ track(`template_event`);
24
+
25
+ // ===== wrapper function =====
26
+ trackFeature("wrapper_function");
27
+
28
+ // ===== object =====
29
+ analytics.track("object_track");
30
+
31
+ // ===== nested object =====
32
+ analytics.events.track("nested_track");
33
+
34
+ // ===== variable =====
35
+ track(eventName);
36
+
37
+ // ===== conditional =====
38
+ if (true) {
39
+ track("conditional_event");
40
+ }
41
+
42
+ // ===== ternary =====
43
+ true
44
+ ? track("ternary_a")
45
+ : track("ternary_b");
46
+
47
+ // ===== arrow function =====
48
+ const run = () => {
49
+ track("arrow_event");
50
+ };
51
+
52
+ // ===== function =====
53
+ function test() {
54
+ track("function_event");
55
+ }
56
+
57
+ // ===== class =====
58
+ class Service {
59
+ run() {
60
+ track("class_event");
61
+ }
62
+ }
63
+
64
+ return (
65
+ <>
66
+ {/* basic */}
67
+ <Button event="next_button" />
68
+
69
+ {/* expression */}
70
+ <Button event={"expression_event"} />
71
+
72
+ {/* wrapper component */}
73
+ <TrackedButton event="tracked_button" />
74
+
75
+ {/* another wrapper */}
76
+ <MyButton event="my_button" />
77
+
78
+ {/* nested */}
79
+ <div>
80
+ <Button event="nested_button" />
81
+ </div>
82
+
83
+ {/* conditional */}
84
+ {true && (
85
+ <Button event="conditional_button" />
86
+ )}
87
+
88
+ {/* ternary */}
89
+ {true
90
+ ? <Button event="ternary_a_button" />
91
+ : <Button event="ternary_b_button" />
92
+ }
93
+
94
+ {/* array */}
95
+ {[
96
+ <Button key="1" event="array_1" />,
97
+ <Button key="2" event="array_2" />
98
+ ]}
99
+ </>
100
+ );
101
+ }
@@ -0,0 +1,104 @@
1
+ /* eslint-disable */
2
+
3
+ declare function track(event: string): void;
4
+ declare function trackFeature(event: string): void;
5
+
6
+ declare const analytics: any;
7
+ declare const Button: any;
8
+ declare const TrackedButton: any;
9
+ declare const MyButton: any;
10
+
11
+ const eventName = "variable_event";
12
+
13
+ // ===== direct =====
14
+ track("direct_event");
15
+
16
+ // ===== multiline =====
17
+ track(
18
+ "multiline_event"
19
+ );
20
+
21
+ // ===== template string =====
22
+ track(`template_event`);
23
+
24
+ // ===== conditional =====
25
+ if (true) {
26
+ track("conditional_event");
27
+ }
28
+
29
+ // ===== ternary =====
30
+ true
31
+ ? track("ternary_a")
32
+ : track("ternary_b");
33
+
34
+ // ===== wrapper function =====
35
+ trackFeature("wrapper_function");
36
+
37
+ // ===== object function =====
38
+ analytics.track("object_track");
39
+
40
+ // ===== nested object =====
41
+ analytics.events.track("nested_track");
42
+
43
+ // ===== variable (optional support) =====
44
+ track(eventName);
45
+
46
+ // ===== function =====
47
+ function test() {
48
+ track("function_event");
49
+ }
50
+
51
+ // ===== arrow function =====
52
+ const run = () => {
53
+ track("arrow_event");
54
+ };
55
+
56
+ // ===== class =====
57
+ class TestService {
58
+ run() {
59
+ track("class_event");
60
+ }
61
+ }
62
+
63
+ // ===== component return =====
64
+ export default function Page() {
65
+ track("page_event");
66
+
67
+ return (
68
+ <>
69
+ {/* basic */}
70
+ <Button event="button_event" />
71
+
72
+ {/* wrapper component */}
73
+ <TrackedButton event="tracked_button" />
74
+
75
+ {/* another wrapper */}
76
+ <MyButton event="my_button" />
77
+
78
+ {/* expression */}
79
+ <Button event={"expression_event"} />
80
+
81
+ {/* nested */}
82
+ <div>
83
+ <Button event="nested_button" />
84
+ </div>
85
+
86
+ {/* conditional */}
87
+ {true && (
88
+ <Button event="conditional_button" />
89
+ )}
90
+
91
+ {/* ternary */}
92
+ {true
93
+ ? <Button event="ternary_a_button" />
94
+ : <Button event="ternary_b_button" />
95
+ }
96
+
97
+ {/* array */}
98
+ {[
99
+ <Button key="1" event="array_1" />,
100
+ <Button key="2" event="array_2" />
101
+ ]}
102
+ </>
103
+ );
104
+ }