@yuneta/gobj-ui 7.26.50 → 7.26.52

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.
@@ -53,8 +53,9 @@
53
53
  * "edit", "new", "delete" in every language). The library scanned
54
54
  * is THIS one -- the package the app installed and builds with,
55
55
  * the one this script ships in. (A LOCALE is not a source: it is
56
- * parsed and then imported, and its top-level code runs; see
57
- * below.) In each file it reads as keys:
56
+ * parsed and then imported, in a process of its own, and its
57
+ * top-level code runs there; see below.) In each file it reads as
58
+ * keys:
58
59
  *
59
60
  * - the key given to a TRANSLATOR: t() or tr() -- whatever binds
60
61
  * the name --, `i18next.t()`, i18next's t imported under any
@@ -131,7 +132,8 @@
131
132
  * every branch of `?:`, `||` and `??`, the right one of `&&`; a
132
133
  * `const` to its value (`const KEY = "save"; t(KEY)` asks for
133
134
  * "save"), and what a destructuring reads from one (`const
134
- * {title} = OPTS`); an import of a const or a function, into the
135
+ * {title} = OPTS`; its default only where what it reads can be
136
+ * undefined: with_default()); an import of a const or a function, into the
135
137
  * module it names, when that module is one this script reads (a
136
138
  * file of the app, a module of the library); a property of a
137
139
  * const object literal (`DEFAULTS.ok`; every value of it for
@@ -140,7 +142,10 @@
140
142
  * that a for...of iterates, or the first parameter of a callback
141
143
  * of one of the methods above (in the same forms, and of
142
144
  * `Array.from(items, cb)`), and what a destructuring reads from
143
- * it;
145
+ * it -- an array one too, `([id, key]) => ...`; an element of an
146
+ * array by its index, `.at()` and `.slice()` (array_method_values(),
147
+ * is_nested_array()), or by every element where the index is not
148
+ * known or the array may hold others by then, never by nothing;
144
149
  * `String(x)`, and a template that is only `${x}`, as x; a
145
150
  * parameter however it is read (`opts?.title`, `arguments[1]`,
146
151
  * `rest[0]`) and its default; a call of a function the script
@@ -305,7 +310,10 @@
305
310
  * es.js, pt-br.js; flags.js and locales.js are not) exporting
306
311
  * `{<code>}` with a `translation` dict. It is parsed first, and
307
312
  * then IMPORTED, so its top-level code runs -- the app's own code,
308
- * but code. It is imported directly, not through locales.js, which
313
+ * but code --, in a PROCESS OF ITS OWN that reports what it exports
314
+ * (import_locales()): whatever a locale does there, the run, which
315
+ * executes no code of the app, ends with its own verdict. It is
316
+ * imported directly, not through locales.js, which
309
317
  * drags in gobj-js and i18next and expects a browser. A file that
310
318
  * LOOKS like a locale -- it holds a `translation` dict, it exports
311
319
  * its own name (es.js exporting `es`), or it has a default export
@@ -332,6 +340,7 @@
332
340
  * All Rights Reserved.
333
341
  ***********************************************************************/
334
342
  import {readdirSync, readFileSync, existsSync, statSync, realpathSync} from "fs";
343
+ import {spawnSync} from "child_process";
335
344
  import {resolve, relative} from "path";
336
345
  import {pathToFileURL, fileURLToPath} from "url";
337
346
  import {I18N_KEY_FUNCTIONS} from "../src/i18n_key_functions.js";
@@ -354,7 +363,6 @@ const LIB_INDEX = new URL("index.js", LIB);
354
363
  const APP_DIR = pathToFileURL(process.cwd() + "/");
355
364
 
356
365
  let errors = 0;
357
- let failed = false; // the verdict, whatever a locale writes into process.exitCode
358
366
 
359
367
  /***************************************************************
360
368
  *
@@ -488,7 +496,7 @@ function locale_export_names(code)
488
496
  /***************************************************************
489
497
  * The locales of the app: {code: translation}
490
498
  ***************************************************************/
491
- async function load_locales(locales_dir)
499
+ function load_locales(locales_dir)
492
500
  {
493
501
  if(!existsSync(locales_dir)) {
494
502
  abort(`no locales directory at ${locales_dir.pathname}`);
@@ -501,46 +509,41 @@ async function load_locales(locales_dir)
501
509
  if(codes.includes("en")) {
502
510
  codes = ["en", ...codes.filter((c) => c !== "en")];
503
511
  }
504
- let locales = {};
505
- let names = new Set();
506
- for(let code of codes) {
507
- let url = new URL(`${code}.js`, locales_dir);
508
- let export_names = locale_export_names(code);
509
512
 
510
- /* Parsed first, never trusted to the import: a syntax error is
511
- * a broken file, not "a helper that needs a browser". */
512
- let ast;
513
+ /* Parsed first, never trusted to the import: a syntax error is
514
+ * a broken file, not "a helper that needs a browser". */
515
+ let parsed = new Map(); // code -> what it looks like ("" if not a locale), or an Error
516
+ for(let code of codes) {
513
517
  try {
514
- ast = parse_source(readFileSync(url, "utf8"));
518
+ let ast = parse_source(readFileSync(new URL(`${code}.js`, locales_dir), "utf8"));
519
+ parsed.set(code, looks_like_a_locale(ast, locale_export_names(code)));
515
520
  } catch(e) {
516
- fail(`${code}.js cannot be parsed (${e.message}): its language is NOT validated`);
517
- continue;
521
+ parsed.set(code, e);
518
522
  }
519
- let looks = looks_like_a_locale(ast, export_names);
523
+ }
524
+ let loaded = import_locales(locales_dir, codes.filter((code) => !(parsed.get(code) instanceof Error)));
520
525
 
521
- let mod;
522
- try {
523
- mod = await import(url.href);
524
- } catch(e) {
526
+ let locales = {};
527
+ let names = new Set();
528
+ for(let code of codes) {
529
+ let looks = parsed.get(code);
530
+ if(looks instanceof Error) {
531
+ fail(`${code}.js cannot be parsed (${looks.message}): its language is NOT validated`);
532
+ continue;
533
+ }
534
+ let got = loaded[code];
535
+ if(got.error !== undefined) {
525
536
  if(looks) {
526
- fail(`${code}.js ${looks} and cannot be loaded (${e.message}): ` +
537
+ fail(`${code}.js ${looks} and cannot be loaded (${got.error}): ` +
527
538
  `its language is NOT validated`);
528
539
  } else {
529
540
  /* A helper that happens to have a locale's name (app.js),
530
541
  * and needs a browser to load: said and skipped */
531
- note(`${code}.js cannot be loaded here (${e.message}): not a locale, skipped`);
542
+ note(`${code}.js cannot be loaded here (${got.error}): not a locale, skipped`);
532
543
  }
533
544
  continue;
534
545
  }
535
- let locale = null;
536
- for(let name of export_names) {
537
- if(mod[name]) {
538
- locale = mod[name];
539
- break;
540
- }
541
- }
542
- locale = locale || mod.default;
543
- if(!locale || !locale.translation || typeof locale.translation !== "object") {
546
+ if(!got.keys) {
544
547
  if(looks) {
545
548
  fail(`${code}.js ${looks} but exports no {${code}} with a translation dict: ` +
546
549
  `its language is NOT validated`);
@@ -549,9 +552,9 @@ async function load_locales(locales_dir)
549
552
  }
550
553
  continue;
551
554
  }
552
- locales[code] = locale.translation;
553
- if(typeof locale.name === "string" && locale.name) {
554
- names.add(locale.name); // its endonym: a literal of a language menu
555
+ locales[code] = Object.fromEntries(got.keys.map((k) => [k, true]));
556
+ if(got.name) {
557
+ names.add(got.name); // its endonym: a literal of a language menu
555
558
  }
556
559
  }
557
560
  if(Object.keys(locales).length === 0) {
@@ -560,6 +563,138 @@ async function load_locales(locales_dir)
560
563
  return {locales, names};
561
564
  }
562
565
 
566
+ /***************************************************************
567
+ * What the locale files `codes` export, read in a PROCESS OF THEIR
568
+ * OWN: a locale is imported, so its code runs, and whatever it does
569
+ * -- process.exit() or process.reallyExit() at its top or in a
570
+ * timer, an exit handler that sets process.exitCode, a timer that
571
+ * throws -- stays in that process, which reports what the files
572
+ * export and ends before any timer of theirs is due after it. Up
573
+ * to gobj-ui 7.26.50 they were imported into the run itself, and an
574
+ * exit handler of a locale (`process.exitCode = 0`, a
575
+ * `reallyExit(0)`), a `reallyExit(0)` at its top or the exit
576
+ * handlers removed turned a failing run into exit code 0, and a
577
+ * timer's exit cut short a list written to a pipe.
578
+ *
579
+ * Returns {code: {keys, name} | {none: true} | {error}}. A
580
+ * promise rejected or an exception thrown with nothing to catch it,
581
+ * in the locales' process, is a violation; that process ending
582
+ * before its report, or not reporting in LOCALES_TIMEOUT_MS, aborts
583
+ * the run: no language could be checked.
584
+ ***************************************************************/
585
+ const LOCALES_TIMEOUT_MS = 60000;
586
+
587
+ function import_locales(locales_dir, codes)
588
+ {
589
+ let request = JSON.stringify({
590
+ dir: locales_dir.href,
591
+ codes: codes.map((code) => [code, locale_export_names(code)])
592
+ });
593
+ let r = spawnSync(process.execPath,
594
+ [...process.execArgv, "-e", `(${report_locales.toString()})(${request});`], {
595
+ stdio: ["ignore", "inherit", "inherit", "pipe"],
596
+ timeout: LOCALES_TIMEOUT_MS,
597
+ killSignal: "SIGKILL",
598
+ maxBuffer: 1 << 30
599
+ });
600
+ const AT = "the locales are imported in a process of their own, and it";
601
+ const WHY = "a locale is imported, so its code runs: it must not end the process, and its loading must end";
602
+ if(r.error && r.error.code === "ETIMEDOUT") {
603
+ abort(`${AT} did not report in ${LOCALES_TIMEOUT_MS / 1000} s: ${WHY} (a loop, or a top-level ` +
604
+ `await that never settles while a timer runs, holds it)`);
605
+ }
606
+ if(r.error) {
607
+ abort(`${AT} could not run: ${r.error.message}`);
608
+ }
609
+ let text = r.output[3]? r.output[3].toString("utf8") : "";
610
+ let report = null;
611
+ if(text.endsWith("\n")) {
612
+ try {
613
+ report = JSON.parse(text);
614
+ } catch(e) {
615
+ report = null;
616
+ }
617
+ }
618
+ if(r.status !== 0 || !report || typeof report.locales !== "object" || !Array.isArray(report.problems)) {
619
+ let how = r.signal? `killed by ${r.signal}` : `exit code ${r.status}`;
620
+ abort(`${AT} ended (${how}) before it reported what they export` +
621
+ `${text? ", or reported what is not its report" : ""}: ${WHY} (a top-level await that ` +
622
+ `never settles ends it too)`);
623
+ }
624
+ for(let problem of report.problems) {
625
+ fail(`${problem} (a locale is imported, so its code runs)`);
626
+ }
627
+ for(let code of codes) {
628
+ if(!report.locales[code]) {
629
+ abort(`the locales' process reported nothing of ${code}.js`);
630
+ }
631
+ }
632
+ return report.locales;
633
+ }
634
+
635
+ /***************************************************************
636
+ * The code of the locales' process (import_locales()), run there
637
+ * from its text: it can use nothing of this module but its
638
+ * argument. What it calls after the locales ran is taken BEFORE
639
+ * them, so a locale that replaces process.reallyExit or
640
+ * JSON.stringify does not reach the report. It reports after a
641
+ * timer of 0 and an immediate: a promise a locale rejects in such
642
+ * a timer, with nothing to handle it, is told first. Its own end is
643
+ * process.reallyExit(0): no exit handler of a locale runs, and a
644
+ * timer of a locale due later never does.
645
+ ***************************************************************/
646
+ async function report_locales(request)
647
+ {
648
+ const {writeSync} = await import("node:fs");
649
+ const really_exit = process.reallyExit.bind(process);
650
+ const stringify = JSON.stringify;
651
+ const keys_of = Object.keys;
652
+ const set_timeout = setTimeout;
653
+ const set_immediate = setImmediate;
654
+ const buffer_from = Buffer.from.bind(Buffer);
655
+ const problems = [];
656
+ process.on("unhandledRejection", (reason) => {
657
+ problems.push(`a promise was rejected and nothing handled it: ${(reason && reason.stack) || reason}`);
658
+ });
659
+ process.on("uncaughtException", (e) => {
660
+ problems.push(`an exception was thrown and nothing caught it: ${(e && e.stack) || e}`);
661
+ });
662
+ const locales = {};
663
+ for(const [code, names] of request.codes) {
664
+ try {
665
+ const mod = await import(new URL(`${code}.js`, request.dir).href);
666
+ let locale = null;
667
+ for(const name of names) {
668
+ if(mod[name]) {
669
+ locale = mod[name];
670
+ break;
671
+ }
672
+ }
673
+ locale = locale || mod.default;
674
+ if(!locale || !locale.translation || typeof locale.translation !== "object") {
675
+ locales[code] = {none: true};
676
+ } else {
677
+ locales[code] = {
678
+ keys: keys_of(locale.translation),
679
+ name: (typeof locale.name === "string" && locale.name)? locale.name : null
680
+ };
681
+ }
682
+ } catch(e) {
683
+ locales[code] = {error: String((e && e.message) || e)};
684
+ }
685
+ }
686
+ set_timeout(() => {
687
+ set_immediate(() => {
688
+ const buf = buffer_from(stringify({locales, problems}) + "\n", "utf8");
689
+ let done = 0;
690
+ while(done < buf.length) {
691
+ done += writeSync(3, buf, done, buf.length - done);
692
+ }
693
+ really_exit(0);
694
+ });
695
+ }, 0);
696
+ }
697
+
563
698
  /***************************************************************
564
699
  * Does a parsed file look like a locale? It does when it holds an
565
700
  * object literal with a `translation` dict, when it exports one of
@@ -681,7 +816,7 @@ function find_plural_groups(locales)
681
816
  async function main()
682
817
  {
683
818
  let opts = parse_args(process.argv.slice(2));
684
- let {locales, names} = await load_locales(opts.locales);
819
+ let {locales, names} = load_locales(opts.locales);
685
820
  opts.language_names = names;
686
821
  let codes = Object.keys(locales);
687
822
 
@@ -1231,8 +1366,8 @@ function scope_names(a)
1231
1366
  if(a.left.type === "VariableDeclaration") {
1232
1367
  for(let d of a.left.declarations) {
1233
1368
  for(let name of pattern_names(d.id)) {
1234
- let at = (a.type === "ForOfStatement")? param_path(d.id, name) : null;
1235
- declare(name, at? {kind: "each", of: a.right, steps: at.steps} :
1369
+ let at = (a.type === "ForOfStatement")? param_path(d.id, name, [], null, true) : null;
1370
+ declare(name, at? {kind: "each", of: a.right, steps: at.steps, default: at.default} :
1236
1371
  {kind: "each_part", of: a.right});
1237
1372
  }
1238
1373
  }
@@ -1356,7 +1491,8 @@ function param_path(p, name, steps = [], dflt = null, arrays = false)
1356
1491
  continue;
1357
1492
  }
1358
1493
  if(e.type === "RestElement") {
1359
- return null;
1494
+ /* `const [a, ...r] = X`: r is X.slice(1). Up to gobj-ui 7.26.50 read as nothing. */
1495
+ return param_path(e.argument, name, [...steps, {slice: true, from: i, to: "end"}], null, arrays);
1360
1496
  }
1361
1497
  return param_path(e, name, [...steps, {prop: String(i), computed: true}], null, arrays);
1362
1498
  }
@@ -1477,11 +1613,13 @@ function binding_of(name, scope)
1477
1613
  let j = params_of(a).get(name);
1478
1614
  if(j !== undefined) {
1479
1615
  let p = a.params[j];
1480
- let at = param_path(p, name);
1481
1616
  let it = (j === 0 && parent && a.type !== "FunctionDeclaration")?
1482
1617
  iteration_callback_of(a, scope, i) : null;
1618
+ /* An element destructured as an array is read by its index: up to gobj-ui
1619
+ * 7.26.50 `X.map(([a, b]) => t(b))` read nothing */
1620
+ let at = param_path(p, name, [], null, !!it);
1483
1621
  if(it) {
1484
- return at? {kind: "each", of: it.receiver, steps: at.steps, scope: it.scope}
1622
+ return at? {kind: "each", of: it.receiver, steps: at.steps, default: at.default, scope: it.scope}
1485
1623
  : {kind: "each_part", of: it.receiver, scope: it.scope};
1486
1624
  }
1487
1625
  let base = {fn: a, name: function_name(a, parent), index: j, scope: scope.slice(0, i + 1)};
@@ -1514,7 +1652,7 @@ function binding_of(name, scope)
1514
1652
  name: d.name || name, scope: scope.slice(0, i + 1)};
1515
1653
  }
1516
1654
  if(d.kind === "each") {
1517
- return {kind: "each", of: d.of, steps: d.steps || [], scope: scope.slice(0, i)};
1655
+ return {kind: "each", of: d.of, steps: d.steps || [], default: d.default || null, scope: scope.slice(0, i)};
1518
1656
  }
1519
1657
  if(d.kind === "each_part") {
1520
1658
  return {kind: "each_part", of: d.of, scope: scope.slice(0, i)};
@@ -2632,7 +2770,10 @@ function expand_node(node, ctx, depth)
2632
2770
  for(let step of b.steps) {
2633
2771
  out = out.flatMap((o) => step_values(o, step, node, depth + 1));
2634
2772
  }
2635
- return out;
2773
+ /* What it cannot follow is the name, where it is read: param_flows() goes on from it */
2774
+ out = out.map((o) => (o.kind === "unknown" && !o.var_root && o.node === node)?
2775
+ Object.assign({}, o, {scope: ctx.scope}) : o);
2776
+ return with_default(out, b.default, {scope: b.scope, via: via, off: true}, depth);
2636
2777
  }
2637
2778
  if(b.kind === "param") {
2638
2779
  let out = [item("param", {binding: b, path: b.path})];
@@ -2674,13 +2815,9 @@ function expand_node(node, ctx, depth)
2674
2815
  && node.arguments.length === 1 && binding_of("String", ctx.scope) === null) {
2675
2816
  return expand(node.arguments[0], here, depth + 1);
2676
2817
  }
2677
- let at = element_at(node, ctx.scope);
2678
- if(at) {
2679
- let out = [];
2680
- for(let o of expand(node.callee.object, here, depth + 1)) {
2681
- out.push(...step_values(o, at, node, depth + 1));
2682
- }
2683
- return out;
2818
+ let part = array_method_values(node, here, depth);
2819
+ if(part) {
2820
+ return part;
2684
2821
  }
2685
2822
  let r = local_call_values(node, ctx, via, depth);
2686
2823
  if(r && r.items) {
@@ -2745,10 +2882,34 @@ function const_values(b, node, scope, via, depth)
2745
2882
  for(let step of b.steps || []) {
2746
2883
  out = out.flatMap((o) => step_values(o, step, node, depth + 1));
2747
2884
  }
2748
- if(b.default) {
2749
- out.push(...expand(b.default, ctx, depth + 1));
2885
+ return with_default(out, b.default, ctx, depth);
2886
+ }
2887
+
2888
+ /***************************************************************
2889
+ * The values of a name a destructuring declares with a default
2890
+ * (`const [a = "d"] = X`, `const {title = "d"} = opts`): what it
2891
+ * reads, and the default where that can be undefined -- nothing
2892
+ * there (past the end, a hole, a property not written, `undefined`)
2893
+ * or a value that is not written out (a parameter, a call, an
2894
+ * unknown) --, which it then is instead of nothing. Up to gobj-ui
2895
+ * 7.26.50 the default was a value wherever it was written, and
2896
+ * `const [a = "d0", b = "d1"] = ["x0", "x1"]; t(b)` asked for "d1",
2897
+ * which no run of the code reaches.
2898
+ ***************************************************************/
2899
+ const NEVER_UNDEFINED = new Set(["Literal", "TemplateLiteral", "ArrayExpression", "ObjectExpression",
2900
+ "ArrowFunctionExpression", "FunctionExpression", "ClassExpression",
2901
+ "NewExpression"]);
2902
+
2903
+ function with_default(values, dflt, ctx, depth)
2904
+ {
2905
+ if(!dflt) {
2906
+ return values;
2750
2907
  }
2751
- return out;
2908
+ let defined = (o) => o.kind === "value" && NEVER_UNDEFINED.has(o.node.type);
2909
+ if(values.length > 0 && values.every(defined)) {
2910
+ return values;
2911
+ }
2912
+ return [...values.filter((o) => o.kind !== "nothing"), ...expand(dflt, ctx, depth + 1)];
2752
2913
  }
2753
2914
 
2754
2915
  /***************************************************************
@@ -3015,32 +3176,133 @@ function written_elements(n, scope)
3015
3176
  }
3016
3177
 
3017
3178
  /***************************************************************
3018
- * `X.at(i)` of a const array (const_array()), as the step that reads
3019
- * the same element: `X[i]`, the element counted from the end for a
3020
- * negative literal (with no spread before), any element for an index
3021
- * not written out; of a let of literal arrays (let_array()), any
3022
- * element. null for anything else. Up to gobj-ui 7.26.48 it was read
3023
- * as nothing.
3179
+ * `x.at(i)` and `x.slice(a, b)` where x holds an array the source
3180
+ * writes out -- an array literal (a const of one, a branch of `?:`,
3181
+ * an element of another, ...), or a `let` of literal arrays
3182
+ * (let_array()) --: the element, or the part, read from each such
3183
+ * value (array_step()); any other value of x is unknown there. null
3184
+ * when x holds none: the call is read as any other.
3185
+ *
3186
+ * `.at()` reads element 0, `.at(i, ...)` element i, and an index not
3187
+ * written out (a variable, a spread) any element; a negative one
3188
+ * counts from the end of an array with no spread. Up to gobj-ui
3189
+ * 7.26.48 `X.at(1)` was read as nothing; up to 7.26.50 so were
3190
+ * `X.at()`, `X.at(-1, 0)`, an `.at()` of anything but a name
3191
+ * (`X[0].at(1)`, `X.at(0).at(1)`), a `.slice()` read by its index
3192
+ * (`X.slice(1)[0]`, `const [b] = X.slice(1)`) and the elements of a
3193
+ * `.slice()` walked (`for(const k of X.slice(1))`).
3024
3194
  ***************************************************************/
3025
- function element_at(call, scope)
3195
+ function array_method_values(call, ctx, depth)
3026
3196
  {
3027
3197
  let c = call.callee;
3028
- if(c.type !== "MemberExpression" || member_step(c).prop !== "at" || c.object.type !== "Identifier"
3029
- || call.arguments.length !== 1 || is_spread(call.arguments[0])) {
3198
+ if(c.type !== "MemberExpression") {
3030
3199
  return null;
3031
3200
  }
3032
- let array = const_array(c.object, scope);
3033
- if(!array) {
3034
- return let_array(binding_of(c.object.name, scope))? {prop: null, computed: true} : null;
3201
+ let name = member_step(c).prop;
3202
+ let step = (name === "slice")? slice_step(call) : (name === "at")? {at: at_index(call)} : null;
3203
+ if(!step) {
3204
+ return null;
3205
+ }
3206
+ let values = expand(c.object, ctx, depth + 1);
3207
+ if(!values.some(holds_array)) {
3208
+ return null;
3209
+ }
3210
+ let via = ctx.off? ctx.via : [...ctx.via, call];
3211
+ return values.flatMap((o) => holds_array(o)? step_values(o, array_step(o, step), call, depth + 1)
3212
+ : [{kind: "unknown", node: call, scope: ctx.scope, via, off: ctx.off}]);
3213
+ }
3214
+
3215
+ /* A value that is an array the source writes out: an array literal,
3216
+ * or what a `let` of literal arrays holds (let_array()) */
3217
+ function holds_array(o)
3218
+ {
3219
+ if(o.kind === "value") {
3220
+ return o.node.type === "ArrayExpression";
3221
+ }
3222
+ return o.kind === "unknown" && !!o.var_root && o.var_root.steps.every(is_element_step)
3223
+ && !!let_array(o.var_root.binding);
3224
+ }
3225
+
3226
+ /* The index `.at()` reads: an integer written out (0 for none), or
3227
+ * null for one that is not */
3228
+ function at_index(call)
3229
+ {
3230
+ let a = call.arguments[0];
3231
+ if(a === undefined) {
3232
+ return 0;
3233
+ }
3234
+ return is_spread(a)? null : literal_integer(a);
3235
+ }
3236
+
3237
+ /* `.slice(a, b)` as a step: {slice, from, to}, each end an integer
3238
+ * written out, "end" for an end left out, null for one that is not
3239
+ * written out. null for a slice that is no read of a part. */
3240
+ function slice_step(call)
3241
+ {
3242
+ if(call.arguments.length > 2 || call.arguments.some(is_spread)) {
3243
+ return call.arguments.some(is_spread)? {slice: true, from: null, to: null} : null;
3244
+ }
3245
+ let bound = (a, dflt) => (a === undefined)? dflt : literal_integer(a);
3246
+ return {slice: true, from: bound(call.arguments[0], 0), to: bound(call.arguments[1], "end")};
3247
+ }
3248
+
3249
+ /* The step `.at()` takes on one value (holds_array()): an index
3250
+ * counted from the start, or any element */
3251
+ function array_step(o, step)
3252
+ {
3253
+ if(step.slice) {
3254
+ return step;
3255
+ }
3256
+ let i = step.at;
3257
+ if(i === null || o.kind !== "value") {
3258
+ return {prop: null, computed: true};
3035
3259
  }
3036
- let i = literal_integer(call.arguments[0]);
3037
- if(i !== null && i < 0 && !array.elements.some(is_spread)) {
3038
- i = array.elements.length + i;
3260
+ if(i < 0) {
3261
+ if(o.node.elements.some(is_spread)) {
3262
+ return {prop: null, computed: true};
3263
+ }
3264
+ i = o.node.elements.length + i;
3039
3265
  if(i < 0) {
3040
- i = array.elements.length; // nothing there
3266
+ i = o.node.elements.length; // nothing there
3267
+ }
3268
+ }
3269
+ return {prop: String(i), computed: true};
3270
+ }
3271
+
3272
+ /***************************************************************
3273
+ * The part of an array literal a slice step reads (slice_step()),
3274
+ * as an array literal of its elements -- made once per array and
3275
+ * part, and read by its index. Only an array read by its index
3276
+ * (is_indexed_array(), is_nested_array()) has a part: of any other,
3277
+ * an end not written out or a spread in it, the part is the whole
3278
+ * array, read by every element -- the array may hold its elements
3279
+ * elsewhere by then (`X.reverse()`).
3280
+ ***************************************************************/
3281
+ const ARRAY_PARTS = new WeakMap(); // an array literal -> Map("from:to" -> its part)
3282
+
3283
+ function array_part(array, step, nested)
3284
+ {
3285
+ let els = array.elements;
3286
+ let exact = step.from !== null && step.to !== null && !els.some(is_spread)
3287
+ && (nested || is_indexed_array(array));
3288
+ let key = exact? `${step.from}:${step.to}` : "*";
3289
+ let parts = ARRAY_PARTS.get(array);
3290
+ if(!parts) {
3291
+ parts = new Map();
3292
+ ARRAY_PARTS.set(array, parts);
3293
+ }
3294
+ if(!parts.has(key)) {
3295
+ let part = els;
3296
+ if(exact) {
3297
+ let len = els.length;
3298
+ let bound = (v) => (v === "end")? len : (v < 0)? Math.max(len + v, 0) : Math.min(v, len);
3299
+ part = els.slice(bound(step.from), bound(step.to));
3041
3300
  }
3301
+ let node = {type: "ArrayExpression", elements: part, start: array.start, end: array.end};
3302
+ INDEXED_ARRAYS.set(node, exact);
3303
+ parts.set(key, node);
3042
3304
  }
3043
- return (i !== null && i >= 0)? {prop: String(i), computed: true} : {prop: null, computed: true};
3305
+ return parts.get(key);
3044
3306
  }
3045
3307
 
3046
3308
  /* An integer written out (`1`, `-1`), or null */
@@ -3174,6 +3436,26 @@ function is_indexed_array(array)
3174
3436
  return INDEXED_ARRAYS.get(array);
3175
3437
  }
3176
3438
 
3439
+ /***************************************************************
3440
+ * An array literal written INSIDE another literal -- an element of
3441
+ * an array, the value of a property (`[["foto", "CLS", "photo"]]`,
3442
+ * `{cols: ["a", "b"]}`) --, which is read by its index, as a property
3443
+ * of an object is: no name holds it, so only a read of the literal
3444
+ * around it reaches it, and what is written into it after that is
3445
+ * not seen, as with an object (the README's list of what stays
3446
+ * invisible). Up to gobj-ui 7.26.50 such an array was read by every
3447
+ * element, and a destructuring of it by nothing.
3448
+ ***************************************************************/
3449
+ function is_nested_array(o)
3450
+ {
3451
+ let parent = o.scope[o.scope.length - 1];
3452
+ if(!parent) {
3453
+ return false;
3454
+ }
3455
+ return (parent.type === "ArrayExpression" && parent.elements.includes(o.node))
3456
+ || (parent.type === "Property" && parent.value === o.node);
3457
+ }
3458
+
3177
3459
  /* The methods of an array that read it and write nothing into it.
3178
3460
  * Those that take a callback hand it the array as their third
3179
3461
  * argument (the fourth for reduce): a callback that can reach it
@@ -3669,6 +3951,16 @@ function step_values(o, step, node, depth)
3669
3951
  if(step.each) {
3670
3952
  return element_values(o, depth);
3671
3953
  }
3954
+ if(step.slice) {
3955
+ if(o.kind === "value" && o.node.type === "ArrayExpression") {
3956
+ return [Object.assign({}, o, {node: array_part(o.node, step, is_nested_array(o))})];
3957
+ }
3958
+ if(o.kind === "nothing") {
3959
+ return [Object.assign({}, o, {node: at})];
3960
+ }
3961
+ return [Object.assign({}, o, {kind: "unknown", binding: null, node: at,
3962
+ var_root: extend_root(o.var_root, step)})];
3963
+ }
3672
3964
  if(o.kind === "param" && o.binding.rest) {
3673
3965
  if(step.computed && step.prop !== null && /^\d+$/.test(step.prop)) {
3674
3966
  let binding = Object.assign({}, o.binding, {index: o.binding.index + Number(step.prop), rest: false});
@@ -3688,7 +3980,7 @@ function step_values(o, step, node, depth)
3688
3980
  }
3689
3981
  if(o.kind === "value" && o.node.type === "ArrayExpression" && (step.computed || array_index(step) !== null)) {
3690
3982
  let i = array_index(step);
3691
- if(i !== null && is_indexed_array(o.node)
3983
+ if(i !== null && (is_indexed_array(o.node) || is_nested_array(o))
3692
3984
  && !o.node.elements.slice(0, i + 1).some((e) => e && e.type === "SpreadElement")) {
3693
3985
  return indexed_values(o, i, at, depth + 1);
3694
3986
  }
@@ -4658,9 +4950,7 @@ function var_leaves(root, seen = new Set(), depth = 0)
4658
4950
  for(let step of v.pick.steps) {
4659
4951
  cur = cur.flatMap((o) => step_values(o, step, null, 0));
4660
4952
  }
4661
- if(v.pick.default) {
4662
- cur.push(...expand(v.pick.default, ctx));
4663
- }
4953
+ cur = with_default(cur, v.pick.default, ctx, 0);
4664
4954
  }
4665
4955
  for(let step of steps.slice(v.steps.length)) {
4666
4956
  cur = cur.flatMap((o) => step_values(o, step, null, 0));
@@ -4718,7 +5008,8 @@ function literal_let_keys(leaf)
4718
5008
  return [];
4719
5009
  }
4720
5010
  let steps = leaf.var_root.steps;
4721
- if(steps.length === 1 && is_element_step(steps[0])) {
5011
+ if(steps.length > 0 && steps.every(is_element_step)) {
5012
+ /* `X[0][1]`, `X.slice(1)[0]`: an element of an element is one of its strings, or nothing */
4722
5013
  return let_array_literals(leaf.var_root.binding) || [];
4723
5014
  }
4724
5015
  if(steps.length > 0) {
@@ -4730,7 +5021,7 @@ function literal_let_keys(leaf)
4730
5021
  /* A step that reads an element: `X[i]`, `X[1]`, an element walked */
4731
5022
  function is_element_step(step)
4732
5023
  {
4733
- return !!step.each || step.computed || array_index(step) !== null;
5024
+ return !!step.each || !!step.slice || step.computed || array_index(step) !== null;
4734
5025
  }
4735
5026
 
4736
5027
  /***************************************************************
@@ -4766,17 +5057,17 @@ function let_array(b)
4766
5057
  if(!v.node) {
4767
5058
  continue;
4768
5059
  }
4769
- if(v.steps.length === 0 && v.node.type === "ArrayExpression") {
4770
- out.length = Math.max(out.length, v.node.elements.length);
4771
- for(let e of v.node.elements) {
4772
- let lits = (!e)? [] : (e.type === "SpreadElement")? null
4773
- : literal_values(e, [...v.scope, v.node], new Set());
4774
- if(lits === null) {
4775
- out = null;
4776
- break;
4777
- }
4778
- out.strings.push(...lits);
4779
- }
5060
+ if(v.steps.length === 1 && v.steps[0].prop === "length") {
5061
+ continue; // `X.length = n` gives no element, only holes
5062
+ }
5063
+ let a = (v.steps.length > 0)? null
5064
+ : (v.into && v.node.type === "ObjectExpression")? assigned_elements(v.node, v.scope)
5065
+ : array_literal_strings(v.node, v.scope);
5066
+ if(a) {
5067
+ out.length = Math.max(out.length, a.length);
5068
+ out.strings.push(...a.strings);
5069
+ } else if(v.steps.length === 0) {
5070
+ out = null;
4780
5071
  } else if(v.steps.length === 1 && is_element_step(v.steps[0]) && !v.spread) {
4781
5072
  let lits = literal_values(v.node, v.scope, new Set());
4782
5073
  if(lits === null) {
@@ -4795,6 +5086,93 @@ function let_array(b)
4795
5086
  return out;
4796
5087
  }
4797
5088
 
5089
+ /***************************************************************
5090
+ * The strings of the elements of what a value is, when it is written
5091
+ * as nothing but literal arrays: {strings, length}, `length` its
5092
+ * longest array. An array literal of literal_values() (a hole is
5093
+ * nothing), `null` and `undefined` (no element), every branch of `?:`,
5094
+ * `||` and `??` (the right one of `&&`), a const of one -- of the
5095
+ * module or imported. null for anything else. Up to gobj-ui 7.26.50 a
5096
+ * `let` given anything but an array literal was not followed, and
5097
+ * `let X = c()? ["a"] : ["b"]; t(X[0])` asked for nothing.
5098
+ ***************************************************************/
5099
+ function array_literal_strings(n, scope, seen = new Set())
5100
+ {
5101
+ if(!n || seen.has(n)) {
5102
+ return null;
5103
+ }
5104
+ seen.add(n);
5105
+ switch(n.type) {
5106
+ case "ArrayExpression": {
5107
+ let out = {strings: [], length: n.elements.length};
5108
+ for(let e of n.elements) {
5109
+ let lits = (!e)? [] : is_spread(e)? null : literal_values(e, [...scope, n], new Set());
5110
+ if(lits === null) {
5111
+ return null;
5112
+ }
5113
+ out.strings.push(...lits);
5114
+ }
5115
+ return out;
5116
+ }
5117
+ case "Literal":
5118
+ return (n.value === null && !n.regex)? {strings: [], length: 0} : null;
5119
+ case "ConditionalExpression":
5120
+ case "LogicalExpression": {
5121
+ let parts = (n.type === "ConditionalExpression")? [n.consequent, n.alternate]
5122
+ : (n.operator === "&&")? [n.right] : [n.left, n.right];
5123
+ let out = {strings: [], length: 0};
5124
+ for(let x of parts) {
5125
+ let a = array_literal_strings(x, scope, seen);
5126
+ if(!a) {
5127
+ return null;
5128
+ }
5129
+ out.strings.push(...a.strings);
5130
+ out.length = Math.max(out.length, a.length);
5131
+ }
5132
+ return out;
5133
+ }
5134
+ case "Identifier": {
5135
+ let b = binding_of(n.name, scope);
5136
+ if(!b) {
5137
+ return (n.name === "undefined")? {strings: [], length: 0} : null;
5138
+ }
5139
+ if(b.kind === "import") {
5140
+ b = import_target(b);
5141
+ }
5142
+ if(b && b.kind === "const" && b.steps.length === 0 && !b.default) {
5143
+ return array_literal_strings(b.init, b.scope, seen);
5144
+ }
5145
+ return null;
5146
+ }
5147
+ default:
5148
+ return null;
5149
+ }
5150
+ }
5151
+
5152
+ /* The elements `Object.assign(X, {1: "k"})` writes into X, when it
5153
+ * writes literal_values(): {strings, length}, or null */
5154
+ function assigned_elements(n, scope)
5155
+ {
5156
+ let out = {strings: [], length: 0};
5157
+ for(let p of n.properties) {
5158
+ if(p.type !== "Property" || p.kind !== "init" || p.method) {
5159
+ return null;
5160
+ }
5161
+ let lits = literal_values(p.value, [...scope, n], new Set());
5162
+ if(lits === null) {
5163
+ return null;
5164
+ }
5165
+ out.strings.push(...lits);
5166
+ let i = array_index({prop: prop_name(p)});
5167
+ if(i !== null) {
5168
+ out.length = Math.max(out.length, i + 1);
5169
+ } else if(prop_name(p) === null) {
5170
+ return null; // a computed key the reader does not follow: an index it cannot bound
5171
+ }
5172
+ }
5173
+ return out;
5174
+ }
5175
+
4798
5176
  function let_array_literals(b)
4799
5177
  {
4800
5178
  let a = let_array(b);
@@ -7204,74 +7582,19 @@ try {
7204
7582
  }
7205
7583
  if(is_main) {
7206
7584
  /*
7207
- * A locale is IMPORTED, so its code runs, timers included, and it
7208
- * can write process.exitCode or call process.exit() before the run
7209
- * ends (in gobj-ui 7.26.47 and 7.26.48, as up to 7.26.45, a
7210
- * timer's `process.exit(0)` or `process.exitCode = 0` turned a
7211
- * failing run into rc 0). The run
7212
- * ends with the code of its own verdict, and an end it did not call
7213
- * is a failure: in a browser `process` does not even exist.
7585
+ * No code of the app runs here: the sources are parsed, and the
7586
+ * locales are imported in a process of their own
7587
+ * (import_locales()). So the run ends when its work does, with the
7588
+ * code of its own verdict, and what it wrote reaches a pipe whole.
7214
7589
  */
7215
- const real_exit = process.exit;
7216
- let ending = false;
7217
- process.on("exit", (code) => {
7218
- if(!ending) {
7219
- process.stderr.write(`validate-locales: the process was ended by process.exit(${code}), which ` +
7220
- `the script did not call: a locale is imported, so its code runs, ` +
7221
- `and a locale must not end the process\n`);
7222
- failed = true;
7223
- }
7224
- if(failed) {
7225
- process.exitCode = 1;
7226
- }
7227
- });
7228
- process.on("unhandledRejection", (reason) => {
7229
- console.error("validate-locales: a promise was rejected and nothing handled it (a locale is " +
7230
- `imported, so its code runs): ${(reason && reason.stack) || reason}`);
7231
- failed = true;
7232
- });
7233
7590
  try {
7234
7591
  await main();
7235
7592
  } catch(e) {
7236
- failed = true;
7237
7593
  if(!(e instanceof Abort)) {
7238
- ending = true;
7239
7594
  throw e;
7240
7595
  }
7241
7596
  }
7242
7597
  if(errors > 0) {
7243
- failed = true;
7598
+ process.exitCode = 1;
7244
7599
  }
7245
- exit_when_written(() => {
7246
- ending = true;
7247
- real_exit.call(process, failed? 1 : 0);
7248
- });
7249
- }
7250
-
7251
- /***************************************************************
7252
- * End the run once what it wrote has left: a locale is IMPORTED,
7253
- * and a timer its code leaves running holds the process for ever,
7254
- * verdict or not (up to gobj-ui 7.26.45, also a run that failed).
7255
- * `end` runs from the callbacks of a last write to each stream,
7256
- * which come after every write before them, so a long list on its
7257
- * way to a pipe still reaches it whole. It waits for a timer first:
7258
- * a promise rejected with nothing to handle it is told only when the
7259
- * loop turns, and a timer of 0 a locale set is due before it -- up to
7260
- * gobj-ui 7.26.46 such a rejection was lost, and the run passed. A
7261
- * timer of more than 0 ms is not waited for (a setInterval would hold
7262
- * the run for ever): what it does after the verdict is not seen.
7263
- ***************************************************************/
7264
- function exit_when_written(end)
7265
- {
7266
- let pending = 2;
7267
- let written = () => {
7268
- pending--;
7269
- if(pending === 0) {
7270
- end();
7271
- }
7272
- };
7273
- setTimeout(() => {
7274
- process.stdout.write("", written);
7275
- process.stderr.write("", written);
7276
- }, 0);
7277
7600
  }