Skip to content

Commit

Permalink
no-unsafe-takeuntil: Accept object properties as operators (#90)
Browse files Browse the repository at this point in the history
* feat: add acceptObjectProperties option

* test: add tests for acceptObjectProperties option

* feat: add description for acceptObjectProperties option

* fix: delete acceptObjectProperties option
  • Loading branch information
alwonder committed Dec 31, 2021
1 parent c8173e5 commit 4fe7423
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
18 changes: 14 additions & 4 deletions source/rules/no-unsafe-takeuntil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getTypeServices,
isCallExpression,
isIdentifier,
isMemberExpression,
} from "eslint-etc";
import { ruleCreator } from "../utils";

Expand Down Expand Up @@ -83,7 +84,7 @@ const rule = ruleCreator({
const { couldBeObservable } = getTypeServices(context);

return {
[`CallExpression[callee.property.name='pipe'] > CallExpression[callee.name=${checkedOperatorsRegExp}]`]:
[`CallExpression[callee.property.name='pipe'] > CallExpression[callee.name=${checkedOperatorsRegExp}], CallExpression[callee.property.name=${checkedOperatorsRegExp}]`]:
(node: es.CallExpression) => {
const pipeCallExpression = getParent(node) as es.CallExpression;
if (
Expand All @@ -100,11 +101,20 @@ const rule = ruleCreator({
return state;
}

if (!isCallExpression(arg) || !isIdentifier(arg.callee)) {
if (!isCallExpression(arg)) {
return "disallowed";
}

if (checkedOperatorsRegExp.test(arg.callee.name)) {
let operatorName: string
if (isIdentifier(arg.callee)) {
operatorName = arg.callee.name
} else if (isMemberExpression(arg.callee) && isIdentifier(arg.callee.property)) {
operatorName = arg.callee.property.name;
} else {
return "disallowed";
}

if (checkedOperatorsRegExp.test(operatorName)) {
if (state === "disallowed") {
context.report({
messageId: "forbidden",
Expand All @@ -114,7 +124,7 @@ const rule = ruleCreator({
return "taken";
}

if (!allow.includes(arg.callee.name)) {
if (!allow.includes(operatorName)) {
return "disallowed";
}
return state;
Expand Down
23 changes: 23 additions & 0 deletions tests/rules/no-unsafe-takeuntil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,5 +288,28 @@ ruleTester({ types: true }).run("no-unsafe-takeuntil", rule, {
],
}
),
fromFixture(
stripIndent`
// before switchMap as an alias and a class member
import { of } from "rxjs";
import { switchMap, takeUntil } from "rxjs/operators";
declare const untilDestroyed: Function;
const a = of("a");
const b = of("b");
const c = of("d");
const d = a.pipe(this.untilDestroyed(), switchMap(_ => b)).subscribe();
~~~~~~~~~~~~~~~~~~~ [forbidden]
`,
{
options: [
{
alias: ["untilDestroyed"],
},
],
}
),
],
});

0 comments on commit 4fe7423

Please sign in to comment.