Skip to content

fix: allow prefer-takeuntil and prefer-composition rules to support p… #12

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 2 commits 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
Empty file modified .husky/pre-commit
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"dependencies": {
"@typescript-eslint/experimental-utils": "^5.0.0",
"common-tags": "^1.8.0",
"eslint-etc": "^5.0.0",
"eslint-etc": "^5.2.0",
"requireindex": "~1.2.0",
"tslib": "^2.0.0"
},
Expand Down
9 changes: 8 additions & 1 deletion source/rules/prefer-composition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
isAssignmentExpression,
isCallExpression,
isIdentifier,
isPrivateIdentifier,
isMemberExpression,
isVariableDeclarator,
} from "eslint-etc";
Expand Down Expand Up @@ -145,7 +146,11 @@ const rule = ruleCreator({
const { callee } = callExpression;
if (isMemberExpression(callee)) {
const { object } = callee;
if (isMemberExpression(object) && isIdentifier(object.property)) {
if (
isMemberExpression(object) &&
(isIdentifier(object.property) ||
isPrivateIdentifier(object.property))
) {
return object.property.name;
}
if (isIdentifier(object)) {
Expand Down Expand Up @@ -186,6 +191,7 @@ const rule = ruleCreator({
// subscription or if it's assigned to a variable that is added to a
// subscription.
const { addCallExpressions, subscriptions } = entry;

const parent = getParent(callExpression);
if (!parent) {
return false;
Expand All @@ -208,6 +214,7 @@ const rule = ruleCreator({
subscriptions.add(name);
return true;
}

if (isVariableDeclarator(parent) && isIdentifier(parent.id)) {
return isVariableComposed(parent.id, entry);
}
Expand Down
7 changes: 4 additions & 3 deletions source/rules/prefer-takeuntil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
isMemberExpression,
isThisExpression,
} from "eslint-etc";
import { ruleCreator } from "../utils";
import { ruleCreator, isPrivateIdentifier } from "../utils";

const messages = {
noDestroy: "`ngOnDestroy` is not implemented.",
Expand Down Expand Up @@ -207,7 +207,7 @@ const rule = ruleCreator({
if (
isMemberExpression(arg) &&
isThisExpression(arg.object) &&
isIdentifier(arg.property)
(isIdentifier(arg.property) || isPrivateIdentifier(arg.property))
) {
return { found: true, name: arg.property.name };
} else if (arg && isIdentifier(arg)) {
Expand All @@ -233,7 +233,8 @@ const rule = ruleCreator({
(isMemberExpression(callee) &&
isMemberExpression(callee.object) &&
isThisExpression(callee.object.object) &&
isIdentifier(callee.object.property) &&
(isIdentifier(callee.object.property) ||
isPrivateIdentifier(callee.object.property)) &&
callee.object.property.name === name)
);
return Boolean(callExpression);
Expand Down
91 changes: 90 additions & 1 deletion tests/rules/prefer-composition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ ruleTester({ types: true }).run("prefer-composition", rule, {
}
`,
},
{
code: stripIndent`
// composed component with private JavaScript property
import { Component, OnDestroy, OnInit } from "@angular/core";
import { of, Subscription } from "rxjs";

@Component({
selector: "composed-component",
template: "<span>{{ value }}</span>"
})
export class ComposedComponent implements OnInit, OnDestroy {
value: string;
#subscription = new Subscription();
ngOnInit() {
this.#subscription.add(of("foo").subscribe(value => this.value = value));
}
ngOnDestroy() {
this.#subscription.unsubscribe();
}
}
`,
},
{
code: stripIndent`
// variable composed component
Expand All @@ -47,7 +69,7 @@ ruleTester({ types: true }).run("prefer-composition", rule, {
private subscription = new Subscription();
ngOnInit() {
let subscription = of("foo").subscribe(value => this.value = value);
this.subscription.add(subscription);1
this.subscription.add(subscription);
subscription = of("bar").subscribe(value => this.value = value);
this.subscription.add(subscription);
}
Expand All @@ -57,6 +79,31 @@ ruleTester({ types: true }).run("prefer-composition", rule, {
}
`,
},
{
code: stripIndent`
// variable composed component with private JavaScript property
import { Component, OnDestroy, OnInit } from "@angular/core";
import { of, Subscription } from "rxjs";

@Component({
selector: "variable-composed-component",
template: "<span>{{ value }}</span>"
})
export class VariableComposedComponent implements OnInit, OnDestroy {
value: string;
#subscription = new Subscription();
ngOnInit() {
let subscription = of("foo").subscribe(value => this.value = value);
this.#subscription.add(subscription);
subscription = of("bar").subscribe(value => this.value = value);
this.#subscription.add(subscription);
}
ngOnDestroy() {
this.#subscription.unsubscribe();
}
}
`,
},
{
code: stripIndent`
// destructured composed component
Expand Down Expand Up @@ -145,6 +192,28 @@ ruleTester({ types: true }).run("prefer-composition", rule, {
}
`
),
fromFixture(
stripIndent`
// not unsubscribed component with private JavaScript property
import { Component, OnDestroy, OnInit } from "@angular/core";
import { of, Subscription } from "rxjs";

@Component({
selector: "not-unsubscribed-component",
template: "<span>{{ value }}</span>"
})
export class NotUnsubscribedComponent implements OnInit, OnDestroy {
value: string;
#subscription = new Subscription();
~~~~~~~~~~~~~ [notUnsubscribed]
ngOnInit() {
this.#subscription.add(of("foo").subscribe(value => this.value = value));
}
ngOnDestroy() {
}
}
`
),
fromFixture(
stripIndent`
// not destroyed component
Expand All @@ -165,6 +234,26 @@ ruleTester({ types: true }).run("prefer-composition", rule, {
}
`
),
fromFixture(
stripIndent`
// not destroyed component with Private JavaScript property
import { Component, OnDestroy, OnInit } from "@angular/core";
import { of, Subscription } from "rxjs";

@Component({
selector: "not-destroyed-component",
template: "<span>{{ value }}</span>"
})
export class NotDestroyedComponent implements OnInit {
~~~~~~~~~~~~~~~~~~~~~ [notImplemented]
value: string;
#subscription = new Subscription();
ngOnInit() {
this.#subscription.add(of("foo").subscribe(value => this.value = value));
}
}
`
),
fromFixture(
stripIndent`
// not declared
Expand Down
Loading