eslint-plugin-ts-data-forge 0.10.8 → 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.
- package/dist/rules/import-binding.d.mts +7 -8
- package/dist/rules/import-binding.d.mts.map +1 -1
- package/dist/rules/import-binding.mjs +3 -13
- package/dist/rules/import-binding.mjs.map +1 -1
- package/dist/rules/no-unnecessary-type-guard.d.mts.map +1 -1
- package/dist/rules/no-unnecessary-type-guard.mjs +2 -0
- package/dist/rules/no-unnecessary-type-guard.mjs.map +1 -1
- package/dist/rules/prefer-arr-generate.d.mts.map +1 -1
- package/dist/rules/prefer-arr-generate.mjs +5 -5
- package/dist/rules/prefer-arr-generate.mjs.map +1 -1
- package/dist/rules/prefer-arr-uniq.mjs +7 -2
- package/dist/rules/prefer-arr-uniq.mjs.map +1 -1
- package/dist/rules/prefer-canonical-array-slicing.d.mts.map +1 -1
- package/dist/rules/prefer-canonical-array-slicing.mjs +49 -78
- package/dist/rules/prefer-canonical-array-slicing.mjs.map +1 -1
- package/dist/rules/prefer-obj-over-entries-round-trip.d.mts.map +1 -1
- package/dist/rules/prefer-obj-over-entries-round-trip.mjs +13 -8
- package/dist/rules/prefer-obj-over-entries-round-trip.mjs.map +1 -1
- package/package.json +7 -7
- package/src/rules/import-binding.mts +10 -31
- package/src/rules/no-unnecessary-type-guard.mts +5 -3
- package/src/rules/prefer-arr-generate.mts +11 -7
- package/src/rules/prefer-arr-generate.test.mts +20 -0
- package/src/rules/prefer-arr-is-non-empty.test.mts +36 -0
- package/src/rules/prefer-arr-uniq.mts +8 -2
- package/src/rules/prefer-arr-uniq.test.mts +37 -0
- package/src/rules/prefer-canonical-array-slicing.mts +83 -118
- package/src/rules/prefer-canonical-array-slicing.test.mts +115 -1
- package/src/rules/prefer-obj-over-entries-round-trip.mts +17 -9
- package/src/rules/prefer-obj-over-entries-round-trip.test.mts +62 -0
|
@@ -5,18 +5,16 @@ import {
|
|
|
5
5
|
type TSESTree,
|
|
6
6
|
} from '@typescript-eslint/utils';
|
|
7
7
|
import {
|
|
8
|
-
bindValueImports,
|
|
9
8
|
getImportDeclarations,
|
|
10
9
|
getImportedLocalName,
|
|
11
10
|
hasForeignBinding,
|
|
12
|
-
importedNameOf,
|
|
13
11
|
skipTypeWrappers,
|
|
14
12
|
typeWrapperLayers,
|
|
15
|
-
type ValueImportBinding,
|
|
16
13
|
} from 'eslint-rule-forge';
|
|
17
14
|
import type { ReadonlyRecord } from 'ts-type-forge';
|
|
18
15
|
import * as ts from 'typescript';
|
|
19
16
|
import { TS_DATA_FORGE_MODULE } from './constants.mjs';
|
|
17
|
+
import { tsDataForgeValueAt } from './import-binding.mjs';
|
|
20
18
|
|
|
21
19
|
/* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
|
|
22
20
|
|
|
@@ -203,25 +201,16 @@ export const preferCanonicalArraySlicing: TSESLint.RuleModule<
|
|
|
203
201
|
|
|
204
202
|
const program = sourceCode.ast;
|
|
205
203
|
|
|
204
|
+
// `import type` binds no value a call could go through.
|
|
206
205
|
const tsDataForgeImports = getImportDeclarations(
|
|
207
206
|
program,
|
|
208
207
|
TS_DATA_FORGE_MODULE,
|
|
209
|
-
);
|
|
208
|
+
).filter((declaration) => declaration.importKind !== 'type');
|
|
210
209
|
|
|
211
210
|
const parserServices = ESLintUtils.getParserServices(context);
|
|
212
211
|
|
|
213
212
|
const checker = parserServices.program.getTypeChecker();
|
|
214
213
|
|
|
215
|
-
/**
|
|
216
|
-
* The in-scope way to reference the `Arr` namespace, the name the reference
|
|
217
|
-
* starts with, and the edits that bring it into scope: a value
|
|
218
|
-
* import of `Arr` (possibly aliased), a namespace import member, or `Arr`
|
|
219
|
-
* bound by {@link bindValueImports}, which adds it to an existing import or
|
|
220
|
-
* drops `type` from one. `undefined` when none can: `import type { Arr }`
|
|
221
|
-
* with no namespace import.
|
|
222
|
-
*/
|
|
223
|
-
const arrTarget = resolveArrTarget(sourceCode, tsDataForgeImports);
|
|
224
|
-
|
|
225
214
|
/** How a call already in the file names `Arr`, to recognize it. */
|
|
226
215
|
const arrLocalName = getImportedLocalName(tsDataForgeImports, 'Arr');
|
|
227
216
|
|
|
@@ -232,7 +221,23 @@ export const preferCanonicalArraySlicing: TSESLint.RuleModule<
|
|
|
232
221
|
specifier.type === AST_NODE_TYPES.ImportNamespaceSpecifier,
|
|
233
222
|
)?.local.name;
|
|
234
223
|
|
|
235
|
-
|
|
224
|
+
/**
|
|
225
|
+
* Whether `identifier` is `localName`, bound by the import from
|
|
226
|
+
* ts-data-forge where it is rather than by a local binding.
|
|
227
|
+
*/
|
|
228
|
+
const isImportedAs = (
|
|
229
|
+
identifier: TSESTree.Node,
|
|
230
|
+
localName: string | undefined,
|
|
231
|
+
): boolean =>
|
|
232
|
+
localName !== undefined &&
|
|
233
|
+
identifier.type === AST_NODE_TYPES.Identifier &&
|
|
234
|
+
identifier.name === localName &&
|
|
235
|
+
!hasForeignBinding(
|
|
236
|
+
sourceCode,
|
|
237
|
+
identifier,
|
|
238
|
+
localName,
|
|
239
|
+
TS_DATA_FORGE_MODULE,
|
|
240
|
+
);
|
|
236
241
|
|
|
237
242
|
/**
|
|
238
243
|
* Deferred (non-concrete) types are reduced to their base constraint before
|
|
@@ -317,39 +322,14 @@ export const preferCanonicalArraySlicing: TSESLint.RuleModule<
|
|
|
317
322
|
: text;
|
|
318
323
|
};
|
|
319
324
|
|
|
320
|
-
const toReplacementText = (op: CopyOp, xsText: string): string => {
|
|
321
|
-
switch (op.fn) {
|
|
322
|
-
case 'tail':
|
|
323
|
-
case 'butLast':
|
|
324
|
-
return `${arrRef}.${op.fn}(${xsText})`;
|
|
325
|
-
|
|
326
|
-
case 'skip':
|
|
327
|
-
case 'take':
|
|
328
|
-
case 'skipLast':
|
|
329
|
-
case 'takeLast':
|
|
330
|
-
return `${arrRef}.${op.fn}(${xsText}, ${op.count})`;
|
|
331
|
-
|
|
332
|
-
// Emit the curried form for `toUnshifted` so the new element stays in
|
|
333
|
-
// the same textual position as in the `[item, ...xs]` source, keeping
|
|
334
|
-
// the rewrite easy to read.
|
|
335
|
-
case 'toUnshifted':
|
|
336
|
-
return `${arrRef}.toUnshifted(${op.itemText})(${xsText})`;
|
|
337
|
-
|
|
338
|
-
case 'toPushed':
|
|
339
|
-
return `${arrRef}.toPushed(${xsText}, ${op.itemText})`;
|
|
340
|
-
}
|
|
341
|
-
};
|
|
342
|
-
|
|
343
325
|
const mut_nodesToFix: {
|
|
344
326
|
node: TSESTree.Node;
|
|
345
|
-
|
|
327
|
+
op: CopyOp;
|
|
328
|
+
xsText: string;
|
|
346
329
|
}[] = [];
|
|
347
330
|
|
|
348
331
|
const report = (node: TSESTree.Node, op: CopyOp, xsText: string): void => {
|
|
349
|
-
mut_nodesToFix.push({
|
|
350
|
-
node,
|
|
351
|
-
replacement: toReplacementText(op, xsText),
|
|
352
|
-
});
|
|
332
|
+
mut_nodesToFix.push({ node, op, xsText });
|
|
353
333
|
};
|
|
354
334
|
|
|
355
335
|
/**
|
|
@@ -654,17 +634,22 @@ export const preferCanonicalArraySlicing: TSESLint.RuleModule<
|
|
|
654
634
|
const receiver = callee.object;
|
|
655
635
|
|
|
656
636
|
// `Arr.skip(xs, 1)` → `Arr.tail(xs)`, `Arr.skipLast(xs, 1)` →
|
|
657
|
-
// `Arr.butLast(xs)` (also via a namespace import or an alias
|
|
637
|
+
// `Arr.butLast(xs)` (also via a namespace import or an alias, and
|
|
638
|
+
// through type wrappers), unless a local binding shadows the import
|
|
639
|
+
// where the call is.
|
|
658
640
|
if (methodName === 'skip' || methodName === 'skipLast') {
|
|
641
|
+
const arrObject = skipTypeWrappers(receiver);
|
|
642
|
+
|
|
659
643
|
const isArrObject =
|
|
660
|
-
(
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
644
|
+
isImportedAs(arrObject, arrLocalName) ||
|
|
645
|
+
(arrObject.type === AST_NODE_TYPES.MemberExpression &&
|
|
646
|
+
!arrObject.computed &&
|
|
647
|
+
isImportedAs(
|
|
648
|
+
skipTypeWrappers(arrObject.object),
|
|
649
|
+
namespaceLocalName,
|
|
650
|
+
) &&
|
|
651
|
+
arrObject.property.type === AST_NODE_TYPES.Identifier &&
|
|
652
|
+
arrObject.property.name === 'Arr');
|
|
668
653
|
|
|
669
654
|
const [arrayArg, countArg] = node.arguments;
|
|
670
655
|
|
|
@@ -858,28 +843,34 @@ export const preferCanonicalArraySlicing: TSESLint.RuleModule<
|
|
|
858
843
|
},
|
|
859
844
|
|
|
860
845
|
'Program:exit': () => {
|
|
846
|
+
if (mut_nodesToFix.length === 0) {
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
const arrAt = tsDataForgeValueAt(sourceCode, 'Arr');
|
|
851
|
+
|
|
861
852
|
// Every fix brings `Arr` into scope itself, so that one applied alone
|
|
862
853
|
// (an editor's quick fix) is complete.
|
|
863
854
|
for (const nodeInfo of mut_nodesToFix) {
|
|
864
|
-
const
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
);
|
|
855
|
+
const arr = arrAt(nodeInfo.node);
|
|
856
|
+
|
|
857
|
+
const replacement = toReplacementText(
|
|
858
|
+
arr?.localName ?? 'Arr',
|
|
859
|
+
nodeInfo.op,
|
|
860
|
+
nodeInfo.xsText,
|
|
861
|
+
);
|
|
872
862
|
|
|
873
863
|
context.report({
|
|
874
864
|
node: nodeInfo.node,
|
|
875
865
|
messageId: 'preferCanonicalArraySlicing',
|
|
876
|
-
data: { replacement
|
|
877
|
-
fix:
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
866
|
+
data: { replacement },
|
|
867
|
+
fix:
|
|
868
|
+
arr === undefined
|
|
869
|
+
? null
|
|
870
|
+
: (fixer) => [
|
|
871
|
+
...arr.edits(fixer),
|
|
872
|
+
fixer.replaceText(nodeInfo.node, replacement),
|
|
873
|
+
],
|
|
883
874
|
});
|
|
884
875
|
}
|
|
885
876
|
},
|
|
@@ -887,56 +878,30 @@ export const preferCanonicalArraySlicing: TSESLint.RuleModule<
|
|
|
887
878
|
},
|
|
888
879
|
} as const;
|
|
889
880
|
|
|
890
|
-
/**
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
if (arrImport !== undefined) {
|
|
916
|
-
return {
|
|
917
|
-
ref: arrImport.local.name,
|
|
918
|
-
boundName: arrImport.local.name,
|
|
919
|
-
edits: () => [],
|
|
920
|
-
};
|
|
921
|
-
}
|
|
922
|
-
|
|
923
|
-
const namespace = valueSpecifiers.find(
|
|
924
|
-
(specifier) => specifier.type === AST_NODE_TYPES.ImportNamespaceSpecifier,
|
|
925
|
-
);
|
|
926
|
-
|
|
927
|
-
if (namespace !== undefined) {
|
|
928
|
-
return {
|
|
929
|
-
ref: `${namespace.local.name}.Arr`,
|
|
930
|
-
boundName: namespace.local.name,
|
|
931
|
-
edits: () => [],
|
|
932
|
-
};
|
|
881
|
+
/** The call `op` is rewritten to, writing `Arr` as `arrRef`. */
|
|
882
|
+
const toReplacementText = (
|
|
883
|
+
arrRef: string,
|
|
884
|
+
op: CopyOp,
|
|
885
|
+
xsText: string,
|
|
886
|
+
): string => {
|
|
887
|
+
switch (op.fn) {
|
|
888
|
+
case 'tail':
|
|
889
|
+
case 'butLast':
|
|
890
|
+
return `${arrRef}.${op.fn}(${xsText})`;
|
|
891
|
+
|
|
892
|
+
case 'skip':
|
|
893
|
+
case 'take':
|
|
894
|
+
case 'skipLast':
|
|
895
|
+
case 'takeLast':
|
|
896
|
+
return `${arrRef}.${op.fn}(${xsText}, ${op.count})`;
|
|
897
|
+
|
|
898
|
+
// Emit the curried form for `toUnshifted` so the new element stays in the
|
|
899
|
+
// same textual position as in the `[item, ...xs]` source, keeping the
|
|
900
|
+
// rewrite easy to read.
|
|
901
|
+
case 'toUnshifted':
|
|
902
|
+
return `${arrRef}.toUnshifted(${op.itemText})(${xsText})`;
|
|
903
|
+
|
|
904
|
+
case 'toPushed':
|
|
905
|
+
return `${arrRef}.toPushed(${xsText}, ${op.itemText})`;
|
|
933
906
|
}
|
|
934
|
-
|
|
935
|
-
const binding = bindValueImports(sourceCode, TS_DATA_FORGE_MODULE, ['Arr']);
|
|
936
|
-
|
|
937
|
-
const localName = binding?.localNames[0];
|
|
938
|
-
|
|
939
|
-
return binding === undefined || localName === undefined
|
|
940
|
-
? undefined
|
|
941
|
-
: { ref: localName, boundName: localName, edits: binding.edits };
|
|
942
907
|
};
|
|
@@ -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
|
-
*
|
|
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
|
|
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
|
|
115
|
+
return [valueName ?? unusedValueParamName(scope), keyName];
|
|
116
116
|
}
|
|
117
117
|
|
|
118
118
|
return valueName !== undefined &&
|
|
119
119
|
isReferencedOutside(scope, valueName, dropped)
|
|
120
|
-
?
|
|
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'])
|
|
163
|
+
const objAt = tsDataForgeValuesAt(sourceCode, ['Obj']);
|
|
164
164
|
|
|
165
165
|
const objOptionalAt = tsDataForgeValuesAt(sourceCode, [
|
|
166
166
|
'Obj',
|
|
167
167
|
'Optional',
|
|
168
|
-
])
|
|
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}, ${
|
|
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`
|