@yuneta/gobj-ui 7.26.43 → 7.26.45
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/README.md +83 -16
- package/dist/gobj-ui.cjs.js +103 -19
- package/dist/gobj-ui.es.js +103 -20
- package/index.js +1 -0
- package/package.json +2 -2
- package/scripts/check-markup-strings.mjs +4 -1
- package/scripts/validate-locales.mjs +531 -155
- package/src/c_yui_form.js +47 -26
- package/src/c_yui_node.js +3 -1
- package/src/c_yui_schema_editor.js +3 -0
- package/src/c_yui_shell.js +55 -1
- package/src/c_yui_window.js +30 -5
- package/src/form_table_edits_ask.wiring.test.js +11 -2
- package/src/form_table_rows.wiring.test.js +163 -7
- package/src/i18n_key_functions.js +2 -2
- package/src/i18n_key_functions.test.js +37 -3
- package/src/node_nav_events.wiring.test.js +6 -0
- package/src/schema_editor_diagram_click.wiring.test.js +28 -0
- package/src/shell_escape_taken.wiring.test.js +34 -0
- package/src/shell_focus_trap.js +15 -0
- package/src/window_dialog.wiring.test.js +80 -1
- package/src/window_rect_rounding.wiring.test.js +22 -0
|
@@ -61,7 +61,8 @@
|
|
|
61
61
|
* name, an alias of one (`const translate = t`, a `.bind()`,
|
|
62
62
|
* a destructuring, `const {t: tt} = i18next` or a parameter
|
|
63
63
|
* `{t: tt}`, a re-export), called `t?.(k)`, `t.call(x, k)` or
|
|
64
|
-
* `t.apply(x, [k])
|
|
64
|
+
* `t.apply(x, [k])` (`t["call"](x, k)` too, and a computed
|
|
65
|
+
* `["bind"]`); an array there is i18next's list of
|
|
65
66
|
* fallback keys, each one a key (`t([k, "fallback"])`). A
|
|
66
67
|
* `.bind()` that binds a key (`const tk = t.bind(null, k)`)
|
|
67
68
|
* hands t() that key, read where the bind is written, and
|
|
@@ -259,13 +260,19 @@
|
|
|
259
260
|
* gobj_create().
|
|
260
261
|
*
|
|
261
262
|
* Where the reader GIVES UP on a value it says so, with the file
|
|
262
|
-
* and the line
|
|
263
|
-
*
|
|
264
|
-
*
|
|
265
|
-
*
|
|
266
|
-
*
|
|
267
|
-
*
|
|
268
|
-
*
|
|
263
|
+
* and the line of the READ (and the place it went past), and
|
|
264
|
+
* that fails: a value read through more than MAX_DEPTH (24)
|
|
265
|
+
* steps -- each const, alias, call, property, element and branch
|
|
266
|
+
* of ?: / || / ?? is one --, a const that leads back to itself,
|
|
267
|
+
* an expression that can take more than MAX_VALUES (10000)
|
|
268
|
+
* values -- a key behind it would go unasked. Up to 7.26.42 it
|
|
269
|
+
* stopped there in silence, and a chain of consts that doubles at
|
|
270
|
+
* every link died with a RangeError naming no file. A const is
|
|
271
|
+
* read once, wherever it is read, and keeps the steps it took, so
|
|
272
|
+
* the verdict does not depend on what was read first. A chain of
|
|
273
|
+
* aliases of a function or of a published list is followed to its
|
|
274
|
+
* end, and is a give-up only when a translator, a key function or
|
|
275
|
+
* a list can be there.
|
|
269
276
|
|
|
270
277
|
* Options (paths relative to the app's directory):
|
|
271
278
|
*
|
|
@@ -360,12 +367,20 @@ function violation_count()
|
|
|
360
367
|
}
|
|
361
368
|
|
|
362
369
|
/***************************************************************
|
|
363
|
-
* A fatal problem of the setup, not a violation: said, exit 1
|
|
370
|
+
* A fatal problem of the setup, not a violation: said, exit 1. It
|
|
371
|
+
* ends the run by throwing Abort (caught where the script runs main()),
|
|
372
|
+
* never by process.exit(): what was written before it to a pipe may
|
|
373
|
+
* still be on its way, and process.exit() drops it.
|
|
364
374
|
***************************************************************/
|
|
375
|
+
class Abort extends Error
|
|
376
|
+
{
|
|
377
|
+
}
|
|
378
|
+
|
|
365
379
|
function abort(msg)
|
|
366
380
|
{
|
|
367
381
|
process.stderr.write(`validate-locales: ${msg}\n`);
|
|
368
|
-
process.
|
|
382
|
+
process.exitCode = 1;
|
|
383
|
+
throw new Abort(msg);
|
|
369
384
|
}
|
|
370
385
|
|
|
371
386
|
/***************************************************************
|
|
@@ -384,8 +399,10 @@ let acorn_parse;
|
|
|
384
399
|
try {
|
|
385
400
|
({parse: acorn_parse} = await import("acorn"));
|
|
386
401
|
} catch(e) {
|
|
387
|
-
|
|
388
|
-
|
|
402
|
+
/* Nothing was said before: exit at once, the module cannot load */
|
|
403
|
+
process.stderr.write(`validate-locales: cannot load acorn (${e.message}): it is a dependency of ` +
|
|
404
|
+
`@yuneta/gobj-ui, so the install is incomplete: run npm install\n`);
|
|
405
|
+
process.exit(1);
|
|
389
406
|
}
|
|
390
407
|
|
|
391
408
|
/***************************************************************
|
|
@@ -774,8 +791,11 @@ async function main()
|
|
|
774
791
|
}
|
|
775
792
|
|
|
776
793
|
if(errors > 0) {
|
|
794
|
+
/* exitCode, not process.exit(): a long list written to a pipe is
|
|
795
|
+
* still on its way, and up to gobj-ui 7.26.44 it was cut short */
|
|
777
796
|
process.stderr.write(`validate-locales: ${errors} violation(s), see above\n`);
|
|
778
|
-
process.
|
|
797
|
+
process.exitCode = 1;
|
|
798
|
+
return;
|
|
779
799
|
}
|
|
780
800
|
|
|
781
801
|
let n_keys = ref_plain.size + bases.size;
|
|
@@ -1769,18 +1789,48 @@ const TRANSLATOR = Object.freeze({kind: "t", name: "t"});
|
|
|
1769
1789
|
* an object, a function of a package
|
|
1770
1790
|
* the reader does not read
|
|
1771
1791
|
***************************************************************/
|
|
1772
|
-
function callee_of(node, scope
|
|
1792
|
+
function callee_of(node, scope)
|
|
1793
|
+
{
|
|
1794
|
+
return from_read(node, scope, () => {
|
|
1795
|
+
let far = {past: null, lost: null, seen: new Set()};
|
|
1796
|
+
let r = callee_at(node, scope, 0, far);
|
|
1797
|
+
if(far.lost) {
|
|
1798
|
+
give_up(far.lost.node, far.lost.scope, far.lost.why);
|
|
1799
|
+
return null;
|
|
1800
|
+
}
|
|
1801
|
+
if(far.past && may_translate(r)) {
|
|
1802
|
+
give_up(far.past.node, far.past.scope, GIVE_UP_DEEP);
|
|
1803
|
+
return null;
|
|
1804
|
+
}
|
|
1805
|
+
return r;
|
|
1806
|
+
});
|
|
1807
|
+
}
|
|
1808
|
+
|
|
1809
|
+
/* Can a callee (callee_of()) be a translator, or a key function of a
|
|
1810
|
+
* table -- a function of the library? */
|
|
1811
|
+
function may_translate(r)
|
|
1812
|
+
{
|
|
1813
|
+
return !!r && (r.kind === "t" || TRANSLATOR_NAMES.has(r.name) || lib_module_of(r.url) !== null);
|
|
1814
|
+
}
|
|
1815
|
+
|
|
1816
|
+
/* callee_of() `depth` steps down: past MAX_DEPTH it goes on, to see
|
|
1817
|
+
* what the chain ends in (`far.past`: where it went past); past
|
|
1818
|
+
* MAX_FOLLOW, or round a cycle, the end is not known (`far.lost`) */
|
|
1819
|
+
function callee_at(node, scope, depth, far)
|
|
1773
1820
|
{
|
|
1774
1821
|
if(!node) {
|
|
1775
1822
|
return null;
|
|
1776
1823
|
}
|
|
1777
|
-
if(depth > MAX_DEPTH) {
|
|
1778
|
-
|
|
1824
|
+
if(depth > MAX_DEPTH && !far.past) {
|
|
1825
|
+
far.past = {node, scope};
|
|
1826
|
+
}
|
|
1827
|
+
if(depth > MAX_FOLLOW) {
|
|
1828
|
+
far.lost = far.lost || {node, scope, why: GIVE_UP_DEEP};
|
|
1779
1829
|
return null;
|
|
1780
1830
|
}
|
|
1781
1831
|
switch(node.type) {
|
|
1782
1832
|
case "ChainExpression":
|
|
1783
|
-
return
|
|
1833
|
+
return callee_at(node.expression, scope, depth + 1, far);
|
|
1784
1834
|
|
|
1785
1835
|
case "Identifier": {
|
|
1786
1836
|
let named = TRANSLATOR_NAMES.has(node.name)? TRANSLATOR : null;
|
|
@@ -1799,13 +1849,20 @@ function callee_of(node, scope, depth = 0)
|
|
|
1799
1849
|
if(FUNCTION_RE.test(b.init.type)) {
|
|
1800
1850
|
return {kind: "fn", fn: b.init, scope: b.scope, name: b.name, url: url_of(b.scope)};
|
|
1801
1851
|
}
|
|
1802
|
-
|
|
1852
|
+
if(far.seen.has(b.init)) {
|
|
1853
|
+
far.lost = far.lost || {node: b.init, scope: b.scope, why: GIVE_UP_CYCLE};
|
|
1854
|
+
return null;
|
|
1855
|
+
}
|
|
1856
|
+
far.seen.add(b.init);
|
|
1857
|
+
let r = callee_at(b.init, b.scope, depth + 1, far);
|
|
1858
|
+
far.seen.delete(b.init);
|
|
1859
|
+
return r || named;
|
|
1803
1860
|
case "var": {
|
|
1804
1861
|
let fn = var_function(b);
|
|
1805
1862
|
return fn? {kind: "fn", fn, scope: b.scope, name: b.name, url: url_of(b.scope)} : named;
|
|
1806
1863
|
}
|
|
1807
1864
|
case "import":
|
|
1808
|
-
return exported_callee(import_target(b), depth) || named;
|
|
1865
|
+
return exported_callee(import_target(b), depth, far) || named;
|
|
1809
1866
|
case "param":
|
|
1810
1867
|
/* `function f({t: tt})` is `opts.t`, the same member */
|
|
1811
1868
|
return /\.(?:t|tr)$/.test(b.path)? TRANSLATOR : named;
|
|
@@ -1825,7 +1882,7 @@ function callee_of(node, scope, depth = 0)
|
|
|
1825
1882
|
if(b && b.kind === "import" && b.imported === "*") {
|
|
1826
1883
|
let ns = import_target(b);
|
|
1827
1884
|
if(ns && ns.kind === "namespace") {
|
|
1828
|
-
let r = exported_callee(module_export(ns.url, step.prop), depth);
|
|
1885
|
+
let r = exported_callee(module_export(ns.url, step.prop), depth, far);
|
|
1829
1886
|
if(r) {
|
|
1830
1887
|
return r;
|
|
1831
1888
|
}
|
|
@@ -1843,7 +1900,7 @@ function callee_of(node, scope, depth = 0)
|
|
|
1843
1900
|
let p = map.init.properties.find((q) => q.type === "Property" && prop_name(q) === step.prop
|
|
1844
1901
|
&& q.kind === "init" && !q.computed);
|
|
1845
1902
|
if(p) {
|
|
1846
|
-
let r =
|
|
1903
|
+
let r = callee_at(p.value, [...map.scope, map.init, p], depth + 1, far);
|
|
1847
1904
|
if(r) {
|
|
1848
1905
|
return r;
|
|
1849
1906
|
}
|
|
@@ -1857,11 +1914,10 @@ function callee_of(node, scope, depth = 0)
|
|
|
1857
1914
|
/* f.bind(x) is f; f.bind(x, a, ...) is f with its first arguments
|
|
1858
1915
|
* BOUND, so what a call of it is handed comes after them */
|
|
1859
1916
|
let c = node.callee;
|
|
1860
|
-
if(c.type !== "MemberExpression" || c.
|
|
1861
|
-
|| c.property.name !== "bind") {
|
|
1917
|
+
if(c.type !== "MemberExpression" || member_step(c).prop !== "bind") {
|
|
1862
1918
|
return null;
|
|
1863
1919
|
}
|
|
1864
|
-
let inner =
|
|
1920
|
+
let inner = callee_at(c.object, scope, depth + 1, far);
|
|
1865
1921
|
let taken = node.arguments.slice(1);
|
|
1866
1922
|
if(!inner || taken.length === 0) {
|
|
1867
1923
|
return inner;
|
|
@@ -1881,7 +1937,7 @@ function callee_of(node, scope, depth = 0)
|
|
|
1881
1937
|
* What an export (module_export()) is as a callee: a function, the
|
|
1882
1938
|
* i18next `t`, or a const that is an alias of one
|
|
1883
1939
|
***************************************************************/
|
|
1884
|
-
function exported_callee(x, depth)
|
|
1940
|
+
function exported_callee(x, depth, far)
|
|
1885
1941
|
{
|
|
1886
1942
|
if(!x) {
|
|
1887
1943
|
return null;
|
|
@@ -1893,7 +1949,14 @@ function exported_callee(x, depth)
|
|
|
1893
1949
|
return (x.source === "i18next" && x.imported === "t")? TRANSLATOR : null;
|
|
1894
1950
|
}
|
|
1895
1951
|
if(x.kind === "const" && x.steps.length === 0) {
|
|
1896
|
-
|
|
1952
|
+
if(far.seen.has(x.init)) {
|
|
1953
|
+
far.lost = far.lost || {node: x.init, scope: x.scope, why: GIVE_UP_CYCLE};
|
|
1954
|
+
return null;
|
|
1955
|
+
}
|
|
1956
|
+
far.seen.add(x.init);
|
|
1957
|
+
let r = callee_at(x.init, x.scope, depth + 1, far);
|
|
1958
|
+
far.seen.delete(x.init);
|
|
1959
|
+
return r;
|
|
1897
1960
|
}
|
|
1898
1961
|
return null;
|
|
1899
1962
|
}
|
|
@@ -1942,8 +2005,7 @@ function call_target(call, scope)
|
|
|
1942
2005
|
}
|
|
1943
2006
|
let r = null;
|
|
1944
2007
|
let callee = call.callee;
|
|
1945
|
-
let method = (callee.type === "MemberExpression"
|
|
1946
|
-
callee.property.name : null;
|
|
2008
|
+
let method = (callee.type === "MemberExpression")? member_step(callee).prop : null;
|
|
1947
2009
|
if(method === "call" || method === "apply") {
|
|
1948
2010
|
let inner = callee_of(callee.object, scope);
|
|
1949
2011
|
if(inner) {
|
|
@@ -1987,8 +2049,7 @@ function call_target(call, scope)
|
|
|
1987
2049
|
function bind_kind(call, scope, table = I18N_KEY_FUNCTIONS)
|
|
1988
2050
|
{
|
|
1989
2051
|
let c = call.callee;
|
|
1990
|
-
if(c.type !== "MemberExpression" || c.
|
|
1991
|
-
|| c.property.name !== "bind" || call.arguments.length < 2) {
|
|
2052
|
+
if(c.type !== "MemberExpression" || member_step(c).prop !== "bind" || call.arguments.length < 2) {
|
|
1992
2053
|
return null;
|
|
1993
2054
|
}
|
|
1994
2055
|
let inner = callee_of(c.object, scope);
|
|
@@ -2093,6 +2154,9 @@ function is_translated_text(n, scope, depth = 0)
|
|
|
2093
2154
|
if(!n) {
|
|
2094
2155
|
return false;
|
|
2095
2156
|
}
|
|
2157
|
+
if(READ_AT.length === 0) {
|
|
2158
|
+
return from_read(n, scope, () => is_translated_text(n, scope, depth));
|
|
2159
|
+
}
|
|
2096
2160
|
if(depth > MAX_DEPTH) {
|
|
2097
2161
|
give_up(n, scope, GIVE_UP_DEEP);
|
|
2098
2162
|
return false;
|
|
@@ -2206,17 +2270,23 @@ function is_key_function(target)
|
|
|
2206
2270
|
***************************/
|
|
2207
2271
|
|
|
2208
2272
|
|
|
2209
|
-
/* How
|
|
2210
|
-
*
|
|
2211
|
-
*
|
|
2212
|
-
*
|
|
2273
|
+
/* How many STEPS a value is read through (a cycle stops here too), and
|
|
2274
|
+
* how many values one expression may take. Past either the reader GIVES
|
|
2275
|
+
* UP, and says so (give_up()): a key behind a value it does not follow
|
|
2276
|
+
* would go unasked, and the build say OK */
|
|
2213
2277
|
const MAX_DEPTH = 24;
|
|
2214
2278
|
const MAX_VALUES = 10000;
|
|
2215
2279
|
|
|
2280
|
+
/* How far an alias of a FUNCTION (callee_of()) or of a published list
|
|
2281
|
+
* (list_mutations()) is followed past MAX_DEPTH, to see what it ends
|
|
2282
|
+
* in: only an end that can be a translator, a key function or a list
|
|
2283
|
+
* makes the reader give up. Past this, the end is not known */
|
|
2284
|
+
const MAX_FOLLOW = 1000;
|
|
2285
|
+
|
|
2216
2286
|
/***************************************************************
|
|
2217
|
-
* Where the reader GAVE UP on a value:
|
|
2218
|
-
* MAX_DEPTH, a const that leads back to itself, an expression
|
|
2219
|
-
* can take more than MAX_VALUES values. A key behind it would go
|
|
2287
|
+
* Where the reader GAVE UP on a value: one read through more than
|
|
2288
|
+
* MAX_DEPTH steps, a const that leads back to itself, an expression
|
|
2289
|
+
* that can take more than MAX_VALUES values. A key behind it would go
|
|
2220
2290
|
* unasked, so it is a violation, with the file and the line
|
|
2221
2291
|
* (collect_used_keys(); the library's own test holds the library to
|
|
2222
2292
|
* none). Up to 7.26.42 the reader stopped there in silence: a chain
|
|
@@ -2224,43 +2294,167 @@ const MAX_VALUES = 10000;
|
|
|
2224
2294
|
* that doubles at every step (`{...O, ...O}`) grew without end and
|
|
2225
2295
|
* died with a RangeError naming no file.
|
|
2226
2296
|
*
|
|
2297
|
+
* It is said at the place the value is READ (READ_AT, the outermost
|
|
2298
|
+
* read in progress), with the node it went past: up to gobj-ui
|
|
2299
|
+
* 7.26.44 it was said at the deepest link only, which can be in
|
|
2300
|
+
* another file than the read that needed it.
|
|
2301
|
+
*
|
|
2227
2302
|
* `node` is the value it gave up on, `scope` the chain of its
|
|
2228
2303
|
* ancestors (the first one, the module: where it is said).
|
|
2229
2304
|
***************************************************************/
|
|
2230
|
-
const GAVE_UP = new Map(); //
|
|
2305
|
+
const GAVE_UP = new Map(); // the read -> {node, scope, why, past: {node, scope} | null}
|
|
2306
|
+
const READ_AT = []; // the reads in progress, the outermost first: {node, scope}
|
|
2231
2307
|
|
|
2232
|
-
const GIVE_UP_DEEP = `it is more than ${MAX_DEPTH} steps
|
|
2233
|
-
`
|
|
2308
|
+
const GIVE_UP_DEEP = `it is read through more than ${MAX_DEPTH} steps (each const, alias, call, ` +
|
|
2309
|
+
`property, element and branch of ?: / || / ?? it goes through is one): shorten the chain`;
|
|
2234
2310
|
const GIVE_UP_CYCLE = `it is a const that leads back to itself: break the cycle`;
|
|
2235
2311
|
const GIVE_UP_MANY = `it can take more than ${MAX_VALUES} values: split it`;
|
|
2236
2312
|
|
|
2237
2313
|
function give_up(node, scope, why)
|
|
2238
2314
|
{
|
|
2239
|
-
|
|
2240
|
-
|
|
2315
|
+
let read = READ_AT[0] || {node, scope};
|
|
2316
|
+
if(read.node && !GAVE_UP.has(read.node)) {
|
|
2317
|
+
/* A scope may be a walker's own chain, which goes on changing: copied */
|
|
2318
|
+
GAVE_UP.set(read.node, {node: read.node, scope: [...(read.scope || [])], why,
|
|
2319
|
+
past: (node && node !== read.node)? {node, scope: [...(scope || [])]} : null});
|
|
2320
|
+
}
|
|
2321
|
+
}
|
|
2322
|
+
|
|
2323
|
+
/* Run a read, as the READ every give_up() inside it is said at --
|
|
2324
|
+
* when no read is in progress already */
|
|
2325
|
+
function from_read(node, scope, fn)
|
|
2326
|
+
{
|
|
2327
|
+
if(READ_AT.length > 0) {
|
|
2328
|
+
return fn();
|
|
2329
|
+
}
|
|
2330
|
+
READ_AT.push({node, scope});
|
|
2331
|
+
try {
|
|
2332
|
+
return fn();
|
|
2333
|
+
} finally {
|
|
2334
|
+
READ_AT.pop();
|
|
2241
2335
|
}
|
|
2242
2336
|
}
|
|
2243
2337
|
|
|
2244
|
-
/* Where the reader gave up so far --
|
|
2245
|
-
* given: [{url, line, text, why}] */
|
|
2338
|
+
/* Where the reader gave up so far -- on reads of one parsed file, when
|
|
2339
|
+
* it is given: [{url, line, text, why, past: {url, line, text} | null}] */
|
|
2246
2340
|
function gave_up(ast = null)
|
|
2247
2341
|
{
|
|
2248
|
-
let
|
|
2249
|
-
for(let {node, scope, why} of GAVE_UP.values()) {
|
|
2342
|
+
let where = (node, scope) => {
|
|
2250
2343
|
let program = (scope.length > 0 && typeof scope[0].code === "string")? scope[0] : null;
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
}
|
|
2254
|
-
out.push({
|
|
2344
|
+
return {
|
|
2345
|
+
program,
|
|
2255
2346
|
url: program? (PROGRAM_URL.get(program) || null) : null,
|
|
2256
2347
|
line: program? line_of(program, node.start) : 0,
|
|
2257
|
-
text: program? program.code.slice(node.start, node.end).replace(/\s+/g, " ").slice(0, 80) : ""
|
|
2258
|
-
|
|
2259
|
-
|
|
2348
|
+
text: program? program.code.slice(node.start, node.end).replace(/\s+/g, " ").slice(0, 80) : ""
|
|
2349
|
+
};
|
|
2350
|
+
};
|
|
2351
|
+
let out = [];
|
|
2352
|
+
for(let {node, scope, why, past} of GAVE_UP.values()) {
|
|
2353
|
+
let at = where(node, scope);
|
|
2354
|
+
if(ast && at.program !== ast) {
|
|
2355
|
+
continue;
|
|
2356
|
+
}
|
|
2357
|
+
let beyond = past? where(past.node, past.scope) : null;
|
|
2358
|
+
out.push({url: at.url, line: at.line, text: at.text, why,
|
|
2359
|
+
past: beyond? {url: beyond.url, line: beyond.line, text: beyond.text} : null});
|
|
2260
2360
|
}
|
|
2261
2361
|
return out;
|
|
2262
2362
|
}
|
|
2263
2363
|
|
|
2364
|
+
/***************************************************************
|
|
2365
|
+
* The DEPTH of a read of expand(), and the values kept for reuse.
|
|
2366
|
+
*
|
|
2367
|
+
* A read counts its steps from the place the value is read: a const,
|
|
2368
|
+
* a property, an element, the returns of a function called, each
|
|
2369
|
+
* read ONCE and kept, keep the steps they took beside their values
|
|
2370
|
+
* (`height`), so a later read of them adds those steps to its own.
|
|
2371
|
+
* The verdict on a read is the same whatever was read before it: up
|
|
2372
|
+
* to gobj-ui 7.26.44 a const kept the values of its FIRST read, cut
|
|
2373
|
+
* at the depth that read had reached, so `t(J16); t(K10)` failed and
|
|
2374
|
+
* `t(K10); t(J16)` passed. A read that goes past MAX_DEPTH is given
|
|
2375
|
+
* up as a whole (TooDeep unwinds it to its outermost frame), and no
|
|
2376
|
+
* value read on the way is kept: each is kept only when it was read
|
|
2377
|
+
* to its end.
|
|
2378
|
+
***************************************************************/
|
|
2379
|
+
const FRAMES = []; // the reads in progress, outermost first: {max: the deepest step reached}
|
|
2380
|
+
|
|
2381
|
+
class TooDeep
|
|
2382
|
+
{
|
|
2383
|
+
constructor(node, scope)
|
|
2384
|
+
{
|
|
2385
|
+
this.node = node;
|
|
2386
|
+
this.scope = scope;
|
|
2387
|
+
}
|
|
2388
|
+
}
|
|
2389
|
+
|
|
2390
|
+
function note_depth(depth)
|
|
2391
|
+
{
|
|
2392
|
+
if(FRAMES.length > 0 && depth > FRAMES[FRAMES.length - 1].max) {
|
|
2393
|
+
FRAMES[FRAMES.length - 1].max = depth;
|
|
2394
|
+
}
|
|
2395
|
+
}
|
|
2396
|
+
|
|
2397
|
+
/* Is a step past MAX_DEPTH? Inside a read it unwinds the read (TooDeep);
|
|
2398
|
+
* outside one, it is given up here and the caller stops: true */
|
|
2399
|
+
function too_deep(depth, node, scope)
|
|
2400
|
+
{
|
|
2401
|
+
note_depth(depth);
|
|
2402
|
+
if(depth <= MAX_DEPTH) {
|
|
2403
|
+
return false;
|
|
2404
|
+
}
|
|
2405
|
+
if(FRAMES.length > 0) {
|
|
2406
|
+
throw new TooDeep(node, scope);
|
|
2407
|
+
}
|
|
2408
|
+
give_up(node, scope, GIVE_UP_DEEP);
|
|
2409
|
+
return true;
|
|
2410
|
+
}
|
|
2411
|
+
|
|
2412
|
+
/* The OUTERMOST read of expand(): the one a TooDeep unwinds to, given
|
|
2413
|
+
* up there (`fallback()` is what it answers then) */
|
|
2414
|
+
function outermost_read(node, scope, fn, fallback)
|
|
2415
|
+
{
|
|
2416
|
+
return from_read(node, scope, () => {
|
|
2417
|
+
FRAMES.push({max: 0});
|
|
2418
|
+
try {
|
|
2419
|
+
return fn();
|
|
2420
|
+
} catch(e) {
|
|
2421
|
+
if(!(e instanceof TooDeep)) {
|
|
2422
|
+
throw e;
|
|
2423
|
+
}
|
|
2424
|
+
give_up(e.node, e.scope, GIVE_UP_DEEP);
|
|
2425
|
+
return fallback();
|
|
2426
|
+
} finally {
|
|
2427
|
+
FRAMES.pop();
|
|
2428
|
+
}
|
|
2429
|
+
});
|
|
2430
|
+
}
|
|
2431
|
+
|
|
2432
|
+
/* What `compute()` answers for `key` of `cache`, read once and kept
|
|
2433
|
+
* with its height, read at `depth` by `node` (in `scope`) */
|
|
2434
|
+
function kept_values(cache, key, depth, node, scope, compute, fallback)
|
|
2435
|
+
{
|
|
2436
|
+
let known = cache.get(key);
|
|
2437
|
+
if(known) {
|
|
2438
|
+
too_deep(depth + known.height, node, scope);
|
|
2439
|
+
return known.values;
|
|
2440
|
+
}
|
|
2441
|
+
if(FRAMES.length === 0) {
|
|
2442
|
+
return outermost_read(node, scope, () => kept_values(cache, key, depth, node, scope, compute, fallback),
|
|
2443
|
+
fallback);
|
|
2444
|
+
}
|
|
2445
|
+
let frame = {max: depth};
|
|
2446
|
+
FRAMES.push(frame);
|
|
2447
|
+
let values;
|
|
2448
|
+
try {
|
|
2449
|
+
values = compute();
|
|
2450
|
+
} finally {
|
|
2451
|
+
FRAMES.pop();
|
|
2452
|
+
}
|
|
2453
|
+
note_depth(frame.max);
|
|
2454
|
+
cache.set(key, {values, height: frame.max - depth});
|
|
2455
|
+
return values;
|
|
2456
|
+
}
|
|
2457
|
+
|
|
2264
2458
|
/***************************************************************
|
|
2265
2459
|
* The values an expression can take, as far as the source writes
|
|
2266
2460
|
* them out. Each is one of
|
|
@@ -2325,10 +2519,15 @@ function gave_up(ast = null)
|
|
|
2325
2519
|
***************************************************************/
|
|
2326
2520
|
function expand(node, ctx, depth = 0)
|
|
2327
2521
|
{
|
|
2522
|
+
let unknown = () => [{kind: "unknown", node, scope: ctx.scope, via: ctx.off? ctx.via : [...ctx.via, node],
|
|
2523
|
+
off: ctx.off}];
|
|
2524
|
+
if(FRAMES.length === 0) {
|
|
2525
|
+
return outermost_read(node, ctx.scope, () => expand(node, ctx, depth), unknown);
|
|
2526
|
+
}
|
|
2328
2527
|
let out = expand_node(node, ctx, depth);
|
|
2329
2528
|
if(out.length > MAX_VALUES) {
|
|
2330
2529
|
give_up(node, ctx.scope, GIVE_UP_MANY);
|
|
2331
|
-
return
|
|
2530
|
+
return unknown();
|
|
2332
2531
|
}
|
|
2333
2532
|
return out;
|
|
2334
2533
|
}
|
|
@@ -2341,8 +2540,7 @@ function expand_node(node, ctx, depth)
|
|
|
2341
2540
|
let via = ctx.off? ctx.via : [...ctx.via, node];
|
|
2342
2541
|
let here = {scope: ctx.scope, via: via, off: ctx.off};
|
|
2343
2542
|
let item = (kind, extra) => Object.assign({kind, node, scope: ctx.scope, via, off: ctx.off}, extra);
|
|
2344
|
-
if(depth
|
|
2345
|
-
give_up(node, ctx.scope, GIVE_UP_DEEP);
|
|
2543
|
+
if(too_deep(depth, node, ctx.scope)) {
|
|
2346
2544
|
return [item("unknown")];
|
|
2347
2545
|
}
|
|
2348
2546
|
|
|
@@ -2374,7 +2572,7 @@ function expand_node(node, ctx, depth)
|
|
|
2374
2572
|
return [item("unknown")];
|
|
2375
2573
|
}
|
|
2376
2574
|
if(b.kind === "const") {
|
|
2377
|
-
return const_values(b, node, via, depth);
|
|
2575
|
+
return const_values(b, node, ctx.scope, via, depth);
|
|
2378
2576
|
}
|
|
2379
2577
|
if(b.kind === "each") {
|
|
2380
2578
|
let out = elements_of(b.of, {scope: b.scope, via: via, off: true}, depth + 1);
|
|
@@ -2396,7 +2594,7 @@ function expand_node(node, ctx, depth)
|
|
|
2396
2594
|
if(b.kind === "import") {
|
|
2397
2595
|
let x = import_target(b);
|
|
2398
2596
|
if(x && x.kind === "const") {
|
|
2399
|
-
return const_values(x, node, via, depth);
|
|
2597
|
+
return const_values(x, node, ctx.scope, via, depth);
|
|
2400
2598
|
}
|
|
2401
2599
|
}
|
|
2402
2600
|
return [item("unknown", {binding: b})];
|
|
@@ -2423,7 +2621,7 @@ function expand_node(node, ctx, depth)
|
|
|
2423
2621
|
&& node.arguments.length === 1 && binding_of("String", ctx.scope) === null) {
|
|
2424
2622
|
return expand(node.arguments[0], here, depth + 1);
|
|
2425
2623
|
}
|
|
2426
|
-
let r = local_call_values(node, ctx, via);
|
|
2624
|
+
let r = local_call_values(node, ctx, via, depth);
|
|
2427
2625
|
if(r && r.items) {
|
|
2428
2626
|
return r.items;
|
|
2429
2627
|
}
|
|
@@ -2446,37 +2644,42 @@ function expand_node(node, ctx, depth)
|
|
|
2446
2644
|
* The values of a const (a binding of binding_of(), or a const
|
|
2447
2645
|
* module_export() found in another module): its value read where it
|
|
2448
2646
|
* is declared, the steps a destructuring reads from it, its default.
|
|
2647
|
+
* `node` reads it, in `scope`, at `depth`.
|
|
2449
2648
|
*
|
|
2450
|
-
* Its value is read ONCE, without repeats, and kept
|
|
2451
|
-
* where it is declared, so what
|
|
2452
|
-
*
|
|
2453
|
-
*
|
|
2454
|
-
* declaration). Up to 7.26.42 a const
|
|
2455
|
-
* so `const O1 = c()? O0 : {...O0,
|
|
2456
|
-
* times as long at every link (eleven
|
|
2457
|
-
* values until a RangeError. A const met
|
|
2458
|
-
* being read leads back to itself: the reader
|
|
2459
|
-
* (give_up()).
|
|
2649
|
+
* Its value is read ONCE, without repeats, and kept with the steps it
|
|
2650
|
+
* took (kept_values()): a const is read where it is declared, so what
|
|
2651
|
+
* it holds is the same wherever it is read -- only `via`, the place it
|
|
2652
|
+
* is read from, differs, and it is the same for every value (the
|
|
2653
|
+
* reader leaves the place for the declaration). Up to 7.26.42 a const
|
|
2654
|
+
* was read again at every read, so `const O1 = c()? O0 : {...O0,
|
|
2655
|
+
* ...O0}`, chained, took three times as long at every link (eleven
|
|
2656
|
+
* links, 906 ms) and grew its values until a RangeError. A const met
|
|
2657
|
+
* again while its value is being read leads back to itself: the reader
|
|
2658
|
+
* gives up on it (give_up()).
|
|
2460
2659
|
***************************************************************/
|
|
2461
|
-
const CONST_VALUES = new WeakMap(); // the init of a const ->
|
|
2660
|
+
const CONST_VALUES = new WeakMap(); // the init of a const -> {values, height}, `via` left out
|
|
2462
2661
|
const CONST_READING = new Set(); // the inits being read
|
|
2463
2662
|
|
|
2464
|
-
function const_values(b, node, via, depth)
|
|
2663
|
+
function const_values(b, node, scope, via, depth)
|
|
2465
2664
|
{
|
|
2466
2665
|
let ctx = {scope: b.scope, via: via, off: true};
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2666
|
+
let unknown = () => [{kind: "unknown", node: b.init, scope: b.scope, via, off: true}];
|
|
2667
|
+
if(!CONST_VALUES.has(b.init) && CONST_READING.has(b.init)) {
|
|
2668
|
+
give_up(b.init, b.scope, GIVE_UP_CYCLE);
|
|
2669
|
+
return unknown();
|
|
2670
|
+
}
|
|
2671
|
+
let values = kept_values(CONST_VALUES, b.init, depth, node, scope, () => {
|
|
2472
2672
|
CONST_READING.add(b.init);
|
|
2473
2673
|
try {
|
|
2474
|
-
|
|
2674
|
+
return unique_values(expand(b.init, {scope: b.scope, via: [], off: true}, depth + 1));
|
|
2475
2675
|
} finally {
|
|
2476
2676
|
CONST_READING.delete(b.init);
|
|
2477
2677
|
}
|
|
2678
|
+
}, () => null);
|
|
2679
|
+
if(values === null) {
|
|
2680
|
+
return unknown();
|
|
2478
2681
|
}
|
|
2479
|
-
let out =
|
|
2682
|
+
let out = values.map((v) => Object.assign({}, v, {via}));
|
|
2480
2683
|
for(let step of b.steps || []) {
|
|
2481
2684
|
out = out.flatMap((o) => step_values(o, step, node, depth + 1));
|
|
2482
2685
|
}
|
|
@@ -2517,7 +2720,7 @@ function member_values(node, step, ctx, via, item, depth)
|
|
|
2517
2720
|
let ns = import_target(b);
|
|
2518
2721
|
let x = (ns && ns.kind === "namespace")? module_export(ns.url, step.prop) : null;
|
|
2519
2722
|
if(x && x.kind === "const") {
|
|
2520
|
-
return const_values(x, node, via, depth);
|
|
2723
|
+
return const_values(x, node, ctx.scope, via, depth);
|
|
2521
2724
|
}
|
|
2522
2725
|
return [item("unknown")];
|
|
2523
2726
|
}
|
|
@@ -2542,13 +2745,13 @@ const KNOWN = new Set(["key", "nothing", "dom"]);
|
|
|
2542
2745
|
* does not read, an async function, a generator, a
|
|
2543
2746
|
* function being read already (a recursion)
|
|
2544
2747
|
***************************************************************/
|
|
2545
|
-
function local_call_values(call, ctx, via)
|
|
2748
|
+
function local_call_values(call, ctx, via, depth = 0)
|
|
2546
2749
|
{
|
|
2547
2750
|
let c = call_kind(call, ctx.scope);
|
|
2548
2751
|
if(!c || !c.target || c.target.kind !== "fn") {
|
|
2549
2752
|
return null;
|
|
2550
2753
|
}
|
|
2551
|
-
let r = fn_returns(c.target);
|
|
2754
|
+
let r = fn_returns(c.target, call, ctx.scope, depth);
|
|
2552
2755
|
if(!r) {
|
|
2553
2756
|
return null;
|
|
2554
2757
|
}
|
|
@@ -2575,47 +2778,48 @@ function local_call_values(call, ctx, via)
|
|
|
2575
2778
|
* of it, `label_of(n)` calling itself in two branches took more
|
|
2576
2779
|
* than two minutes, and in three it never ended (REV-CLOUD V22).
|
|
2577
2780
|
* A function that is not a plain one (async, a generator) is not
|
|
2578
|
-
* followed.
|
|
2781
|
+
* followed. A call is a step of the read that makes it, and the
|
|
2782
|
+
* returns are read one step further (kept_values(): the steps they
|
|
2783
|
+
* take are kept with them); up to gobj-ui 7.26.44 a call started
|
|
2784
|
+
* the count again, so thirty nested calls were read through.
|
|
2785
|
+
* `call` makes it, in `scope`, at `depth`.
|
|
2579
2786
|
***************************************************************/
|
|
2580
|
-
const FN_RETURNS = new WeakMap();
|
|
2787
|
+
const FN_RETURNS = new WeakMap(); // a function -> {values: {items, complete, keys}, height}
|
|
2581
2788
|
const FN_READING = new Set();
|
|
2582
2789
|
|
|
2583
|
-
function fn_returns(target)
|
|
2790
|
+
function fn_returns(target, call, scope, depth)
|
|
2584
2791
|
{
|
|
2585
2792
|
let fn = target.fn;
|
|
2586
|
-
if(FN_RETURNS.has(fn)) {
|
|
2587
|
-
return FN_RETURNS.get(fn);
|
|
2588
|
-
}
|
|
2589
|
-
if(FN_READING.has(fn) || fn.async || fn.generator) {
|
|
2793
|
+
if(!FN_RETURNS.has(fn) && (FN_READING.has(fn) || fn.async || fn.generator)) {
|
|
2590
2794
|
return null;
|
|
2591
2795
|
}
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
let
|
|
2597
|
-
|
|
2598
|
-
|
|
2599
|
-
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
let leaves = items.map(classify);
|
|
2603
|
-
let keys = new Set();
|
|
2604
|
-
for(let l of leaves) {
|
|
2605
|
-
if(l.kind === "key") {
|
|
2606
|
-
keys.add(l.value);
|
|
2796
|
+
return kept_values(FN_RETURNS, fn, depth, call, scope, () => {
|
|
2797
|
+
FN_READING.add(fn);
|
|
2798
|
+
let items = [];
|
|
2799
|
+
try {
|
|
2800
|
+
for(let r of returned_values(fn)) {
|
|
2801
|
+
let values = expand(r.node, {scope: [...target.scope, fn, ...r.path], via: [], off: true}, depth + 1);
|
|
2802
|
+
items = unique_values(items.concat(values));
|
|
2803
|
+
}
|
|
2804
|
+
} finally {
|
|
2805
|
+
FN_READING.delete(fn);
|
|
2607
2806
|
}
|
|
2608
|
-
|
|
2609
|
-
|
|
2807
|
+
let leaves = items.map(classify);
|
|
2808
|
+
let keys = new Set();
|
|
2809
|
+
for(let l of leaves) {
|
|
2810
|
+
if(l.kind === "key") {
|
|
2811
|
+
keys.add(l.value);
|
|
2812
|
+
}
|
|
2813
|
+
for(let k of l.partial || []) {
|
|
2814
|
+
keys.add(k);
|
|
2815
|
+
}
|
|
2610
2816
|
}
|
|
2611
|
-
|
|
2612
|
-
|
|
2613
|
-
|
|
2614
|
-
|
|
2615
|
-
|
|
2616
|
-
};
|
|
2617
|
-
FN_RETURNS.set(fn, result);
|
|
2618
|
-
return result;
|
|
2817
|
+
return {
|
|
2818
|
+
items: items,
|
|
2819
|
+
complete: leaves.length > 0 && leaves.every((l) => KNOWN.has(l.kind)),
|
|
2820
|
+
keys: [...keys]
|
|
2821
|
+
};
|
|
2822
|
+
}, () => null);
|
|
2619
2823
|
}
|
|
2620
2824
|
|
|
2621
2825
|
/***************************************************************
|
|
@@ -2756,10 +2960,11 @@ function assigned_properties(o, step, depth)
|
|
|
2756
2960
|
*
|
|
2757
2961
|
* An object reached from a declaration (`o.off`: every value it
|
|
2758
2962
|
* gives carries the same `via`) is read once per property, without
|
|
2759
|
-
* repeats, and kept -- as a const is
|
|
2760
|
-
* same object twice, `{...O, ...O}`,
|
|
2963
|
+
* repeats, and kept with the steps it took -- as a const is
|
|
2964
|
+
* (const_values()): a spread of the same object twice, `{...O, ...O}`,
|
|
2965
|
+
* gives its values once.
|
|
2761
2966
|
***************************************************************/
|
|
2762
|
-
const PROPERTY_VALUES = new WeakMap(); // an object literal -> Map(property ->
|
|
2967
|
+
const PROPERTY_VALUES = new WeakMap(); // an object literal -> Map(property -> {values, height}, `via` left out)
|
|
2763
2968
|
|
|
2764
2969
|
function property_values(o, name, depth)
|
|
2765
2970
|
{
|
|
@@ -2771,11 +2976,11 @@ function property_values(o, name, depth)
|
|
|
2771
2976
|
known = new Map();
|
|
2772
2977
|
PROPERTY_VALUES.set(o.node, known);
|
|
2773
2978
|
}
|
|
2774
|
-
|
|
2775
|
-
|
|
2776
|
-
|
|
2777
|
-
|
|
2778
|
-
return
|
|
2979
|
+
let bare = Object.assign({}, o, {via: []});
|
|
2980
|
+
let values = kept_values(known, name, depth, o.node, o.scope,
|
|
2981
|
+
() => capped(bare, unique_values(property_values_of(bare, name, depth))),
|
|
2982
|
+
() => [Object.assign({}, bare, {kind: "unknown", binding: null, var_root: null})]);
|
|
2983
|
+
return values.map((v) => Object.assign({}, v, {via: o.via}));
|
|
2779
2984
|
}
|
|
2780
2985
|
|
|
2781
2986
|
/* The values of a value, or the reader giving up on it when they are
|
|
@@ -2854,10 +3059,11 @@ function elements_of(node, ctx, depth)
|
|
|
2854
3059
|
* element of a rest parameter is another parameter: no position can
|
|
2855
3060
|
* name one, and the reader cannot follow it. The elements of an array
|
|
2856
3061
|
* literal (or of a `.map()` of one) reached from a declaration are
|
|
2857
|
-
* read once, without repeats, and kept
|
|
2858
|
-
* object are (property_values()): `[...A, ...A]`
|
|
3062
|
+
* read once, without repeats, and kept with the steps they took, as
|
|
3063
|
+
* the properties of an object are (property_values()): `[...A, ...A]`
|
|
3064
|
+
* gives A's once.
|
|
2859
3065
|
***************************************************************/
|
|
2860
|
-
const ELEMENT_VALUES = new WeakMap(); // an array literal or a .map() ->
|
|
3066
|
+
const ELEMENT_VALUES = new WeakMap(); // an array literal or a .map() -> {values, height}, `via` left out
|
|
2861
3067
|
|
|
2862
3068
|
function element_values(v, depth)
|
|
2863
3069
|
{
|
|
@@ -2868,11 +3074,11 @@ function element_values(v, depth)
|
|
|
2868
3074
|
if(!v.off) {
|
|
2869
3075
|
return capped(v, element_values_of(v, depth));
|
|
2870
3076
|
}
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
return
|
|
3077
|
+
let bare = Object.assign({}, v, {via: []});
|
|
3078
|
+
let values = kept_values(ELEMENT_VALUES, v.node, depth, v.node, v.scope,
|
|
3079
|
+
() => capped(bare, unique_values(element_values_of(bare, depth))),
|
|
3080
|
+
() => [Object.assign({}, bare, {kind: "unknown", binding: null, var_root: null})]);
|
|
3081
|
+
return values.map((x) => Object.assign({}, x, {via: v.via}));
|
|
2876
3082
|
}
|
|
2877
3083
|
|
|
2878
3084
|
function element_values_of(v, depth)
|
|
@@ -3159,6 +3365,9 @@ function values_at(call, path, scope = [], args = call.arguments, fallbacks = nu
|
|
|
3159
3365
|
if(!arg) {
|
|
3160
3366
|
return [];
|
|
3161
3367
|
}
|
|
3368
|
+
if(READ_AT.length === 0) {
|
|
3369
|
+
return from_read(arg, call_scope, () => values_at(call, path, scope, args, fallbacks));
|
|
3370
|
+
}
|
|
3162
3371
|
|
|
3163
3372
|
let ctx = {scope: call_scope, via: [call], off: false};
|
|
3164
3373
|
let cur = first[2]? elements_of(arg, ctx, 0) : expand(arg, ctx);
|
|
@@ -3340,8 +3549,7 @@ function assigned_values(binding, members = false)
|
|
|
3340
3549
|
let b = binding_of(id.name, chain);
|
|
3341
3550
|
return !!b && b.kind === "var" && b.declarators[0] === first;
|
|
3342
3551
|
};
|
|
3343
|
-
|
|
3344
|
-
let chain = [...ancestors, n];
|
|
3552
|
+
let visit = (n, chain) => {
|
|
3345
3553
|
let into = (node) => (members? steps_to(node, (id) => is_it(id, chain)) : null);
|
|
3346
3554
|
/* `n` writes `target` with a value the reader does not read */
|
|
3347
3555
|
let opaque_write = (target) => {
|
|
@@ -3455,10 +3663,109 @@ function assigned_values(binding, members = false)
|
|
|
3455
3663
|
default:
|
|
3456
3664
|
break;
|
|
3457
3665
|
}
|
|
3458
|
-
}
|
|
3666
|
+
};
|
|
3667
|
+
let sites = write_sites(root);
|
|
3668
|
+
let named = sites.by_name.get(binding.name) || [];
|
|
3669
|
+
let prefix = binding.scope.slice(0, -1);
|
|
3670
|
+
for(let i = 0, j = 0; i < named.length || j < sites.evals.length;) {
|
|
3671
|
+
let take_named = j >= sites.evals.length || (i < named.length && named[i].order < sites.evals[j].order);
|
|
3672
|
+
let site = take_named? named[i++] : sites.evals[j++];
|
|
3673
|
+
visit(site.n, prefix.concat(site.chain));
|
|
3674
|
+
}
|
|
3459
3675
|
return {values, unknown};
|
|
3460
3676
|
}
|
|
3461
3677
|
|
|
3678
|
+
/***************************************************************
|
|
3679
|
+
* The nodes of a scope that can WRITE a variable, by the name of
|
|
3680
|
+
* the variable a write is rooted at (`k = v`, `k.a = v`, `[k] = v`,
|
|
3681
|
+
* `k++`, `k.push(v)`, `Object.assign(k, v)`, `for(k of v)`, a
|
|
3682
|
+
* declarator of `k`), and every direct `eval()`, which can write
|
|
3683
|
+
* any: the only nodes assigned_values() reads. Found in ONE walk of
|
|
3684
|
+
* the scope, and kept: up to gobj-ui 7.26.44 the scope was walked
|
|
3685
|
+
* again for every variable read in it, so a module of 1600 `let`s
|
|
3686
|
+
* took 4 s, and twice as many four times as long.
|
|
3687
|
+
*
|
|
3688
|
+
* -> {by_name: Map(name -> [site]), evals: [site]}, each list in
|
|
3689
|
+
* the order of the source; site: {n, chain, order}, `chain`
|
|
3690
|
+
* the nodes from the scope to `n`, both included
|
|
3691
|
+
***************************************************************/
|
|
3692
|
+
const WRITE_SITES = new WeakMap(); // a scope -> its write sites
|
|
3693
|
+
|
|
3694
|
+
function write_sites(root)
|
|
3695
|
+
{
|
|
3696
|
+
let known = WRITE_SITES.get(root);
|
|
3697
|
+
if(known) {
|
|
3698
|
+
return known;
|
|
3699
|
+
}
|
|
3700
|
+
let by_name = new Map();
|
|
3701
|
+
let evals = [];
|
|
3702
|
+
let order = 0;
|
|
3703
|
+
let base_name = (target) => {
|
|
3704
|
+
while(target && target.type === "MemberExpression") {
|
|
3705
|
+
target = target.object;
|
|
3706
|
+
}
|
|
3707
|
+
return (target && target.type === "Identifier")? target.name : null;
|
|
3708
|
+
};
|
|
3709
|
+
walk(root, (n, ancestors) => {
|
|
3710
|
+
let names = [];
|
|
3711
|
+
switch(n.type) {
|
|
3712
|
+
case "VariableDeclarator":
|
|
3713
|
+
pattern_names(n.id, names);
|
|
3714
|
+
break;
|
|
3715
|
+
case "AssignmentExpression":
|
|
3716
|
+
if(n.left.type === "Identifier" || n.left.type === "MemberExpression") {
|
|
3717
|
+
names.push(base_name(n.left));
|
|
3718
|
+
} else {
|
|
3719
|
+
names.push(...write_targets(n.left).map(base_name));
|
|
3720
|
+
}
|
|
3721
|
+
break;
|
|
3722
|
+
case "UpdateExpression":
|
|
3723
|
+
names.push(base_name(n.argument));
|
|
3724
|
+
break;
|
|
3725
|
+
case "CallExpression": {
|
|
3726
|
+
if(n.callee.type === "Identifier" && n.callee.name === "eval" && !n.optional) {
|
|
3727
|
+
evals.push({n, chain: [...ancestors, n], order: order++});
|
|
3728
|
+
return;
|
|
3729
|
+
}
|
|
3730
|
+
let c = n.callee;
|
|
3731
|
+
if(c.type === "MemberExpression" && !c.computed && c.property.type === "Identifier"
|
|
3732
|
+
&& ELEMENT_WRITERS.has(c.property.name)) {
|
|
3733
|
+
names.push(base_name(c.object));
|
|
3734
|
+
} else if(is_object_call(n, "assign") && n.arguments.length > 0) {
|
|
3735
|
+
names.push(base_name(n.arguments[0]));
|
|
3736
|
+
}
|
|
3737
|
+
break;
|
|
3738
|
+
}
|
|
3739
|
+
case "ForOfStatement":
|
|
3740
|
+
case "ForInStatement":
|
|
3741
|
+
if(n.left.type === "VariableDeclaration") {
|
|
3742
|
+
for(let d of n.left.declarations) {
|
|
3743
|
+
pattern_names(d.id, names);
|
|
3744
|
+
}
|
|
3745
|
+
} else {
|
|
3746
|
+
names.push(...write_targets(n.left).map(base_name));
|
|
3747
|
+
}
|
|
3748
|
+
break;
|
|
3749
|
+
default:
|
|
3750
|
+
return;
|
|
3751
|
+
}
|
|
3752
|
+
let site = null;
|
|
3753
|
+
for(let name of new Set(names)) {
|
|
3754
|
+
if(name === null) {
|
|
3755
|
+
continue;
|
|
3756
|
+
}
|
|
3757
|
+
site = site || {n, chain: [...ancestors, n], order: order++};
|
|
3758
|
+
if(!by_name.has(name)) {
|
|
3759
|
+
by_name.set(name, []);
|
|
3760
|
+
}
|
|
3761
|
+
by_name.get(name).push(site);
|
|
3762
|
+
}
|
|
3763
|
+
});
|
|
3764
|
+
known = {by_name, evals};
|
|
3765
|
+
WRITE_SITES.set(root, known);
|
|
3766
|
+
return known;
|
|
3767
|
+
}
|
|
3768
|
+
|
|
3462
3769
|
/***************************************************************
|
|
3463
3770
|
* The leaves of what is read from a `let` / `var` -- the variable
|
|
3464
3771
|
* itself, or a member or an element of it (`root`, a var_root of
|
|
@@ -3479,6 +3786,9 @@ function assigned_values(binding, members = false)
|
|
|
3479
3786
|
***************************************************************/
|
|
3480
3787
|
function var_leaves(root, seen = new Set(), depth = 0)
|
|
3481
3788
|
{
|
|
3789
|
+
if(READ_AT.length === 0) {
|
|
3790
|
+
return from_read(root.binding.declarators[0], root.binding.scope, () => var_leaves(root, seen, depth));
|
|
3791
|
+
}
|
|
3482
3792
|
let b = root.binding;
|
|
3483
3793
|
let steps = root.steps;
|
|
3484
3794
|
let out = {leaves: [], complete: true, written: false, unread: [], opaque: [], lets: [b]};
|
|
@@ -3642,6 +3952,9 @@ function literal_values(n, scope, seen, depth = 0)
|
|
|
3642
3952
|
if(!n) {
|
|
3643
3953
|
return null;
|
|
3644
3954
|
}
|
|
3955
|
+
if(READ_AT.length === 0) {
|
|
3956
|
+
return from_read(n, scope, () => literal_values(n, scope, seen, depth));
|
|
3957
|
+
}
|
|
3645
3958
|
if(depth > MAX_DEPTH) {
|
|
3646
3959
|
give_up(n, scope, GIVE_UP_DEEP);
|
|
3647
3960
|
return null;
|
|
@@ -3775,6 +4088,9 @@ function param_flows(node, scope, seen = new Set(), depth = 0)
|
|
|
3775
4088
|
if(!node || typeof node.type !== "string") {
|
|
3776
4089
|
return [];
|
|
3777
4090
|
}
|
|
4091
|
+
if(READ_AT.length === 0) {
|
|
4092
|
+
return from_read(node, scope, () => param_flows(node, scope, seen, depth));
|
|
4093
|
+
}
|
|
3778
4094
|
if(depth > MAX_DEPTH) {
|
|
3779
4095
|
give_up(node, scope, GIVE_UP_DEEP);
|
|
3780
4096
|
return [];
|
|
@@ -4508,17 +4824,38 @@ function list_mutations(ast, lists = [])
|
|
|
4508
4824
|
/*
|
|
4509
4825
|
* The list a node names, as said in a message, or null: its name
|
|
4510
4826
|
* bound at the top of the module, a member of a namespace import,
|
|
4511
|
-
* or an alias of one of those
|
|
4827
|
+
* or an alias of one of those. A chain of aliases is followed past
|
|
4828
|
+
* MAX_DEPTH to its end, and the reader gives up only when a list is
|
|
4829
|
+
* on it: up to gobj-ui 7.26.44 thirty aliases of any array, written
|
|
4830
|
+
* into, failed a module that publishes a list. `far`: where it went
|
|
4831
|
+
* past MAX_DEPTH, where it lost the end (past MAX_FOLLOW, or round
|
|
4832
|
+
* a cycle of lets), and whether it met a list.
|
|
4512
4833
|
*/
|
|
4513
|
-
let list_of = (n, chain,
|
|
4834
|
+
let list_of = (n, chain) => from_read(n, chain, () => {
|
|
4835
|
+
let far = {past: null, lost: null, list: false, seen: new Set()};
|
|
4836
|
+
let r = list_at(n, chain, 0, far);
|
|
4837
|
+
if(far.lost && (far.list || far.lost.why !== GIVE_UP_CYCLE)) {
|
|
4838
|
+
give_up(far.lost.node, far.lost.scope, far.lost.why);
|
|
4839
|
+
return null;
|
|
4840
|
+
}
|
|
4841
|
+
if(far.past && (r !== null || far.list)) {
|
|
4842
|
+
give_up(far.past.node, far.past.scope, GIVE_UP_DEEP);
|
|
4843
|
+
return null;
|
|
4844
|
+
}
|
|
4845
|
+
return r;
|
|
4846
|
+
});
|
|
4847
|
+
let list_at = (n, chain, depth, far) => {
|
|
4514
4848
|
while(n && n.type === "ChainExpression") {
|
|
4515
4849
|
n = n.expression;
|
|
4516
4850
|
}
|
|
4517
4851
|
if(!n) {
|
|
4518
4852
|
return null;
|
|
4519
4853
|
}
|
|
4520
|
-
if(depth > MAX_DEPTH) {
|
|
4521
|
-
|
|
4854
|
+
if(depth > MAX_DEPTH && !far.past) {
|
|
4855
|
+
far.past = {node: n, scope: chain};
|
|
4856
|
+
}
|
|
4857
|
+
if(depth > MAX_FOLLOW) {
|
|
4858
|
+
far.lost = far.lost || {node: n, scope: chain, why: GIVE_UP_DEEP};
|
|
4522
4859
|
return null;
|
|
4523
4860
|
}
|
|
4524
4861
|
if(n.type === "MemberExpression") {
|
|
@@ -4527,7 +4864,11 @@ function list_mutations(ast, lists = [])
|
|
|
4527
4864
|
return null;
|
|
4528
4865
|
}
|
|
4529
4866
|
let b = binding_of(n.object.name, chain);
|
|
4530
|
-
|
|
4867
|
+
if(b && b.kind === "import" && b.imported === "*") {
|
|
4868
|
+
far.list = true;
|
|
4869
|
+
return `${n.object.name}.${step.prop}`;
|
|
4870
|
+
}
|
|
4871
|
+
return null;
|
|
4531
4872
|
}
|
|
4532
4873
|
if(n.type !== "Identifier") {
|
|
4533
4874
|
return null;
|
|
@@ -4537,10 +4878,17 @@ function list_mutations(ast, lists = [])
|
|
|
4537
4878
|
return null;
|
|
4538
4879
|
}
|
|
4539
4880
|
if(names.has(n.name) && b.scope.length === 1) {
|
|
4881
|
+
far.list = true;
|
|
4540
4882
|
return n.name;
|
|
4541
4883
|
}
|
|
4542
4884
|
if(b.kind === "const" && b.steps.length === 0) {
|
|
4543
|
-
|
|
4885
|
+
if(far.seen.has(b.init)) {
|
|
4886
|
+
far.lost = far.lost || {node: b.init, scope: b.scope, why: GIVE_UP_CYCLE};
|
|
4887
|
+
return null;
|
|
4888
|
+
}
|
|
4889
|
+
far.seen.add(b.init);
|
|
4890
|
+
let inner = list_at(b.init, b.scope, depth + 1, far);
|
|
4891
|
+
far.seen.delete(b.init);
|
|
4544
4892
|
return inner? `${inner} (through its alias \`${n.name}\`)` : null;
|
|
4545
4893
|
}
|
|
4546
4894
|
if(b.kind === "var" && b.picks.size === 0) {
|
|
@@ -4548,13 +4896,24 @@ function list_mutations(ast, lists = [])
|
|
|
4548
4896
|
* first value asked first: `let out = []` is no alias, and costs
|
|
4549
4897
|
* no walk of its scope) */
|
|
4550
4898
|
let first = b.declarators[0];
|
|
4551
|
-
|
|
4552
|
-
|
|
4899
|
+
if(far.seen.has(first)) {
|
|
4900
|
+
far.lost = far.lost || {node: first, scope: b.scope, why: GIVE_UP_CYCLE};
|
|
4553
4901
|
return null;
|
|
4554
4902
|
}
|
|
4555
|
-
|
|
4556
|
-
|
|
4557
|
-
|
|
4903
|
+
far.seen.add(first);
|
|
4904
|
+
let inner = null;
|
|
4905
|
+
try {
|
|
4906
|
+
inner = first.init? list_at(first.init, b.scope, depth + 1, far) : null;
|
|
4907
|
+
if(!inner) {
|
|
4908
|
+
return null;
|
|
4909
|
+
}
|
|
4910
|
+
let {values, unknown} = assigned_values(b);
|
|
4911
|
+
if(unknown.length > 0
|
|
4912
|
+
|| values.some((v) => !v.node || list_at(v.node, v.scope, depth + 1, far) !== inner)) {
|
|
4913
|
+
return null;
|
|
4914
|
+
}
|
|
4915
|
+
} finally {
|
|
4916
|
+
far.seen.delete(first);
|
|
4558
4917
|
}
|
|
4559
4918
|
return `${inner} (through its alias \`${n.name}\`)`;
|
|
4560
4919
|
}
|
|
@@ -4849,7 +5208,8 @@ function callback_leaves(call, callback, scope)
|
|
|
4849
5208
|
{
|
|
4850
5209
|
let via = [call, callback.arg];
|
|
4851
5210
|
if(callback.how === "elements") {
|
|
4852
|
-
return
|
|
5211
|
+
return from_read(callback.receiver, scope,
|
|
5212
|
+
() => elements_of(callback.receiver, {scope, via, off: false}, 0).map(classify));
|
|
4853
5213
|
}
|
|
4854
5214
|
return [{kind: "unknown", node: call, scope: scope.slice(0, -1), via, off: false}];
|
|
4855
5215
|
}
|
|
@@ -4861,15 +5221,17 @@ function callback_leaves(call, callback, scope)
|
|
|
4861
5221
|
***************************************************************/
|
|
4862
5222
|
function key_list_leaves(node, scope, root)
|
|
4863
5223
|
{
|
|
4864
|
-
|
|
4865
|
-
|
|
4866
|
-
|
|
4867
|
-
|
|
4868
|
-
|
|
4869
|
-
|
|
5224
|
+
return from_read(node, scope, () => {
|
|
5225
|
+
let out = [];
|
|
5226
|
+
for(let v of expand(node, {scope, via: [root], off: false})) {
|
|
5227
|
+
if(v.kind === "value" && v.node.type === "ArrayExpression") {
|
|
5228
|
+
out.push(...element_values(v, 0).map(classify));
|
|
5229
|
+
} else {
|
|
5230
|
+
out.push(classify(v));
|
|
5231
|
+
}
|
|
4870
5232
|
}
|
|
4871
|
-
|
|
4872
|
-
|
|
5233
|
+
return out;
|
|
5234
|
+
});
|
|
4873
5235
|
}
|
|
4874
5236
|
|
|
4875
5237
|
/***************************************************************
|
|
@@ -5400,7 +5762,14 @@ function collect_used_keys(opts)
|
|
|
5400
5762
|
for(let g of gave_up()) {
|
|
5401
5763
|
let module = lib_module_of(g.url);
|
|
5402
5764
|
let label = module? `gobj-ui/src/${module}` : g.url? relative(process.cwd(), fileURLToPath(g.url)) : "?";
|
|
5403
|
-
|
|
5765
|
+
let past = "";
|
|
5766
|
+
if(g.past) {
|
|
5767
|
+
let past_module = lib_module_of(g.past.url);
|
|
5768
|
+
let past_label = past_module? `gobj-ui/src/${past_module}` :
|
|
5769
|
+
g.past.url? relative(process.cwd(), fileURLToPath(g.past.url)) : "?";
|
|
5770
|
+
past = ` (past \`${g.past.text}\`, ${past_label}:${g.past.line})`;
|
|
5771
|
+
}
|
|
5772
|
+
fail(`${label}:${g.line}: the reader gave up on \`${g.text}\`${past}, and a key behind it would go ` +
|
|
5404
5773
|
`unasked: ${g.why}`);
|
|
5405
5774
|
}
|
|
5406
5775
|
|
|
@@ -5890,7 +6259,8 @@ export {
|
|
|
5890
6259
|
source_files,
|
|
5891
6260
|
lib_imports,
|
|
5892
6261
|
find_plural_groups,
|
|
5893
|
-
violation_count
|
|
6262
|
+
violation_count,
|
|
6263
|
+
Abort
|
|
5894
6264
|
};
|
|
5895
6265
|
|
|
5896
6266
|
/*
|
|
@@ -5907,5 +6277,11 @@ try {
|
|
|
5907
6277
|
is_main = false;
|
|
5908
6278
|
}
|
|
5909
6279
|
if(is_main) {
|
|
5910
|
-
|
|
6280
|
+
try {
|
|
6281
|
+
await main();
|
|
6282
|
+
} catch(e) {
|
|
6283
|
+
if(!(e instanceof Abort)) {
|
|
6284
|
+
throw e;
|
|
6285
|
+
}
|
|
6286
|
+
}
|
|
5911
6287
|
}
|