@fictjs/compiler 0.5.0 → 0.5.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/README.md CHANGED
@@ -24,6 +24,8 @@ createFictPlugin({
24
24
  onWarn(warning) {
25
25
  console.warn(warning)
26
26
  },
27
+ // Keep single-use derived values as memos (strict memo mode)
28
+ // inlineDerivedMemos: false,
27
29
  // Allow $state/$effect inside reactive-scope callbacks (e.g., renderHook(() => ...))
28
30
  reactiveScopes: ['renderHook'],
29
31
  })
@@ -31,6 +33,7 @@ createFictPlugin({
31
33
 
32
34
  - `dev` (default: `NODE_ENV !== 'production' && NODE_ENV !== 'test'`): enables compiler warnings/diagnostics. Set to `false` to silence warnings.
33
35
  - `onWarn`: custom warning handler (only called when `dev` is enabled).
36
+ - `inlineDerivedMemos` (default: `true`): allow the compiler to inline single-use derived values. Set to `false` for a “strict memo” mode where user-named derived values keep explicit memo accessors (unless `"use no memo"` disables memoization).
34
37
  - `reactiveScopes`: function names whose **first callback argument** is treated as a component-like reactive scope.
35
38
  - Only **direct calls** are recognized (e.g., `renderHook(() => ...)` or `utils.renderHook(() => ...)`).
36
39
  - **Aliases/indirect calls** are not recognized (e.g., `const rh = renderHook; rh(() => ...)`).
package/dist/index.cjs CHANGED
@@ -16138,6 +16138,7 @@ function convertExpression(node, options) {
16138
16138
  const arrow = {
16139
16139
  kind: "ArrowFunction",
16140
16140
  params: nested.params,
16141
+ rawParams: nested.rawParams ?? node.params,
16141
16142
  body: nested.blocks,
16142
16143
  isExpression: false,
16143
16144
  isAsync: node.async,
@@ -16151,6 +16152,7 @@ function convertExpression(node, options) {
16151
16152
  params: node.params.map(
16152
16153
  (p) => t.isPattern(p) ? extractIdentifiersFromPattern(p) : t.isIdentifier(p) ? [{ kind: "Identifier", name: p.name }] : []
16153
16154
  ).flat(),
16155
+ rawParams: node.params,
16154
16156
  body: convertExpression(node.body),
16155
16157
  isExpression: true,
16156
16158
  isAsync: node.async,
@@ -16172,6 +16174,7 @@ function convertExpression(node, options) {
16172
16174
  kind: "FunctionExpression",
16173
16175
  name: node.id?.name ?? "",
16174
16176
  params: nested.params,
16177
+ rawParams: nested.rawParams ?? node.params,
16175
16178
  body: nested.blocks,
16176
16179
  isAsync: node.async,
16177
16180
  reactiveScope: options?.reactiveScope,
@@ -23905,7 +23908,13 @@ function lowerExpressionImpl(expr, ctx, _isAssigned = false) {
23905
23908
  })),
23906
23909
  entryBlock: fn.blocks[0]?.id ?? 0
23907
23910
  };
23908
- const declared = new Set(paramIds.map((p) => p.name));
23911
+ const declared = /* @__PURE__ */ new Set();
23912
+ for (const p of paramIds) {
23913
+ const ids = t4.getBindingIdentifiers(p);
23914
+ for (const name of Object.keys(ids)) {
23915
+ declared.add(name);
23916
+ }
23917
+ }
23909
23918
  return lowerStructuredNodeWithoutRegions(structured, t4, ctx, declared);
23910
23919
  } catch {
23911
23920
  return lowerBlocksToStatements(blocks);
@@ -24206,7 +24215,12 @@ function lowerExpressionImpl(expr, ctx, _isAssigned = false) {
24206
24215
  case "ArrowFunction": {
24207
24216
  const reactiveLowered = lowerReactiveScopeExpression(expr);
24208
24217
  if (reactiveLowered) return reactiveLowered;
24209
- const paramIds = mapParams(expr.params);
24218
+ const useRawParams = expr.rawParams && expr.rawParams.length > 0 && expr.rawParams.every(
24219
+ (param) => t4.isIdentifier(param) || t4.isRestElement(param) && t4.isIdentifier(param.argument)
24220
+ );
24221
+ const paramIds = useRawParams ? expr.rawParams.map(
24222
+ (param) => t4.cloneNode(param, true)
24223
+ ) : mapParams(expr.params);
24210
24224
  const shadowed = new Set(expr.params.map((p) => deSSAVarName(p.name)));
24211
24225
  const localDeclared = collectLocalDeclaredNames(
24212
24226
  expr.params,
@@ -24254,7 +24268,12 @@ function lowerExpressionImpl(expr, ctx, _isAssigned = false) {
24254
24268
  case "FunctionExpression": {
24255
24269
  const reactiveLowered = lowerReactiveScopeExpression(expr);
24256
24270
  if (reactiveLowered) return reactiveLowered;
24257
- const paramIds = mapParams(expr.params);
24271
+ const useRawParams = expr.rawParams && expr.rawParams.length > 0 && expr.rawParams.every(
24272
+ (param) => t4.isIdentifier(param) || t4.isRestElement(param) && t4.isIdentifier(param.argument)
24273
+ );
24274
+ const paramIds = useRawParams ? expr.rawParams.map(
24275
+ (param) => t4.cloneNode(param, true)
24276
+ ) : mapParams(expr.params);
24258
24277
  const shadowed = new Set(expr.params.map((p) => deSSAVarName(p.name)));
24259
24278
  const localDeclared = collectLocalDeclaredNames(expr.params, expr.body, t4);
24260
24279
  return withNonReactiveScope(
@@ -26819,6 +26838,8 @@ function buildListCallExpression(expr, statements, ctx) {
26819
26838
  }
26820
26839
  const keyExpr = extractKeyFromMapCallback(mapCallback);
26821
26840
  const isKeyed = !!keyExpr;
26841
+ const hasRestParam = (mapCallback.kind === "ArrowFunction" || mapCallback.kind === "FunctionExpression") && Array.isArray(mapCallback.rawParams) && mapCallback.rawParams.some((param) => t4.isRestElement(param));
26842
+ const canConstifyKey = isKeyed && keyExpr && !hasRestParam;
26822
26843
  if (isKeyed) {
26823
26844
  ctx.helpersUsed.add("keyedList");
26824
26845
  } else {
@@ -26832,7 +26853,7 @@ function buildListCallExpression(expr, statements, ctx) {
26832
26853
  const prevListKeyExpr = ctx.listKeyExpr;
26833
26854
  const prevListItemParamName = ctx.listItemParamName;
26834
26855
  const prevListKeyParamName = ctx.listKeyParamName;
26835
- if (isKeyed && keyExpr) {
26856
+ if (canConstifyKey && keyExpr) {
26836
26857
  ctx.listKeyExpr = keyExpr;
26837
26858
  ctx.listKeyParamName = "__key";
26838
26859
  if (mapCallback.kind === "ArrowFunction" || mapCallback.kind === "FunctionExpression") {
@@ -26918,7 +26939,7 @@ function buildListCallExpression(expr, statements, ctx) {
26918
26939
  keyExprAst
26919
26940
  );
26920
26941
  const hasIndexParam = (t4.isArrowFunctionExpression(callbackExpr) || t4.isFunctionExpression(callbackExpr)) && callbackExpr.params.length >= 2;
26921
- if (t4.isArrowFunctionExpression(callbackExpr) || t4.isFunctionExpression(callbackExpr)) {
26942
+ if (canConstifyKey && (t4.isArrowFunctionExpression(callbackExpr) || t4.isFunctionExpression(callbackExpr))) {
26922
26943
  const newParams = [...callbackExpr.params];
26923
26944
  while (newParams.length < 2) {
26924
26945
  newParams.push(t4.identifier(newParams.length === 0 ? "__item" : "__index"));
package/dist/index.js CHANGED
@@ -16123,6 +16123,7 @@ function convertExpression(node, options) {
16123
16123
  const arrow = {
16124
16124
  kind: "ArrowFunction",
16125
16125
  params: nested.params,
16126
+ rawParams: nested.rawParams ?? node.params,
16126
16127
  body: nested.blocks,
16127
16128
  isExpression: false,
16128
16129
  isAsync: node.async,
@@ -16136,6 +16137,7 @@ function convertExpression(node, options) {
16136
16137
  params: node.params.map(
16137
16138
  (p) => t.isPattern(p) ? extractIdentifiersFromPattern(p) : t.isIdentifier(p) ? [{ kind: "Identifier", name: p.name }] : []
16138
16139
  ).flat(),
16140
+ rawParams: node.params,
16139
16141
  body: convertExpression(node.body),
16140
16142
  isExpression: true,
16141
16143
  isAsync: node.async,
@@ -16157,6 +16159,7 @@ function convertExpression(node, options) {
16157
16159
  kind: "FunctionExpression",
16158
16160
  name: node.id?.name ?? "",
16159
16161
  params: nested.params,
16162
+ rawParams: nested.rawParams ?? node.params,
16160
16163
  body: nested.blocks,
16161
16164
  isAsync: node.async,
16162
16165
  reactiveScope: options?.reactiveScope,
@@ -23890,7 +23893,13 @@ function lowerExpressionImpl(expr, ctx, _isAssigned = false) {
23890
23893
  })),
23891
23894
  entryBlock: fn.blocks[0]?.id ?? 0
23892
23895
  };
23893
- const declared = new Set(paramIds.map((p) => p.name));
23896
+ const declared = /* @__PURE__ */ new Set();
23897
+ for (const p of paramIds) {
23898
+ const ids = t4.getBindingIdentifiers(p);
23899
+ for (const name of Object.keys(ids)) {
23900
+ declared.add(name);
23901
+ }
23902
+ }
23894
23903
  return lowerStructuredNodeWithoutRegions(structured, t4, ctx, declared);
23895
23904
  } catch {
23896
23905
  return lowerBlocksToStatements(blocks);
@@ -24191,7 +24200,12 @@ function lowerExpressionImpl(expr, ctx, _isAssigned = false) {
24191
24200
  case "ArrowFunction": {
24192
24201
  const reactiveLowered = lowerReactiveScopeExpression(expr);
24193
24202
  if (reactiveLowered) return reactiveLowered;
24194
- const paramIds = mapParams(expr.params);
24203
+ const useRawParams = expr.rawParams && expr.rawParams.length > 0 && expr.rawParams.every(
24204
+ (param) => t4.isIdentifier(param) || t4.isRestElement(param) && t4.isIdentifier(param.argument)
24205
+ );
24206
+ const paramIds = useRawParams ? expr.rawParams.map(
24207
+ (param) => t4.cloneNode(param, true)
24208
+ ) : mapParams(expr.params);
24195
24209
  const shadowed = new Set(expr.params.map((p) => deSSAVarName(p.name)));
24196
24210
  const localDeclared = collectLocalDeclaredNames(
24197
24211
  expr.params,
@@ -24239,7 +24253,12 @@ function lowerExpressionImpl(expr, ctx, _isAssigned = false) {
24239
24253
  case "FunctionExpression": {
24240
24254
  const reactiveLowered = lowerReactiveScopeExpression(expr);
24241
24255
  if (reactiveLowered) return reactiveLowered;
24242
- const paramIds = mapParams(expr.params);
24256
+ const useRawParams = expr.rawParams && expr.rawParams.length > 0 && expr.rawParams.every(
24257
+ (param) => t4.isIdentifier(param) || t4.isRestElement(param) && t4.isIdentifier(param.argument)
24258
+ );
24259
+ const paramIds = useRawParams ? expr.rawParams.map(
24260
+ (param) => t4.cloneNode(param, true)
24261
+ ) : mapParams(expr.params);
24243
24262
  const shadowed = new Set(expr.params.map((p) => deSSAVarName(p.name)));
24244
24263
  const localDeclared = collectLocalDeclaredNames(expr.params, expr.body, t4);
24245
24264
  return withNonReactiveScope(
@@ -26804,6 +26823,8 @@ function buildListCallExpression(expr, statements, ctx) {
26804
26823
  }
26805
26824
  const keyExpr = extractKeyFromMapCallback(mapCallback);
26806
26825
  const isKeyed = !!keyExpr;
26826
+ const hasRestParam = (mapCallback.kind === "ArrowFunction" || mapCallback.kind === "FunctionExpression") && Array.isArray(mapCallback.rawParams) && mapCallback.rawParams.some((param) => t4.isRestElement(param));
26827
+ const canConstifyKey = isKeyed && keyExpr && !hasRestParam;
26807
26828
  if (isKeyed) {
26808
26829
  ctx.helpersUsed.add("keyedList");
26809
26830
  } else {
@@ -26817,7 +26838,7 @@ function buildListCallExpression(expr, statements, ctx) {
26817
26838
  const prevListKeyExpr = ctx.listKeyExpr;
26818
26839
  const prevListItemParamName = ctx.listItemParamName;
26819
26840
  const prevListKeyParamName = ctx.listKeyParamName;
26820
- if (isKeyed && keyExpr) {
26841
+ if (canConstifyKey && keyExpr) {
26821
26842
  ctx.listKeyExpr = keyExpr;
26822
26843
  ctx.listKeyParamName = "__key";
26823
26844
  if (mapCallback.kind === "ArrowFunction" || mapCallback.kind === "FunctionExpression") {
@@ -26903,7 +26924,7 @@ function buildListCallExpression(expr, statements, ctx) {
26903
26924
  keyExprAst
26904
26925
  );
26905
26926
  const hasIndexParam = (t4.isArrowFunctionExpression(callbackExpr) || t4.isFunctionExpression(callbackExpr)) && callbackExpr.params.length >= 2;
26906
- if (t4.isArrowFunctionExpression(callbackExpr) || t4.isFunctionExpression(callbackExpr)) {
26927
+ if (canConstifyKey && (t4.isArrowFunctionExpression(callbackExpr) || t4.isFunctionExpression(callbackExpr))) {
26907
26928
  const newParams = [...callbackExpr.params];
26908
26929
  while (newParams.length < 2) {
26909
26930
  newParams.push(t4.identifier(newParams.length === 0 ? "__item" : "__index"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fictjs/compiler",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "description": "Babel plugin for Fict Compiler",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -48,7 +48,7 @@
48
48
  "@types/babel__helper-plugin-utils": "^7.10.3",
49
49
  "@types/babel__traverse": "^7.28.0",
50
50
  "tsup": "^8.5.1",
51
- "@fictjs/runtime": "0.5.0"
51
+ "@fictjs/runtime": "0.5.2"
52
52
  },
53
53
  "scripts": {
54
54
  "build": "tsup src/index.ts --format cjs,esm --dts",