Skip to content

Commit c3d14cb

Browse files
golergkaazyzz228
authored andcommitted
[Tests] no-restricted-paths: import type tests
Co-authored-by: Max <golergka@gmail.com> Co-authored-by: Aziz Abdullaev <aziz.saidmuratov@duke.edu>
1 parent 2e1edd6 commit c3d14cb

File tree

3 files changed

+276
-3
lines changed

3 files changed

+276
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
4343
- [Refactor] [`no-extraneous-dependencies`] improve performance using cache ([#2374], thanks [@meowtec])
4444
- [meta] `CONTRIBUTING.md`: mention inactive PRs ([#2546], thanks [@stropho])
4545
- [readme] make json for setting groups multiline ([#2570], thanks [@bertyhell])
46+
- [Tests] [`no-restricted-paths`]: Tests for `import type` statements ([#2459], thanks [@golergka])
47+
- [Tests] [`no-restricted-paths`]: fix one failing `import type` test case, submitted by [@golergka], thanks [@azyzz228]
4648

4749
## [2.26.0] - 2022-04-05
4850

@@ -1028,6 +1030,7 @@ for info on changes for earlier releases.
10281030
[#2490]: https://github.com/import-js/eslint-plugin-import/pull/2490
10291031
[#2473]: https://github.com/import-js/eslint-plugin-import/pull/2473
10301032
[#2466]: https://github.com/import-js/eslint-plugin-import/pull/2466
1033+
[#2459]: https://github.com/import-js/eslint-plugin-import/pull/2459
10311034
[#2440]: https://github.com/import-js/eslint-plugin-import/pull/2440
10321035
[#2438]: https://github.com/import-js/eslint-plugin-import/pull/2438
10331036
[#2436]: https://github.com/import-js/eslint-plugin-import/pull/2436
@@ -1554,6 +1557,7 @@ for info on changes for earlier releases.
15541557
[@atav32]: https://github.com/atav32
15551558
[@atikenny]: https://github.com/atikenny
15561559
[@atos1990]: https://github.com/atos1990
1560+
[@azyzz228]: https://github.com/azyzz228
15571561
[@barbogast]: https://github.com/barbogast
15581562
[@be5invis]: https://github.com/be5invis
15591563
[@beatrizrezener]: https://github.com/beatrizrezener
@@ -1609,6 +1613,7 @@ for info on changes for earlier releases.
16091613
[@gavriguy]: https://github.com/gavriguy
16101614
[@georeith]: https://github.com/georeith
16111615
[@giodamelio]: https://github.com/giodamelio
1616+
[@golergka]: https://github.com/golergka
16121617
[@golopot]: https://github.com/golopot
16131618
[@GoodForOneFare]: https://github.com/GoodForOneFare
16141619
[@graingert]: https://github.com/graingert

tests/files/restricted-paths/server/c.ts

Whitespace-only changes.

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

Lines changed: 271 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { RuleTester } from 'eslint';
22
import rule from 'rules/no-restricted-paths';
33

4-
import { test, testFilePath } from '../utils';
4+
import { getTSParsers, test, testFilePath } from '../utils';
55

66
const ruleTester = new RuleTester();
77

@@ -474,8 +474,7 @@ ruleTester.run('no-restricted-paths', rule, {
474474
],
475475
errors: [
476476
{
477-
message: 'Restricted path exceptions must be descendants of the configured ' +
478-
'`from` path for that zone.',
477+
message: 'Restricted path exceptions must be descendants of the configured `from` path for that zone.',
479478
line: 1,
480479
column: 15,
481480
},
@@ -712,3 +711,272 @@ ruleTester.run('no-restricted-paths', rule, {
712711
}),
713712
),
714713
});
714+
715+
context('Typescript', function () {
716+
getTSParsers().forEach(parser => {
717+
const settings = {
718+
'import/parsers': { [parser]: ['.ts'] },
719+
'import/resolver': { 'eslint-import-resolver-typescript': true },
720+
};
721+
ruleTester.run('no-restricted-paths', rule, {
722+
valid: [
723+
test({
724+
code: 'import type a from "../client/a.ts"',
725+
filename: testFilePath('./restricted-paths/server/b.ts'),
726+
options: [{
727+
zones: [{ target: './tests/files/restricted-paths/server', from: './tests/files/restricted-paths/other' }],
728+
}],
729+
parser,
730+
settings,
731+
}),
732+
test({
733+
code: 'import type a from "../client/a.ts"',
734+
filename: testFilePath('./restricted-paths/server/b.ts'),
735+
options: [{
736+
zones: [{ target: '**/*', from: './tests/files/restricted-paths/other' }],
737+
}],
738+
parser,
739+
settings,
740+
}),
741+
test({
742+
code: 'import type a from "../client/a.ts"',
743+
filename: testFilePath('./restricted-paths/client/b.ts'),
744+
options: [{
745+
zones: [{
746+
target: './tests/files/restricted-paths/!(client)/**/*',
747+
from: './tests/files/restricted-paths/client/**/*',
748+
}],
749+
}],
750+
parser,
751+
settings,
752+
}),
753+
test({
754+
code: 'import type b from "../server/b.ts"',
755+
filename: testFilePath('./restricted-paths/client/a.ts'),
756+
options: [{
757+
zones: [{ target: './tests/files/restricted-paths/client', from: './tests/files/restricted-paths/other' }],
758+
}],
759+
parser,
760+
settings,
761+
}),
762+
test({
763+
code: 'import type a from "./a.ts"',
764+
filename: testFilePath('./restricted-paths/server/one/a.ts'),
765+
options: [{
766+
zones: [{
767+
target: './tests/files/restricted-paths/server/one',
768+
from: './tests/files/restricted-paths/server',
769+
except: ['./one'],
770+
}],
771+
}],
772+
parser,
773+
settings,
774+
}),
775+
test({
776+
code: 'import type a from "../two/a.ts"',
777+
filename: testFilePath('./restricted-paths/server/one/a.ts'),
778+
options: [{
779+
zones: [{
780+
target: './tests/files/restricted-paths/server/one',
781+
from: './tests/files/restricted-paths/server',
782+
except: ['./two'],
783+
}],
784+
}],
785+
parser,
786+
settings,
787+
}),
788+
test({
789+
code: 'import type a from "../one/a.ts"',
790+
filename: testFilePath('./restricted-paths/server/two-new/a.ts'),
791+
options: [{
792+
zones: [{
793+
target: './tests/files/restricted-paths/server/two',
794+
from: './tests/files/restricted-paths/server',
795+
except: [],
796+
}],
797+
}],
798+
parser,
799+
settings,
800+
}),
801+
test({
802+
code: 'import type A from "../two/a.ts"',
803+
filename: testFilePath('./restricted-paths/server/one/a.ts'),
804+
options: [{
805+
zones: [{
806+
target: '**/*',
807+
from: './tests/files/restricted-paths/server/**/*',
808+
except: ['**/a.js'],
809+
}],
810+
}],
811+
parser,
812+
settings,
813+
}),
814+
// no config
815+
test({ code: 'import type b from "../server/b.js"', parser, settings }),
816+
test({ code: 'import type * as b from "../server/b.js"', parser, settings }),
817+
],
818+
invalid: [
819+
test({
820+
code: 'import type b from "../server/b"',
821+
filename: testFilePath('./restricted-paths/client/a.ts'),
822+
options: [{
823+
zones: [{ target: './tests/files/restricted-paths/client', from: './tests/files/restricted-paths/server' }],
824+
}],
825+
errors: [{
826+
message: 'Unexpected path "../server/b" imported in restricted zone.',
827+
line: 1,
828+
column: 20,
829+
}],
830+
parser,
831+
settings,
832+
}),
833+
test({
834+
code: 'import type b from "../server/b"',
835+
filename: testFilePath('./restricted-paths/client/a.ts'),
836+
options: [{
837+
zones: [{ target: './tests/files/restricted-paths/client/**/*', from: './tests/files/restricted-paths/server' }],
838+
}],
839+
errors: [{
840+
message: 'Unexpected path "../server/b" imported in restricted zone.',
841+
line: 1,
842+
column: 20,
843+
}],
844+
parser,
845+
settings,
846+
}),
847+
test({
848+
code: 'import type a from "../client/a"\nimport type c from "./c.ts"',
849+
filename: testFilePath('./restricted-paths/server/b.ts'),
850+
options: [{
851+
zones: [
852+
{
853+
target: './tests/files/restricted-paths/server',
854+
from: ['./tests/files/restricted-paths/client', './tests/files/restricted-paths/server/c.ts'],
855+
},
856+
],
857+
}],
858+
errors: [
859+
{
860+
message: 'Unexpected path "../client/a" imported in restricted zone.',
861+
line: 1,
862+
column: 20,
863+
},
864+
{
865+
message: 'Unexpected path "./c.ts" imported in restricted zone.',
866+
line: 2,
867+
column: 20,
868+
},
869+
],
870+
parser,
871+
settings,
872+
}),
873+
test({
874+
code: 'import type b from "../server/b"',
875+
filename: testFilePath('./restricted-paths/client/a'),
876+
options: [{
877+
zones: [{ target: './client', from: './server' }],
878+
basePath: testFilePath('./restricted-paths'),
879+
}],
880+
errors: [{
881+
message: 'Unexpected path "../server/b" imported in restricted zone.',
882+
line: 1,
883+
column: 20,
884+
}],
885+
parser,
886+
settings,
887+
}),
888+
test({
889+
code: 'import type b from "../two/a"',
890+
filename: testFilePath('./restricted-paths/server/one/a.ts'),
891+
options: [{
892+
zones: [{
893+
target: './tests/files/restricted-paths/server/one',
894+
from: './tests/files/restricted-paths/server',
895+
except: ['./one'],
896+
}],
897+
}],
898+
errors: [{
899+
message: 'Unexpected path "../two/a" imported in restricted zone.',
900+
line: 1,
901+
column: 20,
902+
}],
903+
parser,
904+
settings,
905+
}),
906+
test({
907+
code: 'import type b from "../two/a"',
908+
filename: testFilePath('./restricted-paths/server/one/a'),
909+
options: [{
910+
zones: [{
911+
target: './tests/files/restricted-paths/server/one',
912+
from: './tests/files/restricted-paths/server',
913+
except: ['./one'],
914+
message: 'Custom message',
915+
}],
916+
}],
917+
errors: [{
918+
message: 'Unexpected path "../two/a" imported in restricted zone. Custom message',
919+
line: 1,
920+
column: 20,
921+
}],
922+
parser,
923+
settings,
924+
}),
925+
test({
926+
code: 'import type b from "../two/a"',
927+
filename: testFilePath('./restricted-paths/server/one/a.ts'),
928+
options: [{
929+
zones: [{
930+
target: './tests/files/restricted-paths/server/one',
931+
from: './tests/files/restricted-paths/server',
932+
except: ['../client/a'],
933+
}],
934+
}],
935+
errors: [{
936+
message: 'Restricted path exceptions must be descendants of the configured ' +
937+
'`from` path for that zone.',
938+
line: 1,
939+
column: 20,
940+
}],
941+
parser,
942+
settings,
943+
}),
944+
test({
945+
code: 'import type A from "../two/a"',
946+
filename: testFilePath('./restricted-paths/server/one/a.ts'),
947+
options: [{
948+
zones: [{
949+
target: '**/*',
950+
from: './tests/files/restricted-paths/server/**/*',
951+
}],
952+
}],
953+
errors: [{
954+
message: 'Unexpected path "../two/a" imported in restricted zone.',
955+
line: 1,
956+
column: 20,
957+
}],
958+
parser,
959+
settings,
960+
}),
961+
test({
962+
code: 'import type A from "../two/a"',
963+
filename: testFilePath('./restricted-paths/server/one/a.ts'),
964+
options: [{
965+
zones: [{
966+
target: '**/*',
967+
from: './tests/files/restricted-paths/server/**/*',
968+
except: ['a.ts'],
969+
}],
970+
}],
971+
errors: [{
972+
message: 'Restricted path exceptions must be glob patterns when `from` contains glob patterns',
973+
line: 1,
974+
column: 20,
975+
}],
976+
parser,
977+
settings,
978+
}),
979+
],
980+
});
981+
});
982+
});

0 commit comments

Comments
 (0)