@yuneta/gobj-ui 7.26.47 → 7.26.49

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.
@@ -354,6 +354,7 @@ const LIB_INDEX = new URL("index.js", LIB);
354
354
  const APP_DIR = pathToFileURL(process.cwd() + "/");
355
355
 
356
356
  let errors = 0;
357
+ let failed = false; // the verdict, whatever a locale writes into process.exitCode
357
358
 
358
359
  /***************************************************************
359
360
  *
@@ -1263,7 +1264,7 @@ function scope_names(a)
1263
1264
  for(let v of d.declarations) {
1264
1265
  let plain = (v.id.type === "Identifier");
1265
1266
  for(let name of pattern_names(v.id)) {
1266
- let at = plain? {steps: [], default: null} : param_path(v.id, name);
1267
+ let at = plain? {steps: [], default: null} : param_path(v.id, name, [], null, d.kind === "const");
1267
1268
  let what = {kind: "other", init: v.init || null};
1268
1269
  if(at && d.kind === "const" && v.init) {
1269
1270
  what = {kind: "const", init: v.init, steps: at.steps, default: at.default, name};
@@ -1307,7 +1308,7 @@ function scope_names(a)
1307
1308
  * function f({title, opts: {label}}) title: ".title", label: ".opts.label"
1308
1309
  * const {id, ...rest} = opts rest: "" (rest.title is opts.title)
1309
1310
  ***************************************************************/
1310
- function param_path(p, name, steps = [], dflt = null)
1311
+ function param_path(p, name, steps = [], dflt = null, arrays = false)
1311
1312
  {
1312
1313
  if(!p) {
1313
1314
  return null;
@@ -1319,11 +1320,11 @@ function param_path(p, name, steps = [], dflt = null)
1319
1320
  }
1320
1321
  return {steps, path: steps.map((s) => "." + s.prop).join(""), default: dflt};
1321
1322
  case "AssignmentPattern":
1322
- return param_path(p.left, name, steps, p.right);
1323
+ return param_path(p.left, name, steps, p.right, arrays);
1323
1324
  case "ObjectPattern":
1324
1325
  for(let q of p.properties) {
1325
1326
  if(q.type === "RestElement") {
1326
- let r = param_path(q.argument, name, steps, null);
1327
+ let r = param_path(q.argument, name, steps, null, arrays);
1327
1328
  if(r) {
1328
1329
  return r;
1329
1330
  }
@@ -1337,12 +1338,29 @@ function param_path(p, name, steps = [], dflt = null)
1337
1338
  }
1338
1339
  continue;
1339
1340
  }
1340
- let r = param_path(target, name, [...steps, {prop: key, computed: false}], null);
1341
+ let r = param_path(target, name, [...steps, {prop: key, computed: false}], null, arrays);
1341
1342
  if(r) {
1342
1343
  return r;
1343
1344
  }
1344
1345
  }
1345
1346
  return null;
1347
+ case "ArrayPattern":
1348
+ /* Only for a const (`arrays`): `const [a, b] = X` reads X[1]
1349
+ * into b. Up to gobj-ui 7.26.48 it was read as nothing. */
1350
+ if(!arrays) {
1351
+ return null;
1352
+ }
1353
+ for(let i = 0; i < p.elements.length; i++) {
1354
+ let e = p.elements[i];
1355
+ if(!e || !pattern_names(e).includes(name)) {
1356
+ continue;
1357
+ }
1358
+ if(e.type === "RestElement") {
1359
+ return null;
1360
+ }
1361
+ return param_path(e, name, [...steps, {prop: String(i), computed: true}], null, arrays);
1362
+ }
1363
+ return null;
1346
1364
  default:
1347
1365
  return null;
1348
1366
  }
@@ -2656,6 +2674,14 @@ function expand_node(node, ctx, depth)
2656
2674
  && node.arguments.length === 1 && binding_of("String", ctx.scope) === null) {
2657
2675
  return expand(node.arguments[0], here, depth + 1);
2658
2676
  }
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;
2684
+ }
2659
2685
  let r = local_call_values(node, ctx, via, depth);
2660
2686
  if(r && r.items) {
2661
2687
  return r.items;
@@ -2958,7 +2984,9 @@ function written_args(list, scope)
2958
2984
  * literal (its spreads read by written_args(), a hole is an argument
2959
2985
  * not given), or a const bound to one with no spread and no hole --
2960
2986
  * its elements read THROUGH it, as `X[0]`, `X[1]`... (element_ref()),
2961
- * so each is read where the const is declared.
2987
+ * so each is read where the const is declared --, a `let` of literal
2988
+ * arrays (let_array(), each position any element), or a `.slice()` of
2989
+ * one of those (sliced_elements()).
2962
2990
  ***************************************************************/
2963
2991
  function written_elements(n, scope)
2964
2992
  {
@@ -2968,16 +2996,108 @@ function written_elements(n, scope)
2968
2996
  if(n.type === "ArrayExpression") {
2969
2997
  return written_args(n.elements, scope);
2970
2998
  }
2999
+ if(n.type === "CallExpression") {
3000
+ return sliced_elements(n, scope);
3001
+ }
2971
3002
  if(n.type !== "Identifier") {
2972
3003
  return null;
2973
3004
  }
2974
3005
  let array = const_array(n, scope);
2975
- if(!array || array.elements.some((e) => e === null || e.type === "SpreadElement")) {
3006
+ if(!array) {
3007
+ /* A let of literal arrays: each position of its longest array is any element */
3008
+ let a = let_array(binding_of(n.name, scope));
3009
+ return a? Array.from({length: Math.max(a.length, 1)}, (_, i) => element_ref(n, i)) : null;
3010
+ }
3011
+ if(array.elements.some((e) => e === null || e.type === "SpreadElement")) {
2976
3012
  return null;
2977
3013
  }
2978
3014
  return array.elements.map((e, i) => element_ref(n, i));
2979
3015
  }
2980
3016
 
3017
+ /***************************************************************
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.
3024
+ ***************************************************************/
3025
+ function element_at(call, scope)
3026
+ {
3027
+ 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])) {
3030
+ return null;
3031
+ }
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;
3035
+ }
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;
3039
+ if(i < 0) {
3040
+ i = array.elements.length; // nothing there
3041
+ }
3042
+ }
3043
+ return (i !== null && i >= 0)? {prop: String(i), computed: true} : {prop: null, computed: true};
3044
+ }
3045
+
3046
+ /* An integer written out (`1`, `-1`), or null */
3047
+ function literal_integer(n)
3048
+ {
3049
+ if(n && n.type === "UnaryExpression" && n.operator === "-") {
3050
+ let v = literal_integer(n.argument);
3051
+ return (v !== null)? -v : null;
3052
+ }
3053
+ return (n && n.type === "Literal" && Number.isInteger(n.value))? n.value : null;
3054
+ }
3055
+
3056
+ /***************************************************************
3057
+ * The elements `X.slice(a, b)` takes from a const array with no
3058
+ * spread and no hole, `a` and `b` integers written out (or left
3059
+ * out): X[a], ..., read through X (element_ref()); of a let of
3060
+ * literal arrays, what a spread of it is (written_elements()). null
3061
+ * for any other call. Up to gobj-ui 7.26.48 `t(...X.slice(1))` read
3062
+ * nothing.
3063
+ ***************************************************************/
3064
+ function sliced_elements(call, scope)
3065
+ {
3066
+ let c = call.callee;
3067
+ if(c.type !== "MemberExpression" || member_step(c).prop !== "slice" || c.object.type !== "Identifier"
3068
+ || call.arguments.length > 2) {
3069
+ return null;
3070
+ }
3071
+ let array = const_array(c.object, scope);
3072
+ if(!array) {
3073
+ return written_elements(c.object, scope); // a let: every element at each position
3074
+ }
3075
+ if(array.elements.some((e) => e === null || e.type === "SpreadElement")) {
3076
+ return null;
3077
+ }
3078
+ let len = array.elements.length;
3079
+ let bound = (a, dflt) => {
3080
+ if(a === undefined) {
3081
+ return dflt;
3082
+ }
3083
+ let v = literal_integer(a);
3084
+ if(v === null) {
3085
+ return null;
3086
+ }
3087
+ return (v < 0)? Math.max(len + v, 0) : Math.min(v, len);
3088
+ };
3089
+ let from = bound(call.arguments[0], 0);
3090
+ let to = bound(call.arguments[1], len);
3091
+ if(from === null || to === null) {
3092
+ return null;
3093
+ }
3094
+ let out = [];
3095
+ for(let i = from; i < to; i++) {
3096
+ out.push(element_ref(c.object, i));
3097
+ }
3098
+ return out;
3099
+ }
3100
+
2981
3101
  /***************************************************************
2982
3102
  * The array literal a name is a const of, or null: its own value
2983
3103
  * (`Object.freeze()`d too), an import of one, or a const ALIAS of
@@ -3013,86 +3133,497 @@ function const_array(n, scope)
3013
3133
 
3014
3134
  /***************************************************************
3015
3135
  * The array literals an element is read from by its index
3016
- * (step_values()): the value of a const that its scope does not
3017
- * write INTO -- `X[i] = k`, `X.length = n`, `X[i]++`, `delete X[i]`,
3018
- * `X.push()` and every method that moves its elements (`reverse()`,
3019
- * `sort()`, `shift()`, ...), `Object.assign(X, ...)`. Any other
3020
- * array -- one written into, where an element read by its index may
3021
- * be another one by then (`X.reverse(); t(X[0])`), the value of a
3022
- * `let`, an `export default` -- is read by every element written out,
3023
- * at any index. What is written into it is not read (the invisible
3024
- * list of the README), nor a write through an alias of it, from a
3025
- * module that imports it, or by a function it is handed to.
3136
+ * (step_values()): the value of a const -- written out, or
3137
+ * `Object.freeze()`d -- whose every use is a READ that leaves its
3138
+ * elements where they are (array_only_read()). Any other array is
3139
+ * read by every element written out, at any index: one used in any
3140
+ * other way (written into, handed to a function, kept in an object,
3141
+ * returned, exported to a module that does any of these) may hold
3142
+ * another element by then. Up to gobj-ui 7.26.48 the reader looked
3143
+ * for the WRITES it knew (`X[0] = k`, `.reverse()`, ...) and read by
3144
+ * index whatever it did not see written: `Reflect.set(X, 0, k)`,
3145
+ * `Array.prototype.reverse.call(X)`, `X["re" + "verse"]()`, a write
3146
+ * through `{x: X}` or through the third argument of a `.forEach()`
3147
+ * callback, `for(X[0] of ...)` read the element the source wrote
3148
+ * there, not the one the array held.
3026
3149
  ***************************************************************/
3027
- const INDEXED_ARRAYS = new WeakSet(); // the array literals of consts not written into
3028
- const ARRAY_WRITERS = new Set(["push", "unshift", "splice", "fill", "copyWithin", "reverse", "sort", "shift",
3029
- "pop"]);
3030
- const NOTED_ARRAYS = new WeakSet(); // the inits of consts already asked
3150
+ const ARRAY_CONSTS = new WeakMap(); // an array literal -> the binding of the const that holds it
3151
+ const INDEXED_ARRAYS = new WeakMap(); // an array literal -> whether it is read by its index
3031
3152
 
3032
3153
  function note_const_array(b)
3033
3154
  {
3034
3155
  let init = b.init;
3035
- if(!init || init.type !== "ArrayExpression" || NOTED_ARRAYS.has(init) || !b.name || !b.scope) {
3156
+ if(init && is_object_call(init, "freeze") && init.arguments.length === 1) {
3157
+ init = init.arguments[0];
3158
+ }
3159
+ if(!init || init.type !== "ArrayExpression" || ARRAY_CONSTS.has(init) || !b.name || !b.scope) {
3036
3160
  return;
3037
3161
  }
3038
- NOTED_ARRAYS.add(init);
3039
- let root = b.scope[b.scope.length - 1];
3040
- for(let chain of (in_place_writes(root).get(b.name) || [])) {
3041
- let w = binding_of(b.name, chain);
3042
- if(w && w.kind === "const" && w.init === init) {
3043
- return;
3162
+ ARRAY_CONSTS.set(init, b);
3163
+ }
3164
+
3165
+ /* Asked when an index is read, not when the const is: the importers
3166
+ * of an exported array are looked for only then */
3167
+ function is_indexed_array(array)
3168
+ {
3169
+ if(!INDEXED_ARRAYS.has(array)) {
3170
+ let b = ARRAY_CONSTS.get(array);
3171
+ /* `const [a, b] = [...]`: a literal with no name has no other use */
3172
+ INDEXED_ARRAYS.set(array, !!b && ((b.steps || []).length > 0 || array_only_read(b)));
3173
+ }
3174
+ return INDEXED_ARRAYS.get(array);
3175
+ }
3176
+
3177
+ /* The methods of an array that read it and write nothing into it.
3178
+ * Those that take a callback hand it the array as their third
3179
+ * argument (the fourth for reduce): a callback that can reach it
3180
+ * that way is not a read. */
3181
+ const ARRAY_READERS = new Set(["at", "includes", "indexOf", "lastIndexOf", "join", "slice", "concat", "entries",
3182
+ "keys", "values", "toString", "toLocaleString", "flat", "toReversed", "toSorted",
3183
+ "toSpliced", "with"]);
3184
+ const ARRAY_CALLBACK_READERS = new Map([
3185
+ ["map", 2], ["flatMap", 2], ["forEach", 2], ["filter", 2], ["find", 2], ["findIndex", 2], ["findLast", 2],
3186
+ ["findLastIndex", 2], ["some", 2], ["every", 2], ["reduce", 3], ["reduceRight", 3]
3187
+ ]);
3188
+
3189
+ /***************************************************************
3190
+ * Every use of the array a const binds (`b`, of binding_of()) is a
3191
+ * read the reader knows: an element or a property read (`X[1]`,
3192
+ * `X.length`), a method of ARRAY_READERS / ARRAY_CALLBACK_READERS
3193
+ * (`X.at(1)`, `X.map((k) => t(k))`, `X.map(t)`), a spread (`t(...X)`,
3194
+ * `[...X]`), the array of an `.apply()` (`f.apply(x, X)`), a
3195
+ * destructuring (`const [a, b] = X`), a for...of / for...in over it,
3196
+ * a test or an operand (`if(X)`, `X === Y`, `${X}`), `Object.freeze(X)`,
3197
+ * `Array.isArray(X)`. A const alias (`const B = X`,
3198
+ * `const F = Object.freeze(X)`) is a read when every use of IT is one.
3199
+ * An export is a read when every module of the run that imports it
3200
+ * (the app's sources, and the library's for an array of the library)
3201
+ * only reads it; a namespace import, a re-export or a dynamic import()
3202
+ * of the module is not. Anything else -- an argument of any other call
3203
+ * (`Reflect.set(X, ...)`, `shuffle(X)`, `F.call(X)`), a value kept,
3204
+ * returned or handed on (`{x: X}`, `return X`, `a || X`, `let L = X`),
3205
+ * a write into it, a method not listed or named by what is no string
3206
+ * written out -- is not.
3207
+ ***************************************************************/
3208
+ let RUN_SOURCES = null; // the files of the run (collect_used_keys()), where importers are looked for
3209
+
3210
+ function array_only_read(b)
3211
+ {
3212
+ let exported = new Set();
3213
+ let ok = name_only_read(b.name, b.scope, (w) => w.kind === "const" && w.init === b.init, exported, new Map());
3214
+ if(!ok || exported.size === 0) {
3215
+ return ok;
3216
+ }
3217
+ let url = url_of(b.scope);
3218
+ if(!url || b.scope.length !== 1) {
3219
+ return false;
3220
+ }
3221
+ return importers_only_read(url, exported);
3222
+ }
3223
+
3224
+ /* `scope` is the chain down to the scope that declares `name`;
3225
+ * `is_it(binding)` says a binding_of() is the one meant. The names
3226
+ * it is exported as go into `exported`. */
3227
+ function name_only_read(name, scope, is_it, exported, seen)
3228
+ {
3229
+ let root = scope[scope.length - 1];
3230
+ if(!seen.has(root)) {
3231
+ seen.set(root, new Set());
3232
+ }
3233
+ if(seen.get(root).has(name)) {
3234
+ return true; // an alias met again: its uses are being read
3235
+ }
3236
+ seen.get(root).add(name);
3237
+ let prefix = scope.slice(0, -1);
3238
+ let writes = written_members(root);
3239
+ for(let {n, ancestors} of (scope_identifiers(root).get(name) || [])) {
3240
+ let p = ancestors[ancestors.length - 1];
3241
+ if(!is_reference(n, p)) {
3242
+ continue;
3243
+ }
3244
+ let chain = prefix.concat(ancestors);
3245
+ let w = binding_of(name, chain);
3246
+ if(!w || !is_it(w)) {
3247
+ continue;
3248
+ }
3249
+ let use = use_of(n, ancestors, chain, writes);
3250
+ if(use.alias) {
3251
+ if(!name_only_read(use.alias.name, use.alias.scope,
3252
+ (x) => x.kind === "const" && x.init === use.alias.init, exported, seen)) {
3253
+ return false;
3254
+ }
3255
+ } else if(use.export) {
3256
+ exported.add(use.export);
3257
+ } else if(!use.read) {
3258
+ return false;
3259
+ }
3260
+ }
3261
+ if(scope.length === 1 && declared_exports(root).has(name)) {
3262
+ exported.add(name);
3263
+ }
3264
+ return true;
3265
+ }
3266
+
3267
+ /* The names a module exports where it declares them (`export const X`) */
3268
+ const DECLARED_EXPORTS = new WeakMap(); // a parsed file -> Set(name)
3269
+
3270
+ function declared_exports(program)
3271
+ {
3272
+ let known = DECLARED_EXPORTS.get(program);
3273
+ if(!known) {
3274
+ known = new Set();
3275
+ for(let s of program.body) {
3276
+ if(s.type === "ExportNamedDeclaration" && s.declaration && s.declaration.type === "VariableDeclaration") {
3277
+ for(let d of s.declaration.declarations) {
3278
+ for(let name of pattern_names(d.id)) {
3279
+ known.add(name);
3280
+ }
3281
+ }
3282
+ }
3044
3283
  }
3284
+ DECLARED_EXPORTS.set(program, known);
3045
3285
  }
3046
- INDEXED_ARRAYS.add(init);
3286
+ return known;
3047
3287
  }
3048
3288
 
3049
- /* The places a scope writes INTO what a name holds: name -> [the
3050
- * chain from the scope to the name written], found in one walk */
3051
- const IN_PLACE_WRITES = new WeakMap(); // a scope -> Map(name -> [chain])
3289
+ /* The identifiers of a scope by name, each with the chain from the
3290
+ * scope to its parent: found in one walk, so that asking for every
3291
+ * const array of a module is not quadratic */
3292
+ const SCOPE_IDENTIFIERS = new WeakMap(); // a scope -> Map(name -> [{n, ancestors}])
3052
3293
 
3053
- function in_place_writes(root)
3294
+ function scope_identifiers(root)
3054
3295
  {
3055
- let known = IN_PLACE_WRITES.get(root);
3296
+ let known = SCOPE_IDENTIFIERS.get(root);
3056
3297
  if(known) {
3057
3298
  return known;
3058
3299
  }
3059
3300
  known = new Map();
3060
- let add = (target, ancestors) => {
3061
- let chain = [...ancestors];
3062
- while(target && target.type === "MemberExpression") {
3063
- target = target.object;
3301
+ walk(root, (n, ancestors) => {
3302
+ if(n.type !== "Identifier") {
3303
+ return;
3304
+ }
3305
+ if(!known.has(n.name)) {
3306
+ known.set(n.name, []);
3307
+ }
3308
+ known.get(n.name).push({n, ancestors: [...ancestors]});
3309
+ });
3310
+ SCOPE_IDENTIFIERS.set(root, known);
3311
+ return known;
3312
+ }
3313
+
3314
+ /* An identifier that names a binding there, not a property, a key or
3315
+ * a declaration */
3316
+ function is_reference(n, p)
3317
+ {
3318
+ if(!p) {
3319
+ return false;
3320
+ }
3321
+ switch(p.type) {
3322
+ case "MemberExpression":
3323
+ return p.object === n || p.computed;
3324
+ case "Property":
3325
+ return p.value === n && (p.shorthand || p.key !== n);
3326
+ case "MethodDefinition":
3327
+ case "PropertyDefinition":
3328
+ return p.computed && p.key === n;
3329
+ case "VariableDeclarator":
3330
+ return p.init === n;
3331
+ case "FunctionDeclaration":
3332
+ case "FunctionExpression":
3333
+ case "ArrowFunctionExpression":
3334
+ case "ClassDeclaration":
3335
+ case "ClassExpression":
3336
+ case "LabeledStatement":
3337
+ case "BreakStatement":
3338
+ case "ContinueStatement":
3339
+ case "ImportSpecifier":
3340
+ case "ImportDefaultSpecifier":
3341
+ case "ImportNamespaceSpecifier":
3342
+ case "CatchClause":
3343
+ return p.type === "ArrowFunctionExpression" && p.body === n;
3344
+ case "ExportSpecifier":
3345
+ return p.local === n;
3346
+ case "AssignmentPattern":
3347
+ return p.right === n;
3348
+ case "ArrayPattern":
3349
+ case "ObjectPattern":
3350
+ case "RestElement":
3351
+ return false;
3352
+ default:
3353
+ return true;
3354
+ }
3355
+ }
3356
+
3357
+ /* What one use of the array is: {read}, {alias: binding}, {export:
3358
+ * name}, or {} (anything else) */
3359
+ function use_of(n, ancestors, chain, writes)
3360
+ {
3361
+ let p = ancestors[ancestors.length - 1];
3362
+ let gp = ancestors[ancestors.length - 2];
3363
+ let at = (k) => ancestors[ancestors.length - k];
3364
+ switch(p.type) {
3365
+ case "MemberExpression": {
3366
+ if(p.object !== n) {
3367
+ return {read: true}; // `Y[X]`: a key
3368
+ }
3369
+ if(writes.has(p)) {
3370
+ return {};
3371
+ }
3372
+ if(gp && gp.type === "TaggedTemplateExpression" && gp.tag === p) {
3373
+ return {};
3374
+ }
3375
+ if(!gp || gp.type !== "CallExpression" || gp.callee !== p) {
3376
+ return {read: true};
3377
+ }
3378
+ let method = member_step(p).prop;
3379
+ if(ARRAY_READERS.has(method)) {
3380
+ return {read: true};
3381
+ }
3382
+ if(ARRAY_CALLBACK_READERS.has(method)) {
3383
+ let reach = ARRAY_CALLBACK_READERS.get(method);
3384
+ return {read: gp.arguments.every((a) => callback_cannot_reach(a, reach, chain.slice(0, -1)))};
3385
+ }
3386
+ return {};
3064
3387
  }
3065
- if(target && target.type === "Identifier") {
3066
- if(!known.has(target.name)) {
3067
- known.set(target.name, []);
3388
+ case "SpreadElement":
3389
+ return {read: !!gp && /^(CallExpression|NewExpression|ArrayExpression|ObjectExpression)$/.test(gp.type)};
3390
+ case "CallExpression": {
3391
+ if(p.callee === n) {
3392
+ return {};
3393
+ }
3394
+ let method = (p.callee.type === "MemberExpression")? member_step(p.callee).prop : null;
3395
+ if(method === "apply" && p.arguments[1] === n) {
3396
+ return {read: true};
3397
+ }
3398
+ if(p.callee.type === "MemberExpression" && !p.callee.computed && p.callee.object.type === "Identifier"
3399
+ && p.callee.object.name === "Array" && method === "isArray"
3400
+ && binding_of("Array", chain) === null) {
3401
+ return {read: true};
3402
+ }
3403
+ if(is_object_call(p, "freeze") && p.arguments.length === 1 && binding_of("Object", chain) === null) {
3404
+ if(gp && gp.type === "ExpressionStatement") {
3405
+ return {read: true};
3406
+ }
3407
+ return const_alias(gp, at(3), p, chain);
3408
+ }
3409
+ return {};
3410
+ }
3411
+ case "VariableDeclarator":
3412
+ if(p.id.type === "ArrayPattern" || p.id.type === "ObjectPattern") {
3413
+ return {read: true};
3414
+ }
3415
+ return const_alias(p, gp, n, chain);
3416
+ case "AssignmentExpression":
3417
+ return {read: p.right === n && p.operator === "="
3418
+ && (p.left.type === "ArrayPattern" || p.left.type === "ObjectPattern")};
3419
+ case "ForOfStatement":
3420
+ case "ForInStatement":
3421
+ return {read: p.right === n};
3422
+ case "BinaryExpression":
3423
+ return {read: true};
3424
+ case "TemplateLiteral":
3425
+ return {read: !gp || gp.type !== "TaggedTemplateExpression"}; // a tag is handed the array
3426
+ case "UnaryExpression":
3427
+ return {read: p.operator !== "delete"};
3428
+ case "IfStatement":
3429
+ case "WhileStatement":
3430
+ case "DoWhileStatement":
3431
+ case "ConditionalExpression":
3432
+ return {read: p.test === n};
3433
+ case "ExportSpecifier":
3434
+ return {export: p.exported.name || p.exported.value};
3435
+ case "ExportDefaultDeclaration":
3436
+ return {export: "default"};
3437
+ default:
3438
+ return {};
3439
+ }
3440
+ }
3441
+
3442
+ /* `const <name> = <value>`: an alias, whose uses are the array's */
3443
+ function const_alias(declarator, declaration, value, chain)
3444
+ {
3445
+ if(!declarator || declarator.type !== "VariableDeclarator" || declarator.init !== value
3446
+ || declarator.id.type !== "Identifier" || !declaration || declaration.kind !== "const") {
3447
+ return {};
3448
+ }
3449
+ let name = declarator.id.name;
3450
+ let b = binding_of(name, chain);
3451
+ if(!b || b.kind !== "const" || b.init !== value) {
3452
+ return {};
3453
+ }
3454
+ return {alias: b};
3455
+ }
3456
+
3457
+ /* An argument of a method of ARRAY_CALLBACK_READERS that cannot reach
3458
+ * the array: a value that is no function, a function literal that
3459
+ * declares no parameter at `reach` (nor a rest) and reads no
3460
+ * `arguments`, or a translator. A function named by anything else may
3461
+ * take the array and write into it. */
3462
+ function callback_cannot_reach(a, reach, scope)
3463
+ {
3464
+ if(a.type === "Literal" || a.type === "ObjectExpression" || a.type === "ArrayExpression"
3465
+ || a.type === "TemplateLiteral") {
3466
+ return true; // no function: what it holds is a use of its own
3467
+ }
3468
+ if(a.type === "ArrowFunctionExpression" || a.type === "FunctionExpression") {
3469
+ if(a.params.length > reach || a.params.some((q) => q.type === "RestElement")) {
3470
+ return false;
3471
+ }
3472
+ if(a.type === "ArrowFunctionExpression") {
3473
+ return true;
3474
+ }
3475
+ let uses_arguments = false;
3476
+ walk(a.body, (x) => {
3477
+ if(x.type === "Identifier" && x.name === "arguments") {
3478
+ uses_arguments = true;
3479
+ }
3480
+ });
3481
+ return !uses_arguments;
3482
+ }
3483
+ let c = callee_of(a, scope);
3484
+ return !!c && c.kind === "t";
3485
+ }
3486
+
3487
+ /* The members a scope writes: the left of an `=` (through any
3488
+ * destructuring), the head of a for...of / for...in that declares
3489
+ * nothing, `x[i]++`, `delete x[i]`. Found in one walk. */
3490
+ const WRITTEN_MEMBERS = new WeakMap(); // a scope -> Set(member)
3491
+
3492
+ function written_members(root)
3493
+ {
3494
+ let known = WRITTEN_MEMBERS.get(root);
3495
+ if(known) {
3496
+ return known;
3497
+ }
3498
+ known = new Set();
3499
+ let add = (targets) => {
3500
+ for(let t of targets) {
3501
+ if(t.type === "MemberExpression") {
3502
+ known.add(t);
3068
3503
  }
3069
- known.get(target.name).push(chain);
3070
3504
  }
3071
3505
  };
3072
- walk(root, (n, ancestors) => {
3506
+ walk(root, (n) => {
3073
3507
  if(n.type === "AssignmentExpression") {
3074
- for(let t of write_targets(n.left)) {
3075
- if(t.type === "MemberExpression") {
3076
- add(t, [...ancestors, n]);
3077
- }
3508
+ add(write_targets(n.left));
3509
+ } else if((n.type === "ForOfStatement" || n.type === "ForInStatement") && n.left.type !== "VariableDeclaration") {
3510
+ add(write_targets(n.left));
3511
+ } else if(n.type === "UpdateExpression"
3512
+ || (n.type === "UnaryExpression" && n.operator === "delete")) {
3513
+ add([n.argument]);
3514
+ }
3515
+ });
3516
+ WRITTEN_MEMBERS.set(root, known);
3517
+ return known;
3518
+ }
3519
+
3520
+ /***************************************************************
3521
+ * Every module of the run that imports `names` of the module `url`
3522
+ * only reads what it imports (name_only_read()). A namespace import,
3523
+ * a re-export, an `export *` or a dynamic import() of the module is no
3524
+ * read the reader follows. With no run (a test that scans one file),
3525
+ * nothing is known of the importers: false.
3526
+ ***************************************************************/
3527
+ function importers_only_read(url, names)
3528
+ {
3529
+ if(!RUN_SOURCES) {
3530
+ return false;
3531
+ }
3532
+ let files = [...RUN_SOURCES];
3533
+ if(lib_module_of(url) || url.href === LIB_INDEX.href) {
3534
+ files.push(...lib_sources());
3535
+ }
3536
+ for(let f of files) {
3537
+ if(f.href === url.href) {
3538
+ continue;
3539
+ }
3540
+ let ast = load_ast(f);
3541
+ if(!ast) {
3542
+ continue;
3543
+ }
3544
+ for(let imp of module_imports(ast, f)) {
3545
+ if(imp.any) {
3546
+ return false; // `import(path)`: any module, this one too
3078
3547
  }
3079
- } else if((n.type === "UpdateExpression" && n.argument.type === "MemberExpression")
3080
- || (n.type === "UnaryExpression" && n.operator === "delete" && n.argument.type === "MemberExpression")) {
3081
- add(n.argument, [...ancestors, n]);
3082
- } else if(n.type === "CallExpression") {
3083
- let c = n.callee;
3084
- if(c.type === "MemberExpression" && ARRAY_WRITERS.has(member_step(c).prop)) {
3085
- add(c.object, [...ancestors, n]);
3086
- } else if((is_object_call(n, "assign") || is_object_call(n, "defineProperty")
3087
- || is_object_call(n, "defineProperties")) && n.arguments.length > 0) {
3088
- add(n.arguments[0], [...ancestors, n]);
3548
+ if(imp.target !== url.href) {
3549
+ continue;
3550
+ }
3551
+ if(imp.whole) {
3552
+ return false;
3553
+ }
3554
+ if(!names.has(imp.imported)) {
3555
+ continue;
3556
+ }
3557
+ if(imp.reexport) {
3558
+ return false;
3559
+ }
3560
+ let again = new Set(); // exported on by the importer: its own importers are not looked for
3561
+ let ok = name_only_read(imp.local, [ast], (w) => w.kind === "import" && w.scope.length === 1
3562
+ && w.imported === imp.imported && module_url(f, w.source)
3563
+ && module_url(f, w.source).href === url.href, again, new Map());
3564
+ if(!ok || again.size > 0) {
3565
+ return false;
3089
3566
  }
3090
3567
  }
3568
+ }
3569
+ return true;
3570
+ }
3571
+
3572
+ /* What a module takes from others: [{target (href), imported, local,
3573
+ * reexport, whole, any}], `whole` for a namespace, an `export *` or a
3574
+ * dynamic import(), `any` for an import() of a path not written out */
3575
+ const MODULE_IMPORTS = new WeakMap(); // a parsed file -> its imports
3576
+
3577
+ function module_imports(ast, url)
3578
+ {
3579
+ let known = MODULE_IMPORTS.get(ast);
3580
+ if(known) {
3581
+ return known;
3582
+ }
3583
+ known = [];
3584
+ let target = (source) => {
3585
+ let u = module_url(url, source);
3586
+ return u? u.href : null;
3587
+ };
3588
+ for(let s of ast.body) {
3589
+ if(s.type === "ImportDeclaration") {
3590
+ for(let sp of s.specifiers) {
3591
+ let imported = (sp.type === "ImportSpecifier")? (sp.imported.name || sp.imported.value)
3592
+ : (sp.type === "ImportDefaultSpecifier")? "default" : "*";
3593
+ known.push({target: target(s.source.value), imported, local: sp.local.name,
3594
+ reexport: false, whole: imported === "*"});
3595
+ }
3596
+ } else if(s.type === "ExportAllDeclaration") {
3597
+ known.push({target: target(s.source.value), imported: "*", local: null, reexport: true, whole: true});
3598
+ } else if(s.type === "ExportNamedDeclaration" && s.source) {
3599
+ for(let sp of s.specifiers) {
3600
+ known.push({target: target(s.source.value), imported: sp.local.name || sp.local.value, local: null,
3601
+ reexport: true, whole: false});
3602
+ }
3603
+ }
3604
+ }
3605
+ walk(ast, (n) => {
3606
+ if(n.type === "ImportExpression") {
3607
+ let src = static_string(n.source);
3608
+ known.push({target: (src !== null)? target(src) : null, imported: "*", local: null, reexport: false,
3609
+ whole: true, any: src === null});
3610
+ }
3091
3611
  });
3092
- IN_PLACE_WRITES.set(root, known);
3612
+ MODULE_IMPORTS.set(ast, known);
3093
3613
  return known;
3094
3614
  }
3095
3615
 
3616
+ /* The modules of this library (src/, its tests left out) */
3617
+ let LIB_SOURCES = null;
3618
+
3619
+ function lib_sources()
3620
+ {
3621
+ if(!LIB_SOURCES) {
3622
+ LIB_SOURCES = [LIB_INDEX, ...source_files(LIB_SRC, null)];
3623
+ }
3624
+ return LIB_SOURCES;
3625
+ }
3626
+
3096
3627
  function is_spread(n)
3097
3628
  {
3098
3629
  return !!n && n.type === "SpreadElement";
@@ -3155,9 +3686,9 @@ function step_values(o, step, node, depth)
3155
3686
  if(o.kind === "value" && is_object_call(o.node, "assign")) {
3156
3687
  return assigned_properties(o, step, depth + 1);
3157
3688
  }
3158
- if(o.kind === "value" && o.node.type === "ArrayExpression" && step.computed) {
3689
+ if(o.kind === "value" && o.node.type === "ArrayExpression" && (step.computed || array_index(step) !== null)) {
3159
3690
  let i = array_index(step);
3160
- if(i !== null && INDEXED_ARRAYS.has(o.node)
3691
+ if(i !== null && is_indexed_array(o.node)
3161
3692
  && !o.node.elements.slice(0, i + 1).some((e) => e && e.type === "SpreadElement")) {
3162
3693
  return indexed_values(o, i, at, depth + 1);
3163
3694
  }
@@ -4183,12 +4714,93 @@ const LITERAL_LETS = new WeakMap(); // the first declarator of a variable ->
4183
4714
 
4184
4715
  function literal_let_keys(leaf)
4185
4716
  {
4186
- if(leaf.kind !== "unknown" || !leaf.var_root || leaf.var_root.steps.length > 0) {
4717
+ if(leaf.kind !== "unknown" || !leaf.var_root) {
4718
+ return [];
4719
+ }
4720
+ let steps = leaf.var_root.steps;
4721
+ if(steps.length === 1 && is_element_step(steps[0])) {
4722
+ return let_array_literals(leaf.var_root.binding) || [];
4723
+ }
4724
+ if(steps.length > 0) {
4187
4725
  return [];
4188
4726
  }
4189
4727
  return let_literals(leaf.var_root.binding, new Set()) || [];
4190
4728
  }
4191
4729
 
4730
+ /* A step that reads an element: `X[i]`, `X[1]`, an element walked */
4731
+ function is_element_step(step)
4732
+ {
4733
+ return !!step.each || step.computed || array_index(step) !== null;
4734
+ }
4735
+
4736
+ /***************************************************************
4737
+ * The strings an ELEMENT of a plain `let` / `var` can be, when it is
4738
+ * only ever given array literals of literal_values() and elements
4739
+ * that are those (`X[0] = "k"`, `X.push("k")`): every one of them, at
4740
+ * any index -- what a let holds at a place is not known, so `X[1]`
4741
+ * and `t(...X)` read every element (let_array_length()). null for any
4742
+ * other let: given anything else, or written in a way the reader does
4743
+ * not read (assigned_values()). A write the reader does not see
4744
+ * (`Reflect.set(X, 0, k)`, a function it is handed to) adds what it
4745
+ * writes unread. Up to gobj-ui 7.26.48 an element of a let was read
4746
+ * as nothing.
4747
+ ***************************************************************/
4748
+ const LET_ARRAYS = new WeakMap(); // the first declarator of a variable -> {strings, length} | null
4749
+
4750
+ function let_array(b)
4751
+ {
4752
+ if(!b || b.kind !== "var" || b.picks.size > 0) {
4753
+ return null;
4754
+ }
4755
+ let first = b.declarators[0];
4756
+ if(LET_ARRAYS.has(first)) {
4757
+ return LET_ARRAYS.get(first);
4758
+ }
4759
+ LET_ARRAYS.set(first, null); // a let met again while it is read adds nothing
4760
+ let {values, unknown} = assigned_values(b, true);
4761
+ let out = (unknown.length === 0)? {strings: [], length: 0} : null;
4762
+ for(let v of values) {
4763
+ if(out === null) {
4764
+ break;
4765
+ }
4766
+ if(!v.node) {
4767
+ continue;
4768
+ }
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
+ }
4780
+ } else if(v.steps.length === 1 && is_element_step(v.steps[0]) && !v.spread) {
4781
+ let lits = literal_values(v.node, v.scope, new Set());
4782
+ if(lits === null) {
4783
+ out = null;
4784
+ } else {
4785
+ out.strings.push(...lits);
4786
+ }
4787
+ } else {
4788
+ out = null;
4789
+ }
4790
+ }
4791
+ if(out) {
4792
+ out.strings = [...new Set(out.strings)];
4793
+ }
4794
+ LET_ARRAYS.set(first, out);
4795
+ return out;
4796
+ }
4797
+
4798
+ function let_array_literals(b)
4799
+ {
4800
+ let a = let_array(b);
4801
+ return a? a.strings : null;
4802
+ }
4803
+
4192
4804
  /***************************************************************
4193
4805
  * The strings a plain `let` / `var` (binding_of(): {kind: "var"})
4194
4806
  * can hold, when it is only ever given literal_values(): [string],
@@ -5897,6 +6509,21 @@ function shape_owner_check(shape, keys, shapes)
5897
6509
  * violation_count().
5898
6510
  ***************************************************************/
5899
6511
  function collect_used_keys(opts)
6512
+ {
6513
+ RUN_SOURCES = [];
6514
+ for(let dir of opts.src) {
6515
+ if(existsSync(dir)) {
6516
+ RUN_SOURCES.push(...source_files(dir, opts.locales));
6517
+ }
6518
+ }
6519
+ try {
6520
+ return collect_run_keys(opts);
6521
+ } finally {
6522
+ RUN_SOURCES = null;
6523
+ }
6524
+ }
6525
+
6526
+ function collect_run_keys(opts)
5900
6527
  {
5901
6528
  GAVE_UP.clear();
5902
6529
  let keys = new Set();
@@ -5909,6 +6536,7 @@ function collect_used_keys(opts)
5909
6536
  declared: new Set([...attr_keys, ...array_keys])
5910
6537
  };
5911
6538
  let declared_seen = new Set();
6539
+ let excluded = new Set((opts.exclude || []).map((u) => u.href));
5912
6540
  let barrel = barrel_export_map();
5913
6541
  let published = new Map(); // list name -> Set of keys
5914
6542
  let refs = []; // [{label, line, lists, keys}]
@@ -5969,7 +6597,6 @@ function collect_used_keys(opts)
5969
6597
 
5970
6598
  /* The app's own source, and the code it shares with others */
5971
6599
  let pending = [];
5972
- let excluded = new Set((opts.exclude || []).map((u) => u.href));
5973
6600
  for(let dir of opts.src) {
5974
6601
  if(!existsSync(dir)) {
5975
6602
  abort(`no source directory at ${dir.pathname}`);
@@ -6576,40 +7203,71 @@ try {
6576
7203
  is_main = false;
6577
7204
  }
6578
7205
  if(is_main) {
7206
+ /*
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.
7214
+ */
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
+ });
6579
7228
  process.on("unhandledRejection", (reason) => {
6580
7229
  console.error("validate-locales: a promise was rejected and nothing handled it (a locale is " +
6581
7230
  `imported, so its code runs): ${(reason && reason.stack) || reason}`);
6582
- process.exitCode = 1;
7231
+ failed = true;
6583
7232
  });
6584
7233
  try {
6585
7234
  await main();
6586
7235
  } catch(e) {
7236
+ failed = true;
6587
7237
  if(!(e instanceof Abort)) {
7238
+ ending = true;
6588
7239
  throw e;
6589
7240
  }
6590
7241
  }
6591
- exit_when_written();
7242
+ if(errors > 0) {
7243
+ failed = true;
7244
+ }
7245
+ exit_when_written(() => {
7246
+ ending = true;
7247
+ real_exit.call(process, failed? 1 : 0);
7248
+ });
6592
7249
  }
6593
7250
 
6594
7251
  /***************************************************************
6595
- * End the run with its exitCode once what it wrote has left: a
6596
- * locale is IMPORTED, and a timer its code leaves running holds the
6597
- * process for ever, verdict or not (up to gobj-ui 7.26.45, also a run
6598
- * that failed). process.exit() runs from the callbacks of a last
6599
- * write to each stream, which come after every write before them, so
6600
- * a long list on its way to a pipe still reaches it whole. It waits
6601
- * for a timer first: a promise rejected with nothing to handle it is
6602
- * told only when the loop turns, and a timer of 0 a locale set is due
6603
- * before it -- up to gobj-ui 7.26.46 such a rejection was lost, and
6604
- * the run passed.
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.
6605
7263
  ***************************************************************/
6606
- function exit_when_written()
7264
+ function exit_when_written(end)
6607
7265
  {
6608
7266
  let pending = 2;
6609
7267
  let written = () => {
6610
7268
  pending--;
6611
7269
  if(pending === 0) {
6612
- process.exit();
7270
+ end();
6613
7271
  }
6614
7272
  };
6615
7273
  setTimeout(() => {