@litsx/babel-preset-litsx 0.3.0 → 0.5.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.
@@ -8,82 +8,11 @@ function createThisMemberExpression(propName) {
8
8
  return t.memberExpression(t.thisExpression(), t.identifier(propName));
9
9
  }
10
10
 
11
- function getBoundPropName(bindingInfo) {
12
- if (typeof bindingInfo === "string") {
13
- return bindingInfo;
14
- }
15
-
16
- if (bindingInfo && typeof bindingInfo === "object") {
17
- return bindingInfo.bindKey ?? null;
18
- }
19
-
20
- return null;
21
- }
22
-
23
- function isPropBackedCallee(node, localNames) {
24
- if (t.isIdentifier(node)) {
25
- return localNames.includes(node.name);
26
- }
27
-
28
- if (
29
- t.isMemberExpression(node) &&
30
- !node.computed &&
31
- t.isIdentifier(node.object)
32
- ) {
33
- return localNames.includes(node.object.name) && node.object.name === "props";
34
- }
35
-
36
- return false;
37
- }
38
-
39
- function getPropBackedCalleeReplacement(node, bindings) {
40
- if (t.isIdentifier(node)) {
41
- const propName = getBoundPropName(bindings.get(node.name));
42
- return propName ? createThisMemberExpression(propName) : node;
43
- }
44
-
45
- if (
46
- t.isMemberExpression(node) &&
47
- !node.computed &&
48
- t.isIdentifier(node.object)
49
- ) {
50
- const propName = getBoundPropName(bindings.get(node.object.name));
51
- if (!propName) {
52
- return node;
53
- }
54
-
55
- return t.memberExpression(
56
- createThisMemberExpression(propName),
57
- t.cloneNode(node.property),
58
- false
59
- );
60
- }
61
-
62
- return node;
63
- }
64
-
65
11
  export function transformJSXExpressions(jsxPath, bindings, state = null) {
66
12
  const localNames = Array.from(bindings.keys());
67
13
 
68
14
  jsxPath.traverse({
69
15
  JSXExpressionContainer(expressionPath) {
70
- if (t.isCallExpression(expressionPath.node.expression)) {
71
- const { callee, arguments: args } = expressionPath.node.expression;
72
- if (isPropBackedCallee(callee, localNames)) {
73
- if (state) {
74
- state.__litsxNeedsRendererCallImport = true;
75
- }
76
- expressionPath.node.expression = t.callExpression(
77
- t.identifier("renderRendererCall"),
78
- [
79
- getPropBackedCalleeReplacement(callee, bindings),
80
- ...args,
81
- ]
82
- );
83
- return;
84
- }
85
- }
86
-
87
16
  if (t.isIdentifier(expressionPath.node.expression)) {
88
17
  const name = expressionPath.node.expression.name;
89
18
  if (localNames.includes(name)) {
@@ -1117,7 +1117,7 @@ export function extractProperties(functionPath, programPath, options = {}) {
1117
1117
  if (!propertyMap.has(propName) && warn) {
1118
1118
  warn({
1119
1119
  code: 91018,
1120
- message: `Falling back to String for prop "${propName}" inferred from opaque props access. Prefer destructuring, TypeScript types, or ^properties(...) for stronger property metadata.`,
1120
+ message: `Falling back to String for prop "${propName}" inferred from opaque props access. Prefer destructuring, TypeScript types, or static properties = ... for stronger property metadata.`,
1121
1121
  propName,
1122
1122
  localName: bindingName,
1123
1123
  line: memberPath.node.loc?.start?.line ?? null,
@@ -0,0 +1,113 @@
1
+ import {
2
+ createComponentInstanceRefSyncStatement,
3
+ hasRefProp,
4
+ lowerForwardedElementRefs,
5
+ } from "./transform-litsx-refs.js";
6
+ import {
7
+ replaceParamReferences,
8
+ transformJSXExpressions,
9
+ } from "./transform-litsx-param-rewrites.js";
10
+ import { transformJSXRendererCalls } from "./transform-litsx-renderer-calls.js";
11
+
12
+ let t;
13
+
14
+ export function setRenderBodyBabelTypes(nextTypes) {
15
+ t = nextTypes;
16
+ }
17
+
18
+ function createThisMemberExpression(propName) {
19
+ return t.memberExpression(t.thisExpression(), t.identifier(propName));
20
+ }
21
+
22
+ function createNestedInitializerStatement(pattern, root, defaultValue) {
23
+ const rootAccess = createThisMemberExpression(root);
24
+ let sourceExpression = rootAccess;
25
+
26
+ if (defaultValue) {
27
+ sourceExpression = t.logicalExpression(
28
+ "??",
29
+ t.cloneNode(rootAccess),
30
+ t.cloneNode(defaultValue)
31
+ );
32
+ }
33
+
34
+ return t.variableDeclaration("const", [
35
+ t.variableDeclarator(t.cloneNode(pattern), sourceExpression),
36
+ ]);
37
+ }
38
+
39
+ function collectReturnStatement(functionPath, bindings, state) {
40
+ let returnStatement = null;
41
+
42
+ functionPath.traverse({
43
+ ReturnStatement(returnPath) {
44
+ if (t.isJSXElement(returnPath.node.argument)) {
45
+ returnStatement = returnPath.node;
46
+ transformJSXRendererCalls(returnPath, bindings, state);
47
+ transformJSXExpressions(returnPath, bindings, state);
48
+ }
49
+ },
50
+ });
51
+
52
+ return returnStatement;
53
+ }
54
+
55
+ export function prepareComponentRender(functionPath, node, propertyNames, bindings, nestedInitializers, options = {}) {
56
+ const returnStatement = collectReturnStatement(
57
+ functionPath,
58
+ bindings,
59
+ options.state ?? null
60
+ );
61
+
62
+ if (!returnStatement) {
63
+ return null;
64
+ }
65
+
66
+ const capturedPropAliasStatements = replaceParamReferences(
67
+ functionPath,
68
+ bindings,
69
+ propertyNames
70
+ );
71
+ const prefixStatements = [];
72
+
73
+ const forwardRefOptions = options.forwardRef || null;
74
+ const resolvedRefPropName =
75
+ forwardRefOptions?.propName ||
76
+ (propertyNames.has("ref") || hasRefProp(functionPath) ? "ref" : null);
77
+ let needsCallbackRef = false;
78
+
79
+ if (resolvedRefPropName) {
80
+ prefixStatements.push(
81
+ ...lowerForwardedElementRefs(functionPath, resolvedRefPropName)
82
+ );
83
+ needsCallbackRef =
84
+ prefixStatements.some(
85
+ (statement) =>
86
+ t.isExpressionStatement(statement) &&
87
+ t.isCallExpression(statement.expression) &&
88
+ t.isIdentifier(statement.expression.callee, { name: "useCallbackRef" })
89
+ ) || needsCallbackRef;
90
+ }
91
+
92
+ if (resolvedRefPropName && !forwardRefOptions) {
93
+ prefixStatements.push(createComponentInstanceRefSyncStatement());
94
+ needsCallbackRef = true;
95
+ }
96
+
97
+ if (capturedPropAliasStatements.length > 0) {
98
+ prefixStatements.push(...capturedPropAliasStatements);
99
+ }
100
+
101
+ if (nestedInitializers.length > 0) {
102
+ const initializerStatements = nestedInitializers.map(({ pattern, root, defaultValue }) =>
103
+ createNestedInitializerStatement(pattern, root, defaultValue)
104
+ );
105
+ prefixStatements.push(...initializerStatements);
106
+ }
107
+
108
+ return {
109
+ needsCallbackRef,
110
+ prefixStatements,
111
+ returnStatement,
112
+ };
113
+ }
@@ -0,0 +1,92 @@
1
+ let t;
2
+
3
+ export function setRendererCallsBabelTypes(nextTypes) {
4
+ t = nextTypes;
5
+ }
6
+
7
+ function createThisMemberExpression(propName) {
8
+ return t.memberExpression(t.thisExpression(), t.identifier(propName));
9
+ }
10
+
11
+ function getBoundPropName(bindingInfo) {
12
+ if (typeof bindingInfo === "string") {
13
+ return bindingInfo;
14
+ }
15
+
16
+ if (bindingInfo && typeof bindingInfo === "object") {
17
+ return bindingInfo.bindKey ?? null;
18
+ }
19
+
20
+ return null;
21
+ }
22
+
23
+ function isPropBackedCallee(node, localNames) {
24
+ if (t.isIdentifier(node)) {
25
+ return localNames.includes(node.name);
26
+ }
27
+
28
+ if (
29
+ t.isMemberExpression(node) &&
30
+ !node.computed &&
31
+ t.isIdentifier(node.object)
32
+ ) {
33
+ return localNames.includes(node.object.name) && node.object.name === "props";
34
+ }
35
+
36
+ return false;
37
+ }
38
+
39
+ function getPropBackedCalleeReplacement(node, bindings) {
40
+ if (t.isIdentifier(node)) {
41
+ const propName = getBoundPropName(bindings.get(node.name));
42
+ return propName ? createThisMemberExpression(propName) : node;
43
+ }
44
+
45
+ if (
46
+ t.isMemberExpression(node) &&
47
+ !node.computed &&
48
+ t.isIdentifier(node.object)
49
+ ) {
50
+ const propName = getBoundPropName(bindings.get(node.object.name));
51
+ if (!propName) {
52
+ return node;
53
+ }
54
+
55
+ return t.memberExpression(
56
+ createThisMemberExpression(propName),
57
+ t.cloneNode(node.property),
58
+ false
59
+ );
60
+ }
61
+
62
+ return node;
63
+ }
64
+
65
+ export function transformJSXRendererCalls(jsxPath, bindings, state = null) {
66
+ const localNames = Array.from(bindings.keys());
67
+
68
+ jsxPath.traverse({
69
+ JSXExpressionContainer(expressionPath) {
70
+ if (!t.isCallExpression(expressionPath.node.expression)) {
71
+ return;
72
+ }
73
+
74
+ const { callee, arguments: args } = expressionPath.node.expression;
75
+ if (!isPropBackedCallee(callee, localNames)) {
76
+ return;
77
+ }
78
+
79
+ if (state) {
80
+ state.__litsxNeedsRendererCallImport = true;
81
+ }
82
+
83
+ expressionPath.node.expression = t.callExpression(
84
+ t.identifier("renderRendererCall"),
85
+ [
86
+ getPropBackedCalleeReplacement(callee, bindings),
87
+ ...args,
88
+ ]
89
+ );
90
+ },
91
+ });
92
+ }
@@ -1,5 +1,6 @@
1
1
  import jsxSyntaxPlugin from "@babel/plugin-syntax-jsx";
2
2
  import { decodeVirtualAttributeName } from "@litsx/jsx-authoring";
3
+ import { importedBindingNeedsRendererContext } from "./transform-litsx-element-candidates.js";
3
4
 
4
5
  let t;
5
6
 
@@ -238,7 +239,7 @@ function expressionNeedsRendererContext(node, scope, seenBindings = new Set()) {
238
239
  return false;
239
240
  }
240
241
 
241
- function isBindableFunctionReference(expressionPath) {
242
+ function isBindableFunctionReference(expressionPath, options = {}) {
242
243
  const expression = unwrapExpression(expressionPath.node);
243
244
  if (
244
245
  t.isArrowFunctionExpression(expression) ||
@@ -251,7 +252,12 @@ function isBindableFunctionReference(expressionPath) {
251
252
  const binding = expressionPath.scope.getBinding(expression.name);
252
253
  const functionNode = getFunctionNodeFromBinding(binding);
253
254
  if (!functionNode) {
254
- return false;
255
+ const programPath = expressionPath.findParent((entry) => entry.isProgram?.());
256
+ return importedBindingNeedsRendererContext(
257
+ programPath,
258
+ expression.name,
259
+ options
260
+ );
255
261
  }
256
262
  return functionBodyNeedsRendererContext(functionNode.body, binding.path.scope, new Set([binding]));
257
263
  }
@@ -259,7 +265,7 @@ function isBindableFunctionReference(expressionPath) {
259
265
  return false;
260
266
  }
261
267
 
262
- function shouldBindRendererContext(attributePath, rawName, expressionPath) {
268
+ function shouldBindRendererContext(attributePath, rawName, expressionPath, options = {}) {
263
269
  if (typeof rawName !== "string" || rawName[0] !== ".") {
264
270
  return false;
265
271
  }
@@ -274,7 +280,7 @@ function shouldBindRendererContext(attributePath, rawName, expressionPath) {
274
280
  return false;
275
281
  }
276
282
 
277
- return isBindableFunctionReference(expressionPath);
283
+ return isBindableFunctionReference(expressionPath, options);
278
284
  }
279
285
 
280
286
  function ensureRendererBindingImport(programPath) {
@@ -342,7 +348,9 @@ export default function transformLitsxRendererProps(api) {
342
348
  return;
343
349
  }
344
350
 
345
- if (!shouldBindRendererContext(path, rawName, expressionPath)) {
351
+ if (!shouldBindRendererContext(path, rawName, expressionPath, {
352
+ filename: state.file?.opts?.filename || "",
353
+ })) {
346
354
  return;
347
355
  }
348
356
 
@@ -16,10 +16,17 @@ function isLightDomHoist(statement) {
16
16
  if (!t.isIdentifier(statement.expression.callee, { name: "__litsx_static_lightDom" })) {
17
17
  return false;
18
18
  }
19
- if (statement.expression.arguments.length !== 0) {
20
- throw new Error("^lightDom() does not accept arguments.");
19
+
20
+ const args = statement.expression.arguments;
21
+ if (args.length === 0) {
22
+ return true;
23
+ }
24
+
25
+ if (args.length === 1 && t.isBooleanLiteral(args[0], { value: true })) {
26
+ return true;
21
27
  }
22
- return true;
28
+
29
+ throw new Error("static lightDom = true only accepts the literal value true.");
23
30
  }
24
31
 
25
32
  function createStaticHoistGetter(name, symbolId, expression) {
@@ -97,11 +104,11 @@ function getStaticPropsExpression(statement) {
97
104
 
98
105
  const [argument] = statement.expression.arguments;
99
106
  if (isHoistedProperties && (t.isFunctionExpression(argument) || t.isArrowFunctionExpression(argument))) {
100
- throw new Error("^properties(...) only accepts an object literal with static Lit property options.");
107
+ throw new Error("static properties = ... only accepts an object literal with static Lit property options.");
101
108
  }
102
109
 
103
110
  if (!t.isObjectExpression(argument)) {
104
- throw new Error("^properties(...) only accepts an object literal with static Lit property options.");
111
+ throw new Error("static properties = ... only accepts an object literal with static Lit property options.");
105
112
  }
106
113
 
107
114
  return isHoistedProperties ? {
@@ -146,7 +153,7 @@ function normalizeStaticPropOverrideValue(value) {
146
153
  }
147
154
 
148
155
  throw new Error(
149
- "^properties(...) values must be Lit property option objects or constructor references."
156
+ "static properties = ... values must be Lit property option objects or constructor references."
150
157
  );
151
158
  }
152
159
 
@@ -157,12 +164,12 @@ function mergeStaticPropertyObject(targetNode, overrideObject) {
157
164
 
158
165
  overrideObject.properties.forEach((property) => {
159
166
  if (!t.isObjectProperty(property) && !t.isObjectMethod(property)) {
160
- throw new Error("^properties(...) only accepts plain object members.");
167
+ throw new Error("static properties = ... only accepts plain object members.");
161
168
  }
162
169
 
163
170
  const keyName = getStaticPropertyName(property.key);
164
171
  if (!keyName) {
165
- throw new Error("^properties(...) property option names must be static identifiers or strings.");
172
+ throw new Error("static properties = ... property option names must be static identifiers or strings.");
166
173
  }
167
174
 
168
175
  const existing = targetNode.value.properties.find(
@@ -194,12 +201,12 @@ function mergeStaticPropsIntoProperties(propertiesStatic, staticProps) {
194
201
  staticProps.forEach((optionsObject) => {
195
202
  optionsObject.properties.forEach((property) => {
196
203
  if (!t.isObjectProperty(property)) {
197
- throw new Error("^properties(...) only accepts plain object properties.");
204
+ throw new Error("static properties = ... only accepts plain object properties.");
198
205
  }
199
206
 
200
207
  const keyName = getStaticPropertyName(property.key);
201
208
  if (!keyName) {
202
- throw new Error("^properties(...) property names must be static identifiers or strings.");
209
+ throw new Error("static properties = ... property names must be static identifiers or strings.");
203
210
  }
204
211
 
205
212
  const existing = propertyMap.get(keyName);
@@ -302,7 +309,7 @@ function getStaticStylesExpression(statement, functionPath) {
302
309
  const [argument] = statement.expression.arguments;
303
310
 
304
311
  if (isHoistedStyles && (t.isFunctionExpression(argument) || t.isArrowFunctionExpression(argument))) {
305
- throw new Error("^styles(...) only accepts static values. Move dynamic values to useStyle(...) or CSS custom properties.");
312
+ throw new Error("static styles = ... only accepts static values. Move dynamic values to useStyle(...) or CSS custom properties.");
306
313
  }
307
314
 
308
315
  const template = normalizeStylesTemplate(
@@ -310,7 +317,7 @@ function getStaticStylesExpression(statement, functionPath) {
310
317
  functionPath
311
318
  );
312
319
  if (!template) {
313
- throw new Error("^styles(...) only accepts static values. Move dynamic values to useStyle(...) or CSS custom properties.");
320
+ throw new Error("static styles = ... only accepts static values. Move dynamic values to useStyle(...) or CSS custom properties.");
314
321
  }
315
322
 
316
323
  const expression = t.taggedTemplateExpression(t.identifier("css"), template);
@@ -335,7 +342,7 @@ function getStaticHoistExpression(statement, functionPath) {
335
342
  }
336
343
 
337
344
  if (statement.expression.arguments.length !== 1) {
338
- throw new Error(`^${name}(...) expects exactly one argument.`);
345
+ throw new Error(`static ${name} = ... expects exactly one argument.`);
339
346
  }
340
347
 
341
348
  const [argument] = statement.expression.arguments;
@@ -347,15 +354,15 @@ function getStaticHoistExpression(statement, functionPath) {
347
354
  };
348
355
  }
349
356
 
350
- throw new Error("^expose(...) only accepts an object literal.");
357
+ throw new Error("static expose = ... only accepts an object literal.");
351
358
  }
352
359
 
353
360
  if (t.isFunctionExpression(argument) || t.isArrowFunctionExpression(argument)) {
354
- throw new Error(`^${name}(...) only accepts a direct static value.`);
361
+ throw new Error(`static ${name} = ... only accepts a direct static value.`);
355
362
  }
356
363
 
357
364
  if (!isStaticStylesExpression(argument, functionPath)) {
358
- throw new Error(`^${name}(...) only accepts a direct static value.`);
365
+ throw new Error(`static ${name} = ... only accepts a direct static value.`);
359
366
  }
360
367
 
361
368
  return {
@@ -379,7 +386,7 @@ function normalizeExposeHoistExpression(expression) {
379
386
  };
380
387
  }
381
388
 
382
- throw new Error("^expose(...) only accepts an object literal.");
389
+ throw new Error("static expose = ... only accepts an object literal.");
383
390
  }
384
391
 
385
392
  function createExposeClassMethod(property) {
@@ -390,12 +397,12 @@ function createExposeClassMethod(property) {
390
397
 
391
398
  function normalizeExposePropertyToClassMethod(property) {
392
399
  if (t.isSpreadElement(property)) {
393
- throw new Error("^expose(...) does not accept spread elements.");
400
+ throw new Error("static expose = ... does not accept spread elements.");
394
401
  }
395
402
 
396
403
  if (t.isObjectMethod(property)) {
397
404
  if (property.kind !== "method") {
398
- throw new Error("^expose(...) only accepts plain methods.");
405
+ throw new Error("static expose = ... only accepts plain methods.");
399
406
  }
400
407
 
401
408
  return t.classMethod(
@@ -408,12 +415,12 @@ function normalizeExposePropertyToClassMethod(property) {
408
415
  }
409
416
 
410
417
  if (!t.isObjectProperty(property)) {
411
- throw new Error("^expose(...) only accepts plain methods.");
418
+ throw new Error("static expose = ... only accepts plain methods.");
412
419
  }
413
420
 
414
421
  const value = property.value;
415
422
  if (!t.isFunctionExpression(value) && !t.isArrowFunctionExpression(value)) {
416
- throw new Error("^expose(...) values must be functions.");
423
+ throw new Error("static expose = ... values must be functions.");
417
424
  }
418
425
 
419
426
  const body = t.isBlockStatement(value.body)
@@ -451,7 +458,7 @@ export function assertStaticHoistsStayTopLevel(functionPath) {
451
458
 
452
459
  const macroName = callPath.node.callee.name.slice("__litsx_static_".length);
453
460
  throw callPath.buildCodeFrameError(
454
- `^${macroName}(...) must appear as a top-level statement in the component body.`
461
+ `static ${macroName} = ... must appear as a top-level statement in the component body.`
455
462
  );
456
463
  },
457
464
  });
@@ -662,8 +669,12 @@ export function processStaticHoists({
662
669
  }
663
670
  }
664
671
 
665
- if (lightDomRequested && staticHoists.some((entry) => entry.name === "shadowRootOptions")) {
666
- throw new Error("^lightDom() cannot be combined with ^shadowRootOptions(...).");
672
+ if (lightDomRequested) {
673
+ for (let index = staticHoists.length - 1; index >= 0; index -= 1) {
674
+ if (staticHoists[index]?.name === "shadowRootOptions") {
675
+ staticHoists.splice(index, 1);
676
+ }
677
+ }
667
678
  }
668
679
 
669
680
  if (staticProps.length > 0) {
package/src/pipeline.js CHANGED
@@ -11,12 +11,13 @@ const NATIVE_TRANSFORM_OPTION_KEYS = [
11
11
  "inMemoryFiles",
12
12
  "typescriptSession",
13
13
  "suppressNativeClassNameWarning",
14
+ "__litsxCompilationSession",
14
15
  ];
15
16
 
16
17
  const HOOK_FEATURE_PATTERN = /\b(?:useOnConnect|useAfterUpdate|useOnCommit|useMemoValue|useStableCallback|useEvent|useEmit|usePrevious|useReducedState|useState|useControlledState|useAsyncState|useOptimistic|useExpose|useExternalStore|useHost|useHostContent|useSlot|useTextContent|useTransition|useDeferredValue|useStyle|useRef|useCallbackRef)\b/;
17
18
  const REF_FEATURE_PATTERN = /\buseRef\b|\bref\s*=/;
18
19
  const SCOPED_ELEMENTS_PATTERN = /<\s*(?:[A-Z][\w.]*(?=[\s/>])|[a-z][\w]*-[\w-]*(?=[\s/>]))/;
19
- const LIGHT_DOM_PATTERN = /\^lightDom\b/;
20
+ const LIGHT_DOM_PATTERN = /\^lightDom\b|static\s+lightDom\s*=\s*true\b/;
20
21
 
21
22
  export function normalizeTransformLitsxOptions(options = {}) {
22
23
  const transformLitsxOptions = {