babel-plugin-jsx-dom-expressions 0.31.0-beta.0 → 0.31.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.
Files changed (2) hide show
  1. package/index.js +119 -122
  2. package/package.json +5 -5
package/index.js CHANGED
@@ -16,14 +16,12 @@ function _interopNamespace(e) {
16
16
  var d = Object.getOwnPropertyDescriptor(e, k);
17
17
  Object.defineProperty(n, k, d.get ? d : {
18
18
  enumerable: true,
19
- get: function () {
20
- return e[k];
21
- }
19
+ get: function () { return e[k]; }
22
20
  });
23
21
  }
24
22
  });
25
23
  }
26
- n['default'] = e;
24
+ n["default"] = e;
27
25
  return Object.freeze(n);
28
26
  }
29
27
 
@@ -228,35 +226,26 @@ function registerImportMethod(path, name) {
228
226
  const imports =
229
227
  path.scope.getProgramParent().data.imports ||
230
228
  (path.scope.getProgramParent().data.imports = new Set());
229
+ const identifier = t__namespace.identifier(`_$${name}`);
231
230
  if (!imports.has(name)) {
232
231
  helperModuleImports.addNamed(path, name, getConfig(path).moduleName, {
233
232
  nameHint: `_$${name}`
234
233
  });
235
234
  imports.add(name);
236
235
  }
236
+ return identifier;
237
237
  }
238
238
 
239
239
  function jsxElementNameToString(node) {
240
240
  if (t__namespace.isJSXMemberExpression(node)) {
241
241
  return `${jsxElementNameToString(node.object)}.${node.property.name}`;
242
242
  }
243
- if (t__namespace.isJSXIdentifier(node)) {
243
+ if (t__namespace.isJSXIdentifier(node) || t__namespace.isIdentifier(node)) {
244
244
  return node.name;
245
245
  }
246
246
  return `${node.namespace.name}:${node.name.name}`;
247
247
  }
248
248
 
249
- function tagNameToIdentifier(name) {
250
- const parts = name.split(".");
251
- if (parts.length === 1) return t__namespace.identifier(name);
252
- let part;
253
- let base = t__namespace.identifier(parts.shift());
254
- while ((part = parts.shift())) {
255
- base = t__namespace.memberExpression(base, t__namespace.identifier(part));
256
- }
257
- return base;
258
- }
259
-
260
249
  function getTagName(tag) {
261
250
  const jsxName = tag.openingElement.name;
262
251
  return jsxElementNameToString(jsxName);
@@ -396,7 +385,7 @@ function wrappedByText(list, startIndex) {
396
385
  function transformCondition(path, inline, deep) {
397
386
  const config = getConfig(path);
398
387
  const expr = path.node;
399
- registerImportMethod(path, config.memoWrapper);
388
+ const memo = registerImportMethod(path, config.memoWrapper);
400
389
  let dTest, cond, id;
401
390
  if (
402
391
  t__namespace.isConditionalExpression(expr) &&
@@ -411,7 +400,7 @@ function transformCondition(path, inline, deep) {
411
400
  if (!t__namespace.isBinaryExpression(cond))
412
401
  cond = t__namespace.unaryExpression("!", t__namespace.unaryExpression("!", cond, true), true);
413
402
  id = inline
414
- ? t__namespace.callExpression(t__namespace.identifier(`_$${config.memoWrapper}`), [
403
+ ? t__namespace.callExpression(memo, [
415
404
  t__namespace.arrowFunctionExpression([], cond),
416
405
  t__namespace.booleanLiteral(true)
417
406
  ])
@@ -440,7 +429,7 @@ function transformCondition(path, inline, deep) {
440
429
  if (!t__namespace.isBinaryExpression(cond))
441
430
  cond = t__namespace.unaryExpression("!", t__namespace.unaryExpression("!", cond, true), true);
442
431
  id = inline
443
- ? t__namespace.callExpression(t__namespace.identifier(`_$${config.memoWrapper}`), [
432
+ ? t__namespace.callExpression(memo, [
444
433
  t__namespace.arrowFunctionExpression([], cond),
445
434
  t__namespace.booleanLiteral(true)
446
435
  ])
@@ -454,7 +443,7 @@ function transformCondition(path, inline, deep) {
454
443
  t__namespace.variableDeclarator(
455
444
  id,
456
445
  config.memoWrapper
457
- ? t__namespace.callExpression(t__namespace.identifier(`_$${config.memoWrapper}`), [
446
+ ? t__namespace.callExpression(memo, [
458
447
  t__namespace.arrowFunctionExpression([], cond),
459
448
  t__namespace.booleanLiteral(true)
460
449
  ])
@@ -476,6 +465,10 @@ function transformCondition(path, inline, deep) {
476
465
  return deep ? expr : t__namespace.arrowFunctionExpression([], expr);
477
466
  }
478
467
 
468
+ function escapeBackticks(value) {
469
+ return value.replaceAll("`", "\\`");
470
+ }
471
+
479
472
  function escapeHTML(s, attr) {
480
473
  if (typeof s !== "string") return s;
481
474
  const delim = attr ? '"' : "<";
@@ -548,9 +541,8 @@ function transformElement$2(path, info) {
548
541
  results.template += `</${tagName}>`;
549
542
  }
550
543
  if (info.topLevel && config.hydratable && results.hasHydratableEvent) {
551
- registerImportMethod(path, "runHydrationEvents");
552
544
  results.postExprs.push(
553
- t__namespace.expressionStatement(t__namespace.callExpression(t__namespace.identifier("_$runHydrationEvents"), []))
545
+ t__namespace.expressionStatement(t__namespace.callExpression(registerImportMethod(path, "runHydrationEvents"), []))
554
546
  );
555
547
  }
556
548
  if (wrapSVG) results.template += "</svg>";
@@ -586,9 +578,8 @@ function setAttr$2(path, elem, name, value, { isSVG, dynamic, prevId, isCE }) {
586
578
  }
587
579
 
588
580
  if (name === "style") {
589
- registerImportMethod(path, "style");
590
581
  return t__namespace.callExpression(
591
- t__namespace.identifier("_$style"),
582
+ registerImportMethod(path, "style"),
592
583
  prevId ? [elem, value, prevId] : [elem, value]
593
584
  );
594
585
  }
@@ -598,9 +589,8 @@ function setAttr$2(path, elem, name, value, { isSVG, dynamic, prevId, isCE }) {
598
589
  }
599
590
 
600
591
  if (name === "classList") {
601
- registerImportMethod(path, "classList");
602
592
  return t__namespace.callExpression(
603
- t__namespace.identifier("_$classList"),
593
+ registerImportMethod(path, "classList"),
604
594
  prevId ? [elem, value, prevId] : [elem, value]
605
595
  );
606
596
  }
@@ -626,16 +616,14 @@ function setAttr$2(path, elem, name, value, { isSVG, dynamic, prevId, isCE }) {
626
616
  !isSVG && (name = name.toLowerCase());
627
617
  const ns = isNameSpaced && SVGNamespace[name.split(":")[0]];
628
618
  if (ns) {
629
- registerImportMethod(path, "setAttributeNS");
630
- return t__namespace.callExpression(t__namespace.identifier("_$setAttributeNS"), [
619
+ return t__namespace.callExpression(registerImportMethod(path, "setAttributeNS"), [
631
620
  elem,
632
621
  t__namespace.stringLiteral(ns),
633
622
  t__namespace.stringLiteral(name),
634
623
  value
635
624
  ]);
636
625
  } else {
637
- registerImportMethod(path, "setAttribute");
638
- return t__namespace.callExpression(t__namespace.identifier("_$setAttribute"), [elem, t__namespace.stringLiteral(name), value]);
626
+ return t__namespace.callExpression(registerImportMethod(path, "setAttribute"), [elem, t__namespace.stringLiteral(name), value]);
639
627
  }
640
628
  }
641
629
 
@@ -658,8 +646,7 @@ function transformAttributes$2(path, results) {
658
646
  hasHydratableEvent = false,
659
647
  children,
660
648
  attributes = path.get("openingElement").get("attributes");
661
- const spread = t__namespace.identifier("_$spread"),
662
- tagName = getTagName(path.node),
649
+ const tagName = getTagName(path.node),
663
650
  isSVG = SVGElements.has(tagName),
664
651
  isCE = tagName.includes("-"),
665
652
  hasChildren = path.node.children.length > 0,
@@ -794,10 +781,9 @@ function transformAttributes$2(path, results) {
794
781
  .forEach(attribute => {
795
782
  const node = attribute.node;
796
783
  if (t__namespace.isJSXSpreadAttribute(node)) {
797
- registerImportMethod(attribute, "spread");
798
784
  results.exprs.push(
799
785
  t__namespace.expressionStatement(
800
- t__namespace.callExpression(spread, [
786
+ t__namespace.callExpression(registerImportMethod(attribute, "spread"), [
801
787
  elem,
802
788
  isDynamic(attribute.get("argument"), {
803
789
  checkMember: true
@@ -899,9 +885,11 @@ function transformAttributes$2(path, results) {
899
885
  );
900
886
  }
901
887
  } else if (key.startsWith("use:")) {
888
+ // Some trick to treat JSXIdentifier as Identifier
889
+ node.name.name.type = 'Identifier';
902
890
  results.exprs.unshift(
903
891
  t__namespace.expressionStatement(
904
- t__namespace.callExpression(t__namespace.identifier(node.name.name.name), [
892
+ t__namespace.callExpression(node.name.name, [
905
893
  elem,
906
894
  t__namespace.arrowFunctionExpression(
907
895
  [],
@@ -973,10 +961,9 @@ function transformAttributes$2(path, results) {
973
961
  )
974
962
  );
975
963
  } else {
976
- registerImportMethod(path, "addEventListener");
977
964
  results.exprs.unshift(
978
965
  t__namespace.expressionStatement(
979
- t__namespace.callExpression(t__namespace.identifier("_$addEventListener"), [
966
+ t__namespace.callExpression(registerImportMethod(path, "addEventListener"), [
980
967
  elem,
981
968
  t__namespace.stringLiteral(ev),
982
969
  handler,
@@ -1013,10 +1000,9 @@ function transformAttributes$2(path, results) {
1013
1000
  )
1014
1001
  );
1015
1002
  } else {
1016
- registerImportMethod(path, "addEventListener");
1017
1003
  results.exprs.unshift(
1018
1004
  t__namespace.expressionStatement(
1019
- t__namespace.callExpression(t__namespace.identifier("_$addEventListener"), [
1005
+ t__namespace.callExpression(registerImportMethod(path, "addEventListener"), [
1020
1006
  elem,
1021
1007
  t__namespace.stringLiteral(ev),
1022
1008
  handler
@@ -1060,7 +1046,7 @@ function transformAttributes$2(path, results) {
1060
1046
  } else {
1061
1047
  !isSVG && (key = key.toLowerCase());
1062
1048
  results.template += ` ${key}`;
1063
- results.template += value ? `="${value.value}"` : "";
1049
+ results.template += value ? `="${escapeBackticks(value.value)}"` : "";
1064
1050
  }
1065
1051
  }
1066
1052
  });
@@ -1110,7 +1096,7 @@ function transformChildren$2(path, results, config) {
1110
1096
  t__namespace.variableDeclarator(
1111
1097
  child.id,
1112
1098
  config.hydratable && tagName === "html"
1113
- ? t__namespace.callExpression(t__namespace.identifier("_$getNextMatch"), [
1099
+ ? t__namespace.callExpression(registerImportMethod(path, "getNextMatch"), [
1114
1100
  walk,
1115
1101
  t__namespace.stringLiteral(child.tagName)
1116
1102
  ])
@@ -1125,7 +1111,7 @@ function transformChildren$2(path, results, config) {
1125
1111
  nextPlaceholder = null;
1126
1112
  i++;
1127
1113
  } else if (child.exprs.length) {
1128
- registerImportMethod(path, "insert");
1114
+ const insert = registerImportMethod(path, "insert");
1129
1115
  const multi = checkLength(filteredChildren),
1130
1116
  markers = config.hydratable && multi;
1131
1117
  // boxed by textNodes
@@ -1141,7 +1127,7 @@ function transformChildren$2(path, results, config) {
1141
1127
  results.exprs.push(
1142
1128
  t__namespace.expressionStatement(
1143
1129
  t__namespace.callExpression(
1144
- t__namespace.identifier("_$insert"),
1130
+ insert,
1145
1131
  contentId
1146
1132
  ? [results.id, child.exprs[0], exprId, contentId]
1147
1133
  : [results.id, child.exprs[0], exprId]
@@ -1152,7 +1138,7 @@ function transformChildren$2(path, results, config) {
1152
1138
  } else if (multi) {
1153
1139
  results.exprs.push(
1154
1140
  t__namespace.expressionStatement(
1155
- t__namespace.callExpression(t__namespace.identifier("_$insert"), [
1141
+ t__namespace.callExpression(insert, [
1156
1142
  results.id,
1157
1143
  child.exprs[0],
1158
1144
  nextChild$1(childNodes, index) || t__namespace.nullLiteral()
@@ -1163,7 +1149,7 @@ function transformChildren$2(path, results, config) {
1163
1149
  results.exprs.push(
1164
1150
  t__namespace.expressionStatement(
1165
1151
  t__namespace.callExpression(
1166
- t__namespace.identifier("_$insert"),
1152
+ insert,
1167
1153
  config.hydratable
1168
1154
  ? [
1169
1155
  results.id,
@@ -1198,12 +1184,11 @@ function createPlaceholder(path, results, tempPath, i, char) {
1198
1184
  let contentId;
1199
1185
  results.template += `<!${char}>`;
1200
1186
  if (config.hydratable && char === "/") {
1201
- registerImportMethod(path, "getNextMarker");
1202
1187
  contentId = path.scope.generateUidIdentifier("co$");
1203
1188
  results.decl.push(
1204
1189
  t__namespace.variableDeclarator(
1205
1190
  t__namespace.arrayPattern([exprId, contentId]),
1206
- t__namespace.callExpression(t__namespace.identifier("_$getNextMarker"), [
1191
+ t__namespace.callExpression(registerImportMethod(path, "getNextMarker"), [
1207
1192
  t__namespace.memberExpression(t__namespace.identifier(tempPath), t__namespace.identifier("nextSibling"))
1208
1193
  ])
1209
1194
  )
@@ -1269,13 +1254,12 @@ function detectExpressions(children, index, config) {
1269
1254
  }
1270
1255
 
1271
1256
  function contextToCustomElement(path, results) {
1272
- registerImportMethod(path, "getOwner");
1273
1257
  results.exprs.push(
1274
1258
  t__namespace.expressionStatement(
1275
1259
  t__namespace.assignmentExpression(
1276
1260
  "=",
1277
1261
  t__namespace.memberExpression(results.id, t__namespace.identifier("_$owner")),
1278
- t__namespace.callExpression(t__namespace.identifier("_$getOwner"), [])
1262
+ t__namespace.callExpression(registerImportMethod(path, "getOwner"), [])
1279
1263
  )
1280
1264
  )
1281
1265
  );
@@ -1308,8 +1292,7 @@ function createTemplate$2(path, result, wrap) {
1308
1292
  }
1309
1293
  }
1310
1294
  if (wrap && result.dynamic && config.memoWrapper) {
1311
- registerImportMethod(path, config.memoWrapper);
1312
- return t__namespace.callExpression(t__namespace.identifier(`_$${config.memoWrapper}`), [result.exprs[0]]);
1295
+ return t__namespace.callExpression(registerImportMethod(path, config.memoWrapper), [result.exprs[0]]);
1313
1296
  }
1314
1297
  return result.exprs[0];
1315
1298
  }
@@ -1320,11 +1303,10 @@ function appendTemplates$1(path, templates) {
1320
1303
  cooked: template.template,
1321
1304
  raw: template.template
1322
1305
  };
1323
- registerImportMethod(path, "template");
1324
1306
  return t__namespace.variableDeclarator(
1325
1307
  template.id,
1326
1308
  t__namespace.callExpression(
1327
- t__namespace.identifier("_$template"),
1309
+ registerImportMethod(path, "template"),
1328
1310
  [
1329
1311
  t__namespace.templateLiteral([t__namespace.templateElement(tmpl, true)], []),
1330
1312
  t__namespace.numericLiteral(template.elementCount)
@@ -1356,11 +1338,10 @@ function registerTemplate(path, results) {
1356
1338
  });
1357
1339
  }
1358
1340
  }
1359
- hydratable && registerImportMethod(path, "getNextElement");
1360
1341
  decl = t__namespace.variableDeclarator(
1361
1342
  results.id,
1362
1343
  hydratable
1363
- ? t__namespace.callExpression(t__namespace.identifier("_$getNextElement"), templateId ? [templateId] : [])
1344
+ ? t__namespace.callExpression(registerImportMethod(path, "getNextElement"), templateId ? [templateId] : [])
1364
1345
  : t__namespace.callExpression(t__namespace.memberExpression(templateId, t__namespace.identifier("cloneNode")), [
1365
1346
  t__namespace.booleanLiteral(true)
1366
1347
  ])
@@ -1373,9 +1354,8 @@ function registerTemplate(path, results) {
1373
1354
  function wrapDynamics$1(path, dynamics) {
1374
1355
  if (!dynamics.length) return;
1375
1356
  const config = getConfig(path);
1376
- registerImportMethod(path, config.effectWrapper);
1377
1357
 
1378
- const effectWrapperId = `_$${config.effectWrapper}`;
1358
+ const effectWrapperId = registerImportMethod(path, config.effectWrapper);
1379
1359
 
1380
1360
  if (dynamics.length === 1) {
1381
1361
  const prevValue =
@@ -1384,7 +1364,7 @@ function wrapDynamics$1(path, dynamics) {
1384
1364
  : undefined;
1385
1365
 
1386
1366
  return t__namespace.expressionStatement(
1387
- t__namespace.callExpression(t__namespace.identifier(effectWrapperId), [
1367
+ t__namespace.callExpression(effectWrapperId, [
1388
1368
  t__namespace.arrowFunctionExpression(
1389
1369
  prevValue ? [prevValue] : [],
1390
1370
  setAttr$2(path, dynamics[0].elem, dynamics[0].key, dynamics[0].value, {
@@ -1436,7 +1416,7 @@ function wrapDynamics$1(path, dynamics) {
1436
1416
  });
1437
1417
 
1438
1418
  return t__namespace.expressionStatement(
1439
- t__namespace.callExpression(t__namespace.identifier(effectWrapperId), [
1419
+ t__namespace.callExpression(effectWrapperId, [
1440
1420
  t__namespace.arrowFunctionExpression(
1441
1421
  [prevId],
1442
1422
  t__namespace.blockStatement([
@@ -1457,7 +1437,6 @@ function createTemplate$1(path, result) {
1457
1437
 
1458
1438
  let template, id;
1459
1439
 
1460
- registerImportMethod(path, "ssr");
1461
1440
  if (!Array.isArray(result.template)) {
1462
1441
  template = t__namespace.stringLiteral(result.template);
1463
1442
  } else if (result.template.length === 1) {
@@ -1484,7 +1463,7 @@ function createTemplate$1(path, result) {
1484
1463
  } else id = found[0];
1485
1464
 
1486
1465
  return t__namespace.callExpression(
1487
- t__namespace.identifier(`_$ssr`),
1466
+ registerImportMethod(path, "ssr"),
1488
1467
  result.template.length > 1 ? [id, ...result.templateValues] : [id]
1489
1468
  );
1490
1469
  }
@@ -1518,9 +1497,8 @@ function transformElement$1(path, info) {
1518
1497
  tagName
1519
1498
  };
1520
1499
  if (info.topLevel && config.hydratable) {
1521
- registerImportMethod(path, "ssrHydrationKey");
1522
1500
  results.template.push("");
1523
- results.templateValues.push(t__namespace.callExpression(t__namespace.identifier("_$ssrHydrationKey"), []));
1501
+ results.templateValues.push(t__namespace.callExpression(registerImportMethod(path, "ssrHydrationKey"), []));
1524
1502
  }
1525
1503
  transformAttributes$1(path, results);
1526
1504
  appendToTemplate(results.template, ">");
@@ -1586,9 +1564,8 @@ function escapeExpression(path, expression, attr) {
1586
1564
  return expression;
1587
1565
  }
1588
1566
 
1589
- registerImportMethod(path, "escape");
1590
1567
  return t__namespace.callExpression(
1591
- t__namespace.identifier("_$escape"),
1568
+ registerImportMethod(path, "escape"),
1592
1569
  [expression].concat(attr ? [t__namespace.booleanLiteral(true)] : [])
1593
1570
  );
1594
1571
  }
@@ -1599,9 +1576,12 @@ function transformToObject(attrName, attributes, selectedAttributes) {
1599
1576
  for (let i = 0; i < selectedAttributes.length; i++) {
1600
1577
  const attr = selectedAttributes[i].node;
1601
1578
  const computed = !t__namespace.isValidIdentifier(attr.name.name.name);
1579
+ if (!computed) {
1580
+ attr.name.name.type = 'Identifier';
1581
+ }
1602
1582
  properties.push(
1603
1583
  t__namespace.objectProperty(
1604
- computed ? t__namespace.stringLiteral(attr.name.name.name) : t__namespace.identifier(attr.name.name.name),
1584
+ computed ? t__namespace.stringLiteral(attr.name.name.name) : attr.name.name,
1605
1585
  t__namespace.isJSXExpressionContainer(attr.value) ? attr.value.expression : attr.value
1606
1586
  )
1607
1587
  );
@@ -1669,8 +1649,7 @@ function transformAttributes$1(path, results) {
1669
1649
  i && attributes.splice(attributes.indexOf(classAttributes[i].node), 1);
1670
1650
  continue;
1671
1651
  }
1672
- registerImportMethod(path, "ssrClassList");
1673
- expr = t__namespace.callExpression(t__namespace.identifier("_$ssrClassList"), [expr]);
1652
+ expr = t__namespace.callExpression(registerImportMethod(path, "ssrClassList"), [expr]);
1674
1653
  }
1675
1654
  values.push(t__namespace.logicalExpression("||", expr, t__namespace.stringLiteral("")));
1676
1655
  quasis.push(t__namespace.TemplateElement({ raw: isLast ? "" : " " }));
@@ -1684,11 +1663,10 @@ function transformAttributes$1(path, results) {
1684
1663
  attributes.forEach(attribute => {
1685
1664
  const node = attribute.node;
1686
1665
  if (t__namespace.isJSXSpreadAttribute(node)) {
1687
- registerImportMethod(attribute, "ssrSpread");
1688
1666
  appendToTemplate(results.template, " ");
1689
1667
  results.template.push("");
1690
1668
  results.templateValues.push(
1691
- t__namespace.callExpression(t__namespace.identifier("_$ssrSpread"), [
1669
+ t__namespace.callExpression(registerImportMethod(attribute, "ssrSpread"), [
1692
1670
  isDynamic(attribute.get("argument"), {
1693
1671
  checkMember: true,
1694
1672
  native: true
@@ -1745,9 +1723,8 @@ function transformAttributes$1(path, results) {
1745
1723
  dynamic = true;
1746
1724
 
1747
1725
  if (BooleanAttributes.has(key)) {
1748
- registerImportMethod(attribute, "ssrBoolean");
1749
1726
  results.template.push("");
1750
- const fn = t__namespace.callExpression(t__namespace.identifier("_$ssrBoolean"), [
1727
+ const fn = t__namespace.callExpression(registerImportMethod(attribute, "ssrBoolean"), [
1751
1728
  t__namespace.stringLiteral(key),
1752
1729
  value.expression
1753
1730
  ]);
@@ -1760,7 +1737,6 @@ function transformAttributes$1(path, results) {
1760
1737
  t__namespace.isObjectExpression(value.expression) &&
1761
1738
  !value.expression.properties.some(p => t__namespace.isSpreadElement(p))
1762
1739
  ) {
1763
- registerImportMethod(path, "escape");
1764
1740
  const props = value.expression.properties.map((p, i) =>
1765
1741
  t__namespace.binaryExpression(
1766
1742
  "+",
@@ -1773,7 +1749,7 @@ function transformAttributes$1(path, results) {
1773
1749
  ? p.value
1774
1750
  : t__namespace.isTemplateLiteral(p.value) && p.value.expressions.length === 0
1775
1751
  ? t__namespace.stringLiteral(escapeHTML(p.value.quasis[0].value.raw))
1776
- : t__namespace.callExpression(t__namespace.identifier("_$escape"), [p.value, t__namespace.booleanLiteral(true)])
1752
+ : t__namespace.callExpression(registerImportMethod(path, "escape"), [p.value, t__namespace.booleanLiteral(true)])
1777
1753
  )
1778
1754
  );
1779
1755
  let res = props[0];
@@ -1782,8 +1758,7 @@ function transformAttributes$1(path, results) {
1782
1758
  }
1783
1759
  value.expression = res;
1784
1760
  } else {
1785
- registerImportMethod(path, "ssrStyle");
1786
- value.expression = t__namespace.callExpression(t__namespace.identifier("_$ssrStyle"), [value.expression]);
1761
+ value.expression = t__namespace.callExpression(registerImportMethod(path, "ssrStyle"), [value.expression]);
1787
1762
  }
1788
1763
  doEscape = false;
1789
1764
  }
@@ -1800,8 +1775,7 @@ function transformAttributes$1(path, results) {
1800
1775
  value.expression = values[0];
1801
1776
  } else value.expression = t__namespace.templateLiteral(quasis, values);
1802
1777
  } else {
1803
- registerImportMethod(path, "ssrClassList");
1804
- value.expression = t__namespace.callExpression(t__namespace.identifier("_$ssrClassList"), [value.expression]);
1778
+ value.expression = t__namespace.callExpression(registerImportMethod(path, "ssrClassList"), [value.expression]);
1805
1779
  }
1806
1780
  key = "class";
1807
1781
  doEscape = false;
@@ -1833,8 +1807,7 @@ function transformClasslistObject(path, expr, values, quasis) {
1833
1807
  let key = prop.key;
1834
1808
  if (t__namespace.isIdentifier(prop.key) && !prop.computed) key = t__namespace.stringLiteral(key.name);
1835
1809
  else if (prop.computed) {
1836
- registerImportMethod(path, "escape");
1837
- key = t__namespace.callExpression(t__namespace.identifier("_$escape"), [prop.key, t__namespace.booleanLiteral(true)]);
1810
+ key = t__namespace.callExpression(registerImportMethod(path, "escape"), [prop.key, t__namespace.booleanLiteral(true)]);
1838
1811
  } else key = t__namespace.stringLiteral(escapeHTML(prop.key.value));
1839
1812
  if (t__namespace.isBooleanLiteral(prop.value)) {
1840
1813
  if (prop.value.value === true) {
@@ -1914,11 +1887,10 @@ function transformElement(path, info) {
1914
1887
  tagName
1915
1888
  };
1916
1889
 
1917
- registerImportMethod(path, "createElement");
1918
1890
  results.decl.push(
1919
1891
  t__namespace.variableDeclarator(
1920
1892
  results.id,
1921
- t__namespace.callExpression(t__namespace.identifier("_$createElement"), [t__namespace.stringLiteral(tagName)])
1893
+ t__namespace.callExpression(registerImportMethod(path, "createElement"), [t__namespace.stringLiteral(tagName)])
1922
1894
  )
1923
1895
  );
1924
1896
 
@@ -1931,7 +1903,6 @@ function transformElement(path, info) {
1931
1903
  function transformAttributes(path, results) {
1932
1904
  let children;
1933
1905
  const elem = results.id,
1934
- spread = t__namespace.identifier("_$spread"),
1935
1906
  hasChildren = path.node.children.length > 0,
1936
1907
  config = getConfig(path);
1937
1908
 
@@ -1941,10 +1912,9 @@ function transformAttributes(path, results) {
1941
1912
  .forEach(attribute => {
1942
1913
  const node = attribute.node;
1943
1914
  if (t__namespace.isJSXSpreadAttribute(node)) {
1944
- registerImportMethod(attribute, "spread");
1945
1915
  results.exprs.push(
1946
1916
  t__namespace.expressionStatement(
1947
- t__namespace.callExpression(spread, [
1917
+ t__namespace.callExpression(registerImportMethod(attribute, "spread"), [
1948
1918
  elem,
1949
1919
  isDynamic(attribute.get("argument"), {
1950
1920
  checkMember: true
@@ -2023,9 +1993,11 @@ function transformAttributes(path, results) {
2023
1993
  );
2024
1994
  }
2025
1995
  } else if (key.startsWith("use:")) {
1996
+ // Some trick to treat JSXIdentifier as Identifier
1997
+ node.name.name.type = 'Identifier';
2026
1998
  results.exprs.unshift(
2027
1999
  t__namespace.expressionStatement(
2028
- t__namespace.callExpression(t__namespace.identifier(node.name.name.name), [
2000
+ t__namespace.callExpression(node.name.name, [
2029
2001
  elem,
2030
2002
  t__namespace.arrowFunctionExpression(
2031
2003
  [],
@@ -2060,10 +2032,9 @@ function transformAttributes(path, results) {
2060
2032
  }
2061
2033
 
2062
2034
  function setAttr(path, elem, name, value, { prevId } = {}) {
2063
- registerImportMethod(path, "setProp");
2064
2035
  if (!value) value = t__namespace.booleanLiteral(true);
2065
2036
  return t__namespace.callExpression(
2066
- t__namespace.identifier("_$setProp"),
2037
+ registerImportMethod(path, "setProp"),
2067
2038
  prevId ? [elem, t__namespace.stringLiteral(name), value, prevId] : [elem, t__namespace.stringLiteral(name), value]
2068
2039
  );
2069
2040
  }
@@ -2084,36 +2055,36 @@ function transformChildren(path, results) {
2084
2055
  childNodes.forEach((child, index) => {
2085
2056
  if (!child) return;
2086
2057
  if (child.id) {
2087
- registerImportMethod(path, "insertNode");
2058
+ const insertNode = registerImportMethod(path, "insertNode");
2088
2059
  let insert = child.id;
2089
2060
  if (child.text) {
2090
- registerImportMethod(path, "createTextNode");
2061
+ const createTextNode = registerImportMethod(path, "createTextNode");
2091
2062
  if (multi) {
2092
2063
  results.decl.push(
2093
2064
  t__namespace.variableDeclarator(
2094
2065
  child.id,
2095
- t__namespace.callExpression(t__namespace.identifier("_$createTextNode"), [
2066
+ t__namespace.callExpression(createTextNode, [
2096
2067
  t__namespace.stringLiteral(htmlEntities.decode(child.template))
2097
2068
  ])
2098
2069
  )
2099
2070
  );
2100
2071
  } else
2101
- insert = t__namespace.callExpression(t__namespace.identifier("_$createTextNode"), [
2072
+ insert = t__namespace.callExpression(createTextNode, [
2102
2073
  t__namespace.stringLiteral(htmlEntities.decode(child.template))
2103
2074
  ]);
2104
2075
  }
2105
2076
  appends.push(
2106
- t__namespace.expressionStatement(t__namespace.callExpression(t__namespace.identifier("_$insertNode"), [results.id, insert]))
2077
+ t__namespace.expressionStatement(t__namespace.callExpression(insertNode, [results.id, insert]))
2107
2078
  );
2108
2079
  results.decl.push(...child.decl);
2109
2080
  results.exprs.push(...child.exprs);
2110
2081
  results.dynamics.push(...child.dynamics);
2111
2082
  } else if (child.exprs.length) {
2112
- registerImportMethod(path, "insert");
2083
+ const insert = registerImportMethod(path, "insert");
2113
2084
  if (multi) {
2114
2085
  results.exprs.push(
2115
2086
  t__namespace.expressionStatement(
2116
- t__namespace.callExpression(t__namespace.identifier("_$insert"), [
2087
+ t__namespace.callExpression(insert, [
2117
2088
  results.id,
2118
2089
  child.exprs[0],
2119
2090
  nextChild(childNodes, index) || t__namespace.nullLiteral()
@@ -2123,7 +2094,7 @@ function transformChildren(path, results) {
2123
2094
  } else {
2124
2095
  results.exprs.push(
2125
2096
  t__namespace.expressionStatement(
2126
- t__namespace.callExpression(t__namespace.identifier("_$insert"), [results.id, child.exprs[0]])
2097
+ t__namespace.callExpression(insert, [results.id, child.exprs[0]])
2127
2098
  )
2128
2099
  );
2129
2100
  }
@@ -2163,8 +2134,7 @@ function createTemplate(path, result, wrap) {
2163
2134
  }
2164
2135
  }
2165
2136
  if (wrap && result.dynamic && config.memoWrapper) {
2166
- registerImportMethod(path, config.memoWrapper);
2167
- return t__namespace.callExpression(t__namespace.identifier(`_$${config.memoWrapper}`), [result.exprs[0]]);
2137
+ return t__namespace.callExpression(registerImportMethod(path, config.memoWrapper), [result.exprs[0]]);
2168
2138
  }
2169
2139
  return result.exprs[0];
2170
2140
  }
@@ -2172,15 +2142,14 @@ function createTemplate(path, result, wrap) {
2172
2142
  function wrapDynamics(path, dynamics) {
2173
2143
  if (!dynamics.length) return;
2174
2144
  const config = getConfig(path);
2175
- registerImportMethod(path, config.effectWrapper);
2176
2145
 
2177
- const effectWrapperId = `_$${config.effectWrapper}`;
2146
+ const effectWrapperId = registerImportMethod(path, config.effectWrapper);
2178
2147
 
2179
2148
  if (dynamics.length === 1) {
2180
2149
  const prevValue = t__namespace.identifier("_$p");
2181
2150
 
2182
2151
  return t__namespace.expressionStatement(
2183
- t__namespace.callExpression(t__namespace.identifier(effectWrapperId), [
2152
+ t__namespace.callExpression(effectWrapperId, [
2184
2153
  t__namespace.arrowFunctionExpression(
2185
2154
  [prevValue],
2186
2155
  setAttr(path, dynamics[0].elem, dynamics[0].key, dynamics[0].value, {
@@ -2218,7 +2187,7 @@ function wrapDynamics(path, dynamics) {
2218
2187
  });
2219
2188
 
2220
2189
  return t__namespace.expressionStatement(
2221
- t__namespace.callExpression(t__namespace.identifier(effectWrapperId), [
2190
+ t__namespace.callExpression(effectWrapperId, [
2222
2191
  t__namespace.arrowFunctionExpression(
2223
2192
  [prevId],
2224
2193
  t__namespace.blockStatement([
@@ -2232,18 +2201,51 @@ function wrapDynamics(path, dynamics) {
2232
2201
  );
2233
2202
  }
2234
2203
 
2204
+ function convertComponentIdentifier(node) {
2205
+ if (t__namespace.isJSXIdentifier(node)) {
2206
+ node.type = "Identifier";
2207
+ } else if (t__namespace.isJSXMemberExpression(node)) {
2208
+ return t__namespace.memberExpression(
2209
+ convertComponentIdentifier(node.object),
2210
+ convertComponentIdentifier(node.property),
2211
+ );
2212
+ }
2213
+
2214
+ return node;
2215
+ }
2216
+
2217
+
2218
+ function convertJSXIdentifier(node) {
2219
+ if (t__namespace.isJSXIdentifier(node)) {
2220
+ if (t__namespace.isValidIdentifier(node.name, false)) {
2221
+ node.type = "Identifier";
2222
+ } else {
2223
+ return t__namespace.stringLiteral(node.name);
2224
+ }
2225
+ } else if (t__namespace.isJSXMemberExpression(node)) {
2226
+ return t__namespace.memberExpression(
2227
+ convertJSXIdentifier(node.object),
2228
+ convertJSXIdentifier(node.property),
2229
+ );
2230
+ } else if (t__namespace.isJSXNamespacedName(node)) {
2231
+ return t__namespace.stringLiteral(`${node.namespace.name}:${node.name.name}`);
2232
+ }
2233
+
2234
+ return node;
2235
+ }
2236
+
2235
2237
  function transformComponent(path) {
2236
2238
  let exprs = [],
2237
2239
  config = getConfig(path),
2238
- tagName = getTagName(path.node),
2240
+ tagId = convertComponentIdentifier(path.node.openingElement.name),
2239
2241
  props = [],
2240
2242
  runningObject = [],
2241
2243
  dynamicSpread = false,
2242
2244
  hasChildren = path.node.children.length > 0;
2243
2245
 
2244
- if (config.builtIns.indexOf(tagName) > -1 && !path.scope.hasBinding(tagName)) {
2245
- registerImportMethod(path, tagName);
2246
- tagName = `_$${tagName}`;
2246
+ if (config.builtIns.indexOf(tagId.name) > -1 && !path.scope.hasBinding(tagId.name)) {
2247
+ const newTagId = registerImportMethod(path, tagId.name);
2248
+ tagId.name = newTagId.name;
2247
2249
  }
2248
2250
 
2249
2251
  path
@@ -2267,10 +2269,8 @@ function transformComponent(path) {
2267
2269
  );
2268
2270
  } else {
2269
2271
  const value = node.value || t__namespace.booleanLiteral(true),
2270
- key = t__namespace.isJSXNamespacedName(node.name)
2271
- ? `${node.name.namespace.name}:${node.name.name.name}`
2272
- : node.name.name,
2273
- wrapName = t__namespace.isValidIdentifier(key) ? t__namespace.identifier : t__namespace.stringLiteral;
2272
+ id = convertJSXIdentifier(node.name),
2273
+ key = id.name;
2274
2274
  if (hasChildren && key === "children") return;
2275
2275
  if (t__namespace.isJSXExpressionContainer(value))
2276
2276
  if (key === "ref") {
@@ -2356,14 +2356,14 @@ function transformComponent(path) {
2356
2356
  runningObject.push(
2357
2357
  t__namespace.objectMethod(
2358
2358
  "get",
2359
- wrapName(key),
2359
+ id,
2360
2360
  [],
2361
2361
  t__namespace.blockStatement([t__namespace.returnStatement(expr.body)]),
2362
2362
  !t__namespace.isValidIdentifier(key)
2363
2363
  )
2364
2364
  );
2365
- } else runningObject.push(t__namespace.objectProperty(wrapName(key), value.expression));
2366
- else runningObject.push(t__namespace.objectProperty(wrapName(key), value));
2365
+ } else runningObject.push(t__namespace.objectProperty(id, value.expression));
2366
+ else runningObject.push(t__namespace.objectProperty(id, value));
2367
2367
  }
2368
2368
  });
2369
2369
 
@@ -2387,12 +2387,10 @@ function transformComponent(path) {
2387
2387
  if (runningObject.length || !props.length) props.push(t__namespace.objectExpression(runningObject));
2388
2388
 
2389
2389
  if (props.length > 1 || dynamicSpread) {
2390
- registerImportMethod(path, "mergeProps");
2391
- props = [t__namespace.callExpression(t__namespace.identifier("_$mergeProps"), props)];
2390
+ props = [t__namespace.callExpression(registerImportMethod(path, "mergeProps"), props)];
2392
2391
  }
2393
- registerImportMethod(path, "createComponent");
2394
- const componentArgs = [tagNameToIdentifier(tagName), props[0]];
2395
- exprs.push(t__namespace.callExpression(t__namespace.identifier("_$createComponent"), componentArgs));
2392
+ const componentArgs = [tagId, props[0]];
2393
+ exprs.push(t__namespace.callExpression(registerImportMethod(path, "createComponent"), componentArgs));
2396
2394
 
2397
2395
  // handle hoisting conditionals
2398
2396
  if (exprs.length > 1) {
@@ -2547,7 +2545,7 @@ function transformNode(path, info = {}) {
2547
2545
  : trimWhitespace(node.extra.raw);
2548
2546
  if (!text.length) return null;
2549
2547
  const results = {
2550
- template: text,
2548
+ template: config.generate === "dom" ? escapeBackticks(text) : text,
2551
2549
  decl: [],
2552
2550
  exprs: [],
2553
2551
  dynamics: [],
@@ -2617,10 +2615,9 @@ function transformNode(path, info = {}) {
2617
2615
  var postprocess = path => {
2618
2616
  const config = getConfig(path);
2619
2617
  if (path.scope.data.events) {
2620
- registerImportMethod(path, "delegateEvents");
2621
2618
  path.node.body.push(
2622
2619
  t__namespace.expressionStatement(
2623
- t__namespace.callExpression(t__namespace.identifier("_$delegateEvents"), [
2620
+ t__namespace.callExpression(registerImportMethod(path, "delegateEvents"), [
2624
2621
  t__namespace.arrayExpression(Array.from(path.scope.data.events).map(e => t__namespace.stringLiteral(e)))
2625
2622
  ])
2626
2623
  )
@@ -2664,7 +2661,7 @@ var preprocess = (path, { opts }) => {
2664
2661
  var index = () => {
2665
2662
  return {
2666
2663
  name: "JSX DOM Expressions",
2667
- inherits: SyntaxJSX__default['default'],
2664
+ inherits: SyntaxJSX__default["default"],
2668
2665
  visitor: {
2669
2666
  JSXElement: transformJSX,
2670
2667
  JSXFragment: transformJSX,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "babel-plugin-jsx-dom-expressions",
3
3
  "description": "A JSX to DOM plugin that wraps expressions for fine grained change detection",
4
- "version": "0.31.0-beta.0",
4
+ "version": "0.31.0",
5
5
  "author": "Ryan Carniato",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -18,10 +18,10 @@
18
18
  "prepublishOnly": "npm run build"
19
19
  },
20
20
  "dependencies": {
21
- "@babel/helper-module-imports": "^7.10.4",
22
- "@babel/plugin-syntax-jsx": "^7.10.4",
23
- "@babel/types": "^7.11.5",
21
+ "@babel/helper-module-imports": "7.16.0",
22
+ "@babel/plugin-syntax-jsx": "^7.16.5",
23
+ "@babel/types": "^7.16.0",
24
24
  "html-entities": "2.3.2"
25
25
  },
26
- "gitHead": "bdb5b31867eb9002d0f87738a23d912ed025211b"
26
+ "gitHead": "f6597dac2273ec022b8859ce868f6138b0c2a180"
27
27
  }