Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(prefer-tacit): handling functions that don't map to directly to an eslint node #802

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions src/rules/prefer-tacit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,17 @@ import { ruleNameScope } from "#eslint-plugin-functional/utils/misc";
import { type ESFunction } from "#eslint-plugin-functional/utils/node-types";
import {
createRule,
getESTreeNode,
getTypeOfNode,
getTypeOfTSNode,
type NamedCreateRuleCustomMeta,
type RuleResult,
} from "#eslint-plugin-functional/utils/rule";
import { isNested } from "#eslint-plugin-functional/utils/tree";
import {
isBlockStatement,
isCallExpression,
isDefined,
isFunctionLike,
isIdentifier,
isReturnStatement,
isTSFunctionType,
} from "#eslint-plugin-functional/utils/type-guards";

/**
Expand Down Expand Up @@ -106,13 +103,12 @@ function isCallerViolation(
if (tsDeclaration === undefined) {
return false;
}
const declaration = getESTreeNode(tsDeclaration, context);

return (
isDefined(declaration) &&
(isFunctionLike(declaration) || isTSFunctionType(declaration)) &&
declaration.params.length === caller.arguments.length
);
return getTypeOfTSNode(tsDeclaration, context)
.getCallSignatures()
.some(
(signature) => signature.parameters.length === caller.arguments.length,
);
}

/**
Expand Down
18 changes: 14 additions & 4 deletions src/utils/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
*/
export type BaseOptions = ReadonlyArray<unknown>;

export type RuleDefinition<

Check warning on line 53 in src/utils/rule.ts

View workflow job for this annotation

GitHub Actions / lint_js

Missing JSDoc comment
MessageIds extends string,
Options extends BaseOptions,
> = {
Expand Down Expand Up @@ -129,7 +129,7 @@
name: string,
meta: NamedCreateRuleCustomMeta<MessageIds, Options>,
defaultOptions: Options,
ruleFunctionsMap: RuleFunctionsMap<any, MessageIds, Options>,

Check warning on line 132 in src/utils/rule.ts

View workflow job for this annotation

GitHub Actions / lint_js

Unexpected any. Specify a different type
) {
return createRuleUsingFunction(
name,
Expand All @@ -152,7 +152,7 @@
createFunction: (
context: Readonly<RuleContext<MessageIds, Options>>,
options: Readonly<Options>,
) => RuleFunctionsMap<any, MessageIds, Options>,

Check warning on line 155 in src/utils/rule.ts

View workflow job for this annotation

GitHub Actions / lint_js

Unexpected any. Specify a different type
) {
const ruleCreator = RuleCreator(
(ruleName) =>
Expand All @@ -161,7 +161,7 @@

return ruleCreator<Options, MessageIds>({
name,
meta: meta as any,

Check warning on line 164 in src/utils/rule.ts

View workflow job for this annotation

GitHub Actions / lint_js

Unsafe assignment of an `any` value
defaultOptions,
create: (context, options) => {
const ruleFunctionsMap = createFunction(context, options);
Expand All @@ -188,12 +188,22 @@
node: TSESTree.Node,
context: Context,
): Type {
const parserServices = getParserServices(context);
const { esTreeNodeToTSNodeMap } = getParserServices(context);

const checker = parserServices.program.getTypeChecker();
const { esTreeNodeToTSNodeMap } = parserServices;
const tsNode = esTreeNodeToTSNodeMap.get(node);
return getTypeOfTSNode(tsNode, context);
}

/**
* Get the type of the the given ts node.
*/
export function getTypeOfTSNode<
Context extends RuleContext<string, BaseOptions>,
>(node: TSNode, context: Context): Type {
const { program } = getParserServices(context);
const checker = program.getTypeChecker();

const nodeType = checker.getTypeAtLocation(esTreeNodeToTSNodeMap.get(node));
const nodeType = checker.getTypeAtLocation(node);
const constrained = checker.getBaseConstraintOfType(nodeType);
return constrained ?? nodeType;
}
Expand Down
23 changes: 23 additions & 0 deletions tests/rules/prefer-tacit/ts/invalid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,29 @@ const tests: Array<
},
],
},
// Boolean constructor
{
code: dedent`
const foo = [1, 2, 3].map(x => Boolean(x));
`,
optionsSet: [[]],
errors: [
{
messageId: "generic",
type: AST_NODE_TYPES.ArrowFunctionExpression,
line: 1,
column: 27,
suggestions: [
{
messageId: "generic",
output: dedent`
const foo = [1, 2, 3].map(Boolean);
`,
},
],
},
],
},
// Instantiation Expression not supported.
{
code: dedent`
Expand Down
Loading