Skip to content

fix: prevent to report prefer-tacit in direct child of getter #965

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions src/rules/prefer-tacit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ function isCallerViolation(
.some((signature) => signature.parameters.length === caller.arguments.length);
}

/**
* Is the given node a direct child of a getter.
*/
function isDirectChildOfGetter(node: TSESTree.Node): boolean {
const { parent } = node;
if (parent?.type !== TSESTree.AST_NODE_TYPES.Property) {
return false;
}

return parent.kind === 'get';
}

/**
* Get the fixes for a call to a reference violation.
*/
Expand Down Expand Up @@ -260,6 +272,13 @@ function checkFunction(
context: Readonly<RuleContext<keyof typeof errorMessages, RawOptions>>,
options: RawOptions,
): RuleResult<keyof typeof errorMessages, RawOptions> {
if (isDirectChildOfGetter(node)) {
return {
context,
descriptors: [],
};
}

return {
context,
descriptors: [
Expand Down
17 changes: 17 additions & 0 deletions tests/rules/prefer-tacit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,23 @@ describe(name, () => {
`);
});

it("doesn't report getters", () => {
valid(dedent`
const foo = () => {
const getBar = () => 1;
const setBar = (value: number) => {};
return {
get baz() {
return getBar();
},
set baz(value) {
setBar(value);
},
};
};
`);
});

describe("options", () => {
describe("checkMemberExpressions", () => {
it("doesn't report member expressions when disabled", () => {
Expand Down