@next-core/brick-kit 2.201.0 → 2.203.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.
@@ -1446,11 +1446,22 @@
1446
1446
  for (var fn of functions) {
1447
1447
  registeredFunctions.set(fn.name, {
1448
1448
  source: fn.source,
1449
- typescript: fn.typescript
1449
+ typescript: fn.typescript,
1450
+ transformed: fn.transformed
1450
1451
  });
1451
1452
  }
1452
1453
  }
1453
1454
  }
1455
+ function getGlobalVariables(globals) {
1456
+ return supply(globals, getGeneralGlobals(globals, {
1457
+ collectCoverage,
1458
+ widgetId,
1459
+ widgetVersion,
1460
+ app: currentApp,
1461
+ storyboardFunctions,
1462
+ isStoryboardFunction: true
1463
+ }), !!collectCoverage);
1464
+ }
1454
1465
  function getStoryboardFunction(name) {
1455
1466
  var fn = registeredFunctions.get(name);
1456
1467
  if (!fn) {
@@ -1463,35 +1474,39 @@
1463
1474
  if (collectCoverage) {
1464
1475
  collector = collectCoverage.createCollector(name);
1465
1476
  }
1466
- var precooked = brickUtils.precookFunction(fn.source, {
1467
- cacheKey: fn,
1468
- typescript: fn.typescript,
1469
- hooks: collector && {
1470
- beforeVisit: collector.beforeVisit
1471
- }
1472
- });
1473
- fn.cooked = brickUtils.cook(precooked.function, fn.source, {
1474
- rules: {
1475
- noVar: true
1476
- },
1477
- globalVariables: supply(precooked.attemptToVisitGlobals, getGeneralGlobals(precooked.attemptToVisitGlobals, {
1478
- collectCoverage,
1479
- widgetId,
1480
- widgetVersion,
1481
- app: currentApp,
1482
- storyboardFunctions,
1483
- isStoryboardFunction: true
1484
- }), !!collectCoverage),
1485
- hooks: _objectSpread__default["default"]({
1486
- perfCall: needPerf ? duration => {
1487
- perf(name, fn.source, duration);
1488
- } : undefined
1489
- }, collector ? {
1490
- beforeEvaluate: collector.beforeEvaluate,
1491
- beforeCall: collector.beforeCall,
1492
- beforeBranch: collector.beforeBranch
1493
- } : null)
1494
- });
1477
+
1478
+ // Do not use transformed functions when collecting coverage.
1479
+ var transformed = !collector && fn.transformed;
1480
+ if (transformed) {
1481
+ var globalVariables = getGlobalVariables(transformed.globals);
1482
+ // Spread globals as params to prevent accessing forbidden globals.
1483
+ // NOTE: in native mode, forbidden globals are declared as `undefined`,
1484
+ // thus accessing them will not throw a ReferenceError.
1485
+ fn.cooked = new Function(...transformed.globals, "\"use strict\";return (".concat(transformed.source, ")"))(...transformed.globals.map(key => brickUtils.hasOwnProperty(globalVariables, key) ? globalVariables[key] : undefined));
1486
+ } else {
1487
+ var precooked = brickUtils.precookFunction(fn.source, {
1488
+ cacheKey: fn,
1489
+ typescript: fn.typescript,
1490
+ hooks: collector && {
1491
+ beforeVisit: collector.beforeVisit
1492
+ }
1493
+ });
1494
+ fn.cooked = brickUtils.cook(precooked.function, fn.source, {
1495
+ rules: {
1496
+ noVar: true
1497
+ },
1498
+ globalVariables: getGlobalVariables(precooked.attemptToVisitGlobals),
1499
+ hooks: _objectSpread__default["default"]({
1500
+ perfCall: needPerf ? duration => {
1501
+ perf(name, fn.source, duration);
1502
+ } : undefined
1503
+ }, collector ? {
1504
+ beforeEvaluate: collector.beforeEvaluate,
1505
+ beforeCall: collector.beforeCall,
1506
+ beforeBranch: collector.beforeBranch
1507
+ } : null)
1508
+ });
1509
+ }
1495
1510
  fn.processed = true;
1496
1511
  return fn.cooked;
1497
1512
  }
@@ -1501,7 +1516,8 @@
1501
1516
  updateStoryboardFunction(name, data) {
1502
1517
  registeredFunctions.set(name, {
1503
1518
  source: data.source,
1504
- typescript: data.typescript
1519
+ typescript: data.typescript,
1520
+ transformed: data.transformed
1505
1521
  });
1506
1522
  }
1507
1523
  };
@@ -3238,7 +3254,7 @@
3238
3254
  }, computedBaseValue);
3239
3255
  }
3240
3256
 
3241
- function setupTemplateProxy(proxyContext, ref, slots) {
3257
+ function setupTemplateProxy(proxyContext, ref, slots, slotted) {
3242
3258
  var computedPropsFromProxy = {};
3243
3259
  var refForProxy;
3244
3260
  var {
@@ -3284,6 +3300,9 @@
3284
3300
  // Every quasi (indexed by `refPosition`) can be slotted with multiple bricks.
3285
3301
  var quasisMap = new Map();
3286
3302
  if (reversedProxies.slots.has(ref)) {
3303
+ if (slotted) {
3304
+ throw new Error("Can not have proxied slot ref when the ref target has a slot element child, check your template \"".concat(proxyBrick.type, "\" and ref \"").concat(ref, "\""));
3305
+ }
3287
3306
  for (var item of reversedProxies.slots.get(ref)) {
3288
3307
  var _item$refPosition, _externalSlots$item$$, _externalSlots$item$$2;
3289
3308
  if (!quasisMap.has(item.refSlot)) {
@@ -8435,6 +8454,38 @@
8435
8454
  }
8436
8455
  }
8437
8456
 
8457
+ function isEvaluable(raw) {
8458
+ return /^\s*<%[~=]?\s/.test(raw) && /\s%>\s*$/.test(raw);
8459
+ }
8460
+
8461
+ function replaceSlotWithSlottedBricks(brickConf, proxyContext, expand) {
8462
+ var _brickConf$properties, _brickConf$properties2, _slots$$bricks, _slots, _slots$;
8463
+ // Currently, no support for `if` in a slot.
8464
+ if (brickConf.if != null && !brickConf.if || typeof brickConf.if === "string") {
8465
+ throw new Error("Can not use \"if\" in a slot currently, check your template \"".concat(proxyContext.proxyBrick.type, "\""));
8466
+ }
8467
+ var slot = String((_brickConf$properties = (_brickConf$properties2 = brickConf.properties) === null || _brickConf$properties2 === void 0 ? void 0 : _brickConf$properties2.name) !== null && _brickConf$properties !== void 0 ? _brickConf$properties : "");
8468
+
8469
+ // Currently, no support for expression as slot name.
8470
+ if (isEvaluable(slot)) {
8471
+ throw new Error("Can not use an expression as slot name \"".concat(slot, "\" currently, check your template \"").concat(proxyContext.proxyBrick.type, "\""));
8472
+ }
8473
+
8474
+ // Do not repeat the same slot name in a template.
8475
+ if (proxyContext.usedSlots.has(slot)) {
8476
+ throw new Error("Can not have multiple slots with the same name \"".concat(slot, "\", check your template \"").concat(proxyContext.proxyBrick.type, "\""));
8477
+ }
8478
+ proxyContext.usedSlots.add(slot);
8479
+ if (proxyContext.externalSlots && brickUtils.hasOwnProperty(proxyContext.externalSlots, slot)) {
8480
+ var _proxyContext$externa;
8481
+ var insertBricks = (_proxyContext$externa = proxyContext.externalSlots[slot].bricks) !== null && _proxyContext$externa !== void 0 ? _proxyContext$externa : [];
8482
+ if (insertBricks.length > 0) {
8483
+ return insertBricks;
8484
+ }
8485
+ }
8486
+ return ((_slots$$bricks = (_slots = brickConf.slots) === null || _slots === void 0 ? void 0 : (_slots$ = _slots[""]) === null || _slots$ === void 0 ? void 0 : _slots$.bricks) !== null && _slots$$bricks !== void 0 ? _slots$$bricks : []).flatMap(item => expand(item, proxyContext));
8487
+ }
8488
+
8438
8489
  var _excluded$4 = ["properties", "slots"],
8439
8490
  _excluded2 = ["ref", "slots"];
8440
8491
  function expandCustomTemplate(brickConf, proxyBrick, context) {
@@ -8563,38 +8614,48 @@
8563
8614
  templateProperties,
8564
8615
  externalSlots: externalSlots,
8565
8616
  templateContextId: tplContext.id,
8566
- proxyBrick
8617
+ proxyBrick,
8618
+ usedSlots: new Set()
8567
8619
  };
8568
8620
  newBrickConf.slots = {
8569
8621
  "": {
8570
8622
  type: "bricks",
8571
- bricks: bricks.map(item => expandBrickInTemplate(item, proxyContext))
8623
+ bricks: bricks.flatMap(item => expandBrickInTemplate(item, proxyContext))
8572
8624
  }
8573
8625
  };
8574
8626
  return newBrickConf;
8575
8627
  }
8576
- function expandBrickInTemplate(brickConfInTemplate, proxyContext) {
8628
+ function expandBrickInTemplate(brickConfInTemplate, proxyContext, markSlotted) {
8577
8629
  // Ignore `if: null` to make `looseCheckIf` working.
8578
8630
  if (brickConfInTemplate.if === null) {
8579
8631
  delete brickConfInTemplate.if;
8580
8632
  }
8581
- var {
8633
+ if (brickConfInTemplate.brick === "slot") {
8634
+ markSlotted === null || markSlotted === void 0 ? void 0 : markSlotted();
8635
+ return replaceSlotWithSlottedBricks(brickConfInTemplate, proxyContext, expandBrickInTemplate);
8636
+ }
8637
+ var _ref = brickConfInTemplate,
8638
+ {
8582
8639
  ref,
8583
8640
  slots: slotsInTemplate
8584
- } = brickConfInTemplate,
8585
- restBrickConfInTemplate = _objectWithoutProperties__default["default"](brickConfInTemplate, _excluded2);
8586
- var slots = Object.fromEntries(Object.entries(slotsInTemplate !== null && slotsInTemplate !== void 0 ? slotsInTemplate : {}).map(_ref => {
8641
+ } = _ref,
8642
+ restBrickConfInTemplate = _objectWithoutProperties__default["default"](_ref, _excluded2);
8643
+ var slotted = false;
8644
+ var markChild = () => {
8645
+ slotted = true;
8646
+ };
8647
+ var slots = Object.fromEntries(Object.entries(slotsInTemplate !== null && slotsInTemplate !== void 0 ? slotsInTemplate : {}).map(_ref2 => {
8587
8648
  var _slotConf$bricks;
8588
- var [slotName, slotConf] = _ref;
8649
+ var [slotName, slotConf] = _ref2;
8589
8650
  return [slotName, {
8590
8651
  type: "bricks",
8591
- bricks: ((_slotConf$bricks = slotConf.bricks) !== null && _slotConf$bricks !== void 0 ? _slotConf$bricks : []).map(item => expandBrickInTemplate(item, proxyContext))
8652
+ bricks: ((_slotConf$bricks = slotConf.bricks) !== null && _slotConf$bricks !== void 0 ? _slotConf$bricks : []).flatMap(item => expandBrickInTemplate(item, proxyContext, markChild))
8592
8653
  }];
8593
8654
  }));
8594
8655
  return _objectSpread__default["default"](_objectSpread__default["default"]({}, restBrickConfInTemplate), {}, {
8595
8656
  properties: setupUseBrickInTemplate(brickConfInTemplate.properties, proxyContext),
8596
8657
  slots
8597
- }, setupTemplateProxy(proxyContext, ref, slots));
8658
+ }, setupTemplateProxy(proxyContext, ref, slots, slotted));
8598
8659
  }
8599
8660
 
8600
8661
  // If it's a custom template, return the tag name of the template.