Skip to content

Commit 416a961

Browse files
Merge pull request #488 from eslint-functional/issue/486
2 parents 6ce8d29 + a277c2a commit 416a961

File tree

5 files changed

+46
-63
lines changed

5 files changed

+46
-63
lines changed

src/rules/prefer-tacit.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -134,58 +134,57 @@ function isCallerViolation(
134134
const declaration = getESTreeNode(tsDeclaration, context);
135135

136136
return (
137-
(isDefined(declaration) &&
138-
(isFunctionLike(declaration) || isTSFunctionType(declaration)) &&
139-
declaration.params.length === caller.arguments.length) ||
140-
// Check for optional params.
141-
((tsDeclaration as FunctionLikeDeclaration).parameters !== undefined &&
142-
(tsDeclaration as FunctionLikeDeclaration).parameters
143-
.slice(caller.arguments.length)
144-
.every(
145-
(param) =>
146-
param.initializer !== undefined || param.questionToken !== undefined
147-
))
137+
isDefined(declaration) &&
138+
(isFunctionLike(declaration) || isTSFunctionType(declaration)) &&
139+
declaration.params.length === caller.arguments.length
148140
);
149141
}
150142

151143
function fixFunctionCallToReference(
144+
context: ReadonlyDeep<
145+
TSESLint.RuleContext<keyof typeof errorMessages, Options>
146+
>,
152147
fixer: TSESLint.RuleFixer,
153148
node: ESFunction,
154-
caller: ReadonlyDeep<TSESTree.CallExpression>,
155-
callee: ReadonlyDeep<TSESTree.Identifier>
149+
caller: ReadonlyDeep<TSESTree.CallExpression>
156150
): TSESLint.RuleFix[] | null {
157-
const calleeName = callee.name;
158-
159151
// Fix to Instantiation Expression.
160152
if (
161153
caller.typeParameters !== undefined &&
162154
caller.typeParameters.params.length > 0
163155
) {
164156
return isTS4dot7
165157
? [
166-
fixer.removeRange([node.range[0], callee.range[0]]),
158+
fixer.removeRange([node.range[0], caller.callee.range[0]]),
167159
fixer.removeRange([caller.typeParameters.range[1], node.range[1]]),
168160
]
169161
: null;
170162
}
171163

172-
return [fixer.replaceText(node as TSESTree.Node, calleeName)];
164+
return [
165+
fixer.replaceText(
166+
node as TSESTree.Node,
167+
context.getSourceCode().getText(caller.callee as TSESTree.Node)
168+
),
169+
];
173170
}
174171

175172
/**
176173
* Creates the fixer function that returns the instruction how to fix violations of this rule to valid code
177174
*/
178175
function buildFixer(
176+
context: ReadonlyDeep<
177+
TSESLint.RuleContext<keyof typeof errorMessages, Options>
178+
>,
179179
node: ESFunction,
180-
caller: ReadonlyDeep<TSESTree.CallExpression>,
181-
callee: ReadonlyDeep<TSESTree.Identifier>
180+
caller: ReadonlyDeep<TSESTree.CallExpression>
182181
): TSESLint.ReportFixFunction {
183182
return (fixer) => {
184183
const functionCallToReference = fixFunctionCallToReference(
184+
context,
185185
fixer,
186186
node,
187-
caller,
188-
callee
187+
caller
189188
);
190189
if (functionCallToReference === null) {
191190
return null;
@@ -224,7 +223,6 @@ function getCallDescriptors(
224223
const [{ assumeTypes }] = options;
225224

226225
if (
227-
isIdentifier(caller.callee) &&
228226
node.params.length === caller.arguments.length &&
229227
node.params.every((param, index) => {
230228
const callArg = caller.arguments[index];
@@ -254,7 +252,7 @@ function getCallDescriptors(
254252
// Unless user specifies they want it.
255253
(typeof assumeTypes === "object" && !assumeTypes.allowFixer)
256254
? null
257-
: buildFixer(node, caller, caller.callee),
255+
: buildFixer(context, node, caller),
258256
},
259257
];
260258
}

tests/rules/prefer-tacit/es6/invalid.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,6 @@ const tests: ReadonlyArray<InvalidTestCase> = [
3434
},
3535
],
3636
},
37-
// Default parameters.
38-
{
39-
code: dedent`
40-
function f(x, y = 10) {}
41-
const foo = x => f(x);
42-
`,
43-
optionsSet: [[{ assumeTypes: { allowFixer: true } }]],
44-
output: dedent`
45-
function f(x, y = 10) {}
46-
const foo = f;
47-
`,
48-
errors: [
49-
{
50-
messageId: "generic",
51-
type: "ArrowFunctionExpression",
52-
line: 2,
53-
column: 13,
54-
},
55-
],
56-
},
5737
];
5838

5939
export default tests;

tests/rules/prefer-tacit/es6/valid.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import dedent from "dedent";
2+
13
import type { ValidTestCase } from "~/tests/helpers/util";
24

35
const tests: ReadonlyArray<ValidTestCase> = [
@@ -6,6 +8,14 @@ const tests: ReadonlyArray<ValidTestCase> = [
68
code: `const foo = x => f(x);`,
79
optionsSet: [[]],
810
},
11+
// Default parameters.
12+
{
13+
code: dedent`
14+
function f(x, y = 10) {}
15+
const foo = x => f(x);
16+
`,
17+
optionsSet: [[]],
18+
},
919
];
1020

1121
export default tests;

tests/rules/prefer-tacit/ts/invalid.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -124,26 +124,6 @@ const tests: ReadonlyArray<InvalidTestCase> = [
124124
},
125125
],
126126
},
127-
// Optional parameters.
128-
{
129-
code: dedent`
130-
function f(x: number, y?: number) {}
131-
const foo = x => f(x);
132-
`,
133-
optionsSet: [[]],
134-
output: dedent`
135-
function f(x: number, y?: number) {}
136-
const foo = f;
137-
`,
138-
errors: [
139-
{
140-
messageId: "generic",
141-
type: "ArrowFunctionExpression",
142-
line: 2,
143-
column: 13,
144-
},
145-
],
146-
},
147127
// Type parameters
148128
{
149129
code: dedent`

tests/rules/prefer-tacit/ts/valid.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ const tests: ReadonlyArray<ValidTestCase> = [
3636
`,
3737
optionsSet: [[]],
3838
},
39+
// Optional parameters.
40+
{
41+
code: dedent`
42+
function f(x: number, y?: number) {}
43+
const foo = x => f(x);
44+
`,
45+
optionsSet: [[]],
46+
},
47+
{
48+
code: dedent`
49+
const a = ['1', '2'];
50+
a.map((x) => Number.parseInt(x));
51+
`,
52+
optionsSet: [[]],
53+
},
3954
];
4055

4156
export default tests;

0 commit comments

Comments
 (0)