@blumintinc/eslint-plugin-blumint 1.20.106 → 1.20.107
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/lib/index.js
CHANGED
|
@@ -474,6 +474,142 @@ exports.parallelizeAsyncOperations = (0, createRule_1.createRule)({
|
|
|
474
474
|
}
|
|
475
475
|
return names;
|
|
476
476
|
}
|
|
477
|
+
/**
|
|
478
|
+
* Records the binding a single assignment target writes to.
|
|
479
|
+
*
|
|
480
|
+
* A member write records its ROOT object (`obj.a.b = 1` yields `obj`),
|
|
481
|
+
* because the state it mutates is reachable through that binding, and a
|
|
482
|
+
* later await naming the object observes the mutation. Optional chains and
|
|
483
|
+
* TS wrappers (`obj!.x = 1`) are unwrapped so the root is still found.
|
|
484
|
+
* Destructuring targets recurse to their leaf identifiers, since
|
|
485
|
+
* `({ a } = source)` and `[a] = source` write `a` just as `a = source.a`
|
|
486
|
+
* does.
|
|
487
|
+
*/
|
|
488
|
+
function collectAssignmentTarget(target, targets) {
|
|
489
|
+
switch (target.type) {
|
|
490
|
+
case utils_1.AST_NODE_TYPES.Identifier:
|
|
491
|
+
targets.push(target);
|
|
492
|
+
break;
|
|
493
|
+
case utils_1.AST_NODE_TYPES.MemberExpression: {
|
|
494
|
+
let root = target;
|
|
495
|
+
for (;;) {
|
|
496
|
+
if (root.type === utils_1.AST_NODE_TYPES.MemberExpression) {
|
|
497
|
+
root = root.object;
|
|
498
|
+
}
|
|
499
|
+
else if (root.type === utils_1.AST_NODE_TYPES.ChainExpression ||
|
|
500
|
+
root.type === utils_1.AST_NODE_TYPES.TSNonNullExpression ||
|
|
501
|
+
root.type === utils_1.AST_NODE_TYPES.TSAsExpression) {
|
|
502
|
+
root = root.expression;
|
|
503
|
+
}
|
|
504
|
+
else {
|
|
505
|
+
break;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
if (root.type === utils_1.AST_NODE_TYPES.Identifier) {
|
|
509
|
+
targets.push(root);
|
|
510
|
+
}
|
|
511
|
+
break;
|
|
512
|
+
}
|
|
513
|
+
case utils_1.AST_NODE_TYPES.ObjectPattern:
|
|
514
|
+
for (const property of target.properties) {
|
|
515
|
+
if (property.type === utils_1.AST_NODE_TYPES.Property) {
|
|
516
|
+
collectAssignmentTarget(property.value, targets);
|
|
517
|
+
}
|
|
518
|
+
else {
|
|
519
|
+
collectAssignmentTarget(property.argument, targets);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
break;
|
|
523
|
+
case utils_1.AST_NODE_TYPES.ArrayPattern:
|
|
524
|
+
for (const element of target.elements) {
|
|
525
|
+
if (element) {
|
|
526
|
+
collectAssignmentTarget(element, targets);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
break;
|
|
530
|
+
case utils_1.AST_NODE_TYPES.RestElement:
|
|
531
|
+
collectAssignmentTarget(target.argument, targets);
|
|
532
|
+
break;
|
|
533
|
+
case utils_1.AST_NODE_TYPES.AssignmentPattern:
|
|
534
|
+
collectAssignmentTarget(target.left, targets);
|
|
535
|
+
break;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
/**
|
|
539
|
+
* Reports whether an assignment target resolves to a binding DECLARED
|
|
540
|
+
* inside the given expression.
|
|
541
|
+
*
|
|
542
|
+
* Such a binding is a fresh local: `async () => { let tmp; tmp = 1; }`
|
|
543
|
+
* publishes nothing to the enclosing scope, so a later await mentioning
|
|
544
|
+
* `tmp` is reading some other binding entirely. An unresolved name (an
|
|
545
|
+
* implicit global) counts as external, which keeps the barrier in place for
|
|
546
|
+
* the case the analysis cannot see.
|
|
547
|
+
*/
|
|
548
|
+
function isDeclaredWithin(identifier, root) {
|
|
549
|
+
const variable = ASTHelpers_1.ASTHelpers.findVariableInScope(ASTHelpers_1.ASTHelpers.getScope(context, identifier), identifier.name);
|
|
550
|
+
if (!variable || variable.defs.length === 0) {
|
|
551
|
+
return false;
|
|
552
|
+
}
|
|
553
|
+
return variable.defs.every((definition) => definition.name.range[0] >= root.range[0] &&
|
|
554
|
+
definition.name.range[1] <= root.range[1]);
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* Collects the identifier names an awaited expression WRITES.
|
|
558
|
+
*
|
|
559
|
+
* The traversal deliberately crosses function boundaries, which is the
|
|
560
|
+
* opposite of what containsSuspendingAwait needs: the write that matters
|
|
561
|
+
* lives inside the callback handed to the awaited call. `await
|
|
562
|
+
* db.runTransaction(async (tx) => { mutator = new Mutator(tx); })` publishes
|
|
563
|
+
* `mutator` to the enclosing scope by the time it settles, so the callback
|
|
564
|
+
* body is part of what that statement does, not a separate deferred unit.
|
|
565
|
+
*
|
|
566
|
+
* A `VariableDeclarator` id is deliberately NOT a write: `const x = ...`
|
|
567
|
+
* inside a callback creates a fresh local binding rather than publishing a
|
|
568
|
+
* value to an outer one, so it cannot be what a later await reads.
|
|
569
|
+
*/
|
|
570
|
+
function getAssignedNames(node) {
|
|
571
|
+
const targets = [];
|
|
572
|
+
const visit = (current) => {
|
|
573
|
+
if (current.type === utils_1.AST_NODE_TYPES.AssignmentExpression) {
|
|
574
|
+
collectAssignmentTarget(current.left, targets);
|
|
575
|
+
}
|
|
576
|
+
else if (current.type === utils_1.AST_NODE_TYPES.UpdateExpression) {
|
|
577
|
+
collectAssignmentTarget(current.argument, targets);
|
|
578
|
+
}
|
|
579
|
+
else if ((current.type === utils_1.AST_NODE_TYPES.ForOfStatement ||
|
|
580
|
+
current.type === utils_1.AST_NODE_TYPES.ForInStatement) &&
|
|
581
|
+
current.left.type !== utils_1.AST_NODE_TYPES.VariableDeclaration) {
|
|
582
|
+
// `for (captured of items)` assigns an existing binding on every
|
|
583
|
+
// iteration; only the declaration form introduces a fresh local.
|
|
584
|
+
collectAssignmentTarget(current.left, targets);
|
|
585
|
+
}
|
|
586
|
+
for (const key in current) {
|
|
587
|
+
if (key === 'parent' || key === 'range' || key === 'loc')
|
|
588
|
+
continue;
|
|
589
|
+
const child = current[key];
|
|
590
|
+
if (!child || typeof child !== 'object')
|
|
591
|
+
continue;
|
|
592
|
+
if (Array.isArray(child)) {
|
|
593
|
+
for (const item of child) {
|
|
594
|
+
if (item && typeof item === 'object' && 'type' in item) {
|
|
595
|
+
visit(item);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
else if ('type' in child) {
|
|
600
|
+
visit(child);
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
};
|
|
604
|
+
visit(node);
|
|
605
|
+
const names = new Set();
|
|
606
|
+
for (const target of targets) {
|
|
607
|
+
if (!isDeclaredWithin(target, node)) {
|
|
608
|
+
names.add(target.name);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
return names;
|
|
612
|
+
}
|
|
477
613
|
/**
|
|
478
614
|
* Checks if there are dependencies between await expressions
|
|
479
615
|
*/
|
|
@@ -671,6 +807,37 @@ exports.parallelizeAsyncOperations = (0, createRule_1.createRule)({
|
|
|
671
807
|
return true;
|
|
672
808
|
}
|
|
673
809
|
}
|
|
810
|
+
// 10. Closure-write barrier: read after write. An awaited call whose
|
|
811
|
+
// callback ASSIGNS an outer binding carries a data dependency that no
|
|
812
|
+
// value flowing out of the await expresses, so `variableNames` -- which
|
|
813
|
+
// holds only the names the run's own statements DECLARE -- cannot see it.
|
|
814
|
+
// `let mutator; await db.runTransaction(async (tx) => { mutator = new
|
|
815
|
+
// Mutator(tx); }); await mutator?.deleteIfEmptied();` is the shape. The
|
|
816
|
+
// rewrite is silently wrong rather than merely eager: array elements
|
|
817
|
+
// evaluate left to right at construction time, so the second element runs
|
|
818
|
+
// while `mutator` is still undefined -- the optional chain short-circuits
|
|
819
|
+
// and the operation NEVER happens, with no error to reveal it. Dropping
|
|
820
|
+
// the `?.` turns the same rewrite into a TypeError instead. Keyed on a
|
|
821
|
+
// write that a LATER await actually reads, so a callback whose effects
|
|
822
|
+
// nothing downstream observes still parallelizes. (#1723)
|
|
823
|
+
const assignedNames = awaitNodes.map((node) => {
|
|
824
|
+
const awaitExpr = getAwaitExpression(node);
|
|
825
|
+
return awaitExpr
|
|
826
|
+
? getAssignedNames(awaitExpr.argument)
|
|
827
|
+
: new Set();
|
|
828
|
+
});
|
|
829
|
+
for (let i = 1; i < awaitNodes.length; i++) {
|
|
830
|
+
const currentIds = allIdentifiers[i];
|
|
831
|
+
if (currentIds.size === 0)
|
|
832
|
+
continue;
|
|
833
|
+
for (let j = 0; j < i; j++) {
|
|
834
|
+
for (const written of assignedNames[j]) {
|
|
835
|
+
if (currentIds.has(written)) {
|
|
836
|
+
return true;
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
}
|
|
674
841
|
return false;
|
|
675
842
|
}
|
|
676
843
|
/**
|
|
@@ -291,18 +291,29 @@ exports.parallelizeLoopAwaits = (0, createRule_1.createRule)({
|
|
|
291
291
|
return false;
|
|
292
292
|
}
|
|
293
293
|
/**
|
|
294
|
-
* Collects all variables declared INSIDE the loop body
|
|
295
|
-
*
|
|
296
|
-
* iteration
|
|
294
|
+
* Collects all variables declared INSIDE the loop body, including inside
|
|
295
|
+
* callbacks written there. These are iteration-local variables: nothing
|
|
296
|
+
* they hold outlives the iteration that created them.
|
|
297
|
+
*
|
|
298
|
+
* The walk crosses nested function boundaries because the write scan that
|
|
299
|
+
* consults this set crosses them too. A name both declared and assigned
|
|
300
|
+
* inside a callback (`async () => { let tmp; tmp = 1; }`) publishes nothing
|
|
301
|
+
* to the enclosing scope, so if the set stopped at the boundary the write
|
|
302
|
+
* would read as a cross-iteration dependency and silence the loop. (#1724)
|
|
297
303
|
*/
|
|
298
304
|
function collectLoopLocalVars(body) {
|
|
299
305
|
const localVars = new Set();
|
|
300
306
|
function visit(node, isRoot) {
|
|
307
|
+
// A callback's parameters bind afresh on every invocation, so a write
|
|
308
|
+
// through one (`async (page) => { page.total = 1 }`) reaches whatever
|
|
309
|
+
// the caller handed that call rather than state the iterations share.
|
|
301
310
|
if (!isRoot &&
|
|
302
311
|
(node.type === utils_1.AST_NODE_TYPES.FunctionDeclaration ||
|
|
303
312
|
node.type === utils_1.AST_NODE_TYPES.FunctionExpression ||
|
|
304
313
|
node.type === utils_1.AST_NODE_TYPES.ArrowFunctionExpression)) {
|
|
305
|
-
|
|
314
|
+
for (const param of node.params) {
|
|
315
|
+
collectBindingNames(param, localVars);
|
|
316
|
+
}
|
|
306
317
|
}
|
|
307
318
|
if (node.type === utils_1.AST_NODE_TYPES.VariableDeclaration) {
|
|
308
319
|
for (const declarator of node.declarations) {
|
|
@@ -333,14 +344,70 @@ exports.parallelizeLoopAwaits = (0, createRule_1.createRule)({
|
|
|
333
344
|
visit(body, true);
|
|
334
345
|
return localVars;
|
|
335
346
|
}
|
|
347
|
+
/**
|
|
348
|
+
* Collects the BINDINGS an assignment target writes through, returning
|
|
349
|
+
* false when the target's root is not a plain binding at all.
|
|
350
|
+
*
|
|
351
|
+
* A member write reaches the object its ROOT names: `box.value = 1` writes
|
|
352
|
+
* through `box`, and `value` is a field label that binds nothing — the same
|
|
353
|
+
* distinction drawn for a non-computed property key (#1688). Counting the
|
|
354
|
+
* label as a written name makes every member write look like a write to an
|
|
355
|
+
* outer binding, which is what hides a callback writing through its own
|
|
356
|
+
* parameter (`async (page) => { page.total = 1 }`).
|
|
357
|
+
*
|
|
358
|
+
* A root the analysis cannot name — `this.count += 1` reaches instance
|
|
359
|
+
* state every iteration shares — returns false, and the caller reads that
|
|
360
|
+
* as an outer write. The plugin prefers a missed report to a spurious one.
|
|
361
|
+
*/
|
|
362
|
+
function collectAssignmentTargetNames(target, names) {
|
|
363
|
+
switch (target.type) {
|
|
364
|
+
case utils_1.AST_NODE_TYPES.Identifier:
|
|
365
|
+
names.add(target.name);
|
|
366
|
+
return true;
|
|
367
|
+
case utils_1.AST_NODE_TYPES.MemberExpression:
|
|
368
|
+
return collectAssignmentTargetNames(target.object, names);
|
|
369
|
+
case utils_1.AST_NODE_TYPES.ChainExpression:
|
|
370
|
+
case utils_1.AST_NODE_TYPES.TSNonNullExpression:
|
|
371
|
+
case utils_1.AST_NODE_TYPES.TSAsExpression:
|
|
372
|
+
return collectAssignmentTargetNames(target.expression, names);
|
|
373
|
+
case utils_1.AST_NODE_TYPES.ObjectPattern: {
|
|
374
|
+
let resolved = true;
|
|
375
|
+
for (const property of target.properties) {
|
|
376
|
+
const inner = property.type === utils_1.AST_NODE_TYPES.RestElement
|
|
377
|
+
? property.argument
|
|
378
|
+
: property.value;
|
|
379
|
+
if (!collectAssignmentTargetNames(inner, names))
|
|
380
|
+
resolved = false;
|
|
381
|
+
}
|
|
382
|
+
return resolved;
|
|
383
|
+
}
|
|
384
|
+
case utils_1.AST_NODE_TYPES.ArrayPattern: {
|
|
385
|
+
let resolved = true;
|
|
386
|
+
for (const element of target.elements) {
|
|
387
|
+
if (element && !collectAssignmentTargetNames(element, names)) {
|
|
388
|
+
resolved = false;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return resolved;
|
|
392
|
+
}
|
|
393
|
+
case utils_1.AST_NODE_TYPES.RestElement:
|
|
394
|
+
return collectAssignmentTargetNames(target.argument, names);
|
|
395
|
+
case utils_1.AST_NODE_TYPES.AssignmentPattern:
|
|
396
|
+
return collectAssignmentTargetNames(target.left, names);
|
|
397
|
+
default:
|
|
398
|
+
return false;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
336
401
|
/**
|
|
337
402
|
* Detects cross-iteration state patterns that require sequential
|
|
338
403
|
* execution:
|
|
339
404
|
*
|
|
340
405
|
* 1. Accumulator: a variable declared OUTSIDE the loop body (i.e., not
|
|
341
|
-
* in localVars) is ASSIGNED inside the loop body
|
|
342
|
-
*
|
|
343
|
-
*
|
|
406
|
+
* in localVars) is ASSIGNED inside the loop body, whether directly or
|
|
407
|
+
* from inside a callback the body hands to the awaited call. Examples:
|
|
408
|
+
* `total += value`, `cursor = page.nextCursor`, `previousResult =
|
|
409
|
+
* result`. This catches running totals, pagination cursors, and chained
|
|
410
|
+
* results.
|
|
344
411
|
*
|
|
345
412
|
* 2. Direct cross-await dependency: a variable declared by an await
|
|
346
413
|
* inside the loop is then read as an argument to another await in the
|
|
@@ -348,28 +415,54 @@ exports.parallelizeLoopAwaits = (0, createRule_1.createRule)({
|
|
|
348
415
|
*/
|
|
349
416
|
function hasSequentialDependency(body, loopLocalVars) {
|
|
350
417
|
// Pattern 1: outer variable is written inside the loop body.
|
|
351
|
-
// Collect
|
|
352
|
-
// compound
|
|
418
|
+
// Collect every assignment target — the left-hand side of an assignment
|
|
419
|
+
// or compound assignment, and the operand of an increment in a callback.
|
|
353
420
|
let foundOuterWrite = false;
|
|
354
|
-
|
|
421
|
+
/**
|
|
422
|
+
* Reports whether an assignment target reaches a binding the iterations
|
|
423
|
+
* share rather than one the iteration creates.
|
|
424
|
+
*/
|
|
425
|
+
function writesOuterBinding(target) {
|
|
426
|
+
const names = new Set();
|
|
427
|
+
if (!collectAssignmentTargetNames(target, names))
|
|
428
|
+
return true;
|
|
429
|
+
for (const name of names) {
|
|
430
|
+
if (!loopLocalVars.has(name))
|
|
431
|
+
return true;
|
|
432
|
+
}
|
|
433
|
+
return false;
|
|
434
|
+
}
|
|
435
|
+
function findOuterWrites(node, isRoot, inNestedFunction) {
|
|
355
436
|
if (foundOuterWrite)
|
|
356
437
|
return;
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
438
|
+
// The walk deliberately enters callbacks. A write handed to the awaited
|
|
439
|
+
// call is the same data dependency as one written beside it:
|
|
440
|
+
// `await run(item, async (page) => { cursor = page.nextCursor })` has
|
|
441
|
+
// settled — and published `cursor` — by the time the iteration ends, so
|
|
442
|
+
// parallel iterations would race exactly as they would over
|
|
443
|
+
// `cursor = await run(item, cursor)`. (#1724)
|
|
444
|
+
const isNested = inNestedFunction ||
|
|
445
|
+
(!isRoot &&
|
|
446
|
+
(node.type === utils_1.AST_NODE_TYPES.FunctionDeclaration ||
|
|
447
|
+
node.type === utils_1.AST_NODE_TYPES.FunctionExpression ||
|
|
448
|
+
node.type === utils_1.AST_NODE_TYPES.ArrowFunctionExpression));
|
|
449
|
+
if (node.type === utils_1.AST_NODE_TYPES.AssignmentExpression &&
|
|
450
|
+
writesOuterBinding(node.left)) {
|
|
451
|
+
foundOuterWrite = true;
|
|
361
452
|
return;
|
|
362
453
|
}
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
454
|
+
// An increment counts only inside a callback. At the loop-body level it
|
|
455
|
+
// is the loop's own step counter — `while (i < n) { await f(items[i]);
|
|
456
|
+
// i++; }` walks the iteration space, and the `Promise.all(items.map(
|
|
457
|
+
// ...))` rewrite subsumes it — whereas a callback steps no iteration:
|
|
458
|
+
// `count++` there folds what the awaited work produced into a binding
|
|
459
|
+
// the whole loop shares, carrying the same dependency as the compound
|
|
460
|
+
// assignment it stands in for. (#1724)
|
|
461
|
+
if (isNested &&
|
|
462
|
+
node.type === utils_1.AST_NODE_TYPES.UpdateExpression &&
|
|
463
|
+
writesOuterBinding(node.argument)) {
|
|
464
|
+
foundOuterWrite = true;
|
|
465
|
+
return;
|
|
373
466
|
}
|
|
374
467
|
for (const key in node) {
|
|
375
468
|
if (key === 'parent' ||
|
|
@@ -382,17 +475,17 @@ exports.parallelizeLoopAwaits = (0, createRule_1.createRule)({
|
|
|
382
475
|
if (Array.isArray(child)) {
|
|
383
476
|
for (const item of child) {
|
|
384
477
|
if (item && typeof item === 'object' && 'type' in item) {
|
|
385
|
-
findOuterWrites(item, false);
|
|
478
|
+
findOuterWrites(item, false, isNested);
|
|
386
479
|
}
|
|
387
480
|
}
|
|
388
481
|
}
|
|
389
482
|
else if ('type' in child) {
|
|
390
|
-
findOuterWrites(child, false);
|
|
483
|
+
findOuterWrites(child, false, isNested);
|
|
391
484
|
}
|
|
392
485
|
}
|
|
393
486
|
}
|
|
394
487
|
}
|
|
395
|
-
findOuterWrites(body, true);
|
|
488
|
+
findOuterWrites(body, true, false);
|
|
396
489
|
if (foundOuterWrite)
|
|
397
490
|
return true;
|
|
398
491
|
// Pattern 2: a variable declared by an await is used as arg to another await.
|
package/package.json
CHANGED
package/release-manifest.json
CHANGED
|
@@ -1,4 +1,26 @@
|
|
|
1
1
|
[
|
|
2
|
+
{
|
|
3
|
+
"version": "1.20.107",
|
|
4
|
+
"date": "2026-08-05T05:11:58.347Z",
|
|
5
|
+
"rules": [
|
|
6
|
+
{
|
|
7
|
+
"name": "parallelize-async-operations",
|
|
8
|
+
"changeType": "fix",
|
|
9
|
+
"issues": [
|
|
10
|
+
1723
|
|
11
|
+
],
|
|
12
|
+
"summary": "treat a callback's write to an outer binding as a dependency (closes #1723)"
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"name": "parallelize-loop-awaits",
|
|
16
|
+
"changeType": "fix",
|
|
17
|
+
"issues": [
|
|
18
|
+
1724
|
|
19
|
+
],
|
|
20
|
+
"summary": "see a callback's write to an outer binding (closes #1724)"
|
|
21
|
+
}
|
|
22
|
+
]
|
|
23
|
+
},
|
|
2
24
|
{
|
|
3
25
|
"version": "1.20.106",
|
|
4
26
|
"date": "2026-08-05T01:12:16.076Z",
|