@@ -32,6 +32,28 @@ ruleTester({ types: true }).run("prefer-composition", rule, {
3232 }
3333 ` ,
3434 } ,
35+ {
36+ code : stripIndent `
37+ // composed component with private JavaScript property
38+ import { Component, OnDestroy, OnInit } from "@angular/core";
39+ import { of, Subscription } from "rxjs";
40+
41+ @Component({
42+ selector: "composed-component",
43+ template: "<span>{{ value }}</span>"
44+ })
45+ export class ComposedComponent implements OnInit, OnDestroy {
46+ value: string;
47+ #subscription = new Subscription();
48+ ngOnInit() {
49+ this.#subscription.add(of("foo").subscribe(value => this.value = value));
50+ }
51+ ngOnDestroy() {
52+ this.#subscription.unsubscribe();
53+ }
54+ }
55+ ` ,
56+ } ,
3557 {
3658 code : stripIndent `
3759 // variable composed component
@@ -47,7 +69,7 @@ ruleTester({ types: true }).run("prefer-composition", rule, {
4769 private subscription = new Subscription();
4870 ngOnInit() {
4971 let subscription = of("foo").subscribe(value => this.value = value);
50- this.subscription.add(subscription);1
72+ this.subscription.add(subscription);
5173 subscription = of("bar").subscribe(value => this.value = value);
5274 this.subscription.add(subscription);
5375 }
@@ -57,6 +79,31 @@ ruleTester({ types: true }).run("prefer-composition", rule, {
5779 }
5880 ` ,
5981 } ,
82+ {
83+ code : stripIndent `
84+ // variable composed component with private JavaScript property
85+ import { Component, OnDestroy, OnInit } from "@angular/core";
86+ import { of, Subscription } from "rxjs";
87+
88+ @Component({
89+ selector: "variable-composed-component",
90+ template: "<span>{{ value }}</span>"
91+ })
92+ export class VariableComposedComponent implements OnInit, OnDestroy {
93+ value: string;
94+ #subscription = new Subscription();
95+ ngOnInit() {
96+ let subscription = of("foo").subscribe(value => this.value = value);
97+ this.#subscription.add(subscription);
98+ subscription = of("bar").subscribe(value => this.value = value);
99+ this.#subscription.add(subscription);
100+ }
101+ ngOnDestroy() {
102+ this.#subscription.unsubscribe();
103+ }
104+ }
105+ ` ,
106+ } ,
60107 {
61108 code : stripIndent `
62109 // destructured composed component
@@ -145,6 +192,28 @@ ruleTester({ types: true }).run("prefer-composition", rule, {
145192 }
146193 `
147194 ) ,
195+ fromFixture (
196+ stripIndent `
197+ // not unsubscribed component with private JavaScript property
198+ import { Component, OnDestroy, OnInit } from "@angular/core";
199+ import { of, Subscription } from "rxjs";
200+
201+ @Component({
202+ selector: "not-unsubscribed-component",
203+ template: "<span>{{ value }}</span>"
204+ })
205+ export class NotUnsubscribedComponent implements OnInit, OnDestroy {
206+ value: string;
207+ #subscription = new Subscription();
208+ ~~~~~~~~~~~~~ [notUnsubscribed]
209+ ngOnInit() {
210+ this.#subscription.add(of("foo").subscribe(value => this.value = value));
211+ }
212+ ngOnDestroy() {
213+ }
214+ }
215+ `
216+ ) ,
148217 fromFixture (
149218 stripIndent `
150219 // not destroyed component
@@ -165,6 +234,26 @@ ruleTester({ types: true }).run("prefer-composition", rule, {
165234 }
166235 `
167236 ) ,
237+ fromFixture (
238+ stripIndent `
239+ // not destroyed component with Private JavaScript property
240+ import { Component, OnDestroy, OnInit } from "@angular/core";
241+ import { of, Subscription } from "rxjs";
242+
243+ @Component({
244+ selector: "not-destroyed-component",
245+ template: "<span>{{ value }}</span>"
246+ })
247+ export class NotDestroyedComponent implements OnInit {
248+ ~~~~~~~~~~~~~~~~~~~~~ [notImplemented]
249+ value: string;
250+ #subscription = new Subscription();
251+ ngOnInit() {
252+ this.#subscription.add(of("foo").subscribe(value => this.value = value));
253+ }
254+ }
255+ `
256+ ) ,
168257 fromFixture (
169258 stripIndent `
170259 // not declared
0 commit comments