Skip to content

Commit 36e3f00

Browse files
committed
[Tests] no-restricted-paths: import type tests
1 parent 3767479 commit 36e3f00

File tree

2 files changed

+270
-2
lines changed

2 files changed

+270
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1515

1616
### Changed
1717
- [Tests] `named`: Run all TypeScript test ([#2427], thanks [@ProdigySim])
18+
- [Tests] `no-restricted-paths`: Tests for `import type` statements, thanks [@golergka]
1819
- [readme] note use of typescript in readme `import/extensions` section ([#2440], thanks [@OutdatedVersion])
1920
- [Docs] `order`: use correct default value ([#2392], thanks [@hyperupcall])
2021
- [meta] replace git.io link in comments with the original URL ([#2444], thanks [@liby])
@@ -1557,6 +1558,7 @@ for info on changes for earlier releases.
15571558
[@gausie]: https://github.com/gausie
15581559
[@gavriguy]: https://github.com/gavriguy
15591560
[@giodamelio]: https://github.com/giodamelio
1561+
[@golergka]: https://github.com/golergka
15601562
[@golopot]: https://github.com/golopot
15611563
[@GoodForOneFare]: https://github.com/GoodForOneFare
15621564
[@graingert]: https://github.com/graingert

tests/src/rules/no-restricted-paths.js

Lines changed: 268 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { RuleTester } from 'eslint';
2+
import { getTSParsers, test, testFilePath } from '../utils';
23
import rule from 'rules/no-restricted-paths';
34

4-
import { test, testFilePath } from '../utils';
5-
65
const ruleTester = new RuleTester();
76

87
ruleTester.run('no-restricted-paths', rule, {
@@ -262,3 +261,270 @@ ruleTester.run('no-restricted-paths', rule, {
262261
}),
263262
],
264263
});
264+
265+
context('Typescript', function () {
266+
getTSParsers().forEach(parser => {
267+
const settings = {
268+
'import/parsers': { [parser]: ['.ts'] },
269+
'import/resolver': { 'eslint-import-resolver-typescript': true },
270+
};
271+
ruleTester.run('no-restricted-paths', rule, {
272+
valid: [
273+
test({
274+
code: 'import type a from "../client/a.ts"',
275+
filename: testFilePath('./restricted-paths/server/b.ts'),
276+
options: [ {
277+
zones: [ { target: './tests/files/restricted-paths/server', from: './tests/files/restricted-paths/other' } ],
278+
} ],
279+
parser,
280+
settings,
281+
}),
282+
test({
283+
code: 'import type a from "../client/a.ts"',
284+
filename: testFilePath('./restricted-paths/server/b.ts'),
285+
options: [ {
286+
zones: [ { target: '**/*', from: './tests/files/restricted-paths/other' } ],
287+
} ],
288+
parser,
289+
settings,
290+
}),
291+
test({
292+
code: 'import type a from "../client/a.ts"',
293+
filename: testFilePath('./restricted-paths/client/b.ts'),
294+
options: [ {
295+
zones: [ {
296+
target: './tests/files/restricted-paths/!(client)/**/*',
297+
from: './tests/files/restricted-paths/client/**/*',
298+
} ],
299+
} ],
300+
parser,
301+
settings,
302+
}),
303+
test({
304+
code: 'import type b from "../server/b.ts"',
305+
filename: testFilePath('./restricted-paths/client/a.ts'),
306+
options: [ {
307+
zones: [ { target: './tests/files/restricted-paths/client', from: './tests/files/restricted-paths/other' } ],
308+
} ],
309+
parser,
310+
settings,
311+
}),
312+
test({
313+
code: 'import type a from "./a.ts"',
314+
filename: testFilePath('./restricted-paths/server/one/a.ts'),
315+
options: [ {
316+
zones: [ {
317+
target: './tests/files/restricted-paths/server/one',
318+
from: './tests/files/restricted-paths/server',
319+
except: ['./one'],
320+
} ],
321+
} ],
322+
parser,
323+
settings,
324+
}),
325+
test({
326+
code: 'import type a from "../two/a.ts"',
327+
filename: testFilePath('./restricted-paths/server/one/a.ts'),
328+
options: [ {
329+
zones: [ {
330+
target: './tests/files/restricted-paths/server/one',
331+
from: './tests/files/restricted-paths/server',
332+
except: ['./two'],
333+
} ],
334+
} ],
335+
parser,
336+
settings,
337+
}),
338+
test({
339+
code: 'import type a from "../one/a.ts"',
340+
filename: testFilePath('./restricted-paths/server/two-new/a.ts'),
341+
options: [ {
342+
zones: [ {
343+
target: './tests/files/restricted-paths/server/two',
344+
from: './tests/files/restricted-paths/server',
345+
except: [],
346+
} ],
347+
} ],
348+
parser,
349+
settings,
350+
}),
351+
test({
352+
code: 'import type A from "../two/a.ts"',
353+
filename: testFilePath('./restricted-paths/server/one/a.ts'),
354+
options: [ {
355+
zones: [ {
356+
target: '**/*',
357+
from: './tests/files/restricted-paths/server/**/*',
358+
except: ['**/a.js'],
359+
} ],
360+
} ],
361+
parser,
362+
settings,
363+
}),
364+
// no config
365+
test({ code: 'import type b from "../server/b.js"', parser, settings }),
366+
test({ code: 'import type * as b from "../server/b.js"', parser, settings }),
367+
],
368+
invalid: [
369+
test({
370+
code: 'import type b from "../server/b"',
371+
filename: testFilePath('./restricted-paths/client/a.ts'),
372+
options: [ {
373+
zones: [ { target: './tests/files/restricted-paths/client', from: './tests/files/restricted-paths/server' } ],
374+
} ],
375+
errors: [ {
376+
message: 'Unexpected path "../server/b" imported in restricted zone.',
377+
line: 1,
378+
column: 20,
379+
} ],
380+
parser,
381+
settings,
382+
}),
383+
test({
384+
code: 'import type b from "../server/b"',
385+
filename: testFilePath('./restricted-paths/client/a.ts'),
386+
options: [ {
387+
zones: [ { target: './tests/files/restricted-paths/client/**/*', from: './tests/files/restricted-paths/server' } ],
388+
} ],
389+
errors: [ {
390+
message: 'Unexpected path "../server/b" imported in restricted zone.',
391+
line: 1,
392+
column: 20,
393+
} ],
394+
parser,
395+
settings,
396+
}),
397+
test({
398+
code: 'import type a from "../client/a"\nimport type c from "./c.ts"',
399+
filename: testFilePath('./restricted-paths/server/b.ts'),
400+
options: [ {
401+
zones: [
402+
{ target: './tests/files/restricted-paths/server', from: './tests/files/restricted-paths/client' },
403+
{ target: './tests/files/restricted-paths/server', from: './tests/files/restricted-paths/server/c.ts' },
404+
],
405+
} ],
406+
errors: [
407+
{
408+
message: 'Unexpected path "../client/a" imported in restricted zone.',
409+
line: 1,
410+
column: 20,
411+
},
412+
{
413+
message: 'Unexpected path "./c" imported in restricted zone.',
414+
line: 2,
415+
column: 20,
416+
},
417+
],
418+
parser,
419+
settings,
420+
}),
421+
test({
422+
code: 'import type b from "../server/b"',
423+
filename: testFilePath('./restricted-paths/client/a'),
424+
options: [ {
425+
zones: [ { target: './client', from: './server' } ],
426+
basePath: testFilePath('./restricted-paths'),
427+
} ],
428+
errors: [ {
429+
message: 'Unexpected path "../server/b" imported in restricted zone.',
430+
line: 1,
431+
column: 20,
432+
} ],
433+
parser,
434+
settings,
435+
}),
436+
test({
437+
code: 'import type b from "../two/a"',
438+
filename: testFilePath('./restricted-paths/server/one/a.ts'),
439+
options: [ {
440+
zones: [ {
441+
target: './tests/files/restricted-paths/server/one',
442+
from: './tests/files/restricted-paths/server',
443+
except: ['./one'],
444+
} ],
445+
} ],
446+
errors: [ {
447+
message: 'Unexpected path "../two/a" imported in restricted zone.',
448+
line: 1,
449+
column: 20,
450+
} ],
451+
parser,
452+
settings,
453+
}),
454+
test({
455+
code: 'import type b from "../two/a"',
456+
filename: testFilePath('./restricted-paths/server/one/a'),
457+
options: [ {
458+
zones: [ {
459+
target: './tests/files/restricted-paths/server/one',
460+
from: './tests/files/restricted-paths/server',
461+
except: ['./one'],
462+
message: 'Custom message',
463+
} ],
464+
} ],
465+
errors: [ {
466+
message: 'Unexpected path "../two/a" imported in restricted zone. Custom message',
467+
line: 1,
468+
column: 20,
469+
} ],
470+
parser,
471+
settings,
472+
}),
473+
test({
474+
code: 'import type b from "../two/a"',
475+
filename: testFilePath('./restricted-paths/server/one/a.ts'),
476+
options: [ {
477+
zones: [ {
478+
target: './tests/files/restricted-paths/server/one',
479+
from: './tests/files/restricted-paths/server',
480+
except: ['../client/a'],
481+
} ],
482+
} ],
483+
errors: [ {
484+
message: 'Restricted path exceptions must be descendants of the configured ' +
485+
'`from` path for that zone.',
486+
line: 1,
487+
column: 20,
488+
} ],
489+
parser,
490+
settings,
491+
}),
492+
test({
493+
code: 'import type A from "../two/a"',
494+
filename: testFilePath('./restricted-paths/server/one/a.ts'),
495+
options: [ {
496+
zones: [ {
497+
target: '**/*',
498+
from: './tests/files/restricted-paths/server/**/*',
499+
} ],
500+
} ],
501+
errors: [ {
502+
message: 'Unexpected path "../two/a" imported in restricted zone.',
503+
line: 1,
504+
column: 20,
505+
} ],
506+
parser,
507+
settings,
508+
}),
509+
test({
510+
code: 'import type A from "../two/a"',
511+
filename: testFilePath('./restricted-paths/server/one/a.ts'),
512+
options: [ {
513+
zones: [ {
514+
target: '**/*',
515+
from: './tests/files/restricted-paths/server/**/*',
516+
except: ['a.ts'],
517+
} ],
518+
} ],
519+
errors: [ {
520+
message: 'Restricted path exceptions must be glob patterns when`from` is a glob pattern',
521+
line: 1,
522+
column: 20,
523+
} ],
524+
parser,
525+
settings,
526+
}),
527+
],
528+
});
529+
});
530+
});

0 commit comments

Comments
 (0)