@immense/vue-pom-generator 1.0.104 → 1.0.105

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/dist/index.mjs CHANGED
@@ -9,7 +9,7 @@ import { parse as parse$1, compileScript } from "@vue/compiler-sfc";
9
9
  import { parseExpression, parse } from "@babel/parser";
10
10
  import { NodeTypes, stringifyExpression, createSimpleExpression, processExpression, ConstantTypes, ElementTypes } from "@vue/compiler-core";
11
11
  import { Project, QuoteKind, NewLineKind, IndentationText, CodeBlockWriter, StructureKind, VariableDeclarationKind } from "ts-morph";
12
- import { isIdentifier, isObjectPattern, isRestElement, isObjectProperty, isArrayExpression, isStringLiteral, isTemplateLiteral, isArrowFunctionExpression, isExpressionStatement, isAssignmentPattern, isMemberExpression, isOptionalMemberExpression, isCallExpression, isOptionalCallExpression, isAssignmentExpression, isObjectExpression, isFile, isBlockStatement, isLogicalExpression, isConditionalExpression, isSequenceExpression, isProgram, VISITOR_KEYS, isBooleanLiteral, isNumericLiteral, isNullLiteral, isImportDeclaration, isImportSpecifier, isExportDefaultDeclaration, isExpression, isVariableDeclaration, isFunctionDeclaration, isFunctionExpression, isReturnStatement, isTSAsExpression, isTSTypeAssertion, isTSNonNullExpression, isForInStatement, isIfStatement } from "@babel/types";
12
+ import { isIdentifier, isObjectPattern, isRestElement, isObjectProperty, isArrayExpression, isStringLiteral, isTemplateLiteral, isArrowFunctionExpression, isExpressionStatement, isAssignmentPattern, isMemberExpression, isOptionalMemberExpression, isCallExpression, isOptionalCallExpression, isAssignmentExpression, isSequenceExpression, isObjectExpression, isFile, isBlockStatement, isLogicalExpression, isConditionalExpression, isProgram, isArrayPattern, VISITOR_KEYS, isBooleanLiteral, isNumericLiteral, isNullLiteral, isImportDeclaration, isImportSpecifier, isExportDefaultDeclaration, isExpression, isVariableDeclaration, isFunctionDeclaration, isFunctionExpression, isReturnStatement, isTSAsExpression, isTSTypeAssertion, isTSNonNullExpression, isForInStatement, isIfStatement } from "@babel/types";
13
13
  import { POM_ROUTER_GLOBAL_NAME } from "./router.mjs";
14
14
  import vue from "@vitejs/plugin-vue";
15
15
  import { performance } from "node:perf_hooks";
@@ -1354,6 +1354,145 @@ function findTemplateSlotScopeExpression(node) {
1354
1354
  function buildSlotScopeFallbackKeyExpression(identifier) {
1355
1355
  return `${identifier}.key ?? ${identifier}.data?.id ?? ${identifier}.id ?? ${identifier}.value ?? ${identifier}.url ?? ${identifier}`;
1356
1356
  }
1357
+ function getDegenerateSlotScopeFallbackKeyVariable(expression) {
1358
+ if (!expression) {
1359
+ return null;
1360
+ }
1361
+ const trimmed = expression.trim();
1362
+ if (!trimmed) {
1363
+ return null;
1364
+ }
1365
+ const parts = splitNullishCoalescingExpression(trimmed);
1366
+ if (parts.length < 2) {
1367
+ return null;
1368
+ }
1369
+ const variableName = parts[parts.length - 1].trim();
1370
+ if (!variableName) {
1371
+ return null;
1372
+ }
1373
+ let terminalIsIdentifier;
1374
+ try {
1375
+ terminalIsIdentifier = isIdentifier(parseExpression(variableName, { plugins: ["typescript"] }));
1376
+ } catch {
1377
+ terminalIsIdentifier = false;
1378
+ }
1379
+ if (!terminalIsIdentifier) {
1380
+ return null;
1381
+ }
1382
+ const toMemberChainBase = (node) => {
1383
+ let current = node;
1384
+ while (isMemberExpression(current) || isOptionalMemberExpression(current)) {
1385
+ current = current.object;
1386
+ }
1387
+ return current;
1388
+ };
1389
+ const allMembersOfVariable = parts.slice(0, -1).every((part) => {
1390
+ try {
1391
+ const memberAst = parseExpression(part, { plugins: ["typescript"] });
1392
+ const base = toMemberChainBase(memberAst);
1393
+ return isIdentifier(base) && base.name === variableName;
1394
+ } catch {
1395
+ return false;
1396
+ }
1397
+ });
1398
+ return allMembersOfVariable ? variableName : null;
1399
+ }
1400
+ function getSlotScopeVariablesUsedAsBareCallbackHandlers(templateNode) {
1401
+ const scopeExpression = findTemplateSlotScopeExpression(templateNode);
1402
+ if (!scopeExpression) {
1403
+ return [];
1404
+ }
1405
+ const scopeVariables = new Set(tryExtractSlotScopeVariableNames(scopeExpression));
1406
+ if (scopeVariables.size === 0) {
1407
+ return [];
1408
+ }
1409
+ const used = /* @__PURE__ */ new Set();
1410
+ const collectScopeBindings = (node) => {
1411
+ const names = [];
1412
+ const slotScope = findTemplateSlotScopeExpression(node);
1413
+ if (slotScope) {
1414
+ names.push(...tryExtractSlotScopeVariableNames(slotScope));
1415
+ }
1416
+ const vFor = node.props.find(
1417
+ (prop) => prop.type === NodeTypes.DIRECTIVE && prop.name === "for"
1418
+ );
1419
+ if (vFor?.exp) {
1420
+ const forSource = getVueExpressionSource(vFor.exp, "content", "compiled");
1421
+ if (forSource) {
1422
+ const aliasMatch = /^\s*([\s\S]+?)\s+(?:in|of)\s+[\s\S]+$/.exec(forSource);
1423
+ const aliasSource = aliasMatch?.[1];
1424
+ if (aliasSource) {
1425
+ try {
1426
+ const aliasAst = parseExpression(aliasSource, { plugins: ["typescript"] });
1427
+ if (isSequenceExpression(aliasAst)) {
1428
+ for (const part of aliasAst.expressions) {
1429
+ names.push(...collectBindingPatternNames(part));
1430
+ }
1431
+ } else {
1432
+ names.push(...collectBindingPatternNames(aliasAst));
1433
+ }
1434
+ } catch {
1435
+ }
1436
+ }
1437
+ }
1438
+ }
1439
+ return names;
1440
+ };
1441
+ const visit = (current, shadowed) => {
1442
+ if (!current || typeof current !== "object" || !("type" in current)) {
1443
+ return;
1444
+ }
1445
+ if (current.type === NodeTypes.ELEMENT) {
1446
+ const element = current;
1447
+ const isSlotTemplate = element.tag === "template" && findTemplateSlotScopeExpression(element) !== null;
1448
+ const isForElement = element.props.some(
1449
+ (prop) => prop.type === NodeTypes.DIRECTIVE && prop.name === "for"
1450
+ );
1451
+ if (!isSlotTemplate && !isForElement) {
1452
+ const handlerName = tryGetBareCallbackClickHandlerName(element);
1453
+ if (handlerName && !shadowed.has(handlerName) && scopeVariables.has(handlerName)) {
1454
+ used.add(handlerName);
1455
+ }
1456
+ }
1457
+ const introduced = collectScopeBindings(element);
1458
+ if (introduced.length > 0) {
1459
+ const innerShadowed = new Set(shadowed);
1460
+ for (const name of introduced) {
1461
+ innerShadowed.add(name);
1462
+ }
1463
+ for (const child of element.children ?? []) {
1464
+ visit(child, innerShadowed);
1465
+ }
1466
+ return;
1467
+ }
1468
+ for (const child of element.children ?? []) {
1469
+ visit(child, shadowed);
1470
+ }
1471
+ return;
1472
+ }
1473
+ if (current.type === NodeTypes.ROOT) {
1474
+ for (const child of current.children ?? []) {
1475
+ visit(child, shadowed);
1476
+ }
1477
+ return;
1478
+ }
1479
+ if (current.type === NodeTypes.IF) {
1480
+ for (const branch of current.branches ?? []) {
1481
+ visit(branch, shadowed);
1482
+ }
1483
+ return;
1484
+ }
1485
+ if (current.type === NodeTypes.IF_BRANCH || current.type === NodeTypes.FOR) {
1486
+ for (const child of current.children ?? []) {
1487
+ visit(child, shadowed);
1488
+ }
1489
+ }
1490
+ };
1491
+ for (const child of templateNode.children ?? []) {
1492
+ visit(child, /* @__PURE__ */ new Set());
1493
+ }
1494
+ return [...used];
1495
+ }
1357
1496
  function tryGetBindingIdentifierName(node) {
1358
1497
  if (!node) {
1359
1498
  return null;
@@ -1510,6 +1649,39 @@ function tryGetTemplateSlotScopeKeyInfo(expression) {
1510
1649
  const candidateExpression = bindingNode ? tryGetSlotScopeKeyCandidate(bindingNode)?.expression ?? null : null;
1511
1650
  return candidateExpression ? toResolvedKeyInfo(candidateExpression) : null;
1512
1651
  }
1652
+ function collectBindingPatternNames(node) {
1653
+ if (!node) {
1654
+ return [];
1655
+ }
1656
+ if (isIdentifier(node)) {
1657
+ return [node.name];
1658
+ }
1659
+ if (isAssignmentPattern(node)) {
1660
+ return collectBindingPatternNames(node.left);
1661
+ }
1662
+ if (isRestElement(node)) {
1663
+ return collectBindingPatternNames(node.argument);
1664
+ }
1665
+ if (isObjectPattern(node)) {
1666
+ const names = [];
1667
+ for (const property of node.properties) {
1668
+ if (isRestElement(property)) {
1669
+ names.push(...collectBindingPatternNames(property.argument));
1670
+ } else if (isObjectProperty(property)) {
1671
+ names.push(...collectBindingPatternNames(property.value));
1672
+ }
1673
+ }
1674
+ return names;
1675
+ }
1676
+ if (isArrayPattern(node)) {
1677
+ const names = [];
1678
+ for (const element of node.elements) {
1679
+ names.push(...collectBindingPatternNames(element));
1680
+ }
1681
+ return names;
1682
+ }
1683
+ return [];
1684
+ }
1513
1685
  function tryExtractSlotScopeVariableNames(expression) {
1514
1686
  const bindingNode = tryGetTemplateSlotScopeBindingNode(expression);
1515
1687
  if (!bindingNode) {
@@ -9090,6 +9262,7 @@ function createTestIdTransform(componentName, componentHierarchyMap, nativeWrapp
9090
9262
  const conditionalMergeGroupByIfBranch = /* @__PURE__ */ new WeakMap();
9091
9263
  const componentInstanceNameOwnerByName = /* @__PURE__ */ new Map();
9092
9264
  let conditionalMergeGroupCounter = 0;
9265
+ const slotScopeCallbackVariablesByTemplate = /* @__PURE__ */ new WeakMap();
9093
9266
  const getElementLocationKey = (element) => {
9094
9267
  const startOffset = element.loc?.start.offset;
9095
9268
  const endOffset = element.loc?.end.offset;
@@ -9166,6 +9339,12 @@ function createTestIdTransform(componentName, componentHierarchyMap, nativeWrapp
9166
9339
  }
9167
9340
  const element = node;
9168
9341
  const parentIsRoot = context?.parent?.type === NodeTypes.ROOT;
9342
+ if (element.tag === "template" && !slotScopeCallbackVariablesByTemplate.has(element)) {
9343
+ const callbackVariables = getSlotScopeVariablesUsedAsBareCallbackHandlers(element);
9344
+ if (callbackVariables.length > 0) {
9345
+ slotScopeCallbackVariablesByTemplate.set(element, new Set(callbackVariables));
9346
+ }
9347
+ }
9169
9348
  const grandParent = context.grandParent;
9170
9349
  const parentElement = !parentIsRoot && context?.parent?.type === NodeTypes.ELEMENT ? context.parent : !parentIsRoot && grandParent?.type === NodeTypes.ELEMENT ? grandParent : null;
9171
9350
  hierarchyMap.set(element, parentElement);
@@ -9331,6 +9510,11 @@ File: ${context.filename ?? "unknown"}:${loc.line}:${loc.column}`
9331
9510
  if (isSlotScopeCallbackClickHandler(element, hierarchyMap)) {
9332
9511
  return null;
9333
9512
  }
9513
+ const slotTemplateNode = getContainedInSlotTemplateNode(element, hierarchyMap);
9514
+ const degenerateKeyVariable = getDegenerateSlotScopeFallbackKeyVariable(slotKeyInfo.rawExpression);
9515
+ if (degenerateKeyVariable && slotTemplateNode && slotScopeCallbackVariablesByTemplate.get(slotTemplateNode)?.has(degenerateKeyVariable)) {
9516
+ return null;
9517
+ }
9334
9518
  return slotKeyInfo;
9335
9519
  }
9336
9520
  if (crossFileKeyRegistry) {