@@ -20,6 +20,16 @@ ruleTester({ types: true }).run('no-topromise', noTopromiseRule, {
2020 };
2121 a.toPromise().then(value => console.log(value));
2222 ` ,
23+ stripIndent `
24+ // no imports
25+ class Observable {
26+ toPromise() {
27+ return Promise.resolve("a");
28+ }
29+ }
30+ const a = new Observable();
31+ a.toPromise().then(value => console.log(value));
32+ ` ,
2333 ] ,
2434 invalid : [
2535 fromFixture (
@@ -28,17 +38,197 @@ ruleTester({ types: true }).run('no-topromise', noTopromiseRule, {
2838 import { of } from "rxjs";
2939 const a = of("a");
3040 a.toPromise().then(value => console.log(value));
31- ~~~~~~~~~ [forbidden]
41+ ~~~~~~~~~ [forbidden suggest 0 1 ]
3242 ` ,
43+ {
44+ suggestions : [
45+ {
46+ messageId : 'suggestLastValueFrom' ,
47+ output : stripIndent `
48+ // observable toPromise
49+ import { of, lastValueFrom } from "rxjs";
50+ const a = of("a");
51+ lastValueFrom(a).then(value => console.log(value));
52+ ` ,
53+ } ,
54+ {
55+ messageId : 'suggestFirstValueFrom' ,
56+ output : stripIndent `
57+ // observable toPromise
58+ import { of, firstValueFrom } from "rxjs";
59+ const a = of("a");
60+ firstValueFrom(a).then(value => console.log(value));
61+ ` ,
62+ } ,
63+ ] ,
64+ } ,
3365 ) ,
3466 fromFixture (
3567 stripIndent `
3668 // subject toPromise
3769 import { Subject } from "rxjs";
3870 const a = new Subject<string>();
3971 a.toPromise().then(value => console.log(value));
40- ~~~~~~~~~ [forbidden]
72+ ~~~~~~~~~ [forbidden suggest 0 1]
73+ ` ,
74+ {
75+ suggestions : [
76+ {
77+ messageId : 'suggestLastValueFrom' ,
78+ output : stripIndent `
79+ // subject toPromise
80+ import { Subject, lastValueFrom } from "rxjs";
81+ const a = new Subject<string>();
82+ lastValueFrom(a).then(value => console.log(value));
83+ ` ,
84+ } ,
85+ {
86+ messageId : 'suggestFirstValueFrom' ,
87+ output : stripIndent `
88+ // subject toPromise
89+ import { Subject, firstValueFrom } from "rxjs";
90+ const a = new Subject<string>();
91+ firstValueFrom(a).then(value => console.log(value));
92+ ` ,
93+ } ,
94+ ] ,
95+ } ,
96+ ) ,
97+ fromFixture (
98+ stripIndent `
99+ // weird whitespace
100+ import { of } from "rxjs";
101+ const a = { foo$: of("a") };
102+ a
103+ .foo$
104+ .toPromise().then(value => console.log(value))
105+ ~~~~~~~~~ [forbidden suggest 0 1]
106+ .catch(error => console.error(error));
107+ ` ,
108+ {
109+ suggestions : [
110+ {
111+ messageId : 'suggestLastValueFrom' ,
112+ output : stripIndent `
113+ // weird whitespace
114+ import { of, lastValueFrom } from "rxjs";
115+ const a = { foo$: of("a") };
116+ lastValueFrom(a
117+ .foo$).then(value => console.log(value))
118+ .catch(error => console.error(error));
119+ ` ,
120+ } ,
121+ {
122+ messageId : 'suggestFirstValueFrom' ,
123+ output : stripIndent `
124+ // weird whitespace
125+ import { of, firstValueFrom } from "rxjs";
126+ const a = { foo$: of("a") };
127+ firstValueFrom(a
128+ .foo$).then(value => console.log(value))
129+ .catch(error => console.error(error));
130+ ` ,
131+ } ,
132+ ] ,
133+ } ,
134+ ) ,
135+ fromFixture (
136+ stripIndent `
137+ // lastValueFrom already imported
138+ import { lastValueFrom as lvf, of } from "rxjs";
139+ const a = of("a");
140+ a.toPromise().then(value => console.log(value));
141+ ~~~~~~~~~ [forbidden suggest 0 1]
142+ ` ,
143+ {
144+ suggestions : [
145+ {
146+ messageId : 'suggestLastValueFrom' ,
147+ output : stripIndent `
148+ // lastValueFrom already imported
149+ import { lastValueFrom as lvf, of } from "rxjs";
150+ const a = of("a");
151+ lvf(a).then(value => console.log(value));
152+ ` ,
153+ } ,
154+ {
155+ messageId : 'suggestFirstValueFrom' ,
156+ output : stripIndent `
157+ // lastValueFrom already imported
158+ import { lastValueFrom as lvf, of, firstValueFrom } from "rxjs";
159+ const a = of("a");
160+ firstValueFrom(a).then(value => console.log(value));
161+ ` ,
162+ } ,
163+ ] ,
164+ } ,
165+ ) ,
166+ fromFixture (
167+ stripIndent `
168+ // rxjs not already imported
169+ import { fromFetch } from "rxjs/fetch";
170+
171+ const a = fromFetch("https://api.some.com");
172+ a.toPromise().then(value => console.log(value));
173+ ~~~~~~~~~ [forbidden suggest 0 1]
174+ ` ,
175+ {
176+ suggestions : [
177+ {
178+ messageId : 'suggestLastValueFrom' ,
179+ output : stripIndent `
180+ // rxjs not already imported
181+ import { fromFetch } from "rxjs/fetch";
182+ import { lastValueFrom } from "rxjs";
183+
184+ const a = fromFetch("https://api.some.com");
185+ lastValueFrom(a).then(value => console.log(value));
186+ ` ,
187+ } ,
188+ {
189+ messageId : 'suggestFirstValueFrom' ,
190+ output : stripIndent `
191+ // rxjs not already imported
192+ import { fromFetch } from "rxjs/fetch";
193+ import { firstValueFrom } from "rxjs";
194+
195+ const a = fromFetch("https://api.some.com");
196+ firstValueFrom(a).then(value => console.log(value));
197+ ` ,
198+ } ,
199+ ] ,
200+ } ,
201+ ) ,
202+ fromFixture (
203+ stripIndent `
204+ // namespace import
205+ import * as Rx from "rxjs";
206+ const a = Rx.of("a");
207+ a.toPromise().then(value => console.log(value));
208+ ~~~~~~~~~ [forbidden suggest 0 1]
41209 ` ,
210+ {
211+ suggestions : [
212+ {
213+ messageId : 'suggestLastValueFrom' ,
214+ output : stripIndent `
215+ // namespace import
216+ import * as Rx from "rxjs";
217+ const a = Rx.of("a");
218+ Rx.lastValueFrom(a).then(value => console.log(value));
219+ ` ,
220+ } ,
221+ {
222+ messageId : 'suggestFirstValueFrom' ,
223+ output : stripIndent `
224+ // namespace import
225+ import * as Rx from "rxjs";
226+ const a = Rx.of("a");
227+ Rx.firstValueFrom(a).then(value => console.log(value));
228+ ` ,
229+ } ,
230+ ] ,
231+ } ,
42232 ) ,
43233 ] ,
44234} ) ;
0 commit comments