forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
missing_injectable_rule_spec.ts
271 lines (209 loc) · 9.5 KB
/
missing_injectable_rule_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {readFileSync, writeFileSync} from 'fs';
import {dirname, join} from 'path';
import * as shx from 'shelljs';
import {Configuration, Linter} from 'tslint';
describe('Google3 missing injectable tslint rule', () => {
const rulesDirectory =
dirname(require.resolve('../../migrations/google3/noMissingInjectableRule'));
let tmpDir: string;
beforeEach(() => {
tmpDir = join(process.env['TEST_TMPDIR'] !, 'google3-test');
shx.mkdir('-p', tmpDir);
writeFile('tsconfig.json', JSON.stringify({compilerOptions: {module: 'es2015'}}));
});
afterEach(() => shx.rm('-r', tmpDir));
function runTSLint(fix = true) {
const program = Linter.createProgram(join(tmpDir, 'tsconfig.json'));
const linter = new Linter({fix, rulesDirectory: [rulesDirectory]}, program);
const config = Configuration.parseConfigFile({rules: {'no-missing-injectable': true}});
program.getRootFileNames().forEach(fileName => {
linter.lint(fileName, program.getSourceFile(fileName) !.getFullText(), config);
});
return linter;
}
function writeFile(fileName: string, content: string) {
writeFileSync(join(tmpDir, fileName), content);
}
function getFile(fileName: string) { return readFileSync(join(tmpDir, fileName), 'utf8'); }
describe('NgModule', () => createTests('NgModule', 'providers'));
describe('Directive', () => createTests('Directive', 'providers'));
describe('Component', () => {
createTests('Component', 'providers');
createTests('Component', 'viewProviders');
it('should migrate all providers defined in "viewProviders" and "providers" in the ' +
'same component',
() => {
writeFile('/index.ts', `
import {Component} from '@angular/core';
export class MyService {}
export class MySecondService {}
@Component({
providers: [MyService],
viewProviders: [MySecondService],
})
export class TestClass {}
`);
const result = runTSLint().getResult();
expect(result.errorCount).toBe(0);
expect(getFile('/index.ts')).toMatch(/@Injectable\(\)\s+export class MyService/);
expect(getFile('/index.ts')).toMatch(/@Injectable\(\)\s+export class MySecondService/);
expect(getFile('/index.ts')).toContain(`{ Component, Injectable } from '@angular/core`);
});
});
function createTests(
type: 'NgModule' | 'Directive' | 'Component', propName: 'providers' | 'viewProviders') {
it('should create proper failures for missing injectable providers', () => {
writeFile('index.ts', `
import { ${type} } from '@angular/core';
export class A {}
@${type}({${propName}: [A]})
export class TestClass {}
`);
const linter = runTSLint(false);
const failures = linter.getResult().failures;
expect(failures.length).toBe(2);
expect(failures[0].getFailure())
.toMatch(/Class needs to be decorated with "@Injectable\(\)".*provided by "TestClass"/);
expect(failures[0].getStartPosition().getLineAndCharacter()).toEqual({line: 3, character: 8});
expect(failures[1].getFailure()).toMatch(/Import needs to be updated to import.*Injectable/);
expect(failures[1].getStartPosition().getLineAndCharacter())
.toEqual({line: 1, character: 15});
});
it('should update provider classes which need to be migrated in Ivy', () => {
writeFile('/index.ts', `
import {Pipe, Directive, Component, NgModule} from '@angular/core';
@Pipe()
export class WithPipe {}
@Directive()
export class WithDirective {}
@Component()
export class WithComponent {}
export class MyServiceA {}
export class MyServiceB {}
export class MyServiceC {}
export class MyServiceD {}
export class MyServiceE {}
export class MyServiceF {}
export class MyServiceG {}
export class MyServiceH {}
@${type}({${propName}: [
WithPipe,
[
WithDirective,
WithComponent,
MyServiceA,
]
MyServiceB,
{provide: MyServiceC},
{provide: null, useClass: MyServiceD},
{provide: null, useExisting: MyServiceE},
{provide: MyServiceF, useFactory: () => null},
{provide: MyServiceG, useValue: null},
{provide: MyServiceH, deps: []},
]})
export class TestClass {}
`);
runTSLint();
expect(getFile('/index.ts')).toMatch(/'@angular\/core';\s+@Pipe\(\)\s+export class WithPipe/);
expect(getFile('/index.ts'))
.toMatch(/WithPipe {}\s+@Directive\(\)\s+export class WithDirective/);
expect(getFile('/index.ts'))
.toMatch(/WithDirective {}\s+@Component\(\)\s+export class WithComponent/);
expect(getFile('/index.ts')).toMatch(/@Injectable\(\)\s+export class MyServiceA/);
expect(getFile('/index.ts')).toMatch(/@Injectable\(\)\s+export class MyServiceB/);
expect(getFile('/index.ts')).toMatch(/MyServiceB {}\s+export class MyServiceC/);
expect(getFile('/index.ts')).toMatch(/@Injectable\(\)\s+export class MyServiceD/);
expect(getFile('/index.ts')).toMatch(/MyServiceD {}\s+export class MyServiceE/);
expect(getFile('/index.ts')).toMatch(/MyServiceE {}\s+export class MyServiceF/);
expect(getFile('/index.ts')).toMatch(/MyServiceF {}\s+export class MyServiceG/);
expect(getFile('/index.ts')).toMatch(/MyServiceG {}\s+export class MyServiceH/);
expect(getFile('/index.ts')).toContain(`{ provide: MyServiceC, useValue: undefined },`);
});
it(`should migrate provider once if referenced in multiple ${type} definitions`, () => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
export class ServiceA {}
@${type}({${propName}: [ServiceA]})
export class TestClass {}
`);
writeFile('/second.ts', `
import {${type}} from '@angular/core';
import {ServiceA} from './index';
export class ServiceB {}
@${type}({${propName}: [ServiceA, ServiceB]})
export class TestClass2 {}
`);
runTSLint();
expect(getFile('/index.ts'))
.toMatch(/@angular\/core';\s+@Injectable\(\)\s+export class ServiceA/);
expect(getFile('/index.ts')).toContain(`{ ${type}, Injectable } from '@angular/core`);
expect(getFile('/second.ts')).toMatch(/@Injectable\(\)\s+export class ServiceB/);
expect(getFile('/second.ts')).toContain(`{ ${type}, Injectable } from '@angular/core`);
});
it('should warn if a referenced provider could not be resolved', () => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
@${type}({${propName}: [NotPresent]})
export class TestClass {}
`);
const linter = runTSLint();
const failures = linter.getResult().failures;
expect(failures.length).toBe(1);
expect(failures[0].getFailure()).toMatch(/Provider is not statically analyzable./);
expect(failures[0].getStartPosition().getLineAndCharacter())
.toEqual({line: 3, character: 14 + type.length + propName.length});
});
it(`should warn if the "${propName}" value could not be resolved`, () => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
@${type}({${propName}: NOT_ANALYZABLE)
export class TestClass {}
`);
const linter = runTSLint();
const failures = linter.getResult().failures;
expect(failures.length).toBe(1);
expect(failures[0].getFailure()).toMatch(/Providers.*not statically analyzable./);
expect(failures[0].getStartPosition().getLineAndCharacter())
.toEqual({line: 3, character: 13 + type.length + propName.length});
});
it('should create new import for @Injectable when migrating provider', () => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
import {MyService, MySecondService} from './service';
@${type}({${propName}: [MyService, MySecondService]})
export class TestClass {}
`);
writeFile('/service.ts', `export class MyService {}
export class MySecondService {}
`);
runTSLint();
expect(getFile('/service.ts')).toMatch(/@Injectable\(\)\s+export class MyService/);
expect(getFile('/service.ts')).toMatch(/@Injectable\(\)\s+export class MySecondService/);
expect(getFile('/service.ts')).toMatch(/import { Injectable } from "@angular\/core";/);
});
it('should remove @Inject decorator for providers which are migrated', () => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';
import {MyService} from './service';
@${type}({${propName}: [MyService]})
export class TestClass {}
`);
writeFile('/service.ts', `
import {Inject} from '@angular/core';
@Inject()
export class MyService {}
`);
runTSLint();
expect(getFile('/service.ts')).toMatch(/core';\s+@Injectable\(\)\s+export class MyService/);
expect(getFile('/service.ts'))
.toMatch(/import { Inject, Injectable } from '@angular\/core';/);
});
}
});