@eventra_dev/cli-plugin-vue 1.0.7 → 1.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.
package/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  <p align="center">
8
8
  <a href="https://www.npmjs.com/package/@eventra_dev/cli-plugin-vue"><img alt="npm version" src="https://img.shields.io/npm/v/@eventra_dev/cli-plugin-vue.svg?style=flat-square&color=blue"></a>
9
9
  <a href="https://www.npmjs.com/package/@eventra_dev/cli-plugin-vue"><img alt="npm downloads" src="https://img.shields.io/npm/dm/@eventra_dev/cli-plugin-vue.svg?style=flat-square&color=blue"></a>
10
- <img alt="tests passing" src="https://img.shields.io/badge/tests-27%20passing-brightgreen?style=flat-square&logo=vitest&logoColor=white">
10
+ <img alt="tests passing" src="https://img.shields.io/badge/tests-51%20passing-brightgreen?style=flat-square&logo=vitest&logoColor=white">
11
11
  <img alt="coverage" src="https://img.shields.io/badge/coverage-100%25-brightgreen?style=flat-square&logo=vitest&logoColor=white">
12
12
  <img alt="node" src="https://img.shields.io/node/v/@eventra_dev/cli-plugin-vue?style=flat-square&color=darkgreen&logo=node.js&logoColor=white">
13
13
  <a href="https://www.typescriptlang.org/"><img alt="TypeScript" src="https://img.shields.io/badge/TypeScript-ready-blue?style=flat-square&logo=typescript&logoColor=white"></a>
@@ -92,6 +92,19 @@ The expression is copied as-is into the same module scope as the script. If it r
92
92
 
93
93
  ---
94
94
 
95
+ ### `<template>` - calls in handlers and expressions
96
+
97
+ Tracking calls written in the template itself are read, not only `event` attributes:
98
+
99
+ ```vue
100
+ <template>
101
+ <button @click="() => { open(); trackEvent('plan:cogm_open', { id }) }">Open</button>
102
+ <p>{{ trackEvent('panel:viewed') }}</p>
103
+ </template>
104
+ ```
105
+
106
+ Read: `@click` / `v-on` handlers (one or several statements, multi-line arrow functions), `{{ }}` interpolations the source of `v-for` (`item in items()`), dynamic arguments (`:[name()]`, `@[event()]`) and the expressions of `v-bind`, `v-if`, `v-else-if`, `v-show`, `v-model`, `v-html` and `v-text`. They are placed in the same module as the script, so a function imported or defined there resolves normally; an argument that comes from a `v-for` variable is reported as a dynamic occurrence. The loop variables of `v-for` and `v-slot` parameters are not code and are skipped; the value of a custom directive (`v-focus="x"`) is left alone.
107
+
95
108
  ## Nuxt
96
109
 
97
110
  Nuxt pages, layouts, and components are ordinary `.vue` SFCs — parsed identically, no extra configuration needed.
@@ -121,7 +134,7 @@ export interface CliPluginVue {
121
134
  }
122
135
  ```
123
136
 
124
- No dependency on `@eventra_dev/eventra-cli` — the CLI adapts this shape internally. `.vue` → one virtual `.vue.ts` module (compiled script content, then a function wrapping synthetic calls for every template `event="..."` binding). `staticSinks` describes those synthetic calls; the CLI builds its own sink detector from them. See [`@eventra_dev/eventra-cli`'s plugin docs](https://www.npmjs.com/package/@eventra_dev/eventra-cli#plugin-contract-for-authors) for the full external-plugin contract.
137
+ No dependency on `@eventra_dev/eventra-cli` - the CLI adapts this shape internally. `.vue` → one virtual `.vue.ts` module (compiled script content, then a function wrapping synthetic calls for every template `event="..."` binding and the handlers, interpolations and directive expressions copied from the template, under a `// --- vue template (auto-generated) ---` comment the CLI uses to tell generated code from your own lines). `staticSinks` describes those synthetic calls; the CLI builds its own sink detector from them. See [`@eventra_dev/eventra-cli`'s plugin docs](https://www.npmjs.com/package/@eventra_dev/eventra-cli#plugin-contract-for-authors) for the full external-plugin contract.
125
138
 
126
139
  ---
127
140
 
@@ -136,7 +149,7 @@ No dependency on `@eventra_dev/eventra-cli` — the CLI adapts this shape intern
136
149
 
137
150
  **100% statement/branch/function/line coverage** (v8 provider, `pnpm test:coverage`), enforced via a `coverage.thresholds` block in `vitest.config.ts`.
138
151
 
139
- **27 unit tests** (vitest), covering:
152
+ **51 unit tests** (vitest), covering:
140
153
 
141
154
  | Area | Covers |
142
155
  |---|---|
@@ -11,9 +11,20 @@ export interface TemplateEventBinding {
11
11
  readonly kind: "literal" | "dynamic";
12
12
  readonly value: string;
13
13
  }
14
+ /** One piece of template code. A handler may be several statements; any other expression is a single one. */
15
+ export interface TemplateHandler {
16
+ readonly handler: boolean;
17
+ readonly code: string;
18
+ }
14
19
  export interface ParsedVueSfc {
15
20
  readonly scripts: readonly VueScriptBlock[];
16
21
  readonly templateEvents: readonly TemplateEventBinding[];
22
+ /**
23
+ * Raw JS of the template's own code, copied verbatim: event handlers (`@click="() => track('x')"`,
24
+ * `@click="a(); b()"`), `{{ }}` interpolations and the expressions of `v-bind`/`v-if`/`v-show`/...
25
+ * Any call to a tracking function in there is an event the component fires.
26
+ */
27
+ readonly templateHandlers: readonly TemplateHandler[];
17
28
  readonly errors: readonly string[];
18
29
  }
19
30
  /** Parse a Vue SFC via the real `@vue/compiler-sfc` parser (handles comments, `src=`, malformed markup, etc). */
@@ -1 +1 @@
1
- {"version":3,"file":"parse-vue-sfc.d.ts","sourceRoot":"","sources":["../src/parse-vue-sfc.ts"],"names":[],"mappings":"AAeA,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,+FAA+F;IAC/F,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED,8EAA8E;AAC9E,MAAM,WAAW,oBAAoB;IACnC,0GAA0G;IAC1G,QAAQ,CAAC,IAAI,EAAE,SAAS,GAAG,SAAS,CAAC;IACrC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,OAAO,EAAE,SAAS,cAAc,EAAE,CAAC;IAC5C,QAAQ,CAAC,cAAc,EAAE,SAAS,oBAAoB,EAAE,CAAC;IACzD,QAAQ,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC;CACpC;AAyDD,iHAAiH;AACjH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MA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AiB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAEzD;AAID;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,YAAY,GAAG,MAAM,CAgC9D"}
@@ -21,7 +21,12 @@ function readSrcFile(sfcPath, src) {
21
21
  return null;
22
22
  }
23
23
  }
24
- function collectEventBindings(root, out) {
24
+ // the part of `item in items()` / `(item, i) of items()` that is an expression: after the `in` / `of`
25
+ const V_FOR_SOURCE = /\s(?:in|of)\s+([\s\S]+)$/;
26
+ // directives whose value is plain JS. `v-for` is handled apart (its value is `item in source`), and
27
+ // `v-slot` (a parameter list), `v-pre` and `v-cloak` hold no expression
28
+ const CODE_DIRECTIVES = new Set(["on", "bind", "if", "else-if", "show", "html", "text", "model"]);
29
+ function collectEventBindings(root, out, handlers) {
25
30
  function visitChildren(children) {
26
31
  for (const child of children) {
27
32
  visitNode(child);
@@ -46,6 +51,24 @@ function collectEventBindings(root, out) {
46
51
  if (expr) {
47
52
  out.push({ kind: "dynamic", value: expr });
48
53
  }
54
+ continue;
55
+ }
56
+ if (prop.type !== compiler_core_1.NodeTypes.DIRECTIVE)
57
+ continue;
58
+ // `:[expr]="..."` / `@[expr]="..."`: the argument is an expression of its own
59
+ if (prop.arg?.type === compiler_core_1.NodeTypes.SIMPLE_EXPRESSION && !prop.arg.isStatic) {
60
+ handlers.push({ handler: false, code: prop.arg.content.trim() });
61
+ }
62
+ if (prop.exp?.type !== compiler_core_1.NodeTypes.SIMPLE_EXPRESSION)
63
+ continue;
64
+ if (prop.name === "for") {
65
+ const source = V_FOR_SOURCE.exec(prop.exp.content)?.[1]?.trim();
66
+ if (source) {
67
+ handlers.push({ handler: false, code: source });
68
+ }
69
+ }
70
+ else if (CODE_DIRECTIVES.has(prop.name)) {
71
+ handlers.push({ handler: prop.name === "on", code: prop.exp.content.trim() });
49
72
  }
50
73
  }
51
74
  }
@@ -57,6 +80,14 @@ function collectEventBindings(root, out) {
57
80
  if (node.type === compiler_core_1.NodeTypes.ELEMENT) {
58
81
  visitElementProps(node.props);
59
82
  visitChildren(node.children);
83
+ return;
84
+ }
85
+ // `{{ expr }}`
86
+ if (node.type === compiler_core_1.NodeTypes.INTERPOLATION && node.content.type === compiler_core_1.NodeTypes.SIMPLE_EXPRESSION) {
87
+ const code = node.content.content.trim();
88
+ if (code) {
89
+ handlers.push({ handler: false, code });
90
+ }
60
91
  }
61
92
  }
62
93
  visitChildren(root.children);
@@ -80,10 +111,11 @@ function parseVueSfc(source, filePath) {
80
111
  });
81
112
  }
82
113
  const templateEvents = [];
114
+ const templateHandlers = [];
83
115
  const template = descriptor.template;
84
116
  if (template) {
85
117
  if (template.ast) {
86
- collectEventBindings(template.ast, templateEvents);
118
+ collectEventBindings(template.ast, templateEvents, templateHandlers);
87
119
  }
88
120
  else {
89
121
  // @vue/compiler-sfc only ever leaves `ast` unset when `src` is set
@@ -93,7 +125,7 @@ function parseVueSfc(source, filePath) {
93
125
  if (template.src) {
94
126
  const external = readSrcFile(filePath, template.src);
95
127
  if (external) {
96
- collectEventBindings((0, compiler_dom_1.parse)(external), templateEvents);
128
+ collectEventBindings((0, compiler_dom_1.parse)(external), templateEvents, templateHandlers);
97
129
  }
98
130
  }
99
131
  }
@@ -101,6 +133,8 @@ function parseVueSfc(source, filePath) {
101
133
  return {
102
134
  scripts,
103
135
  templateEvents,
136
+ // an empty expression (`@click=""`) holds no code
137
+ templateHandlers: templateHandlers.filter((handler) => handler.code !== ""),
104
138
  errors: errors.map((error) => {
105
139
  // @vue/compiler-sfc errors are always Error instances
106
140
  /* v8 ignore else */
@@ -138,11 +172,14 @@ function buildScriptModule(parsed) {
138
172
  const noCheck = first.lang !== "ts" && first.lang !== "tsx";
139
173
  parts.push((0, layout_1.placeBlocks)(parsed.scripts, noCheck ? "// @ts-nocheck" : undefined));
140
174
  }
141
- if (parsed.templateEvents.length > 0) {
175
+ if (parsed.templateEvents.length > 0 || parsed.templateHandlers.length > 0) {
142
176
  const lines = [
143
177
  "// --- vue template (auto-generated) ---",
144
178
  `declare function ${TEMPLATE_EVENT_CALLEE}(name: string): void;`,
145
179
  "function __eventraVueTemplate() {",
180
+ // template code goes through the same detection as script code, so a call to a tracking
181
+ // function inside a handler or an interpolation is an event like any other
182
+ ...parsed.templateHandlers.map(renderTemplateCode),
146
183
  ...parsed.templateEvents.map((binding) => binding.kind === "literal"
147
184
  ? ` ${TEMPLATE_EVENT_CALLEE}("${escapeString(binding.value)}");`
148
185
  : ` ${TEMPLATE_EVENT_CALLEE}(${binding.value});`),
@@ -152,6 +189,18 @@ function buildScriptModule(parsed) {
152
189
  }
153
190
  return `${parts.join("\n\n")}\n`;
154
191
  }
192
+ /**
193
+ * A handler is a statement list when it is written as one (`a(); b()`), an expression otherwise
194
+ * (`() => a()`, `handler`); both are valid as the body of a block, except a bare `function` which
195
+ * would read as a declaration. Other expressions are parenthesised: `{ a: 1 }` is an object here.
196
+ * The closing brace sits on its own line, so a trailing `// comment` cannot swallow it.
197
+ */
198
+ function renderTemplateCode({ handler, code }) {
199
+ if (handler && !/^(async\s+)?function\b/.test(code)) {
200
+ return ` {\n${code}\n }`;
201
+ }
202
+ return ` (${code}\n );`;
203
+ }
155
204
  function escapeString(value) {
156
205
  return value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
157
206
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eventra_dev/cli-plugin-vue",
3
- "version": "1.0.7",
3
+ "version": "1.1.0",
4
4
  "description": "Eventra CLI plugin — extract track() calls from Vue SFC (.vue) files",
5
5
  "license": "MIT",
6
6
  "author": {