@marko/runtime-tags 6.0.109 → 6.0.110

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.
@@ -2827,10 +2827,7 @@ function getExprIfSerialized(section, reason, expr) {
2827
2827
 
2828
2828
  // src/translator/util/walks.ts
2829
2829
  var import_compiler15 = require("@marko/compiler");
2830
- var [getWalks] = createSectionState(
2831
- "walks",
2832
- () => [""]
2833
- );
2830
+ var [getWalks] = createSectionState("walks", () => [""]);
2834
2831
  var [getWalkComment] = createSectionState(
2835
2832
  "walkComment",
2836
2833
  () => []
@@ -2871,16 +2868,12 @@ function enterShallow(path5) {
2871
2868
  const steps = getSteps(section);
2872
2869
  steps.push(0 /* Enter */, 1 /* Exit */);
2873
2870
  }
2874
- function injectWalks(tag, expr) {
2871
+ function injectWalks(tag, name2, expr) {
2875
2872
  const section = getSection(tag);
2876
2873
  const walks = getWalks(section);
2877
2874
  const walkComment = getWalkComment(section);
2878
2875
  visitInternal(section);
2879
- walkComment.push(
2880
- `${walkCodeToName[tag.node.var ? 48 /* BeginChildWithVar */ : 47 /* BeginChild */]}`,
2881
- expr && import_compiler15.types.isIdentifier(expr) ? expr.name : tag.get("name").toString(),
2882
- walkCodeToName[38 /* EndChild */]
2883
- );
2876
+ walkComment.push(`<${name2}${tag.node.var ? "/var" : ""}>`);
2884
2877
  appendLiteral(
2885
2878
  walks,
2886
2879
  String.fromCharCode(
@@ -2979,18 +2972,18 @@ function getWalkString(section) {
2979
2972
  }
2980
2973
  visitInternal(section);
2981
2974
  const walks = getWalks(section);
2982
- const walkLiteral = normalizeStringExpression(walks);
2975
+ const walkLiteral = normalizeStringExpression(walks.map(unwrapWalk));
2983
2976
  if (walkLiteral && walkLiteral.value !== "") {
2984
2977
  withLeadingComment(walkLiteral, getWalkComment(section).join(", "));
2985
2978
  }
2986
2979
  return walkLiteral;
2987
2980
  }
2981
+ function unwrapWalk(walk) {
2982
+ return typeof walk === "function" ? walk() || "" : walk;
2983
+ }
2988
2984
 
2989
2985
  // src/translator/util/writer.ts
2990
- var [getWrites] = createSectionState(
2991
- "writes",
2992
- () => [""]
2993
- );
2986
+ var [getWrites] = createSectionState("writes", () => [""]);
2994
2987
  var [getTrailerWrites] = createSectionState(
2995
2988
  "trailerWrites",
2996
2989
  () => [""]
@@ -3011,7 +3004,7 @@ function consumeHTML(path5) {
3011
3004
  const section = getSection(path5);
3012
3005
  const writes = getWrites(section);
3013
3006
  const trailers = getTrailerWrites(section);
3014
- const writeResult = normalizeStringExpression(writes);
3007
+ const writeResult = normalizeStringExpression(writes.map(unwrapWrite));
3015
3008
  const trailerResult = normalizeStringExpression(trailers);
3016
3009
  writes.length = 0;
3017
3010
  writes[0] = "";
@@ -3050,9 +3043,12 @@ var [getSectionMeta] = createSectionState(
3050
3043
  const writePostfix = section.content?.endType === 1 /* Dynamic */ ? "<!>" : "";
3051
3044
  const writes = getWrites(section);
3052
3045
  const meta = {
3053
- setup: getSetup(section),
3054
3046
  walks: getWalkString(section),
3055
- writes: normalizeStringExpression([writePrefix, ...writes, writePostfix]),
3047
+ writes: normalizeStringExpression([
3048
+ writePrefix,
3049
+ ...writes.map(unwrapWrite),
3050
+ writePostfix
3051
+ ]),
3056
3052
  decls: void 0
3057
3053
  };
3058
3054
  return meta;
@@ -3097,6 +3093,9 @@ function markNode(path5, nodeBinding, reason) {
3097
3093
  }
3098
3094
  }
3099
3095
  }
3096
+ function unwrapWrite(write) {
3097
+ return typeof write === "function" ? write() || "" : write;
3098
+ }
3100
3099
 
3101
3100
  // src/translator/core/return.ts
3102
3101
  var tagsWithReturn = /* @__PURE__ */ new WeakSet();
@@ -4362,8 +4361,9 @@ function getRegisteredFnExpression(node) {
4362
4361
  var dom_default = {
4363
4362
  translate: {
4364
4363
  exit(program) {
4364
+ forEachSectionReverse(getSectionMeta);
4365
4365
  const section = getSectionForBody(program);
4366
- const { walks, writes, setup, decls } = getSectionMeta(section);
4366
+ const { walks, writes, decls } = getSectionMeta(section);
4367
4367
  const domExports = program.node.extra.domExports;
4368
4368
  const templateIdentifier = import_compiler23.types.identifier(domExports.template);
4369
4369
  const walksIdentifier = import_compiler23.types.identifier(domExports.walks);
@@ -4398,14 +4398,15 @@ var dom_default = {
4398
4398
  });
4399
4399
  const tagParamsSignal = childSection.params && initValue(childSection.params);
4400
4400
  const tagParamsIdentifier = tagParamsSignal && signalHasStatements(tagParamsSignal) ? tagParamsSignal.identifier : void 0;
4401
- const { walks: walks2, writes: writes2, setup: setup2, decls: decls2 } = getSectionMeta(childSection);
4401
+ const { walks: walks2, writes: writes2, decls: decls2 } = getSectionMeta(childSection);
4402
+ const setup = getSetup(childSection);
4402
4403
  writeSignals(childSection);
4403
4404
  if (!childSection.downstreamBinding || bindingHasDownstreamExpressions(childSection.downstreamBinding)) {
4404
4405
  if (getSectionParentIsOwner(childSection)) {
4405
4406
  setSectionRendererArgs(childSection, [
4406
4407
  writes2,
4407
4408
  walks2,
4408
- setup2,
4409
+ setup,
4409
4410
  tagParamsIdentifier
4410
4411
  ]);
4411
4412
  } else {
@@ -4415,7 +4416,7 @@ var dom_default = {
4415
4416
  ...replaceNullishAndEmptyFunctionsWith0([
4416
4417
  writes2,
4417
4418
  walks2,
4418
- setup2,
4419
+ setup,
4419
4420
  tagParamsIdentifier,
4420
4421
  childSection.hoisted || childSection.isHoistThrough ? getSectionInstancesAccessorLiteral(childSection) : void 0
4421
4422
  ])
@@ -4455,13 +4456,13 @@ var dom_default = {
4455
4456
  }
4456
4457
  }
4457
4458
  if (decls2) {
4458
- extraDecls = extraDecls ? [...extraDecls, ...decls2] : decls2;
4459
+ extraDecls = extraDecls ? [...decls2, ...extraDecls] : decls2;
4459
4460
  }
4460
4461
  }
4461
4462
  });
4462
4463
  writeSignals(section);
4463
4464
  writeRegisteredFns();
4464
- if (!setup) {
4465
+ if (!getSetup(section)) {
4465
4466
  program.node.body.unshift(
4466
4467
  import_compiler23.types.exportNamedDeclaration(
4467
4468
  import_compiler23.types.variableDeclaration("const", [
@@ -8484,8 +8485,8 @@ function finalizeReferences() {
8484
8485
  }
8485
8486
  }
8486
8487
  }
8488
+ forEachSection(finalizeParamSerializeReasonGroups);
8487
8489
  forEachSectionReverse((section) => {
8488
- finalizeParamSerializeReasonGroups(section);
8489
8490
  finalizeKnownTags(section);
8490
8491
  finalizeSerializeReason(section);
8491
8492
  finalizeParamSerializeReasonGroups(section);
@@ -9421,6 +9422,11 @@ var define_default = {
9421
9422
  const babelBinding = tag.scope.getBinding(tag.node.var.name);
9422
9423
  let allDirectReferences = true;
9423
9424
  for (const ref of babelBinding.referencePaths) {
9425
+ if (isReferenceHoisted(babelBinding.path, ref)) {
9426
+ throw ref.buildCodeFrameError(
9427
+ "Hoisted values must be functions. The `<define>` tag variable may not be hoisted. Please move the `<define>` tag prior to any references or extract to a separate `.marko` file."
9428
+ );
9429
+ }
9424
9430
  if (ref.parent.type === "MarkoTag" && ref.parent.name === ref.node) {
9425
9431
  (ref.parent.extra ??= {}).defineBodySection = bodySection;
9426
9432
  dropReferences(ref.parent.name);
@@ -11478,6 +11484,7 @@ function buildUndefined4() {
11478
11484
  // src/translator/core/try.ts
11479
11485
  var import_compiler57 = require("@marko/compiler");
11480
11486
  var import_babel_utils46 = require("@marko/compiler/babel-utils");
11487
+ var hasEnabledCatch = /* @__PURE__ */ new WeakSet();
11481
11488
  var kDOMBinding2 = Symbol("try tag dom binding");
11482
11489
  var try_default = {
11483
11490
  analyze(tag) {
@@ -11591,9 +11598,13 @@ var try_default = {
11591
11598
  translatedAttrs.statements
11592
11599
  );
11593
11600
  }
11594
- (0, import_babel_utils46.getProgram)().node.body.push(
11595
- import_compiler57.types.expressionStatement(callRuntime("_enable_catch"))
11596
- );
11601
+ const program = (0, import_babel_utils46.getProgram)().node;
11602
+ if (!hasEnabledCatch.has(program)) {
11603
+ hasEnabledCatch.add(program);
11604
+ program.body.push(
11605
+ import_compiler57.types.expressionStatement(callRuntime("_enable_catch"))
11606
+ );
11607
+ }
11597
11608
  addValue(
11598
11609
  section,
11599
11610
  referencedBindings,
@@ -12181,7 +12192,7 @@ function translateDOM(tag) {
12181
12192
  }
12182
12193
  );
12183
12194
  write`${import_compiler62.types.identifier(childExports.template)}`;
12184
- injectWalks(tag, import_compiler62.types.identifier(childExports.walks));
12195
+ injectWalks(tag, tagName, import_compiler62.types.identifier(childExports.walks));
12185
12196
  } else {
12186
12197
  knownTagTranslateDOM(
12187
12198
  tag,
@@ -12214,6 +12225,7 @@ function translateDOM(tag) {
12214
12225
  write`${(0, import_babel_utils50.importNamed)(file, relativePath, childExports.template, `${tagName}_template`)}`;
12215
12226
  injectWalks(
12216
12227
  tag,
12228
+ tagName,
12217
12229
  (0, import_babel_utils50.importNamed)(file, relativePath, childExports.walks, `${tagName}_walks`)
12218
12230
  );
12219
12231
  }
@@ -12359,11 +12371,12 @@ var dynamic_tag_default = {
12359
12371
  }
12360
12372
  }
12361
12373
  );
12362
- const sectionMeta = getSectionMetaIdentifiers(definedBodySection);
12363
- if (sectionMeta.writes) {
12364
- write`${sectionMeta.writes}`;
12365
- }
12366
- injectWalks(tag, sectionMeta.walks);
12374
+ write`${() => getSectionMetaIdentifiers(definedBodySection).writes || ""}`;
12375
+ injectWalks(
12376
+ tag,
12377
+ tag.get("name").toString(),
12378
+ () => getSectionMetaIdentifiers(definedBodySection).walks
12379
+ );
12367
12380
  tag.remove();
12368
12381
  }
12369
12382
  return;
@@ -86,6 +86,7 @@ export declare function trackDomVarReferences(tag: t.NodePath<t.MarkoTag>, bindi
86
86
  export declare function trackVarReferences(tag: t.NodePath<t.MarkoTag>, type: BindingType, upstreamAlias?: Binding["upstreamAlias"]): Binding | undefined;
87
87
  export declare function trackParamsReferences(body: t.NodePath<t.MarkoTagBody | t.Program>, type: BindingType, upstreamAlias?: Binding["upstreamAlias"]): Binding | undefined;
88
88
  export declare function trackHoistedReference(referencePath: t.NodePath<t.Identifier>, binding: Binding): void;
89
+ export declare function isReferenceHoisted(bindingPath: t.NodePath, reference: t.NodePath): boolean;
89
90
  export declare function setReferencesScope(path: t.NodePath<any>): void;
90
91
  export declare function mergeReferences<T extends t.Node>(section: Section, target: T, nodes: (t.Node | undefined)[]): NonNullable<T["extra"]> & ReferencedExtra;
91
92
  export declare function compareReferences(a: ReferencedBindings, b: ReferencedBindings): number;
@@ -1,6 +1,7 @@
1
1
  import { types as t } from "@marko/compiler";
2
2
  import { WalkCode } from "../../common/types";
3
3
  import { type Section } from "../util/sections";
4
+ type Walk = string | t.Expression | (() => undefined | string | t.Expression);
4
5
  export declare enum Step {
5
6
  Enter = 0,
6
7
  Exit = 1
@@ -9,7 +10,7 @@ type VisitCodes = WalkCode.Get | WalkCode.Inside | WalkCode.Replace | WalkCode.D
9
10
  export declare function enter(path: t.NodePath<any>): void;
10
11
  export declare function exit(path: t.NodePath<any>): void;
11
12
  export declare function enterShallow(path: t.NodePath<any>): void;
12
- export declare function injectWalks(tag: t.NodePath<t.MarkoTag>, expr: t.Expression | undefined): void;
13
+ export declare function injectWalks(tag: t.NodePath<t.MarkoTag>, name: string, expr: Walk): void;
13
14
  export declare function visit(path: t.NodePath<t.MarkoTag | t.MarkoPlaceholder | t.Program>, code: VisitCodes): void;
14
15
  export declare function getWalkString(section: Section): t.Expression | undefined;
15
16
  export {};
@@ -2,12 +2,12 @@ import { types as t } from "@marko/compiler";
2
2
  import { type Section } from "../util/sections";
3
3
  import { type Binding } from "./references";
4
4
  import type { SerializeReason } from "./serialize-reasons";
5
- export declare function writeTo(path: t.NodePath<any>, trailer?: boolean): (strs: TemplateStringsArray, ...exprs: Array<string | t.Expression>) => void;
5
+ type Write = string | t.Expression | (() => undefined | string | t.Expression);
6
+ export declare function writeTo(path: t.NodePath<any>, trailer?: boolean): (strs: TemplateStringsArray, ...exprs: Write[]) => void;
6
7
  export declare function consumeHTML(path: t.NodePath<any>): t.ExpressionStatement | undefined;
7
8
  export declare function flushBefore(path: t.NodePath<any>): void;
8
9
  export declare function flushInto(path: t.NodePath<t.MarkoTag> | t.NodePath<t.Program>): void;
9
10
  interface SectionMeta {
10
- setup: t.Expression | undefined;
11
11
  walks: t.Expression | undefined;
12
12
  writes: t.Expression | undefined;
13
13
  decls: t.VariableDeclarator[] | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marko/runtime-tags",
3
- "version": "6.0.109",
3
+ "version": "6.0.110",
4
4
  "description": "Optimized runtime for Marko templates.",
5
5
  "keywords": [
6
6
  "api",