@intentius/chant 0.104.0 → 0.104.1

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/src/yaml.ts CHANGED
@@ -2,13 +2,22 @@
2
2
  * YAML emitter and parser.
3
3
  *
4
4
  * The emitter writes the block-style YAML chant's serializers produce. The
5
- * parser is js-yaml with chant's schema (#3006): YAML 1.2 core types,
5
+ * parser is a vendored js-yaml loader with chant's schema (#3006): YAML 1.2 core types,
6
6
  * merge keys, `yes`/`no` booleans, and local tags such as `!reference` read
7
7
  * as strings. `splitYAMLDocuments` splits a multi-document stream. Input the
8
8
  * parser cannot read throws a `YAMLParseError` naming the line.
9
9
  */
10
10
 
11
- import yaml from "js-yaml";
11
+ import {
12
+ FLOAT_TYPE,
13
+ INT_TYPE,
14
+ load,
15
+ MAP_TYPE,
16
+ NULL_TYPE,
17
+ SEQ_TYPE,
18
+ STR_TYPE,
19
+ YAMLException,
20
+ } from "./vendor/js-yaml-load";
12
21
 
13
22
  // ---------------------------------------------------------------------------
14
23
  // Emitter
@@ -191,14 +200,12 @@ const DOCUMENT_MARKER = /^(?:---|\.\.\.)(?:[ \t]+#.*)?[ \t]*$/;
191
200
  * Booleans as YAML 1.2's core schema reads them, plus `yes` and `no`, which
192
201
  * this parser has always read as booleans and its callers expect.
193
202
  */
194
- const BOOL = new yaml.Type("tag:yaml.org,2002:bool", {
203
+ const BOOL = {
204
+ tag: "tag:yaml.org,2002:bool",
195
205
  kind: "scalar",
196
206
  resolve: (data: string | null) => data !== null && /^(?:true|True|TRUE|false|False|FALSE|yes|no)$/.test(data),
197
207
  construct: (data: string) => /^(?:true|True|TRUE|yes)$/.test(data),
198
- predicate: (value: unknown) => typeof value === "boolean",
199
- represent: { lowercase: (value: object) => (value ? "true" : "false") },
200
- defaultStyle: "lowercase",
201
- });
208
+ };
202
209
 
203
210
  /** A tagged value written back as text, the way it appeared: `!reference [.base, script]`. */
204
211
  function flowText(value: unknown): string {
@@ -215,31 +222,34 @@ function flowText(value: unknown): string {
215
222
  * `!reference [.base, script]` is the string `"!reference [.base, script]"`.
216
223
  * A tag on a mapping has no such form and is an error.
217
224
  */
218
- const TAGGED_SCALAR = new yaml.Type("!", {
225
+ const TAGGED_SCALAR = {
226
+ tag: "!",
219
227
  kind: "scalar",
220
228
  multi: true,
221
229
  construct: (data: string | null, tag?: string) => (data === null || data === "" ? tag : `${tag} ${data}`),
222
- });
223
- const TAGGED_SEQUENCE = new yaml.Type("!", {
230
+ };
231
+ const TAGGED_SEQUENCE = {
232
+ tag: "!",
224
233
  kind: "sequence",
225
234
  multi: true,
226
235
  construct: (data: unknown[] | null, tag?: string) => `${tag} ${flowText(data ?? [])}`,
227
- });
236
+ };
228
237
 
229
- /** The merge key `<<`, as js-yaml's own merge type reads it (its typings omit `types`). */
230
- const MERGE = new yaml.Type("tag:yaml.org,2002:merge", {
238
+ /** The merge key `<<`, as js-yaml's own merge type reads it. */
239
+ const MERGE = {
240
+ tag: "tag:yaml.org,2002:merge",
231
241
  kind: "scalar",
232
242
  resolve: (data: string | null) => data === "<<" || data === null,
233
- });
243
+ };
234
244
 
235
245
  /**
236
246
  * YAML 1.2's core schema (no timestamps, so a date stays a string) with merge
237
247
  * keys, `yes`/`no` booleans and local tags read as strings.
238
248
  */
239
- const SCHEMA = yaml.CORE_SCHEMA.extend({
240
- implicit: [BOOL, MERGE],
241
- explicit: [TAGGED_SCALAR, TAGGED_SEQUENCE],
242
- });
249
+ const SCHEMA = {
250
+ implicit: [NULL_TYPE, BOOL, INT_TYPE, FLOAT_TYPE, MERGE],
251
+ explicit: [STR_TYPE, SEQ_TYPE, MAP_TYPE, TAGGED_SCALAR, TAGGED_SEQUENCE],
252
+ };
243
253
 
244
254
  /**
245
255
  * Give every alias its own copy of the anchored value. js-yaml shares one
@@ -266,7 +276,7 @@ function unshareAliases(value: unknown, seen = new Set<object>()): unknown {
266
276
  * multiline key. Report the line where that key starts instead, the text
267
277
  * that is not a key.
268
278
  */
269
- function errorLine(lines: string[], err: yaml.YAMLException): number {
279
+ function errorLine(lines: string[], err: YAMLException): number {
270
280
  let line = err.mark?.line ?? 0;
271
281
  if (/multiline key may not be an implicit key/.test(err.reason)) {
272
282
  const indent = lines[line]?.search(/\S/) ?? -1;
@@ -297,9 +307,9 @@ function loadDocument(content: string): unknown {
297
307
  .join("\n");
298
308
  let value: unknown;
299
309
  try {
300
- value = yaml.load(text, { schema: SCHEMA, json: true });
310
+ value = load(text, SCHEMA);
301
311
  } catch (err) {
302
- if (!(err instanceof yaml.YAMLException)) throw err;
312
+ if (!(err instanceof YAMLException)) throw err;
303
313
  const line = errorLine(text.split("\n"), err);
304
314
  const tag = err.reason.match(/^unknown tag !<(![^>]*)>/)?.[1];
305
315
  throw new YAMLParseError(line, tag ? `the tag ${JSON.stringify(tag)} on a mapping is not supported` : err.reason);