@decocms/blocks 8.5.2 → 8.7.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 (126) hide show
  1. package/dist/async/default.d.ts +10 -0
  2. package/dist/async/default.d.ts.map +1 -0
  3. package/dist/async/default.js +5 -0
  4. package/dist/async/default.js.map +1 -0
  5. package/dist/async/node.d.ts +3 -0
  6. package/dist/async/node.d.ts.map +1 -0
  7. package/dist/async/node.js +11 -0
  8. package/dist/async/node.js.map +1 -0
  9. package/dist/async/types.d.ts +6 -0
  10. package/dist/async/types.d.ts.map +1 -0
  11. package/dist/async/types.js +2 -0
  12. package/dist/async/types.js.map +1 -0
  13. package/dist/index.d.ts +1 -1
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +1 -1
  16. package/dist/index.js.map +1 -1
  17. package/dist/v8/bucketing.d.ts +24 -0
  18. package/dist/v8/bucketing.d.ts.map +1 -0
  19. package/dist/v8/bucketing.js +78 -0
  20. package/dist/v8/bucketing.js.map +1 -0
  21. package/dist/v8/builtins/campaigns.d.ts +36 -0
  22. package/dist/v8/builtins/campaigns.d.ts.map +1 -0
  23. package/dist/v8/builtins/campaigns.js +107 -0
  24. package/dist/v8/builtins/campaigns.js.map +1 -0
  25. package/dist/v8/builtins/data.d.ts +2 -2
  26. package/dist/v8/builtins/data.js +2 -2
  27. package/dist/v8/builtins/experiments.d.ts +27 -0
  28. package/dist/v8/builtins/experiments.d.ts.map +1 -0
  29. package/dist/v8/builtins/experiments.js +62 -0
  30. package/dist/v8/builtins/experiments.js.map +1 -0
  31. package/dist/v8/builtins/index.d.ts.map +1 -1
  32. package/dist/v8/builtins/index.js +7 -2
  33. package/dist/v8/builtins/index.js.map +1 -1
  34. package/dist/v8/builtins/matchers.d.ts +0 -14
  35. package/dist/v8/builtins/matchers.d.ts.map +1 -1
  36. package/dist/v8/builtins/matchers.js +0 -19
  37. package/dist/v8/builtins/matchers.js.map +1 -1
  38. package/dist/v8/cli/builtins.d.ts +6 -0
  39. package/dist/v8/cli/builtins.d.ts.map +1 -1
  40. package/dist/v8/cli/builtins.js +19 -0
  41. package/dist/v8/cli/builtins.js.map +1 -1
  42. package/dist/v8/cli/check/index.d.ts.map +1 -1
  43. package/dist/v8/cli/check/index.js +231 -8
  44. package/dist/v8/cli/check/index.js.map +1 -1
  45. package/dist/v8/cli/schema/builtinSchemas.d.ts.map +1 -1
  46. package/dist/v8/cli/schema/builtinSchemas.js +76 -21
  47. package/dist/v8/cli/schema/builtinSchemas.js.map +1 -1
  48. package/dist/v8/cli/schema/generate.d.ts.map +1 -1
  49. package/dist/v8/cli/schema/generate.js +48 -2
  50. package/dist/v8/cli/schema/generate.js.map +1 -1
  51. package/dist/v8/client.d.ts +25 -0
  52. package/dist/v8/client.d.ts.map +1 -1
  53. package/dist/v8/client.js +58 -10
  54. package/dist/v8/client.js.map +1 -1
  55. package/dist/v8/cms.d.ts.map +1 -1
  56. package/dist/v8/cms.js +113 -44
  57. package/dist/v8/cms.js.map +1 -1
  58. package/dist/v8/content.d.ts +80 -1
  59. package/dist/v8/content.d.ts.map +1 -1
  60. package/dist/v8/content.js +149 -4
  61. package/dist/v8/content.js.map +1 -1
  62. package/dist/v8/current.d.ts +28 -0
  63. package/dist/v8/current.d.ts.map +1 -0
  64. package/dist/v8/current.js +77 -0
  65. package/dist/v8/current.js.map +1 -0
  66. package/dist/v8/draft.d.ts +40 -21
  67. package/dist/v8/draft.d.ts.map +1 -1
  68. package/dist/v8/draft.js +160 -25
  69. package/dist/v8/draft.js.map +1 -1
  70. package/dist/v8/experiments.d.ts +107 -0
  71. package/dist/v8/experiments.d.ts.map +1 -0
  72. package/dist/v8/experiments.js +292 -0
  73. package/dist/v8/experiments.js.map +1 -0
  74. package/dist/v8/hosts.d.ts +3 -2
  75. package/dist/v8/hosts.d.ts.map +1 -1
  76. package/dist/v8/hosts.js +3 -2
  77. package/dist/v8/hosts.js.map +1 -1
  78. package/dist/v8/index.d.ts +7 -5
  79. package/dist/v8/index.d.ts.map +1 -1
  80. package/dist/v8/index.js +6 -4
  81. package/dist/v8/index.js.map +1 -1
  82. package/dist/v8/preview/DraftBadge.d.ts +9 -1
  83. package/dist/v8/preview/DraftBadge.d.ts.map +1 -1
  84. package/dist/v8/preview/DraftBadge.js +42 -2
  85. package/dist/v8/preview/DraftBadge.js.map +1 -1
  86. package/dist/v8/remoteLoader.d.ts.map +1 -1
  87. package/dist/v8/remoteLoader.js +169 -21
  88. package/dist/v8/remoteLoader.js.map +1 -1
  89. package/dist/v8/resolver.d.ts +4 -2
  90. package/dist/v8/resolver.d.ts.map +1 -1
  91. package/dist/v8/resolver.js +11 -3
  92. package/dist/v8/resolver.js.map +1 -1
  93. package/dist/v8/settings.d.ts +16 -4
  94. package/dist/v8/settings.d.ts.map +1 -1
  95. package/dist/v8/settings.js +24 -21
  96. package/dist/v8/settings.js.map +1 -1
  97. package/dist/v8/types.d.ts +162 -22
  98. package/dist/v8/types.d.ts.map +1 -1
  99. package/package.json +35 -1
  100. package/src/async/default.ts +13 -0
  101. package/src/async/node.ts +12 -0
  102. package/src/async/types.ts +5 -0
  103. package/src/index.ts +4 -0
  104. package/src/v8/bucketing.ts +88 -0
  105. package/src/v8/builtins/campaigns.ts +120 -0
  106. package/src/v8/builtins/data.ts +2 -2
  107. package/src/v8/builtins/experiments.ts +72 -0
  108. package/src/v8/builtins/index.ts +7 -2
  109. package/src/v8/builtins/matchers.ts +0 -26
  110. package/src/v8/cli/builtins.ts +23 -0
  111. package/src/v8/cli/check/index.ts +289 -8
  112. package/src/v8/cli/schema/builtinSchemas.ts +82 -22
  113. package/src/v8/cli/schema/generate.ts +48 -3
  114. package/src/v8/client.ts +76 -10
  115. package/src/v8/cms.ts +134 -45
  116. package/src/v8/content.ts +168 -4
  117. package/src/v8/current.ts +98 -0
  118. package/src/v8/draft.ts +170 -30
  119. package/src/v8/experiments.ts +340 -0
  120. package/src/v8/hosts.ts +3 -2
  121. package/src/v8/index.ts +8 -4
  122. package/src/v8/preview/DraftBadge.tsx +72 -2
  123. package/src/v8/remoteLoader.ts +193 -26
  124. package/src/v8/resolver.ts +14 -4
  125. package/src/v8/settings.ts +35 -29
  126. package/src/v8/types.ts +163 -20
@@ -0,0 +1,72 @@
1
+ /**
2
+ * A/B tests as content (see /next/built-in-blocks#split-traffic): the
3
+ * built-in type `split-traffic`, which the saved block `experiments` has, and
4
+ * the built-in rule `experiment`.
5
+ *
6
+ * - `experiments` maps each test's name to the whole percentage of visitors
7
+ * in it (0–100: 0 off, 100 everyone). Resolving it gives each name and
8
+ * whether the client's visitor is in it, never a percentage.
9
+ * - `experiment { name }` is `true` when that answer is `true`.
10
+ *
11
+ * The visitor arrives as an argument to `cms.client({ visitor })`; these
12
+ * read it through `currentClient()`, before any `await`. A test the client
13
+ * is given (`cms.client({ experiments })`) is on or off as given, whatever
14
+ * the visitor and its percentage (a preview may switch on a test at 0%); a
15
+ * name the block doesn't have stays out.
16
+ */
17
+ import { currentClient, experimentOverrides } from "../current.ts";
18
+ import { type Assignments, assign } from "../experiments.ts";
19
+
20
+ /** The saved block every `experiment` rule reads, and its built-in type. */
21
+ export const EXPERIMENTS_BLOCK = "experiments";
22
+ export const SPLIT_TRAFFIC_TYPE = "split-traffic";
23
+
24
+ /** The built-in rule that reads a test. */
25
+ export const EXPERIMENT_RULE = "experiment";
26
+
27
+ /**
28
+ * `split-traffic`: each test and whether the current client's visitor is in
29
+ * it (`assign` from `@decocms/blocks/experiments`), with the tests the
30
+ * client was given applied on top. Without a visitor every test it wasn't
31
+ * given is off.
32
+ */
33
+ export const splitTraffic = (allocations: Record<string, number> = {}): Assignments => {
34
+ const client = currentClient();
35
+ const assignments = assign(allocations, client.visitor);
36
+ const given = experimentOverrides(client);
37
+ if (given !== undefined) {
38
+ for (const name of Object.keys(given)) {
39
+ if (Object.hasOwn(assignments, name)) assignments[name] = given[name] === true;
40
+ }
41
+ }
42
+ return assignments;
43
+ };
44
+
45
+ /** Inputs of the built-in `experiment` rule. */
46
+ interface ExperimentProps {
47
+ /** A test in the saved block `experiments`. */
48
+ name?: string;
49
+ }
50
+
51
+ /**
52
+ * `experiment`: `true` when the current client's visitor is in the test
53
+ * `name` of the saved block `experiments`. An unknown name, no `experiments`
54
+ * block, or no visitor give `false`. The block is resolved once per client,
55
+ * however many rules ask.
56
+ */
57
+ export const experiment = ({ name }: ExperimentProps = {}): boolean | Promise<boolean> => {
58
+ const client = currentClient();
59
+ if (typeof name !== "string") return false;
60
+ return client
61
+ .resolve<Record<string, unknown>>(EXPERIMENTS_BLOCK)
62
+ .then(([tests]) => isTrueAt(tests, name));
63
+ };
64
+
65
+ function isTrueAt(record: unknown, name: string): boolean {
66
+ return (
67
+ typeof record === "object" &&
68
+ record !== null &&
69
+ Object.hasOwn(record, name) &&
70
+ (record as Record<string, unknown>)[name] === true
71
+ );
72
+ }
@@ -1,7 +1,9 @@
1
1
  import type { BlockFunction } from "../types.ts";
2
+ import { CALENDAR_TYPE, calendar, campaign } from "./campaigns.ts";
2
3
  import { cmsSettings, page, redirect, SETTINGS_TYPE } from "./data.ts";
4
+ import { experiment, SPLIT_TRAFFIC_TYPE, splitTraffic } from "./experiments.ts";
3
5
  import { lazy } from "./lazy.ts";
4
- import { always, date, never } from "./matchers.ts";
6
+ import { always, never } from "./matchers.ts";
5
7
  import { multivariate } from "./multivariate.ts";
6
8
  import { secretBlock } from "./secret.ts";
7
9
 
@@ -14,11 +16,14 @@ export const builtIns: Readonly<Record<string, BlockFunction>> = Object.freeze({
14
16
  multivariate,
15
17
  always,
16
18
  never,
17
- date,
19
+ experiment,
20
+ campaign,
18
21
  page,
19
22
  redirect,
20
23
  [SETTINGS_TYPE]: cmsSettings,
21
24
  secret: secretBlock(),
25
+ [SPLIT_TRAFFIC_TYPE]: splitTraffic,
26
+ [CALENDAR_TYPE]: calendar,
22
27
  });
23
28
 
24
29
  /** Names no saved block can take (see /next/saved-blocks#names). */
@@ -3,29 +3,3 @@ export const always = (): boolean => true;
3
3
 
4
4
  /** `never`: the rule the site editor uses to hide a block. */
5
5
  export const never = (): boolean => false;
6
-
7
- /** Inputs of the built-in `date` matcher: ISO 8601 strings, both optional. */
8
- interface DateProps {
9
- /** Inclusive. */
10
- start?: string;
11
- /** Exclusive: `end` itself doesn't match. */
12
- end?: string;
13
- }
14
-
15
- /**
16
- * `date`: `true` from `start` (inclusive) until `end` (exclusive). A date
17
- * without a time means midnight UTC. An unparseable bound never matches, so a
18
- * typo can't switch a campaign on.
19
- */
20
- export const date = ({ start, end }: DateProps = {}): boolean => {
21
- const now = Date.now();
22
- if (start !== undefined && start !== null && start !== "") {
23
- const from = Date.parse(start);
24
- if (Number.isNaN(from) || now < from) return false;
25
- }
26
- if (end !== undefined && end !== null && end !== "") {
27
- const until = Date.parse(end);
28
- if (Number.isNaN(until) || now >= until) return false;
29
- }
30
- return true;
31
- };
@@ -11,6 +11,29 @@ export function isBuiltIn(name: string): boolean {
11
11
  return RESERVED_NAMES.has(name);
12
12
  }
13
13
 
14
+ /**
15
+ * What relies on each built-in, in Studio and in the runtime: `deco check`
16
+ * names it when a block map replaces one (a site may mean to; it is told
17
+ * what it takes over, not that it's wrong).
18
+ */
19
+ export const BUILT_IN_USES: Readonly<Record<string, string>> = Object.freeze({
20
+ lazy: "every Lazy<T> field relies on it, such as a variant's value, which runs only when chosen",
21
+ multivariate: "Studio's variants, the fields whose value changes by rule, rely on it",
22
+ always: "the default variant Studio adds to each list of variants relies on it",
23
+ never: "a variant Studio turns off relies on it",
24
+ experiment:
25
+ "Studio's A/B test rules rely on it, each naming a test of the saved block experiments",
26
+ campaign:
27
+ "Studio's campaign rules rely on it, each naming a campaign of the saved block campaigns",
28
+ page: "Studio's page list and page editor rely on it",
29
+ redirect: "Studio's redirect list relies on it",
30
+ "cms-settings": "the saved block CMS relies on it, and Studio's settings edit that block",
31
+ secret: "Studio's secret fields rely on it to keep their values encrypted",
32
+ "split-traffic":
33
+ "the saved block experiments relies on it, and Studio's Experiments view edits its percentages",
34
+ calendar: "the saved block campaigns relies on it, and Studio's Campaigns view edits its dates",
35
+ });
36
+
14
37
  /**
15
38
  * The legacy alias table lives with the runtime's alias bridge, so the
16
39
  * schema, the content module and resolution share one list. The site
@@ -21,10 +21,21 @@ import fs from "node:fs";
21
21
  import { Ajv, type ValidateFunction } from "ajv";
22
22
  import { isWellFormedCiphertext } from "../../../protocol/secrets.ts";
23
23
  import { BLOCKS_MAJOR } from "../../../protocol/types.ts";
24
+ import {
25
+ CALENDAR_TYPE,
26
+ CAMPAIGN_RULE,
27
+ CAMPAIGNS_BLOCK,
28
+ campaignTime,
29
+ } from "../../builtins/campaigns.ts";
24
30
  import { SETTINGS_BLOCK, SETTINGS_TYPE } from "../../builtins/data.ts";
31
+ import {
32
+ EXPERIMENT_RULE,
33
+ EXPERIMENTS_BLOCK,
34
+ SPLIT_TRAFFIC_TYPE,
35
+ } from "../../builtins/experiments.ts";
25
36
  import { isBlock, own } from "../../json.ts";
26
37
  import { findRouteConflicts } from "../../matchRoute.ts";
27
- import { isBuiltIn, storesPlainVariants } from "../builtins.ts";
38
+ import { BUILT_IN_BLOCKS, BUILT_IN_USES, isBuiltIn, storesPlainVariants } from "../builtins.ts";
28
39
  import { paint } from "../color.ts";
29
40
  import { readSavedBlocks, type SavedBlocks } from "../content.ts";
30
41
  import { consoleReporter, type Reporter } from "../log.ts";
@@ -73,12 +84,36 @@ function blockDefinitionName(schema: Json): string | null {
73
84
  return e && e.length === 1 && typeof schema.title === "string" && !schema.format ? e[0] : null;
74
85
  }
75
86
 
76
- /** Prerelease built-ins folded into the CMS settings block, with no alias. */
87
+ /** Prerelease built-ins folded into the CMS settings block, or renamed, with no alias. */
77
88
  const FOLDED_BUILT_INS: Record<string, string> = {
78
89
  telemetry: `the built-in is now the telemetry section of the "${SETTINGS_BLOCK}" block (type "${SETTINGS_TYPE}"); see /next/renames-and-migrations#site-settings`,
79
90
  analytics: `the built-in is now the analytics section of the "${SETTINGS_BLOCK}" block (type "${SETTINGS_TYPE}"); see /next/renames-and-migrations#site-settings`,
91
+ "date-windows": `the built-in is now "${CALENDAR_TYPE}": set the saved block "${CAMPAIGNS_BLOCK}" to { "__resolveType": "${CALENDAR_TYPE}", … }`,
80
92
  };
81
93
 
94
+ /** A `CMS` block saved with preview hosts, which Studio keeps now (in the release it publishes). */
95
+ const PREVIEW_FIELD_GONE =
96
+ "the site editor now keeps the addresses this site can be previewed on, so this field isn't read any more: you can delete it";
97
+
98
+ /** The prerelease `date` rule, which the `campaign` rule replaced. */
99
+ const OLD_DATE_RULE = "date";
100
+
101
+ /**
102
+ * What to write instead of a prerelease `date` rule: one naming a campaign
103
+ * becomes a `campaign` rule; one with its dates inline moves them into a
104
+ * campaign first.
105
+ */
106
+ function oldDateRuleProblem(block: Record<string, Json>): string {
107
+ const rule = `{ "__resolveType": "${CAMPAIGN_RULE}", "name": … }`;
108
+ if (typeof block.campaign === "string") {
109
+ return `unknown block type "${OLD_DATE_RULE}": the date rule is now "${CAMPAIGN_RULE}", so write { "__resolveType": "${CAMPAIGN_RULE}", "name": ${JSON.stringify(block.campaign)} }`;
110
+ }
111
+ if ("start" in block || "end" in block) {
112
+ return `unknown block type "${OLD_DATE_RULE}": this date rule has its dates inline; move them into a campaign of the saved block "${CAMPAIGNS_BLOCK}" (type "${CALENDAR_TYPE}") and write ${rule} naming it`;
113
+ }
114
+ return `unknown block type "${OLD_DATE_RULE}": the date rule is now "${CAMPAIGN_RULE}": write ${rule} naming a campaign of the saved block "${CAMPAIGNS_BLOCK}"`;
115
+ }
116
+
82
117
  // ---------------------------------------------------------------------------
83
118
  // The validation document: the schema, with every field also accepting a block
84
119
  // ---------------------------------------------------------------------------
@@ -216,8 +251,10 @@ function isDate(value: string): boolean {
216
251
  /**
217
252
  * The formats `deco check` enforces: the standard ones the schema's widgets
218
253
  * write (`@format date`, `date-time`, `uri`). A date-time is a date, `T` (or a
219
- * space) and a time whose seconds and zone are optional, since that is what
220
- * the site editor's date-time picker saves. Every other format names a
254
+ * space) and a time whose seconds and zone are optional: content written by
255
+ * hand or by older tools may leave them out (the site editor's date-time
256
+ * picker saves the offset, as in `2026-11-27T00:00:00-03:00`). A campaign
257
+ * bound needs the offset, which `unreadableBound` checks. Every other format names a
221
258
  * site-editor widget (`image-uri`, `color`, `rich-text`, …), not a rule, so it
222
259
  * accepts any string.
223
260
  */
@@ -230,6 +267,35 @@ const STANDARD_FORMATS: Record<string, (value: string) => boolean> = {
230
267
  uri: (value) => /^[a-z][a-z0-9+.-]*:/i.test(value) && URL.canParse(value),
231
268
  };
232
269
 
270
+ /** The offset the examples in campaign messages show (São Paulo), to replace with the store's own. */
271
+ const CAMPAIGN_EXAMPLE_OFFSET = "-03:00";
272
+
273
+ /**
274
+ * Why `deco check` rejects a campaign bound the runtime can't read
275
+ * (`campaignTime` is null), or null when it reads or is left to the generic
276
+ * date-time format error (not a date at all). A date or a date-time without
277
+ * an offset names a different moment depending on where it is read, so the
278
+ * message says to add the store's offset.
279
+ */
280
+ function unreadableBound(campaign: string, bound: "start" | "end", value: unknown): string | null {
281
+ if (typeof value !== "string" || value === "" || campaignTime(value) !== null) return null;
282
+ const verb = bound === "start" ? "starts" : "ends";
283
+ const what = `the campaign "${campaign}" ${verb} at ${JSON.stringify(value)}`;
284
+ if (isDate(value)) {
285
+ const like = `${value}T00:00${CAMPAIGN_EXAMPLE_OFFSET}`;
286
+ return `${what}, a date without a time or offset, so it never runs: add the time and your store's offset, like ${JSON.stringify(like)}`;
287
+ }
288
+ if (!STANDARD_FORMATS["date-time"]?.(value)) return null;
289
+ const iso = `${value.slice(0, 10)}T${value.slice(11)}`;
290
+ if (!/(?:z|[+-]\d{2}:?\d{2})$/i.test(value)) {
291
+ const like = `${iso}${CAMPAIGN_EXAMPLE_OFFSET}`;
292
+ return `${what}, which has no offset, so it never runs: add your store's offset, like ${JSON.stringify(like)}`;
293
+ }
294
+ const fixed = iso.replace(/z$/i, "Z").replace(/([+-]\d{2})(\d{2})$/, "$1:$2");
295
+ const like = campaignTime(fixed) === null ? `${value.slice(0, 10)}T00:00Z` : fixed;
296
+ return `${what}, which isn't written as a campaign reads it, so it never runs: write the date, "T", the time and the offset as Z or ±hh:mm, like ${JSON.stringify(like)}`;
297
+ }
298
+
233
299
  function formatsFor(doc: Json): Record<string, true | ((value: string) => boolean)> {
234
300
  const formats: Record<string, true | ((value: string) => boolean)> = { ...STANDARD_FORMATS };
235
301
  const seen = new Set<object>();
@@ -372,6 +438,112 @@ class Checker {
372
438
  return this.underlyingType(entry.__resolveType, [...chain, name]);
373
439
  }
374
440
 
441
+ /**
442
+ * Whether `name` is the built-in itself here: the block map didn't define
443
+ * the same key (which replaces the built-in, schema included).
444
+ */
445
+ private isOwnBuiltIn(name: string): boolean {
446
+ return isBuiltIn(name) && this.siteNamespaceOf(name) === undefined;
447
+ }
448
+
449
+ /** The namespace of the block map's own entry named `name`, if it has one. */
450
+ private siteNamespaceOf(name: string): string | undefined {
451
+ for (const entries of Object.values(this.meta.manifest?.blocks ?? {})) {
452
+ const entry = own(entries, name) as { namespace?: unknown } | undefined;
453
+ if (entry && entry.namespace !== "deco") return String(entry.namespace ?? "site");
454
+ }
455
+ return undefined;
456
+ }
457
+
458
+ /**
459
+ * The names in the saved block `name` (the tests of `experiments`, the
460
+ * campaigns of `campaigns`), or null when there is no such block of `type`.
461
+ */
462
+ private savedNames(name: string, type: string): Set<string> | null {
463
+ const entry = own(this.saved.blocks, name);
464
+ if (!isBlock(entry) || this.canonicalType(entry.__resolveType) !== type) return null;
465
+ return new Set(Object.keys(entry).filter((key) => key !== "__resolveType"));
466
+ }
467
+
468
+ /**
469
+ * The inputs a built-in `rule` (`experiment`, `campaign`) gets: the block's own,
470
+ * inline or over the saved block it names (whose inputs it may override).
471
+ * Undefined for any other block, or when the block map replaces the built-in.
472
+ */
473
+ private ruleInputs(block: Json, rule: string): Record<string, Json> | undefined {
474
+ if (!isBlock(block) || !this.isOwnBuiltIn(rule)) return undefined;
475
+ if (this.canonicalType(block.__resolveType) !== null) {
476
+ return this.canonicalType(block.__resolveType) === rule ? block : undefined;
477
+ }
478
+ const saved = own(this.saved.blocks, block.__resolveType);
479
+ if (!isBlock(saved) || this.canonicalType(saved.__resolveType) !== rule) return undefined;
480
+ return { ...saved, ...block };
481
+ }
482
+
483
+ /** Whether `type` is the built-in `calendar` (not one the block map replaces). */
484
+ private isCalendar(type: unknown): boolean {
485
+ return this.canonicalType(type as string) === CALENDAR_TYPE && this.isOwnBuiltIn(CALENDAR_TYPE);
486
+ }
487
+
488
+ /**
489
+ * A `calendar` block wherever it is (the saved block `campaigns`, another
490
+ * saved block, or inline in a section's props): its props, then the dates
491
+ * of every campaign in it, whose own messages replace the generic
492
+ * date-time format error at the same path.
493
+ */
494
+ private checkCalendar(ctx: Context, block: Record<string, Json>, path: string): void {
495
+ const before = ctx.problems.length;
496
+ this.checkProps(ctx, block, path, true);
497
+ const found = ctx.problems.splice(before);
498
+ const dates = this.checkCampaignWindows(ctx, block, path);
499
+ ctx.problems.push(
500
+ ...found.filter((p) => !(dates.has(p.path) && p.message.endsWith("isn't a valid date-time"))),
501
+ );
502
+ }
503
+
504
+ /**
505
+ * Each campaign of a `calendar` block with a bound the runtime can't read
506
+ * (a date without a time, a date-time without an offset, or one not written
507
+ * as ISO 8601: `campaignTime`), which is an error since that campaign never
508
+ * runs, or whose end isn't after its start. Returns the paths of the dates
509
+ * reported here, whose generic format error says less and is dropped.
510
+ */
511
+ private checkCampaignWindows(
512
+ ctx: Context,
513
+ entry: Record<string, Json>,
514
+ path: string,
515
+ ): Set<string> {
516
+ const reported = new Set<string>();
517
+ for (const key of Object.keys(entry).sort()) {
518
+ if (key === "__resolveType") continue;
519
+ const window = entry[key];
520
+ if (!isObject(window)) continue;
521
+ const { start, end } = window;
522
+ const at = (bound: string) =>
523
+ joinPath(path, `/${key.replace(/~/g, "~0").replace(/\//g, "~1")}/${bound}`);
524
+ for (const [bound, value] of [
525
+ ["start", start],
526
+ ["end", end],
527
+ ] as const) {
528
+ const problem = unreadableBound(key, bound, value);
529
+ if (problem === null) continue;
530
+ reported.add(at(bound));
531
+ this.report(ctx, at(bound), problem);
532
+ }
533
+ if (typeof start !== "string" || typeof end !== "string") continue;
534
+ const from = campaignTime(start);
535
+ const until = campaignTime(end);
536
+ if (typeof from !== "number" || typeof until !== "number" || until > from) continue;
537
+ this.report(
538
+ ctx,
539
+ at("end"),
540
+ `the campaign "${key}" ends before it starts, so it never runs: set an end after the start`,
541
+ "warning",
542
+ );
543
+ }
544
+ return reported;
545
+ }
546
+
375
547
  private report(
376
548
  ctx: Context,
377
549
  path: string,
@@ -493,9 +665,11 @@ class Checker {
493
665
  this.report(
494
666
  ctx,
495
667
  path,
496
- Object.hasOwn(FOLDED_BUILT_INS, name)
497
- ? `unknown block type "${name}": ${FOLDED_BUILT_INS[name]}`
498
- : `unknown block type "${name}"`,
668
+ name === OLD_DATE_RULE
669
+ ? oldDateRuleProblem(block)
670
+ : Object.hasOwn(FOLDED_BUILT_INS, name)
671
+ ? `unknown block type "${name}": ${FOLDED_BUILT_INS[name]}`
672
+ : `unknown block type "${name}"`,
499
673
  );
500
674
  return;
501
675
  }
@@ -521,7 +695,8 @@ class Checker {
521
695
  const target = this.saved.blocks[name];
522
696
  const targetType = String(target.__resolveType);
523
697
  const merged = { ...target, ...block, __resolveType: targetType };
524
- if (this.canonicalType(targetType)) this.checkProps(ctx, merged, path, true);
698
+ if (this.isCalendar(targetType)) this.checkCalendar(ctx, merged, path);
699
+ else if (this.canonicalType(targetType)) this.checkProps(ctx, merged, path, true);
525
700
  }
526
701
  return;
527
702
  }
@@ -538,6 +713,10 @@ class Checker {
538
713
  return;
539
714
  }
540
715
 
716
+ if (this.isCalendar(name)) {
717
+ this.checkCalendar(ctx, block, path);
718
+ return;
719
+ }
541
720
  switch (type) {
542
721
  case "multivariate":
543
722
  this.checkVariants(ctx, block, path, field);
@@ -555,6 +734,47 @@ class Checker {
555
734
  }
556
735
  return;
557
736
  }
737
+ case EXPERIMENT_RULE: {
738
+ this.checkProps(ctx, block, path, true);
739
+ const name = this.ruleInputs(block, EXPERIMENT_RULE)?.name;
740
+ if (
741
+ typeof name === "string" &&
742
+ !this.savedNames(EXPERIMENTS_BLOCK, SPLIT_TRAFFIC_TYPE)?.has(name)
743
+ ) {
744
+ this.report(
745
+ ctx,
746
+ joinPath(path, "/name"),
747
+ `no test "${name}" in the saved block "${EXPERIMENTS_BLOCK}": this rule is always false`,
748
+ "warning",
749
+ );
750
+ }
751
+ return;
752
+ }
753
+ case CAMPAIGN_RULE: {
754
+ this.checkProps(ctx, block, path, true);
755
+ const inputs = this.ruleInputs(block, CAMPAIGN_RULE);
756
+ const campaign = inputs?.name;
757
+ if (inputs !== undefined && typeof campaign !== "string") {
758
+ this.report(
759
+ ctx,
760
+ path,
761
+ `this campaign rule names no campaign, so it is always false: name a campaign of the saved block "${CAMPAIGNS_BLOCK}" (name)`,
762
+ "warning",
763
+ );
764
+ }
765
+ if (
766
+ typeof campaign === "string" &&
767
+ !this.savedNames(CAMPAIGNS_BLOCK, CALENDAR_TYPE)?.has(campaign)
768
+ ) {
769
+ this.report(
770
+ ctx,
771
+ joinPath(path, "/name"),
772
+ `no campaign "${campaign}" in the saved block "${CAMPAIGNS_BLOCK}": this rule is always false`,
773
+ "warning",
774
+ );
775
+ }
776
+ return;
777
+ }
558
778
  default:
559
779
  this.checkProps(ctx, block, path, true);
560
780
  }
@@ -674,12 +894,73 @@ class Checker {
674
894
  "warning",
675
895
  );
676
896
  }
897
+ if (
898
+ name === EXPERIMENTS_BLOCK &&
899
+ this.canonicalType(entry.__resolveType) !== SPLIT_TRAFFIC_TYPE
900
+ ) {
901
+ this.report(
902
+ ctx,
903
+ "",
904
+ `"${EXPERIMENTS_BLOCK}" is the saved block every experiment rule reads, of type ` +
905
+ `"${SPLIT_TRAFFIC_TYPE}"; with type "${entry.__resolveType}" every experiment rule is false`,
906
+ "warning",
907
+ );
908
+ }
909
+ if (name === CAMPAIGNS_BLOCK && this.isOwnBuiltIn(CALENDAR_TYPE)) {
910
+ if (this.canonicalType(entry.__resolveType) !== CALENDAR_TYPE) {
911
+ this.report(
912
+ ctx,
913
+ "",
914
+ `"${CAMPAIGNS_BLOCK}" is the saved block every campaign rule reads, of type ` +
915
+ `"${CALENDAR_TYPE}"; with type "${entry.__resolveType}" every campaign rule is false`,
916
+ "warning",
917
+ );
918
+ }
919
+ }
920
+ if (
921
+ name === SETTINGS_BLOCK &&
922
+ entry.__resolveType === SETTINGS_TYPE &&
923
+ Object.hasOwn(entry, "preview")
924
+ ) {
925
+ // Saved by an earlier version: Studio now keeps where the site previews.
926
+ const before = problems.length;
927
+ this.checkBlock(ctx, entry, "", null);
928
+ const found = problems.splice(before);
929
+ problems.push(
930
+ ...found.filter((p) => p.path !== "preview" && !p.path.startsWith("preview.")),
931
+ );
932
+ this.report(ctx, "preview", PREVIEW_FIELD_GONE, "warning");
933
+ continue;
934
+ }
677
935
  this.checkBlock(ctx, entry, "", null);
678
936
  }
937
+ problems.push(...this.builtInOverrides());
679
938
  problems.push(...this.routeProblems());
680
939
  return problems;
681
940
  }
682
941
 
942
+ /**
943
+ * A block map entry named after a built-in replaces it, schema included.
944
+ * Often deliberate (a site's own `page` with fields of its own), so a
945
+ * warning that says what relies on the built-in, never an error.
946
+ */
947
+ private builtInOverrides(): Problem[] {
948
+ const problems: Problem[] = [];
949
+ for (const name of BUILT_IN_BLOCKS) {
950
+ if (this.siteNamespaceOf(name) === undefined) continue;
951
+ problems.push({
952
+ file: ".deco/schema.gen.json",
953
+ path: name,
954
+ severity: "warning",
955
+ message:
956
+ `the block map has its own "${name}", in place of the built-in: ` +
957
+ `${BUILT_IN_USES[name] ?? "Studio relies on it"}. If that's intended, keep the ` +
958
+ "built-in's fields; to use the built-in again, rename yours",
959
+ });
960
+ }
961
+ return problems;
962
+ }
963
+
683
964
  private routeProblems(): Problem[] {
684
965
  type RouteEntry = { name: string; path: string };
685
966
  const routes: RouteEntry[] = [];