@eventra_dev/cli-plugin-vue 1.2.4 → 1.3.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-127%20passing-brightgreen?style=flat-square&logo=vitest&logoColor=white">
10
+ <img alt="tests passing" src="https://img.shields.io/badge/tests-147%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>
@@ -130,13 +130,13 @@ import Button from './Button.vue'
130
130
 
131
131
  **Not followed** (those components are scanned as before, so nothing is lost, but a prop that only they use is not found):
132
132
 
133
- - Options API components (`props: [...]` read through `this`).
134
- - `v-bind="obj"` spreads.
133
+ - An Options API component whose `export default` is not the last statement of its `<script>`, whose `props` is not written out in the object (`props: sharedProps`), or whose `this` is read inside a nested `function` (an arrow keeps it). A component with `props: [...]` or `props: { ... }` and `this.x` in its methods is followed, with or without `defineComponent`.
134
+ - `v-bind="obj"` when `obj` is not an object the analysis can see into (a call, a parameter of the page); an object written out or held in a `const` is read, in its place among the props.
135
135
  - Components registered by name instead of imported, and dynamic components (`<component :is="X">`).
136
136
  - Nuxt auto-imported components (used with no `import`): the tag is not matched to its file.
137
137
  - A component whose normal `<script>` has its own `export default`, an `export` other than a type in `<script setup>`, an import after other code, or a `src=` script.
138
138
 
139
- A value the CLI cannot resolve to a plain string (a `v-for` variable, a prop of the page itself) is listed under "Dynamic event names" instead of becoming an event. The same list shows the tracking call inside the component itself, one line per component, because what it tracks is whatever each caller passes; the callers that pass a literal are still found.
139
+ A value the CLI cannot resolve to a plain string (a `v-for` variable, a prop of the page itself) is listed under "Dynamic event names" instead of becoming an event, with the reason (`env`, `variable`, `template`, `call`, `expression`). The tracking call inside the component itself is listed apart, under "Inside a wrapper", because what it tracks is whatever each caller passes; the callers that pass a literal are still found.
140
140
 
141
141
  ## Nuxt
142
142
 
@@ -182,7 +182,7 @@ No dependency on `@eventra_dev/eventra-cli` - the CLI adapts this shape internal
182
182
 
183
183
  **100% statement/branch/function/line coverage** (v8 provider, `pnpm test:coverage`), enforced via a `coverage.thresholds` block in `vitest.config.ts`.
184
184
 
185
- **127 unit tests** (vitest), covering:
185
+ **147 unit tests** (vitest), covering:
186
186
 
187
187
  | Area | Covers |
188
188
  |---|---|
@@ -31,4 +31,16 @@ export declare function scriptBindings(blocks: readonly VueScriptBlock[]): Scrip
31
31
  * component is scanned as before.
32
32
  */
33
33
  export declare function wrapScriptSetup(block: VueScriptBlock, componentName: string, declaredElsewhere: ReadonlySet<string>): ComponentModule | null;
34
+ /**
35
+ * Turns an Options API component that declares `props` into a function of them, like `wrapScriptSetup` does
36
+ * for `<script setup>`, so a prop that reaches a track() call through `this` is followed from the parent:
37
+ *
38
+ * export default { props: ["track"], methods: { go() { sdk.track(this.track) } } }
39
+ * -> function Button(__props: any) { const __options = { ... go() { sdk.track(__props.track) } } ...
40
+ *
41
+ * `defineComponent({...})` is read the same way. Statements keep their line numbers. Returns null when the
42
+ * block is not an options component with `props`, when `export default` is not the last statement, or when
43
+ * it is written some other way, in which case the component is scanned as before.
44
+ */
45
+ export declare function wrapOptionsApi(block: VueScriptBlock, componentName: string): ComponentModule | null;
34
46
  //# sourceMappingURL=component.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"component.d.ts","sourceRoot":"","sources":["../src/component.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAWtD,MAAM,WAAW,eAAe;IAC9B,oHAAoH;IACpH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,uEAAuE;IACvE,QAAQ,CAAC,SAAS,EAAE,SAAS,MAAM,EAAE,CAAC;IACtC,4FAA4F;IAC5F,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAID,wFAAwF;AACxF,MAAM,WAAW,cAAc;IAC7B,+GAA+G;IAC/G,QAAQ,CAAC,UAAU,EAAE,SAAS,MAAM,EAAE,CAAC;IACvC,QAAQ,CAAC,QAAQ,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IACvC,4GAA4G;IAC5G,QAAQ,CAAC,gBAAgB,EAAE,OAAO,CAAC;CACpC;AAuCD,wGAAwG;AACxG,wBAAgB,cAAc,CAAC,MAAM,EAAE,SAAS,cAAc,EAAE,GAAG,cAAc,CAiBhF;AA+DD;;;;;;;;;;;;GAYG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,EAAE,iBAAiB,EAAE,WAAW,CAAC,MAAM,CAAC,GAAG,eAAe,GAAG,IAAI,CAwE5I"}
1
+ {"version":3,"file":"component.d.ts","sourceRoot":"","sources":["../src/component.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAWtD,MAAM,WAAW,eAAe;IAC9B,oHAAoH;IACpH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,uEAAuE;IACvE,QAAQ,CAAC,SAAS,EAAE,SAAS,MAAM,EAAE,CAAC;IACtC,4FAA4F;IAC5F,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAID,wFAAwF;AACxF,MAAM,WAAW,cAAc;IAC7B,+GAA+G;IAC/G,QAAQ,CAAC,UAAU,EAAE,SAAS,MAAM,EAAE,CAAC;IACvC,QAAQ,CAAC,QAAQ,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IACvC,4GAA4G;IAC5G,QAAQ,CAAC,gBAAgB,EAAE,OAAO,CAAC;CACpC;AAuCD,wGAAwG;AACxG,wBAAgB,cAAc,CAAC,MAAM,EAAE,SAAS,cAAc,EAAE,GAAG,cAAc,CAiBhF;AA+DD;;;;;;;;;;;;GAYG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,EAAE,iBAAiB,EAAE,WAAW,CAAC,MAAM,CAAC,GAAG,eAAe,GAAG,IAAI,CAwE5I;AAmDD;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,GAAG,eAAe,GAAG,IAAI,CA4BnG"}
package/dist/component.js CHANGED
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.scriptBindings = scriptBindings;
4
4
  exports.wrapScriptSetup = wrapScriptSetup;
5
+ exports.wrapOptionsApi = wrapOptionsApi;
5
6
  const compiler_sfc_1 = require("@vue/compiler-sfc");
6
7
  const IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
7
8
  function parseScript(block) {
@@ -243,3 +244,105 @@ function wrapScriptSetup(block, componentName, declaredElsewhere) {
243
244
  code = code.slice(0, headerAt) + header + code.slice(headerAt);
244
245
  return { code, propNames: names, name: componentName };
245
246
  }
247
+ /** `props: ["a", "b"]` or `props: { a: String, b: { type: String } }` of an options object: the names a parent can pass. */
248
+ function optionsPropNames(options) {
249
+ for (const property of options.properties) {
250
+ if (property.type !== "ObjectProperty" || property.computed)
251
+ continue;
252
+ const key = property.key;
253
+ if (!(key.type === "Identifier" ? key.name === "props" : key.type === "StringLiteral" && key.value === "props"))
254
+ continue;
255
+ const value = property.value;
256
+ const names = [];
257
+ if (value.type === "ArrayExpression") {
258
+ for (const element of value.elements)
259
+ if (element?.type === "StringLiteral")
260
+ names.push(element.value);
261
+ }
262
+ else if (value.type === "ObjectExpression") {
263
+ for (const prop of value.properties) {
264
+ if (prop.type !== "ObjectProperty" && prop.type !== "ObjectMethod")
265
+ continue;
266
+ if (prop.key.type === "Identifier" && !prop.computed)
267
+ names.push(prop.key.name);
268
+ else if (prop.key.type === "StringLiteral")
269
+ names.push(prop.key.value);
270
+ }
271
+ }
272
+ else {
273
+ return null;
274
+ }
275
+ return names;
276
+ }
277
+ return null;
278
+ }
279
+ // a nested `function` has a `this` of its own, so a `this.x` inside it is not the component's
280
+ const OWN_THIS = new Set(["FunctionExpression", "FunctionDeclaration", "ClassMethod", "ClassPrivateMethod", "ClassDeclaration", "ClassExpression"]);
281
+ function thisEdits(node, props, out) {
282
+ if (OWN_THIS.has(node.type))
283
+ return;
284
+ // this.track -> __props.track
285
+ if (node.type === "MemberExpression" && node.object.type === "ThisExpression" && !node.computed && node.property.type === "Identifier" && props.has(node.property.name)) {
286
+ out.push({ start: node.start, end: node.end, text: `__props.${node.property.name}` });
287
+ return;
288
+ }
289
+ // const { track } = this -> const { track } = __props
290
+ if (node.type === "VariableDeclarator" && node.id.type === "ObjectPattern" && node.init?.type === "ThisExpression") {
291
+ out.push({ start: node.init.start, end: node.init.end, text: "__props" });
292
+ }
293
+ for (const key of Object.keys(node)) {
294
+ if (key === "loc" || key === "leadingComments" || key === "trailingComments" || key === "innerComments")
295
+ continue;
296
+ const child = node[key];
297
+ if (Array.isArray(child)) {
298
+ for (const item of child)
299
+ if (item && typeof item.type === "string")
300
+ thisEdits(item, props, out);
301
+ }
302
+ else if (child && typeof child.type === "string") {
303
+ thisEdits(child, props, out);
304
+ }
305
+ }
306
+ }
307
+ /**
308
+ * Turns an Options API component that declares `props` into a function of them, like `wrapScriptSetup` does
309
+ * for `<script setup>`, so a prop that reaches a track() call through `this` is followed from the parent:
310
+ *
311
+ * export default { props: ["track"], methods: { go() { sdk.track(this.track) } } }
312
+ * -> function Button(__props: any) { const __options = { ... go() { sdk.track(__props.track) } } ...
313
+ *
314
+ * `defineComponent({...})` is read the same way. Statements keep their line numbers. Returns null when the
315
+ * block is not an options component with `props`, when `export default` is not the last statement, or when
316
+ * it is written some other way, in which case the component is scanned as before.
317
+ */
318
+ function wrapOptionsApi(block, componentName) {
319
+ if (block.setup || block.startLine === undefined)
320
+ return null;
321
+ const statements = parseScript(block);
322
+ if (!statements)
323
+ return null;
324
+ const last = statements[statements.length - 1];
325
+ if (last?.type !== "ExportDefaultDeclaration")
326
+ return null;
327
+ let options = last.declaration;
328
+ if (options.type === "CallExpression" && options.callee.type === "Identifier" && options.callee.name === "defineComponent" && options.arguments.length === 1) {
329
+ options = options.arguments[0];
330
+ }
331
+ if (options.type !== "ObjectExpression")
332
+ return null;
333
+ const names = optionsPropNames(options);
334
+ if (!names || names.length === 0)
335
+ return null;
336
+ const edits = [];
337
+ thisEdits(options, new Set(names), edits);
338
+ // the header replaces `export default [defineComponent(]`, and what follows the options is dropped
339
+ // (`)` and `;`), keeping the line breaks of both, so the lines of the code keep their numbers
340
+ const header = `${/\bawait\b/.test(block.content) ? "async " : ""}function ${componentName}(__props: any) { const __options = `;
341
+ edits.push({ start: last.start, end: options.start, text: header + blank(block.content.slice(last.start, options.start)) });
342
+ edits.push({ start: options.end, end: last.end, text: `;${blank(block.content.slice(options.end, last.end))}` });
343
+ let code = block.content;
344
+ for (const edit of edits.sort((a, b) => b.start - a.start)) {
345
+ code = code.slice(0, edit.start) + edit.text + code.slice(edit.end);
346
+ }
347
+ return { code, propNames: names, name: componentName };
348
+ }
@@ -20,7 +20,8 @@ export interface TemplateHandler {
20
20
  export interface TemplateComponentProp {
21
21
  /** camelCased, the way the child declares it */
22
22
  readonly name: string;
23
- readonly kind: "literal" | "expression";
23
+ /** `spread`: `v-bind="obj"`, whose properties are props in the place it stands among the others (`name` is empty). */
24
+ readonly kind: "literal" | "expression" | "spread";
24
25
  readonly value: string;
25
26
  }
26
27
  /** An element that may be a child component, with the props written on it. Whether it is one depends on the script's imports. */
@@ -1 +1 @@
1
- {"version":3,"file":"parse-vue-sfc.d.ts","sourceRoot":"","sources":["../src/parse-vue-sfc.ts"],"names":[],"mappings":"AAgBA,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,6GAA6G;AAC7G,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED,+GAA+G;AAC/G,MAAM,WAAW,qBAAqB;IACpC,gDAAgD;IAChD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,SAAS,GAAG,YAAY,CAAC;IACxC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB;AAED,iIAAiI;AACjI,MAAM,WAAW,sBAAsB;IACrC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,KAAK,EAAE,SAAS,qBAAqB,EAAE,CAAC;CAClD;AAED,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,OAAO,EAAE,SAAS,cAAc,EAAE,CAAC;IAC5C,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,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,sHAAsH;IACtH,QAAQ,CAAC,IAAI,EAAE,SAAS,GAAG,YAAY,GAAG,QAAQ,CAAC;IACnD,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB;AAED,iIAAiI;AACjI,MAAM,WAAW,sBAAsB;IACrC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,KAAK,EAAE,SAAS,qBAAqB,EAAE,CAAC;CAClD;AAED,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,OAAO,EAAE,SAAS,cAAc,EAAE,CAAC;IAC5C,QAAQ,CAAC,cAAc,EAAE,SAAS,oBAAoB,EAAE,CAAC;IACzD;;;;OAIG;IACH,QAAQ,CAAC,gBAAgB,EAAE,SAAS,eAAe,EAAE,CAAC;IACtD,8GAA8G;IAC9G,QAAQ,CAAC,eAAe,CAAC,EAAE,SAAS,sBAAsB,EAAE,CAAC;IAC7D,QAAQ,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC;CACpC;AAmHD,iHAAiH;AACjH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,YAAY,CAoD1E;AAED,gFAAgF;AAChF,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAEzD;AAID;;;;;;;;;;;;;GAaG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,YAAY,EAAE,QAAQ,SAAK,GAAG,MAAM,CAuD7E"}
@@ -42,6 +42,11 @@ function collectEventBindings(root, out, handlers, usages) {
42
42
  found.push({ name: camelize(prop.name), kind: "literal", value: prop.value.content });
43
43
  continue;
44
44
  }
45
+ // `v-bind="obj"`: every property of the object is a prop, and a later prop wins over it, as at runtime
46
+ if (prop.name === "bind" && !prop.arg && prop.exp?.type === compiler_core_1.NodeTypes.SIMPLE_EXPRESSION && prop.exp.content.trim()) {
47
+ found.push({ name: "", kind: "spread", value: prop.exp.content.trim() });
48
+ continue;
49
+ }
45
50
  // `:track="expr"`; the same-name shorthand `:track` has no expression and reads the variable of that name
46
51
  if (prop.name === "bind" && prop.arg?.type === compiler_core_1.NodeTypes.SIMPLE_EXPRESSION && prop.arg.isStatic) {
47
52
  const name = camelize(prop.arg.content);
@@ -195,7 +200,8 @@ function buildScriptModule(parsed, filePath = "") {
195
200
  const last = parsed.scripts[parsed.scripts.length - 1];
196
201
  const others = last ? (0, component_1.scriptBindings)(parsed.scripts.slice(0, -1)) : undefined;
197
202
  if (last && !others?.hasDefaultExport) {
198
- const wrapped = (0, component_1.wrapScriptSetup)(last, componentName(filePath, bindings.declared), others.declared);
203
+ const name = componentName(filePath, bindings.declared);
204
+ const wrapped = (0, component_1.wrapScriptSetup)(last, name, others.declared) ?? (0, component_1.wrapOptionsApi)(last, name);
199
205
  if (wrapped) {
200
206
  scripts = [...parsed.scripts.slice(0, -1), { ...last, content: wrapped.code }];
201
207
  component = wrapped.name;
@@ -260,6 +266,8 @@ function componentCalls(usages, components) {
260
266
  if (!binding)
261
267
  continue;
262
268
  const props = usage.props.map((prop) => {
269
+ if (prop.kind === "spread")
270
+ return `...(${prop.value}\n)`;
263
271
  const key = KEY.test(prop.name) ? prop.name : `"${escapeString(prop.name)}"`;
264
272
  return `${key}: ${prop.kind === "literal" ? `"${escapeString(prop.value)}"` : `(${prop.value}\n)`}`;
265
273
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eventra_dev/cli-plugin-vue",
3
- "version": "1.2.4",
3
+ "version": "1.3.0",
4
4
  "description": "Eventra CLI plugin — extract track() calls from Vue SFC (.vue) files",
5
5
  "license": "MIT",
6
6
  "author": {