@@ -4,8 +4,8 @@ import typescriptConfig from '../../../config/typescript';
4
4
5
5
import { RuleTester } from 'eslint' ;
6
6
import fs from 'fs' ;
7
- import semver from 'semver' ;
8
7
import eslintPkg from 'eslint/package.json' ;
8
+ import semver from 'semver' ;
9
9
10
10
// TODO: figure out why these tests fail in eslint 4
11
11
const isESLint4TODO = semver . satisfies ( eslintPkg . version , '^4' ) ;
@@ -108,7 +108,6 @@ ruleTester.run('no-unused-modules', rule, {
108
108
// tests for exports
109
109
ruleTester . run ( 'no-unused-modules' , rule , {
110
110
valid : [
111
-
112
111
test ( {
113
112
options : unusedExportsOptions ,
114
113
code : 'import { o2 } from "./file-o";export default () => 12' ,
@@ -149,6 +148,54 @@ ruleTester.run('no-unused-modules', rule, {
149
148
code : 'const o0 = 0; const o1 = 1; export { o0, o1 as o2 }; export default () => {}' ,
150
149
filename : testFilePath ( './no-unused-modules/file-o.js' ) ,
151
150
} ) ,
151
+ test ( {
152
+ options : unusedExportsOptions ,
153
+ code : 'import { o2 } from "./file-o";export default () => 12' ,
154
+ filename : testFilePath ( './no-unused-modules/file-a.js' ) ,
155
+ parser : require . resolve ( 'babel-eslint' ) ,
156
+ } ) ,
157
+ test ( {
158
+ options : unusedExportsOptions ,
159
+ code : 'export const b = 2' ,
160
+ filename : testFilePath ( './no-unused-modules/file-b.js' ) ,
161
+ parser : require . resolve ( 'babel-eslint' ) ,
162
+ } ) ,
163
+ test ( {
164
+ options : unusedExportsOptions ,
165
+ code : 'const c1 = 3; function c2() { return 3 }; export { c1, c2 }' ,
166
+ filename : testFilePath ( './no-unused-modules/file-c.js' ) ,
167
+ parser : require . resolve ( 'babel-eslint' ) ,
168
+ } ) ,
169
+ test ( {
170
+ options : unusedExportsOptions ,
171
+ code : 'export function d() { return 4 }' ,
172
+ filename : testFilePath ( './no-unused-modules/file-d.js' ) ,
173
+ parser : require . resolve ( 'babel-eslint' ) ,
174
+ } ) ,
175
+ test ( {
176
+ options : unusedExportsOptions ,
177
+ code : 'export class q { q0() {} }' ,
178
+ filename : testFilePath ( './no-unused-modules/file-q.js' ) ,
179
+ parser : require . resolve ( 'babel-eslint' ) ,
180
+ } ) ,
181
+ test ( {
182
+ options : unusedExportsOptions ,
183
+ code : 'const e0 = 5; export { e0 as e }' ,
184
+ filename : testFilePath ( './no-unused-modules/file-e.js' ) ,
185
+ parser : require . resolve ( 'babel-eslint' ) ,
186
+ } ) ,
187
+ test ( {
188
+ options : unusedExportsOptions ,
189
+ code : 'const l0 = 5; const l = 10; export { l0 as l1, l }; export default () => {}' ,
190
+ filename : testFilePath ( './no-unused-modules/file-l.js' ) ,
191
+ parser : require . resolve ( 'babel-eslint' ) ,
192
+ } ) ,
193
+ test ( {
194
+ options : unusedExportsOptions ,
195
+ code : 'const o0 = 0; const o1 = 1; export { o0, o1 as o2 }; export default () => {}' ,
196
+ filename : testFilePath ( './no-unused-modules/file-o.js' ) ,
197
+ parser : require . resolve ( 'babel-eslint' ) ,
198
+ } ) ,
152
199
] ,
153
200
invalid : [
154
201
test ( {
@@ -235,7 +282,123 @@ ruleTester.run('no-unused-modules', rule, {
235
282
] ,
236
283
} ) ;
237
284
238
- // // test for export from
285
+ // test for unused exports with `import()`
286
+ ruleTester . run ( 'no-unused-modules' , rule , {
287
+ valid : [
288
+ test ( {
289
+ options : unusedExportsOptions ,
290
+ code : `
291
+ export const a = 10
292
+ export const b = 20
293
+ export const c = 30
294
+ const d = 40
295
+ export default d
296
+ ` ,
297
+ parser : require . resolve ( 'babel-eslint' ) ,
298
+ filename : testFilePath ( './no-unused-modules/exports-for-dynamic-js.js' ) ,
299
+ } ) ,
300
+ ] ,
301
+ invalid : [
302
+ test ( {
303
+ options : unusedExportsOptions ,
304
+ code : `
305
+ export const a = 10
306
+ export const b = 20
307
+ export const c = 30
308
+ const d = 40
309
+ export default d
310
+ ` ,
311
+ parser : require . resolve ( 'babel-eslint' ) ,
312
+ filename : testFilePath ( './no-unused-modules/exports-for-dynamic-js-2.js' ) ,
313
+ errors : [
314
+ error ( `exported declaration 'a' not used within other modules` ) ,
315
+ error ( `exported declaration 'b' not used within other modules` ) ,
316
+ error ( `exported declaration 'c' not used within other modules` ) ,
317
+ error ( `exported declaration 'default' not used within other modules` ) ,
318
+ ] ,
319
+ } ) ,
320
+ ] ,
321
+ } ) ;
322
+ typescriptRuleTester . run ( 'no-unused-modules' , rule , {
323
+ valid : [
324
+ test ( {
325
+ options : unusedExportsTypescriptOptions ,
326
+ code : `
327
+ export const ts_a = 10
328
+ export const ts_b = 20
329
+ export const ts_c = 30
330
+ const ts_d = 40
331
+ export default ts_d
332
+ ` ,
333
+ parser : require . resolve ( '@typescript-eslint/parser' ) ,
334
+ filename : testFilePath ( './no-unused-modules/typescript/exports-for-dynamic-ts.ts' ) ,
335
+ } ) ,
336
+ ] ,
337
+ invalid : [
338
+ ] ,
339
+ } ) ;
340
+
341
+ describe ( 'dynamic imports' , ( ) => {
342
+ if ( semver . satisfies ( eslintPkg . version , '< 6' ) ) {
343
+ this . skip ( ) ;
344
+ return ;
345
+ }
346
+
347
+ // test for unused exports with `import()`
348
+ ruleTester . run ( 'no-unused-modules' , rule , {
349
+ valid : [
350
+ test ( { options : unusedExportsOptions ,
351
+ code : `
352
+ export const a = 10
353
+ export const b = 20
354
+ export const c = 30
355
+ const d = 40
356
+ export default d
357
+ ` ,
358
+ parser : require . resolve ( 'babel-eslint' ) ,
359
+ filename : testFilePath ( './no-unused-modules/exports-for-dynamic-js.js' ) ,
360
+ } ) ,
361
+ ] ,
362
+ invalid : [
363
+ test ( { options : unusedExportsOptions ,
364
+ code : `
365
+ export const a = 10
366
+ export const b = 20
367
+ export const c = 30
368
+ const d = 40
369
+ export default d
370
+ ` ,
371
+ parser : require . resolve ( 'babel-eslint' ) ,
372
+ filename : testFilePath ( './no-unused-modules/exports-for-dynamic-js-2.js' ) ,
373
+ errors : [
374
+ error ( `exported declaration 'a' not used within other modules` ) ,
375
+ error ( `exported declaration 'b' not used within other modules` ) ,
376
+ error ( `exported declaration 'c' not used within other modules` ) ,
377
+ error ( `exported declaration 'default' not used within other modules` ) ,
378
+ ] ,
379
+ } ) ,
380
+ ] ,
381
+ } ) ;
382
+ typescriptRuleTester . run ( 'no-unused-modules' , rule , {
383
+ valid : [
384
+ test ( { options : unusedExportsTypescriptOptions ,
385
+ code : `
386
+ export const ts_a = 10
387
+ export const ts_b = 20
388
+ export const ts_c = 30
389
+ const ts_d = 40
390
+ export default ts_d
391
+ ` ,
392
+ parser : require . resolve ( '@typescript-eslint/parser' ) ,
393
+ filename : testFilePath ( './no-unused-modules/typescript/exports-for-dynamic-ts.ts' ) ,
394
+ } ) ,
395
+ ] ,
396
+ invalid : [
397
+ ] ,
398
+ } ) ;
399
+ } ) ;
400
+
401
+ // test for export from
239
402
ruleTester . run ( 'no-unused-modules' , rule , {
240
403
valid : [
241
404
test ( {
0 commit comments