@eventra_dev/cli-plugin-vue 1.0.7 → 1.1.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/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);
@@ -30,7 +35,7 @@ function collectEventBindings(root, out) {
30
35
  function visitElementProps(props) {
31
36
  for (const prop of props) {
32
37
  if (prop.type === compiler_core_1.NodeTypes.ATTRIBUTE && prop.name === "event") {
33
- const value = prop.value?.content?.trim();
38
+ const value = prop.value?.content.trim();
34
39
  if (value) {
35
40
  out.push({ kind: "literal", value });
36
41
  }
@@ -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 });
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,12 @@ 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
+ // Vue already trims the expression; an empty `{{ }}` is dropped with the other empty expressions at the end of parseVueSfc
88
+ handlers.push({ handler: false, code: node.content.content });
60
89
  }
61
90
  }
62
91
  visitChildren(root.children);
@@ -80,10 +109,11 @@ function parseVueSfc(source, filePath) {
80
109
  });
81
110
  }
82
111
  const templateEvents = [];
112
+ const templateHandlers = [];
83
113
  const template = descriptor.template;
84
114
  if (template) {
85
115
  if (template.ast) {
86
- collectEventBindings(template.ast, templateEvents);
116
+ collectEventBindings(template.ast, templateEvents, templateHandlers);
87
117
  }
88
118
  else {
89
119
  // @vue/compiler-sfc only ever leaves `ast` unset when `src` is set
@@ -93,7 +123,7 @@ function parseVueSfc(source, filePath) {
93
123
  if (template.src) {
94
124
  const external = readSrcFile(filePath, template.src);
95
125
  if (external) {
96
- collectEventBindings((0, compiler_dom_1.parse)(external), templateEvents);
126
+ collectEventBindings((0, compiler_dom_1.parse)(external), templateEvents, templateHandlers);
97
127
  }
98
128
  }
99
129
  }
@@ -101,6 +131,8 @@ function parseVueSfc(source, filePath) {
101
131
  return {
102
132
  scripts,
103
133
  templateEvents,
134
+ // an empty expression (`@click=""`) holds no code
135
+ templateHandlers: templateHandlers.filter((handler) => handler.code !== ""),
104
136
  errors: errors.map((error) => {
105
137
  // @vue/compiler-sfc errors are always Error instances
106
138
  /* v8 ignore else */
@@ -138,11 +170,14 @@ function buildScriptModule(parsed) {
138
170
  const noCheck = first.lang !== "ts" && first.lang !== "tsx";
139
171
  parts.push((0, layout_1.placeBlocks)(parsed.scripts, noCheck ? "// @ts-nocheck" : undefined));
140
172
  }
141
- if (parsed.templateEvents.length > 0) {
173
+ if (parsed.templateEvents.length > 0 || parsed.templateHandlers.length > 0) {
142
174
  const lines = [
143
175
  "// --- vue template (auto-generated) ---",
144
176
  `declare function ${TEMPLATE_EVENT_CALLEE}(name: string): void;`,
145
177
  "function __eventraVueTemplate() {",
178
+ // template code goes through the same detection as script code, so a call to a tracking
179
+ // function inside a handler or an interpolation is an event like any other
180
+ ...parsed.templateHandlers.map(renderTemplateCode),
146
181
  ...parsed.templateEvents.map((binding) => binding.kind === "literal"
147
182
  ? ` ${TEMPLATE_EVENT_CALLEE}("${escapeString(binding.value)}");`
148
183
  : ` ${TEMPLATE_EVENT_CALLEE}(${binding.value});`),
@@ -152,6 +187,18 @@ function buildScriptModule(parsed) {
152
187
  }
153
188
  return `${parts.join("\n\n")}\n`;
154
189
  }
190
+ /**
191
+ * A handler is a statement list when it is written as one (`a(); b()`), an expression otherwise
192
+ * (`() => a()`, `handler`); both are valid as the body of a block, except a bare `function` which
193
+ * would read as a declaration. Other expressions are parenthesised: `{ a: 1 }` is an object here.
194
+ * The closing brace sits on its own line, so a trailing `// comment` cannot swallow it.
195
+ */
196
+ function renderTemplateCode({ handler, code }) {
197
+ if (handler && !/^(async\s+)?function\b/.test(code)) {
198
+ return ` {\n${code}\n }`;
199
+ }
200
+ return ` (${code}\n );`;
201
+ }
155
202
  function escapeString(value) {
156
203
  return value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
157
204
  }
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.1",
4
4
  "description": "Eventra CLI plugin — extract track() calls from Vue SFC (.vue) files",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -35,7 +35,8 @@
35
35
  "test": "vitest run",
36
36
  "test:watch": "vitest",
37
37
  "test:coverage": "vitest run --coverage",
38
- "typecheck": "tsc --noEmit -p tsconfig.json"
38
+ "typecheck": "tsc --noEmit -p tsconfig.json",
39
+ "test:mutation": "stryker run"
39
40
  },
40
41
  "dependencies": {
41
42
  "@vue/compiler-core": "^3.5.40",
@@ -43,6 +44,8 @@
43
44
  "@vue/compiler-sfc": "^3.5.40"
44
45
  },
45
46
  "devDependencies": {
47
+ "@stryker-mutator/core": "9.6.1",
48
+ "@stryker-mutator/vitest-runner": "9.6.1",
46
49
  "@types/node": "^20.0.0",
47
50
  "@vitest/coverage-v8": "^4.1.7",
48
51
  "typescript": "^5.3.0",