@litsx/babel-preset-litsx 0.8.0 → 0.8.2

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.cjs CHANGED
@@ -13,7 +13,7 @@ require('./internal/transform-litsx-hooks.cjs');
13
13
  require('./internal/transform-litsx-renderer-props.cjs');
14
14
  require('@babel/plugin-syntax-jsx');
15
15
  require('@litsx/authoring');
16
- require('./shared/transform-litsx-element-candidates-JMFlPFXK.cjs');
16
+ require('./shared/transform-litsx-element-candidates-D0uvqG4Y.cjs');
17
17
  require('@babel/helper-plugin-utils');
18
18
  require('@babel/traverse');
19
19
  require('@litsx/babel-parser');
@@ -4,7 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var jsxSyntaxPlugin = require('@babel/plugin-syntax-jsx');
6
6
  var internal_transformLitsxProperties = require('./transform-litsx-properties.cjs');
7
- var transformLitsxElementCandidates = require('../shared/transform-litsx-element-candidates-JMFlPFXK.cjs');
7
+ var transformLitsxElementCandidates = require('../shared/transform-litsx-element-candidates-D0uvqG4Y.cjs');
8
8
  var authoring = require('@litsx/authoring');
9
9
  require('module');
10
10
  require('@litsx/typescript-session');
@@ -1693,6 +1693,42 @@ function createThisMemberExpression$2(propName) {
1693
1693
  return t$4.memberExpression(t$4.thisExpression(), t$4.identifier(propName));
1694
1694
  }
1695
1695
 
1696
+ function createPropsObjectExpression(bindingInfo, propertyMap = new Map()) {
1697
+ if (
1698
+ bindingInfo &&
1699
+ typeof bindingInfo !== "object" &&
1700
+ bindingInfo !== "props"
1701
+ ) {
1702
+ return null;
1703
+ }
1704
+
1705
+ const propNames = new Set([
1706
+ ...(bindingInfo && typeof bindingInfo === "object" && bindingInfo.kind === "alias"
1707
+ ? Array.from(bindingInfo.properties?.keys?.() || [])
1708
+ : []),
1709
+ ...Array.from(propertyMap.keys?.() || []),
1710
+ ]);
1711
+ const properties = Array.from(propNames)
1712
+ .filter((propName) => typeof propName === "string" && propName.length > 0)
1713
+ .sort()
1714
+ .map((propName) =>
1715
+ t$4.objectProperty(
1716
+ t$4.isValidIdentifier(propName) ? t$4.identifier(propName) : t$4.stringLiteral(propName),
1717
+ createThisMemberExpression$2(propName)
1718
+ )
1719
+ );
1720
+
1721
+ return t$4.objectExpression(properties);
1722
+ }
1723
+
1724
+ function isObjectDestructuringInitializer(refPath) {
1725
+ return (
1726
+ refPath.parentPath?.isVariableDeclarator() &&
1727
+ refPath.parentKey === "init" &&
1728
+ t$4.isObjectPattern(refPath.parentPath.node.id)
1729
+ );
1730
+ }
1731
+
1696
1732
  function transformJSXExpressions(jsxPath, bindings, state = null) {
1697
1733
  const localNames = Array.from(bindings.keys());
1698
1734
 
@@ -1830,10 +1866,20 @@ function replaceParamReferences(functionPath, bindings, propertyMap = new Map(),
1830
1866
  refPath.parentPath.isObjectProperty({ shorthand: true }) &&
1831
1867
  refPath.parentKey === "value"
1832
1868
  ) {
1869
+ const propsObject = createPropsObjectExpression(bindingInfo, propertyMap);
1870
+ if (propsObject) {
1871
+ refPath.parentPath.node.shorthand = false;
1872
+ refPath.replaceWith(propsObject);
1873
+ return;
1874
+ }
1833
1875
  return;
1834
1876
  }
1835
1877
 
1836
- refPath.replaceWith(t$4.thisExpression());
1878
+ refPath.replaceWith(
1879
+ isObjectDestructuringInitializer(refPath)
1880
+ ? t$4.thisExpression()
1881
+ : createPropsObjectExpression(bindingInfo, propertyMap) ?? t$4.thisExpression()
1882
+ );
1837
1883
  return;
1838
1884
  }
1839
1885
 
@@ -1915,6 +1961,14 @@ function replaceParamReferences(functionPath, bindings, propertyMap = new Map(),
1915
1961
  }
1916
1962
  }
1917
1963
 
1964
+ if (localName === "props") {
1965
+ const propsObject = createPropsObjectExpression(bindingInfo, propertyMap);
1966
+ if (propsObject) {
1967
+ refPath.replaceWith(propsObject);
1968
+ return;
1969
+ }
1970
+ }
1971
+
1918
1972
  refPath.replaceWith(getReplacementForProp(targetProp || localName, refPath));
1919
1973
  });
1920
1974
  });
@@ -2069,12 +2123,20 @@ function throwFirstImplicitChildrenProjectionIssue(functionPath) {
2069
2123
  throw functionPath.buildCodeFrameError(issue.message);
2070
2124
  }
2071
2125
 
2126
+ function isRenderableJsx(node) {
2127
+ return t$2.isJSXElement(node) || t$2.isJSXFragment(node);
2128
+ }
2129
+
2072
2130
  function collectReturnStatement(functionPath, bindings, state) {
2073
2131
  let returnStatement = null;
2074
2132
 
2075
2133
  functionPath.traverse({
2076
2134
  ReturnStatement(returnPath) {
2077
- if (t$2.isJSXElement(returnPath.node.argument)) {
2135
+ if (returnPath.getFunctionParent() !== functionPath) {
2136
+ return;
2137
+ }
2138
+
2139
+ if (isRenderableJsx(returnPath.node.argument)) {
2078
2140
  returnStatement = returnPath.node;
2079
2141
  transformJSXRendererCalls(returnPath, bindings, state);
2080
2142
  transformJSXExpressions(returnPath, bindings, state);