-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathdoctor-service.ts
257 lines (247 loc) · 8.87 KB
/
doctor-service.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
import { DoctorService } from "../../lib/services/doctor-service";
import { Yok } from "../../lib/common/yok";
import { LoggerStub } from "../stubs";
import { assert } from "chai";
import * as path from "path";
class DoctorServiceInheritor extends DoctorService {
constructor($analyticsService: IAnalyticsService,
$hostInfo: IHostInfo,
$logger: ILogger,
$childProcess: IChildProcess,
$injector: IInjector,
$projectDataService: IProjectDataService,
$fs: IFileSystem,
$terminalSpinnerService: ITerminalSpinnerService,
$versionsService: IVersionsService) {
super($analyticsService, $hostInfo, $logger, $childProcess, $injector, $projectDataService, $fs, $terminalSpinnerService, $versionsService);
}
public getDeprecatedShortImportsInFiles(files: string[], projectDir: string): { file: string, line: string }[] {
return super.getDeprecatedShortImportsInFiles(files, projectDir);
}
}
describe("doctorService", () => {
const createTestInjector = (): IInjector => {
const testInjector = new Yok();
testInjector.register("doctorService", DoctorServiceInheritor);
testInjector.register("analyticsService", {});
testInjector.register("hostInfo", {});
testInjector.register("logger", LoggerStub);
testInjector.register("childProcess", {});
testInjector.register("projectDataService", {});
testInjector.register("fs", {});
testInjector.register("terminalSpinnerService", {});
testInjector.register("versionsService", {});
return testInjector;
};
describe("checkForDeprecatedShortImportsInAppDir", () => {
const tnsCoreModulesDirs = [
"application",
"data",
"text"
];
const testData: { filesContents: IStringDictionary, expectedShortImports: any[] }[] = [
{
filesContents: {
file1: 'const application = require("application");'
},
expectedShortImports: [{ file: "file1", line: 'const application = require("application")' }]
},
{
filesContents: {
file1: 'const application = require("tns-core-modules/application");'
},
expectedShortImports: []
},
{
filesContents: {
file1: 'const Observable = require("data/observable").Observable;'
},
expectedShortImports: [{ file: "file1", line: 'const Observable = require("data/observable").Observable' }]
},
{
filesContents: {
file1: 'const Observable = require("tns-core-modules/data/observable").Observable;'
},
expectedShortImports: []
},
{
filesContents: {
file1: 'import * as application from "application";'
},
expectedShortImports: [{ file: "file1", line: 'import * as application from "application"' }]
},
{
filesContents: {
file1: 'import * as application from "tns-core-modules/application";'
},
expectedShortImports: []
},
{
filesContents: {
file1: 'import { run } from "application";'
},
expectedShortImports: [{ file: "file1", line: 'import { run } from "application"' }]
},
{
filesContents: {
file1: 'import { run } from "tns-core-modules/application";'
},
expectedShortImports: []
},
{
// Using single quotes
filesContents: {
file1: "import { run } from 'application';"
},
expectedShortImports: [{ file: "file1", line: "import { run } from 'application'" }]
},
{
// Using single quotes
filesContents: {
file1: "import { run } from 'tns-core-modules/application';"
},
expectedShortImports: []
},
{
filesContents: {
file1: `const application = require("application");
const Observable = require("data/observable").Observable;
`
},
expectedShortImports: [
{ file: "file1", line: 'const application = require("application")' },
{ file: "file1", line: 'const Observable = require("data/observable").Observable' },
]
},
{
filesContents: {
file1: `const application = require("application");
const Observable = require("tns-core-modules/data/observable").Observable;
`
},
expectedShortImports: [
{ file: "file1", line: 'const application = require("application")' },
]
},
{
filesContents: {
file1: `const application = require("application");
const Observable = require("tns-core-modules/data/observable").Observable;
`,
file2: `const application = require("tns-core-modules/application");
const Observable = require("data/observable").Observable;`
},
expectedShortImports: [
{ file: "file1", line: 'const application = require("application")' },
{ file: "file2", line: 'const Observable = require("data/observable").Observable' },
]
},
{
filesContents: {
// this is not from tns-core-modules
file1: `const application = require("application1");
const Observable = require("tns-core-modules/data/observable").Observable;
`,
file2: `const application = require("some-name-tns-core-modules-widgets/application");
const Observable = require("tns-core-modules-widgets/data/observable").Observable;`
},
expectedShortImports: []
},
{
filesContents: {
// several statements on one line
file1: 'const _ = require("lodash");console.log("application");'
},
expectedShortImports: []
},
{
filesContents: {
// several statements on one line with actual short imports
file1: 'const _ = require("lodash");const application = require("application");console.log("application");',
file2: 'const _ = require("lodash");const application = require("application");const Observable = require("data/observable").Observable;'
},
expectedShortImports: [
{ file: "file1", line: 'const application = require("application")' },
{ file: "file2", line: 'const application = require("application")' },
{ file: "file2", line: 'const Observable = require("data/observable").Observable' },
]
},
{
filesContents: {
// several statements on one line withoutshort imports
file1: 'const _ = require("lodash");const application = require("tns-core-modules/application");console.log("application");',
file2: 'const _ = require("lodash");const application = require("tns-core-modules/application");const Observable = require("tns-core-modules/data/observable").Observable;'
},
expectedShortImports: []
},
{
filesContents: {
// minified code that has both require and some of the tns-core-modules subdirs (i.e. text)
file1: `o.cache&&(r+="&cache="+u(o.cache)),t=["<!DOCTYPE html>","<html>","<head>",'<meta charset="UTF-8" />','<script type="text/javascript">'," var UWA = {hosts:"+n(e.hosts)+"},",' curl = {apiName: "require"};`
},
expectedShortImports: []
},
{
filesContents: {
// spaces around require
file1: 'const application = require ( "application" ); console.log("application");',
},
expectedShortImports: [
{ file: "file1", line: 'const application = require ( "application" )' }
]
},
{
filesContents: {
// spaces around require
file1: 'const application = require ( "tns-core-modules/application" ); console.log("application");',
},
expectedShortImports: []
},
{
filesContents: {
// spaces in import line
file1: "import { run } from 'application' ;",
},
expectedShortImports: [
{ file: "file1", line: "import { run } from 'application' " }
]
},
{
filesContents: {
// spaces in import line
file1: "import { run } from 'tns-core-modules/application' ;",
},
expectedShortImports: []
},
{
// Incorrect behavior, currently by design
// In case you have a multiline string and one of the lines matches our RegExp we'll detect it as short import
filesContents: {
file1: 'const _ = require("lodash");const application = require("application");console.log("application");console.log(`this is line\nyou should import some long words here require("application") module and other words here`)',
},
expectedShortImports: [
{ file: "file1", line: 'const application = require("application")' },
{ file: "file1", line: 'you should import some long words here require("application") module and other words here`)' },
]
},
];
it("getDeprecatedShortImportsInFiles returns correct results", () => {
const testInjector = createTestInjector();
const doctorService = testInjector.resolve<DoctorServiceInheritor>("doctorService");
const fs = testInjector.resolve<IFileSystem>("fs");
fs.getFsStats = (file) => <any>({
isDirectory: () => true
});
fs.readDirectory = (dirPath) => {
if (dirPath.indexOf(path.join("node_modules", "tns-core-modules"))) {
return tnsCoreModulesDirs;
}
};
testData.forEach(({ filesContents, expectedShortImports }) => {
fs.readText = (filePath) => filesContents[filePath];
const shortImports = doctorService.getDeprecatedShortImportsInFiles(_.keys(filesContents), "projectDir");
assert.deepEqual(shortImports, expectedShortImports);
});
});
});
});