@blumintinc/eslint-plugin-blumint 1.20.83 → 1.20.85

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
@@ -223,7 +223,7 @@ function noFrontendImportsFromFunctionsPatterns(pattern) {
223
223
  module.exports = {
224
224
  meta: {
225
225
  name: '@blumintinc/eslint-plugin-blumint',
226
- version: '1.20.83',
226
+ version: '1.20.85',
227
227
  },
228
228
  parseOptions: {
229
229
  ecmaVersion: 2020,
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.preferSpreadOverReassembly = void 0;
4
4
  const utils_1 = require("@typescript-eslint/utils");
5
5
  const createRule_1 = require("../utils/createRule");
6
+ const ASTHelpers_1 = require("../utils/ASTHelpers");
6
7
  const DEFAULT_MIN_FIELDS = 2;
7
8
  /**
8
9
  * Collects all identifier references (not declarations) used anywhere in a
@@ -70,6 +71,219 @@ function getSimpleDestructuredNames(pattern) {
70
71
  }
71
72
  return names;
72
73
  }
74
+ /**
75
+ * Array methods that hand each element of the receiver to their callback. They
76
+ * are the contextual route by which an unannotated destructured parameter still
77
+ * has a knowable type: the element type of the array being iterated.
78
+ */
79
+ const ELEMENT_CALLBACK_METHODS = new Set([
80
+ 'map',
81
+ 'forEach',
82
+ 'filter',
83
+ 'flatMap',
84
+ ]);
85
+ const ARRAY_TYPE_NAMES = new Set(['Array', 'ReadonlyArray']);
86
+ /**
87
+ * Type operators that are homomorphic over their argument: each one rewrites
88
+ * the modifiers of every member and leaves the key set identical, so a member
89
+ * list read through them describes the wrapped type exactly. `Readonly<{...}>`
90
+ * is the idiomatic spelling of a data record, and refusing to see through it
91
+ * makes the narrowing proof inert on the code it exists to protect (#1643).
92
+ *
93
+ * `Pick`, `Omit`, `Record`, `Exclude` and `Extract` are deliberately absent:
94
+ * they rewrite the key set, and a wrong proof silences a report the rule owes
95
+ * rather than merely failing to find one.
96
+ */
97
+ const KEY_PRESERVING_TYPE_OPERATORS = new Set([
98
+ 'Readonly',
99
+ 'Required',
100
+ 'Partial',
101
+ ]);
102
+ /**
103
+ * Collects every name the file itself binds in a way that can stand in front of
104
+ * a type argument list — a type alias, an interface, a class, an enum, a
105
+ * namespace or an import. A file spelling `Readonly` as one of these is talking
106
+ * about its own declaration rather than the lib utility, so the key-preserving
107
+ * unwrap must not apply to it.
108
+ *
109
+ * The walk covers nested declarations too, since a `type Partial<T>` inside a
110
+ * function body shadows the global just as effectively as a top-level one.
111
+ */
112
+ function collectLocallyBoundNames(node, names) {
113
+ switch (node.type) {
114
+ case utils_1.AST_NODE_TYPES.TSTypeAliasDeclaration:
115
+ case utils_1.AST_NODE_TYPES.TSInterfaceDeclaration:
116
+ case utils_1.AST_NODE_TYPES.TSEnumDeclaration:
117
+ case utils_1.AST_NODE_TYPES.TSImportEqualsDeclaration:
118
+ names.add(node.id.name);
119
+ break;
120
+ case utils_1.AST_NODE_TYPES.ClassDeclaration:
121
+ case utils_1.AST_NODE_TYPES.ClassExpression:
122
+ if (node.id) {
123
+ names.add(node.id.name);
124
+ }
125
+ break;
126
+ case utils_1.AST_NODE_TYPES.TSModuleDeclaration:
127
+ if (node.id.type === utils_1.AST_NODE_TYPES.Identifier) {
128
+ names.add(node.id.name);
129
+ }
130
+ break;
131
+ case utils_1.AST_NODE_TYPES.ImportSpecifier:
132
+ case utils_1.AST_NODE_TYPES.ImportDefaultSpecifier:
133
+ case utils_1.AST_NODE_TYPES.ImportNamespaceSpecifier:
134
+ names.add(node.local.name);
135
+ break;
136
+ default:
137
+ break;
138
+ }
139
+ for (const key of Object.keys(node)) {
140
+ if (key === 'parent')
141
+ continue;
142
+ const value = node[key];
143
+ if (Array.isArray(value)) {
144
+ for (const child of value) {
145
+ if (child && typeof child === 'object' && 'type' in child) {
146
+ collectLocallyBoundNames(child, names);
147
+ }
148
+ }
149
+ }
150
+ else if (value && typeof value === 'object' && 'type' in value) {
151
+ collectLocallyBoundNames(value, names);
152
+ }
153
+ }
154
+ }
155
+ /**
156
+ * Resolves `Promise<T>` to `T`, leaving anything else as it stands, which is
157
+ * what `await` does to the type of the expression it operates on.
158
+ */
159
+ function unwrapPromise(typeNode) {
160
+ if (typeNode.type === utils_1.AST_NODE_TYPES.TSTypeReference &&
161
+ typeNode.typeName.type === utils_1.AST_NODE_TYPES.Identifier &&
162
+ typeNode.typeName.name === 'Promise' &&
163
+ typeNode.typeParameters?.params.length === 1) {
164
+ return typeNode.typeParameters.params[0];
165
+ }
166
+ return typeNode;
167
+ }
168
+ /** The element type of an array type, or null when the type is not an array. */
169
+ function arrayElementTypeOf(typeNode) {
170
+ if (typeNode.type === utils_1.AST_NODE_TYPES.TSArrayType) {
171
+ return typeNode.elementType;
172
+ }
173
+ // `readonly Unit[]` wraps the array type in a type operator.
174
+ if (typeNode.type === utils_1.AST_NODE_TYPES.TSTypeOperator &&
175
+ typeNode.operator === 'readonly' &&
176
+ typeNode.typeAnnotation) {
177
+ return arrayElementTypeOf(typeNode.typeAnnotation);
178
+ }
179
+ if (typeNode.type === utils_1.AST_NODE_TYPES.TSTypeReference &&
180
+ typeNode.typeName.type === utils_1.AST_NODE_TYPES.Identifier &&
181
+ ARRAY_TYPE_NAMES.has(typeNode.typeName.name) &&
182
+ typeNode.typeParameters?.params.length === 1) {
183
+ return typeNode.typeParameters.params[0];
184
+ }
185
+ return null;
186
+ }
187
+ /**
188
+ * The property names a member list declares, or null when the list cannot be
189
+ * enumerated exactly. An index signature, a call/construct signature or a
190
+ * computed key all describe members whose names are not written down, and a
191
+ * member set that may be larger than what is read would let a narrowing pick
192
+ * pass for an exhaustive one.
193
+ */
194
+ function namesOfMembers(members) {
195
+ const names = new Set();
196
+ for (const member of members) {
197
+ if (member.type !== utils_1.AST_NODE_TYPES.TSPropertySignature &&
198
+ member.type !== utils_1.AST_NODE_TYPES.TSMethodSignature) {
199
+ return null;
200
+ }
201
+ if (member.computed) {
202
+ return null;
203
+ }
204
+ const key = member.key;
205
+ if (key.type === utils_1.AST_NODE_TYPES.Identifier) {
206
+ names.add(key.name);
207
+ }
208
+ else if (key.type === utils_1.AST_NODE_TYPES.Literal &&
209
+ typeof key.value === 'string') {
210
+ names.add(key.value);
211
+ }
212
+ else {
213
+ return null;
214
+ }
215
+ }
216
+ return names;
217
+ }
218
+ /**
219
+ * Finds a type alias or interface declared at the top level of the file being
220
+ * linted, including one that is exported.
221
+ *
222
+ * Resolution stops at the file boundary on purpose: an imported name's members
223
+ * live in a module this rule cannot read, and guessing at them would be the
224
+ * opposite of a proof.
225
+ */
226
+ function findLocalTypeDeclaration(program, name) {
227
+ for (const statement of program.body) {
228
+ const declaration = statement.type === utils_1.AST_NODE_TYPES.ExportNamedDeclaration &&
229
+ statement.declaration
230
+ ? statement.declaration
231
+ : statement;
232
+ if ((declaration.type === utils_1.AST_NODE_TYPES.TSTypeAliasDeclaration ||
233
+ declaration.type === utils_1.AST_NODE_TYPES.TSInterfaceDeclaration) &&
234
+ declaration.id.name === name) {
235
+ return declaration;
236
+ }
237
+ }
238
+ return null;
239
+ }
240
+ /**
241
+ * Enumerates every property name a type node declares, or null when the member
242
+ * list cannot be established with certainty.
243
+ *
244
+ * Only an unambiguous, fully written-out member list qualifies, reached either
245
+ * directly or through the key-preserving operators in
246
+ * {@link KEY_PRESERVING_TYPE_OPERATORS}. A union, an intersection, a mapped or
247
+ * conditional type, any other generic instantiation and an interface with an
248
+ * `extends` clause all describe a member set assembled elsewhere, so none of
249
+ * them can prove anything here.
250
+ */
251
+ function memberNamesOf(typeNode, scope, seen = new Set()) {
252
+ if (typeNode.type === utils_1.AST_NODE_TYPES.TSTypeLiteral) {
253
+ return namesOfMembers(typeNode.members);
254
+ }
255
+ if (typeNode.type !== utils_1.AST_NODE_TYPES.TSTypeReference ||
256
+ typeNode.typeName.type !== utils_1.AST_NODE_TYPES.Identifier) {
257
+ return null;
258
+ }
259
+ const name = typeNode.typeName.name;
260
+ if (typeNode.typeParameters) {
261
+ // An arity other than one is not the lib utility this name spells, so
262
+ // nothing about the wrapped member list follows from it.
263
+ if (!KEY_PRESERVING_TYPE_OPERATORS.has(name) ||
264
+ typeNode.typeParameters.params.length !== 1 ||
265
+ scope.isLocallyBound(name)) {
266
+ return null;
267
+ }
268
+ return memberNamesOf(typeNode.typeParameters.params[0], scope, seen);
269
+ }
270
+ // A self-referential alias (`type T = T`) would otherwise recur forever.
271
+ if (seen.has(name)) {
272
+ return null;
273
+ }
274
+ seen.add(name);
275
+ const declaration = findLocalTypeDeclaration(scope.program, name);
276
+ if (!declaration || declaration.typeParameters) {
277
+ return null;
278
+ }
279
+ if (declaration.type === utils_1.AST_NODE_TYPES.TSTypeAliasDeclaration) {
280
+ return memberNamesOf(declaration.typeAnnotation, scope, seen);
281
+ }
282
+ if (declaration.extends && declaration.extends.length > 0) {
283
+ return null;
284
+ }
285
+ return namesOfMembers(declaration.body.body);
286
+ }
73
287
  /**
74
288
  * For a JSX element, returns the set of destructured names that are forwarded
75
289
  * with identical key names (e.g. `hits={hits}`, `isLoading={isLoading}`).
@@ -492,6 +706,173 @@ exports.preferSpreadOverReassembly = (0, createRule_1.createRule)({
492
706
  create(context, [options]) {
493
707
  const minFields = options?.minFields ?? DEFAULT_MIN_FIELDS;
494
708
  const sourceCode = context.getSourceCode();
709
+ const program = sourceCode.ast;
710
+ // The scan walks the whole file, so it is deferred until a key-preserving
711
+ // operator actually turns up in a position the proof depends on — most
712
+ // files never reach it.
713
+ let locallyBoundNames = null;
714
+ const typeScope = {
715
+ program,
716
+ isLocallyBound: (name) => {
717
+ if (!locallyBoundNames) {
718
+ locallyBoundNames = new Set();
719
+ collectLocallyBoundNames(program, locallyBoundNames);
720
+ }
721
+ return locallyBoundNames.has(name);
722
+ },
723
+ };
724
+ /**
725
+ * Walks an expression toward its syntactic root and returns the type node
726
+ * that root declares, mirroring the receiver trace in
727
+ * `enforce-firestore-doc-ref-generic`.
728
+ *
729
+ * The trace is syntax only. `parserOptions.project` is absent from the
730
+ * shared testers and from many consumer configs, so a type-checker branch
731
+ * would be dead exactly where the destructured pick it must protect lives —
732
+ * inside an `Array.prototype.map` callback, whose signature comes from
733
+ * `lib.d.ts`.
734
+ */
735
+ function typeNodeOfExpression(node, visited) {
736
+ // Guards against a self-referential declaration such as `const a = a.b;`.
737
+ if (!node || visited.has(node)) {
738
+ return null;
739
+ }
740
+ visited.add(node);
741
+ switch (node.type) {
742
+ case utils_1.AST_NODE_TYPES.AwaitExpression: {
743
+ const awaited = typeNodeOfExpression(node.argument, visited);
744
+ return awaited ? unwrapPromise(awaited) : null;
745
+ }
746
+ case utils_1.AST_NODE_TYPES.ChainExpression:
747
+ case utils_1.AST_NODE_TYPES.TSNonNullExpression:
748
+ return typeNodeOfExpression(node.expression, visited);
749
+ // An assertion states the type outright, which is stronger evidence
750
+ // than anything the operand could supply.
751
+ case utils_1.AST_NODE_TYPES.TSAsExpression:
752
+ case utils_1.AST_NODE_TYPES.TSSatisfiesExpression:
753
+ return node.typeAnnotation;
754
+ case utils_1.AST_NODE_TYPES.Identifier:
755
+ return typeNodeOfIdentifier(node, visited);
756
+ case utils_1.AST_NODE_TYPES.CallExpression:
757
+ return typeNodeOfCallResult(node);
758
+ default:
759
+ // A member expression is deliberately absent: a property's type lives
760
+ // in the type of its object, which syntax alone does not supply.
761
+ return null;
762
+ }
763
+ }
764
+ function typeNodeOfIdentifier(node, visited) {
765
+ const scope = ASTHelpers_1.ASTHelpers.getScope(context, node);
766
+ const variable = ASTHelpers_1.ASTHelpers.findVariableInScope(scope, node.name);
767
+ if (!variable || variable.defs.length !== 1) {
768
+ return null;
769
+ }
770
+ const def = variable.defs[0];
771
+ if (def.type === 'Parameter') {
772
+ return def.name.type === utils_1.AST_NODE_TYPES.Identifier &&
773
+ def.name.typeAnnotation
774
+ ? def.name.typeAnnotation.typeAnnotation
775
+ : null;
776
+ }
777
+ if (def.type !== 'Variable' ||
778
+ def.node.type !== utils_1.AST_NODE_TYPES.VariableDeclarator) {
779
+ return null;
780
+ }
781
+ const declarator = def.node;
782
+ // An annotation constrains every assignment rather than just the
783
+ // initializer, so it describes the binding even when it is a `let`.
784
+ if (declarator.id.type === utils_1.AST_NODE_TYPES.Identifier &&
785
+ declarator.id.typeAnnotation) {
786
+ return declarator.id.typeAnnotation.typeAnnotation;
787
+ }
788
+ // Without an annotation only an immutable binding still holds its
789
+ // initializer's type by the time the callback runs.
790
+ if (def.parent?.type !== utils_1.AST_NODE_TYPES.VariableDeclaration ||
791
+ def.parent.kind !== 'const') {
792
+ return null;
793
+ }
794
+ return typeNodeOfExpression(declarator.init, visited);
795
+ }
796
+ /**
797
+ * A receiver that is a call result takes its type from what the callee
798
+ * declares it returns; an inferred return type is not written down and so
799
+ * proves nothing.
800
+ */
801
+ function typeNodeOfCallResult(node) {
802
+ if (node.callee.type !== utils_1.AST_NODE_TYPES.Identifier) {
803
+ return null;
804
+ }
805
+ const scope = ASTHelpers_1.ASTHelpers.getScope(context, node.callee);
806
+ const variable = ASTHelpers_1.ASTHelpers.findVariableInScope(scope, node.callee.name);
807
+ if (!variable || variable.defs.length !== 1) {
808
+ return null;
809
+ }
810
+ const def = variable.defs[0];
811
+ // A hoisted declaration binds the helper the same way a `const` arrow
812
+ // does, so both spellings are read.
813
+ if (def.type === 'FunctionName') {
814
+ return def.node.returnType?.typeAnnotation ?? null;
815
+ }
816
+ if (def.type !== 'Variable' ||
817
+ def.node.type !== utils_1.AST_NODE_TYPES.VariableDeclarator ||
818
+ def.parent?.type !== utils_1.AST_NODE_TYPES.VariableDeclaration ||
819
+ def.parent.kind !== 'const') {
820
+ return null;
821
+ }
822
+ const init = def.node.init;
823
+ if (init?.type !== utils_1.AST_NODE_TYPES.ArrowFunctionExpression &&
824
+ init?.type !== utils_1.AST_NODE_TYPES.FunctionExpression) {
825
+ return null;
826
+ }
827
+ return init.returnType?.typeAnnotation ?? null;
828
+ }
829
+ /**
830
+ * The member names of the element type of the array whose method call this
831
+ * function is the callback of, e.g. `Unit` for `units.map(fn)` where
832
+ * `units` is annotated `Unit[]`.
833
+ */
834
+ function contextualElementMemberNames(fn) {
835
+ const call = fn.parent;
836
+ if (!call ||
837
+ call.type !== utils_1.AST_NODE_TYPES.CallExpression ||
838
+ call.arguments[0] !== fn ||
839
+ call.callee.type !== utils_1.AST_NODE_TYPES.MemberExpression ||
840
+ call.callee.computed ||
841
+ call.callee.property.type !== utils_1.AST_NODE_TYPES.Identifier ||
842
+ !ELEMENT_CALLBACK_METHODS.has(call.callee.property.name)) {
843
+ return null;
844
+ }
845
+ const receiverType = typeNodeOfExpression(call.callee.object, new Set());
846
+ if (!receiverType) {
847
+ return null;
848
+ }
849
+ const elementType = arrayElementTypeOf(receiverType);
850
+ return elementType ? memberNamesOf(elementType, typeScope) : null;
851
+ }
852
+ /**
853
+ * Reports whether the destructured pick is provably a PROPER subset of the
854
+ * source object's own type, in which case spreading the parameter would add
855
+ * the members the author left out and change what the function produces
856
+ * (#1642: a GitHub review payload gained unknown keys).
857
+ *
858
+ * The proof runs in the safe direction only. A member set that matches the
859
+ * pick exactly is exhaustive, so the rewrite is behavior-preserving and the
860
+ * rule still reports; a type it cannot resolve — imported, a union, an
861
+ * index signature, or an instantiation of anything but a key-preserving
862
+ * operator — yields no proof and the rule likewise still reports. Silence
863
+ * is reserved for the case where the widening is demonstrated.
864
+ */
865
+ function isProvablyNarrowingPick(fn, param, destructuredNames) {
866
+ // An explicit annotation overrides whatever the call site would imply,
867
+ // so the contextual route is consulted only in its absence.
868
+ const memberNames = param.typeAnnotation
869
+ ? memberNamesOf(param.typeAnnotation.typeAnnotation, typeScope)
870
+ : contextualElementMemberNames(fn);
871
+ if (!memberNames || memberNames.size <= destructuredNames.length) {
872
+ return false;
873
+ }
874
+ return destructuredNames.every((name) => memberNames.has(name));
875
+ }
495
876
  function checkFunction(fn) {
496
877
  // Must have exactly one parameter that is an ObjectPattern.
497
878
  if (fn.params.length !== 1)
@@ -547,6 +928,11 @@ exports.preferSpreadOverReassembly = (0, createRule_1.createRule)({
547
928
  return;
548
929
  }
549
930
  }
931
+ // The pick may exist precisely because the omitted members must not flow
932
+ // through; spreading would reinstate them.
933
+ if (isProvablyNarrowingPick(fn, param, destructuredNames)) {
934
+ return;
935
+ }
550
936
  context.report({
551
937
  node: param,
552
938
  messageId: 'preferSpread',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blumintinc/eslint-plugin-blumint",
3
- "version": "1.20.83",
3
+ "version": "1.20.85",
4
4
  "description": "Custom eslint rules for use within BluMint",
5
5
  "author": {
6
6
  "name": "Brodie McGuire",
@@ -1,4 +1,32 @@
1
1
  [
2
+ {
3
+ "version": "1.20.85",
4
+ "date": "2026-08-03T03:21:29.711Z",
5
+ "rules": [
6
+ {
7
+ "name": "prefer-spread-over-reassembly",
8
+ "changeType": "fix",
9
+ "issues": [
10
+ 1643
11
+ ],
12
+ "summary": "read the narrowing proof through Readonly (closes #1643)"
13
+ }
14
+ ]
15
+ },
16
+ {
17
+ "version": "1.20.84",
18
+ "date": "2026-08-03T02:56:38.740Z",
19
+ "rules": [
20
+ {
21
+ "name": "prefer-spread-over-reassembly",
22
+ "changeType": "fix",
23
+ "issues": [
24
+ 1642
25
+ ],
26
+ "summary": "stay silent on a provably narrowing pick (closes #1642)"
27
+ }
28
+ ]
29
+ },
2
30
  {
3
31
  "version": "1.20.83",
4
32
  "date": "2026-08-03T02:05:36.672Z",