-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
no-extraneous-dependencies.js
429 lines (414 loc) · 15.5 KB
/
no-extraneous-dependencies.js
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import { getTSParsers, test, testFilePath } from '../utils';
import typescriptConfig from '../../../config/typescript';
import path from 'path';
import fs from 'fs';
import { RuleTester } from 'eslint';
import flatMap from 'array.prototype.flatmap';
const ruleTester = new RuleTester();
const typescriptRuleTester = new RuleTester(typescriptConfig);
const rule = require('rules/no-extraneous-dependencies');
const packageDirWithSyntaxError = path.join(__dirname, '../../files/with-syntax-error');
const packageFileWithSyntaxErrorMessage = (() => {
try {
JSON.parse(fs.readFileSync(path.join(packageDirWithSyntaxError, 'package.json')));
} catch (error) {
return error.message;
}
})();
const packageDirWithFlowTyped = path.join(__dirname, '../../files/with-flow-typed');
const packageDirWithTypescriptDevDependencies = path.join(__dirname, '../../files/with-typescript-dev-dependencies');
const packageDirMonoRepoRoot = path.join(__dirname, '../../files/monorepo');
const packageDirMonoRepoWithNested = path.join(__dirname, '../../files/monorepo/packages/nested-package');
const packageDirWithEmpty = path.join(__dirname, '../../files/empty');
const packageDirBundleDeps = path.join(__dirname, '../../files/bundled-dependencies/as-array-bundle-deps');
const packageDirBundledDepsAsObject = path.join(__dirname, '../../files/bundled-dependencies/as-object');
const packageDirBundledDepsRaceCondition = path.join(__dirname, '../../files/bundled-dependencies/race-condition');
const {
dependencies: deps,
devDependencies: devDeps,
} = require('../../files/package.json');
ruleTester.run('no-extraneous-dependencies', rule, {
valid: [
...flatMap(Object.keys(deps).concat(Object.keys(devDeps)), (pkg) => [
test({ code: `import "${pkg}"` }),
test({ code: `import foo, { bar } from "${pkg}"` }),
test({ code: `require("${pkg}")` }),
test({ code: `var foo = require("${pkg}")` }),
test({ code: `export { foo } from "${pkg}"` }),
test({ code: `export * from "${pkg}"` }),
]),
test({ code: 'import "eslint"' }),
test({ code: 'import "eslint/lib/api"' }),
test({ code: 'import "fs"' }),
test({ code: 'import "./foo"' }),
test({ code: 'import "@org/package"' }),
test({ code: 'import "electron"', settings: { 'import/core-modules': ['electron'] } }),
test({
code: 'import "eslint"',
options: [{ peerDependencies: true }],
}),
// 'project' type
test({
code: 'import "importType"',
settings: { 'import/resolver': { node: { paths: [ path.join(__dirname, '../../files') ] } } },
}),
test({
code: 'import chai from "chai"',
options: [{ devDependencies: ['*.spec.js'] }],
filename: 'foo.spec.js',
}),
test({
code: 'import chai from "chai"',
options: [{ devDependencies: ['*.spec.js'] }],
filename: path.join(process.cwd(), 'foo.spec.js'),
}),
test({
code: 'import chai from "chai"',
options: [{ devDependencies: ['*.test.js', '*.spec.js'] }],
filename: path.join(process.cwd(), 'foo.spec.js'),
}),
test({ code: 'require(6)' }),
test({
code: 'import "doctrine"',
options: [{ packageDir: path.join(__dirname, '../../../') }],
}),
test({
code: 'import type MyType from "myflowtyped";',
options: [{ packageDir: packageDirWithFlowTyped }],
parser: require.resolve('babel-eslint'),
}),
test({
code: `
// @flow
import typeof TypeScriptModule from 'typescript';
`,
options: [{ packageDir: packageDirWithFlowTyped }],
parser: require.resolve('babel-eslint'),
}),
test({
code: 'import react from "react";',
options: [{ packageDir: packageDirMonoRepoWithNested }],
}),
test({
code: 'import leftpad from "left-pad";',
options: [{ packageDir: [packageDirMonoRepoWithNested, packageDirMonoRepoRoot] }],
}),
test({
code: 'import leftpad from "left-pad";',
options: [{ packageDir: packageDirMonoRepoRoot }],
}),
test({
code: 'import react from "react";',
options: [{ packageDir: [packageDirMonoRepoRoot, packageDirMonoRepoWithNested] }],
}),
test({
code: 'import leftpad from "left-pad";',
options: [{ packageDir: [packageDirMonoRepoRoot, packageDirMonoRepoWithNested] }],
}),
test({
code: 'import rightpad from "right-pad";',
options: [{ packageDir: [packageDirMonoRepoRoot, packageDirMonoRepoWithNested] }],
}),
test({ code: 'import foo from "@generated/foo"' }),
test({
code: 'import foo from "@generated/foo"',
options: [{ packageDir: packageDirBundleDeps }],
}),
test({
code: 'import foo from "@generated/foo"',
options: [{ packageDir: packageDirBundledDepsAsObject }],
}),
test({
code: 'import foo from "@generated/foo"',
options: [{ packageDir: packageDirBundledDepsRaceCondition }],
}),
test({ code: 'export function getToken() {}' }),
test({ code: 'export class Component extends React.Component {}' }),
test({ code: 'export function Component() {}' }),
test({ code: 'export const Component = () => {}' }),
test({
code: 'import "not-a-dependency"',
filename: path.join(packageDirMonoRepoRoot, 'foo.js'),
options: [{ packageDir: packageDirMonoRepoRoot }],
settings: { 'import/core-modules': ['not-a-dependency'] },
}),
test({
code: 'import "@generated/bar/module"',
settings: { 'import/core-modules': ['@generated/bar'] },
}),
test({
code: 'import "@generated/bar/and/sub/path"',
settings: { 'import/core-modules': ['@generated/bar'] },
}),
// check if "rxjs" dependency declaration fix the "rxjs/operators subpackage
test({
code: 'import "rxjs/operators"',
}),
test({
code: 'import "esm-package/esm-module";',
}),
test({
code: `
import "alias/esm-package/esm-module";
import 'expose-loader?exposes[]=$&exposes[]=jQuery!jquery';
`,
settings: { 'import/resolver': 'webpack' },
}),
],
invalid: [
test({
code: 'import "not-a-dependency"',
filename: path.join(packageDirMonoRepoRoot, 'foo.js'),
options: [{ packageDir: packageDirMonoRepoRoot }],
errors: [{
message: '\'not-a-dependency\' should be listed in the project\'s dependencies. Run \'npm i -S not-a-dependency\' to add it',
}],
}),
test({
code: 'import "not-a-dependency"',
filename: path.join(packageDirMonoRepoWithNested, 'foo.js'),
options: [{ packageDir: packageDirMonoRepoRoot }],
errors: [{
message: '\'not-a-dependency\' should be listed in the project\'s dependencies. Run \'npm i -S not-a-dependency\' to add it',
}],
}),
test({
code: 'import "not-a-dependency"',
options: [{ packageDir: packageDirMonoRepoRoot }],
errors: [{
message: '\'not-a-dependency\' should be listed in the project\'s dependencies. Run \'npm i -S not-a-dependency\' to add it',
}],
}),
test({
code: 'import "not-a-dependency"',
errors: [{
message: '\'not-a-dependency\' should be listed in the project\'s dependencies. Run \'npm i -S not-a-dependency\' to add it',
}],
}),
test({
code: 'var donthaveit = require("@org/not-a-dependency")',
errors: [{
message: '\'@org/not-a-dependency\' should be listed in the project\'s dependencies. Run \'npm i -S @org/not-a-dependency\' to add it',
}],
}),
test({
code: 'var donthaveit = require("@org/not-a-dependency/foo")',
errors: [{
message: '\'@org/not-a-dependency\' should be listed in the project\'s dependencies. Run \'npm i -S @org/not-a-dependency\' to add it',
}],
}),
test({
code: 'import "eslint"',
options: [{ devDependencies: false, peerDependencies: false }],
errors: [{
message: '\'eslint\' should be listed in the project\'s dependencies, not devDependencies.',
}],
}),
test({
code: 'import "lodash.isarray"',
options: [{ optionalDependencies: false }],
errors: [{
message: '\'lodash.isarray\' should be listed in the project\'s dependencies, not optionalDependencies.',
}],
}),
test({
code: 'var foo = require("not-a-dependency")',
errors: [{
message: '\'not-a-dependency\' should be listed in the project\'s dependencies. Run \'npm i -S not-a-dependency\' to add it',
}],
}),
test({
code: 'var glob = require("glob")',
options: [{ devDependencies: false }],
errors: [{
message: '\'glob\' should be listed in the project\'s dependencies, not devDependencies.',
}],
}),
test({
code: 'import chai from "chai"',
options: [{ devDependencies: ['*.test.js'] }],
filename: 'foo.tes.js',
errors: [{
message: '\'chai\' should be listed in the project\'s dependencies, not devDependencies.',
}],
}),
test({
code: 'import chai from "chai"',
options: [{ devDependencies: ['*.test.js'] }],
filename: path.join(process.cwd(), 'foo.tes.js'),
errors: [{
message: '\'chai\' should be listed in the project\'s dependencies, not devDependencies.',
}],
}),
test({
code: 'import chai from "chai"',
options: [{ devDependencies: ['*.test.js', '*.spec.js'] }],
filename: 'foo.tes.js',
errors: [{
message: '\'chai\' should be listed in the project\'s dependencies, not devDependencies.',
}],
}),
test({
code: 'import chai from "chai"',
options: [{ devDependencies: ['*.test.js', '*.spec.js'] }],
filename: path.join(process.cwd(), 'foo.tes.js'),
errors: [{
message: '\'chai\' should be listed in the project\'s dependencies, not devDependencies.',
}],
}),
test({
code: 'var eslint = require("lodash.isarray")',
options: [{ optionalDependencies: false }],
errors: [{
message: '\'lodash.isarray\' should be listed in the project\'s dependencies, not optionalDependencies.',
}],
}),
test({
code: 'import "not-a-dependency"',
options: [{ packageDir: path.join(__dirname, '../../../') }],
errors: [{
message: '\'not-a-dependency\' should be listed in the project\'s dependencies. Run \'npm i -S not-a-dependency\' to add it',
}],
}),
test({
code: 'import "bar"',
options: [{ packageDir: path.join(__dirname, './doesn-exist/') }],
errors: [{
message: 'The package.json file could not be found.',
}],
}),
test({
code: 'import foo from "foo"',
options: [{ packageDir: packageDirWithSyntaxError }],
errors: [{
message: 'The package.json file could not be parsed: ' + packageFileWithSyntaxErrorMessage,
}],
}),
test({
code: 'import leftpad from "left-pad";',
filename: path.join(packageDirMonoRepoWithNested, 'foo.js'),
options: [{ packageDir: packageDirMonoRepoWithNested }],
errors: [{
message: "'left-pad' should be listed in the project's dependencies. Run 'npm i -S left-pad' to add it",
}],
}),
test({
code: 'import react from "react";',
filename: path.join(packageDirMonoRepoRoot, 'foo.js'),
errors: [{
message: "'react' should be listed in the project's dependencies. Run 'npm i -S react' to add it",
}],
}),
test({
code: 'import react from "react";',
filename: path.join(packageDirMonoRepoWithNested, 'foo.js'),
options: [{ packageDir: packageDirMonoRepoRoot }],
errors: [{
message: "'react' should be listed in the project's dependencies. Run 'npm i -S react' to add it",
}],
}),
test({
code: 'import "react";',
filename: path.join(packageDirWithEmpty, 'index.js'),
options: [{ packageDir: packageDirWithEmpty }],
errors: [{
message: "'react' should be listed in the project's dependencies. Run 'npm i -S react' to add it",
}],
}),
test({
code: 'import bar from "@generated/bar"',
errors: ["'@generated/bar' should be listed in the project's dependencies. Run 'npm i -S @generated/bar' to add it"],
}),
test({
code: 'import foo from "@generated/foo"',
options: [{ bundledDependencies: false }],
errors: ["'@generated/foo' should be listed in the project's dependencies. Run 'npm i -S @generated/foo' to add it"],
}),
test({
code: 'import bar from "@generated/bar"',
options: [{ packageDir: packageDirBundledDepsRaceCondition }],
errors: ["'@generated/bar' should be listed in the project's dependencies. Run 'npm i -S @generated/bar' to add it"],
}),
test({
code: 'export { foo } from "not-a-dependency";',
errors: [{
message: '\'not-a-dependency\' should be listed in the project\'s dependencies. Run \'npm i -S not-a-dependency\' to add it',
}],
}),
test({
code: 'export * from "not-a-dependency";',
errors: [{
message: '\'not-a-dependency\' should be listed in the project\'s dependencies. Run \'npm i -S not-a-dependency\' to add it',
}],
}),
test({
code: 'import chai from "alias/chai";',
settings: { 'import/resolver': 'webpack' },
errors: [{
// missing dependency is chai not alias
message: "'chai' should be listed in the project's dependencies. Run 'npm i -S chai' to add it",
}],
}),
test({
code: 'import "not-a-dependency"',
filename: path.join(packageDirMonoRepoRoot, 'foo.js'),
options: [{ packageDir: packageDirMonoRepoRoot }],
errors: [{
message: `'not-a-dependency' should be listed in the project's dependencies. Run 'npm i -S not-a-dependency' to add it`,
}],
}),
test({
code: 'import "esm-package-not-in-pkg-json/esm-module";',
errors: [{
message: `'esm-package-not-in-pkg-json' should be listed in the project's dependencies. Run 'npm i -S esm-package-not-in-pkg-json' to add it`,
}],
}),
],
});
describe('TypeScript', () => {
getTSParsers()
// Type-only imports were added in TypeScript ESTree 2.23.0
.filter((parser) => parser !== require.resolve('typescript-eslint-parser'))
.forEach((parser) => {
const parserConfig = {
parser,
settings: {
'import/parsers': { [parser]: ['.ts'] },
'import/resolver': ['node', 'typescript'],
},
};
ruleTester.run('no-extraneous-dependencies', rule, {
valid: [
test(Object.assign({
code: 'import type T from "a";',
options: [{ packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }],
}, parserConfig)),
],
invalid: [
test(Object.assign({
code: 'import T from "a";',
options: [{ packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }],
errors: [{
message: "'a' should be listed in the project's dependencies, not devDependencies.",
}],
}, parserConfig)),
],
});
});
});
typescriptRuleTester.run('no-extraneous-dependencies typescript type imports', rule, {
valid: [
test({
code: 'import type MyType from "not-a-dependency";',
filename: testFilePath('./no-unused-modules/typescript/file-ts-a.ts'),
parser: require.resolve('babel-eslint'),
}),
test({
code: 'import type { MyType } from "not-a-dependency";',
filename: testFilePath('./no-unused-modules/typescript/file-ts-a.ts'),
parser: require.resolve('babel-eslint'),
}),
],
invalid: [
],
});