@embeddable.com/sdk-react 2.2.24 → 2.2.25

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/lib/index.js CHANGED
@@ -530,8 +530,15 @@ const componentMetaSchema = zod.z
530
530
  }
531
531
  });
532
532
 
533
+ const getObjectProperty = (value) => value.object.name + "." + value.property.name;
533
534
  // @ts-ignore
534
535
  const babelTraverse = traverse.default;
536
+ const parseAndTraverse = (code, visitor) => {
537
+ const ast = parser.parse(code || "", {
538
+ sourceType: "module",
539
+ });
540
+ babelTraverse(ast, visitor);
541
+ };
535
542
  const componentMetaValidator = (metaInfo) => {
536
543
  const result = componentMetaSchema.safeParse(metaInfo.meta);
537
544
  if (!result.success)
@@ -539,14 +546,58 @@ const componentMetaValidator = (metaInfo) => {
539
546
  let moduleNameErrors = validateModuleName(metaInfo);
540
547
  const variableErrors = validateVariables(metaInfo.meta);
541
548
  const eventErrors = validateComponentEvents(metaInfo);
542
- return [...moduleNameErrors, ...variableErrors, ...eventErrors];
549
+ const loadDataErrors = validateComponentProps(metaInfo);
550
+ return [
551
+ ...moduleNameErrors,
552
+ ...variableErrors,
553
+ ...eventErrors,
554
+ ...loadDataErrors,
555
+ ];
556
+ };
557
+ const validateComponentProps = (metaInfo) => {
558
+ const errors = [];
559
+ parseAndTraverse(metaInfo.moduleInfo.code, {
560
+ ExportDefaultDeclaration: (path) => {
561
+ var _a, _b, _c;
562
+ const componentConfig = path.node.declaration
563
+ .arguments[2];
564
+ const propsNode = (_a = componentConfig.properties) === null || _a === void 0 ? void 0 : _a.find((x) => { var _a; return ((_a = x.key) === null || _a === void 0 ? void 0 : _a.name) === "props"; });
565
+ // There is no props defined
566
+ if (!propsNode)
567
+ return;
568
+ const propsReturnStatement = propsNode.value.body.body.find((x) => x.type === "ReturnStatement");
569
+ const results = (_b = propsReturnStatement === null || propsReturnStatement === void 0 ? void 0 : propsReturnStatement.argument) === null || _b === void 0 ? void 0 : _b.properties.find((x) => { var _a; return ((_a = x === null || x === void 0 ? void 0 : x.key) === null || _a === void 0 ? void 0 : _a.name) === "results"; });
570
+ // There is no results defined inside props
571
+ if (results === undefined) {
572
+ return;
573
+ }
574
+ const loadDataProperties = (_c = results === null || results === void 0 ? void 0 : results.value) === null || _c === void 0 ? void 0 : _c.arguments[0].properties;
575
+ const dimensions = loadDataProperties.find((prop) => prop.key.name === "dimensions");
576
+ const timeDimensions = loadDataProperties.find((prop) => prop.key.name === "timeDimensions");
577
+ // There is no missuse of dimensions and timeDimensions
578
+ if (!dimensions || !timeDimensions)
579
+ return;
580
+ const usedDimensions = dimensions.value.elements.map((x) => getObjectProperty(x));
581
+ const usedTimeDimensions = timeDimensions.value.elements
582
+ .map((x) => x.properties)
583
+ .flatMap((group) => group
584
+ .filter((property) => property.key.name === "dimension")
585
+ .map((property) => getObjectProperty(property.value.object)));
586
+ usedDimensions.forEach((dimension) => {
587
+ if (usedTimeDimensions.includes(dimension)) {
588
+ errors.push(`Dimension ${dimension} should not be requested as both a dimension and a timeDimension.`);
589
+ }
590
+ });
591
+ },
592
+ });
593
+ return errors;
543
594
  };
544
595
  const validateComponentEvents = (metaInfo) => {
545
596
  var _a, _b;
546
597
  const definedEvents = (_b = (_a = metaInfo.meta.events) === null || _a === void 0 ? void 0 : _a.map((e) => e.name)) !== null && _b !== void 0 ? _b : [];
547
598
  let implementedEvents = [];
548
599
  const errors = [];
549
- babelTraverse(metaInfo.moduleInfo.ast, {
600
+ parseAndTraverse(metaInfo.moduleInfo.code, {
550
601
  ExportDefaultDeclaration: (path) => {
551
602
  var _a, _b, _c, _d;
552
603
  const componentConfig = path.node.declaration