eslint-plugin-ts-data-forge 0.10.9 → 0.10.10

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.
@@ -780,7 +780,35 @@ describe('prefer-canonical-array-slicing through type wrappers', () => {
780
780
 
781
781
  describe('prefer-canonical-array-slicing and the import it adds', () => {
782
782
  tester.run('prefer-canonical-array-slicing', preferCanonicalArraySlicing, {
783
- valid: [],
783
+ valid: [
784
+ {
785
+ name: 'a namespace imported by `import type` binds no value',
786
+ code: dedent`
787
+ import type * as T from 'ts-data-forge';
788
+ declare const xs: readonly number[];
789
+ const ys = T.Arr.skip(xs, 1);
790
+ `,
791
+ },
792
+ {
793
+ name: 'a local binding that shadows the imported `Arr` is not ts-data-forge',
794
+ code: dedent`
795
+ import { Arr } from 'ts-data-forge';
796
+ declare const xs: readonly number[];
797
+ const f = (Arr: { skip: (a: unknown, n: number) => unknown }) =>
798
+ Arr.skip(xs, 1);
799
+ `,
800
+ },
801
+ {
802
+ name: 'a local binding that shadows the namespace import is not ts-data-forge',
803
+ code: dedent`
804
+ import * as tdf from 'ts-data-forge';
805
+ declare const xs: readonly number[];
806
+ const f = (tdf: {
807
+ Arr: { skip: (a: unknown, n: number) => unknown };
808
+ }) => tdf.Arr.skip(xs, 1);
809
+ `,
810
+ },
811
+ ],
784
812
  invalid: [
785
813
  {
786
814
  name: 'writes through a namespace import, which needs no edit',
@@ -829,6 +857,92 @@ describe('prefer-canonical-array-slicing and the import it adds', () => {
829
857
  output: null,
830
858
  errors: [{ messageId: 'preferCanonicalArraySlicing' }],
831
859
  },
860
+ {
861
+ name: 'sees `Arr.skip` and `ns.Arr.skip` through type wrappers',
862
+ code: dedent`
863
+ import { Arr } from 'ts-data-forge';
864
+ import * as tdf from 'ts-data-forge';
865
+ declare const xs: readonly number[];
866
+ const a = (Arr as typeof Arr).skip(xs, 1);
867
+ const b = (tdf satisfies typeof tdf).Arr.skipLast(xs, 1);
868
+ `,
869
+ output: dedent`
870
+ import { Arr } from 'ts-data-forge';
871
+ import * as tdf from 'ts-data-forge';
872
+ declare const xs: readonly number[];
873
+ const a = Arr.tail(xs);
874
+ const b = Arr.butLast(xs);
875
+ `,
876
+ errors: [
877
+ { messageId: 'preferCanonicalArraySlicing' },
878
+ { messageId: 'preferCanonicalArraySlicing' },
879
+ ],
880
+ },
881
+ {
882
+ name: 'drops `type` from an inline type specifier',
883
+ code: dedent`
884
+ import { type Arr, asInt } from 'ts-data-forge';
885
+ declare const xs: readonly number[];
886
+ const ys = xs.slice(1);
887
+ `,
888
+ output: dedent`
889
+ import { Arr, asInt } from 'ts-data-forge';
890
+ declare const xs: readonly number[];
891
+ const ys = Arr.tail(xs);
892
+ `,
893
+ errors: [{ messageId: 'preferCanonicalArraySlicing' }],
894
+ },
895
+ {
896
+ name: 'adds the import after a leading directive',
897
+ code: dedent`
898
+ 'use client';
899
+ declare const xs: readonly number[];
900
+ const ys = xs.slice(1);
901
+ `,
902
+ output: dedent`
903
+ 'use client';
904
+ import { Arr } from 'ts-data-forge';
905
+ declare const xs: readonly number[];
906
+ const ys = Arr.tail(xs);
907
+ `,
908
+ errors: [{ messageId: 'preferCanonicalArraySlicing' }],
909
+ },
910
+ {
911
+ name: 'imports `Arr` by name where a local binding shadows the namespace import',
912
+ code: dedent`
913
+ import * as tdf from 'ts-data-forge';
914
+ declare const xs: readonly number[];
915
+ const f = (tdf: unknown) => xs.slice(1);
916
+ `,
917
+ output: dedent`
918
+ import { Arr } from 'ts-data-forge';
919
+ import * as tdf from 'ts-data-forge';
920
+ declare const xs: readonly number[];
921
+ const f = (tdf: unknown) => Arr.tail(xs);
922
+ `,
923
+ errors: [
924
+ {
925
+ messageId: 'preferCanonicalArraySlicing',
926
+ data: { replacement: 'Arr.tail(xs)' },
927
+ },
928
+ ],
929
+ },
930
+ {
931
+ name: 'no fix where a local binding shadows the namespace import and another import takes `Arr`',
932
+ code: dedent`
933
+ import * as tdf from 'ts-data-forge';
934
+ import { Arr } from 'other';
935
+ declare const xs: readonly number[];
936
+ const f = (tdf: unknown) => xs.slice(1);
937
+ `,
938
+ output: null,
939
+ errors: [
940
+ {
941
+ messageId: 'preferCanonicalArraySlicing',
942
+ data: { replacement: 'Arr.tail(xs)' },
943
+ },
944
+ ],
945
+ },
832
946
  ],
833
947
  });
834
948
  });
@@ -92,11 +92,11 @@ export const preferObjOverEntriesRoundTrip: TSESLint.RuleModule<
92
92
  }[] = [];
93
93
 
94
94
  /**
95
- * Rebuilds the callback's parameter list. The entry pattern binds
95
+ * The names of the rebuilt callback's parameters. The entry pattern binds
96
96
  * `[key, value]`; `Obj` passes `(value, key)`, and either parameter is
97
97
  * dropped when the rewritten body no longer refers to it.
98
98
  */
99
- const buildParams = (roundTrip: RoundTrip): string => {
99
+ const rebuiltParamNames = (roundTrip: RoundTrip): readonly string[] => {
100
100
  const scope = sourceCode.getScope(roundTrip.callback);
101
101
 
102
102
  const dropped = new Set<TSESTree.Node>(roundTrip.dropped);
@@ -112,13 +112,13 @@ export const preferObjOverEntriesRoundTrip: TSESLint.RuleModule<
112
112
  if (keyName !== undefined) {
113
113
  // `Obj` puts the value first, so a callback that only needs the key
114
114
  // still has to name a value parameter.
115
- return `(${valueName ?? unusedValueParamName(scope)}, ${keyName})`;
115
+ return [valueName ?? unusedValueParamName(scope), keyName];
116
116
  }
117
117
 
118
118
  return valueName !== undefined &&
119
119
  isReferencedOutside(scope, valueName, dropped)
120
- ? `(${valueName})`
121
- : '()';
120
+ ? [valueName]
121
+ : [];
122
122
  };
123
123
 
124
124
  const buildCallbackBody = (
@@ -160,12 +160,12 @@ export const preferObjOverEntriesRoundTrip: TSESLint.RuleModule<
160
160
  },
161
161
 
162
162
  'Program:exit': () => {
163
- const objAt = tsDataForgeValuesAt(sourceCode, ['Obj']).at;
163
+ const objAt = tsDataForgeValuesAt(sourceCode, ['Obj']);
164
164
 
165
165
  const objOptionalAt = tsDataForgeValuesAt(sourceCode, [
166
166
  'Obj',
167
167
  'Optional',
168
- ]).at;
168
+ ]);
169
169
 
170
170
  // Every fix brings the names it writes into scope itself, so that one
171
171
  // applied alone (an editor's quick fix) is complete.
@@ -177,11 +177,19 @@ export const preferObjOverEntriesRoundTrip: TSESLint.RuleModule<
177
177
  const [objName, optionalName = 'Optional'] =
178
178
  binding?.localNames ?? [];
179
179
 
180
+ const paramNames = rebuiltParamNames(roundTrip);
181
+
182
+ // `Optional` is written inside the rebuilt callback, where a
183
+ // parameter it keeps would capture the name `Optional` starts with.
184
+ const isCaptured =
185
+ roundTrip.body.kind === 'optional' &&
186
+ paramNames.includes(optionalName.split('.', 1)[0] ?? optionalName);
187
+
180
188
  context.report({
181
189
  node,
182
190
  messageId: MESSAGE_ID_OF[roundTrip.method],
183
191
  fix:
184
- binding === undefined || objName === undefined
192
+ binding === undefined || objName === undefined || isCaptured
185
193
  ? null
186
194
  : (fixer) => {
187
195
  const recordText = getArgumentText(
@@ -189,7 +197,7 @@ export const preferObjOverEntriesRoundTrip: TSESLint.RuleModule<
189
197
  roundTrip.record,
190
198
  );
191
199
 
192
- const replacement = `${objName}.${roundTrip.method}(${recordText}, ${buildParams(roundTrip)} => ${buildCallbackBody(roundTrip, optionalName)})`;
200
+ const replacement = `${objName}.${roundTrip.method}(${recordText}, (${paramNames.join(', ')}) => ${buildCallbackBody(roundTrip, optionalName)})`;
193
201
 
194
202
  return [
195
203
  ...binding.edits(fixer),
@@ -308,6 +308,68 @@ describe('prefer-obj-over-entries-round-trip', () => {
308
308
  `,
309
309
  errors: [{ messageId: 'useObjFilterMap' }],
310
310
  },
311
+ {
312
+ name: 'writes through a namespace import',
313
+ code: dedent`
314
+ import * as tdf from 'ts-data-forge';
315
+ const scores = { alice: 1, bob: 2 };
316
+ const scaled = Object.fromEntries(
317
+ Object.entries(scores).flatMap(([k, v]) =>
318
+ v > 1 ? [[k, v * 10]] : [],
319
+ ),
320
+ );
321
+ `,
322
+ output: dedent`
323
+ import * as tdf from 'ts-data-forge';
324
+ const scores = { alice: 1, bob: 2 };
325
+ const scaled = tdf.Obj.filterMap(scores, (v) => v > 1 ? tdf.Optional.some(v * 10) : tdf.Optional.none);
326
+ `,
327
+ errors: [{ messageId: 'useObjFilterMap' }],
328
+ },
329
+ {
330
+ name: 'no fix when a parameter the callback keeps shadows the namespace it reads',
331
+ code: dedent`
332
+ import * as tdf from 'ts-data-forge';
333
+ const scores = { alice: 1, bob: 2 };
334
+ const scaled = Object.fromEntries(
335
+ Object.entries(scores).flatMap(([k, tdf]) =>
336
+ tdf > 1 ? [[k, tdf * 10]] : [],
337
+ ),
338
+ );
339
+ `,
340
+ output: null,
341
+ errors: [{ messageId: 'useObjFilterMap' }],
342
+ },
343
+ {
344
+ name: 'no fix when a parameter the callback keeps is named Optional',
345
+ code: dedent`
346
+ const scores = { alice: 1, bob: 2 };
347
+ const scaled = Object.fromEntries(
348
+ Object.entries(scores).flatMap(([k, Optional]) =>
349
+ Optional > 1 ? [[k, Optional * 10]] : [],
350
+ ),
351
+ );
352
+ `,
353
+ output: null,
354
+ errors: [{ messageId: 'useObjFilterMap' }],
355
+ },
356
+ {
357
+ name: 'a dropped key parameter named Optional captures nothing',
358
+ code: dedent`
359
+ const scores = { alice: 1, bob: 2 };
360
+ const scaled = Object.fromEntries(
361
+ Object.entries(scores).flatMap(([Optional, v]) =>
362
+ v > 1 ? [[Optional, v * 10]] : [],
363
+ ),
364
+ );
365
+ `,
366
+ output: dedent`
367
+ import { Obj, Optional } from 'ts-data-forge';
368
+ const scores = { alice: 1, bob: 2 };
369
+ const scaled = Obj.filterMap(scores, (v) => v > 1 ? Optional.some(v * 10) : Optional.none);
370
+ `,
371
+ errors: [{ messageId: 'useObjFilterMap' }],
372
+ },
311
373
  {
312
374
  name: 'adds Obj and Optional to one existing import',
313
375
  code: dedent`