Skip to content

Commit 180c3b5

Browse files
golergkaljharb
authored andcommitted
[Tests] no-restricted-paths: import type tests
1 parent 9f401a8 commit 180c3b5

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
@@ -33,6 +33,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
3333
- [Performance] `ExportMap`: add caching after parsing for an ambiguous module ([#2531], thanks [@stenin-nikita])
3434
- [Docs] [`no-useless-path-segments`]: fix paths ([#2424], thanks [@s-h-a-d-o-w])
3535
- [Tests] [`no-cycle`]: add passing test cases ([#2438], thanks [@georeith])
36+
- [Tests] [`no-restricted-paths`]: Tests for `import type` statements, thanks [@golergka]
3637

3738
## [2.26.0] - 2022-04-05
3839

@@ -1588,6 +1589,7 @@ for info on changes for earlier releases.
15881589
[@georeith]: https://github.com/georeith
15891590
[@gavriguy]: https://github.com/gavriguy
15901591
[@giodamelio]: https://github.com/giodamelio
1592+
[@golergka]: https://github.com/golergka
15911593
[@golopot]: https://github.com/golopot
15921594
[@GoodForOneFare]: https://github.com/GoodForOneFare
15931595
[@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, {
@@ -712,3 +711,270 @@ 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+
{ target: './tests/files/restricted-paths/server', from: './tests/files/restricted-paths/client' },
853+
{ target: './tests/files/restricted-paths/server', from: './tests/files/restricted-paths/server/c.ts' },
854+
],
855+
} ],
856+
errors: [
857+
{
858+
message: 'Unexpected path "../client/a" imported in restricted zone.',
859+
line: 1,
860+
column: 20,
861+
},
862+
{
863+
message: 'Unexpected path "./c" imported in restricted zone.',
864+
line: 2,
865+
column: 20,
866+
},
867+
],
868+
parser,
869+
settings,
870+
}),
871+
test({
872+
code: 'import type b from "../server/b"',
873+
filename: testFilePath('./restricted-paths/client/a'),
874+
options: [ {
875+
zones: [ { target: './client', from: './server' } ],
876+
basePath: testFilePath('./restricted-paths'),
877+
} ],
878+
errors: [ {
879+
message: 'Unexpected path "../server/b" imported in restricted zone.',
880+
line: 1,
881+
column: 20,
882+
} ],
883+
parser,
884+
settings,
885+
}),
886+
test({
887+
code: 'import type b from "../two/a"',
888+
filename: testFilePath('./restricted-paths/server/one/a.ts'),
889+
options: [ {
890+
zones: [ {
891+
target: './tests/files/restricted-paths/server/one',
892+
from: './tests/files/restricted-paths/server',
893+
except: ['./one'],
894+
} ],
895+
} ],
896+
errors: [ {
897+
message: 'Unexpected path "../two/a" imported in restricted zone.',
898+
line: 1,
899+
column: 20,
900+
} ],
901+
parser,
902+
settings,
903+
}),
904+
test({
905+
code: 'import type b from "../two/a"',
906+
filename: testFilePath('./restricted-paths/server/one/a'),
907+
options: [ {
908+
zones: [ {
909+
target: './tests/files/restricted-paths/server/one',
910+
from: './tests/files/restricted-paths/server',
911+
except: ['./one'],
912+
message: 'Custom message',
913+
} ],
914+
} ],
915+
errors: [ {
916+
message: 'Unexpected path "../two/a" imported in restricted zone. Custom message',
917+
line: 1,
918+
column: 20,
919+
} ],
920+
parser,
921+
settings,
922+
}),
923+
test({
924+
code: 'import type b from "../two/a"',
925+
filename: testFilePath('./restricted-paths/server/one/a.ts'),
926+
options: [ {
927+
zones: [ {
928+
target: './tests/files/restricted-paths/server/one',
929+
from: './tests/files/restricted-paths/server',
930+
except: ['../client/a'],
931+
} ],
932+
} ],
933+
errors: [ {
934+
message: 'Restricted path exceptions must be descendants of the configured ' +
935+
'`from` path for that zone.',
936+
line: 1,
937+
column: 20,
938+
} ],
939+
parser,
940+
settings,
941+
}),
942+
test({
943+
code: 'import type A from "../two/a"',
944+
filename: testFilePath('./restricted-paths/server/one/a.ts'),
945+
options: [ {
946+
zones: [ {
947+
target: '**/*',
948+
from: './tests/files/restricted-paths/server/**/*',
949+
} ],
950+
} ],
951+
errors: [ {
952+
message: 'Unexpected path "../two/a" imported in restricted zone.',
953+
line: 1,
954+
column: 20,
955+
} ],
956+
parser,
957+
settings,
958+
}),
959+
test({
960+
code: 'import type A from "../two/a"',
961+
filename: testFilePath('./restricted-paths/server/one/a.ts'),
962+
options: [ {
963+
zones: [ {
964+
target: '**/*',
965+
from: './tests/files/restricted-paths/server/**/*',
966+
except: ['a.ts'],
967+
} ],
968+
} ],
969+
errors: [ {
970+
message: 'Restricted path exceptions must be glob patterns when `from` contains glob patterns',
971+
line: 1,
972+
column: 20,
973+
} ],
974+
parser,
975+
settings,
976+
}),
977+
],
978+
});
979+
});
980+
});

0 commit comments

Comments
 (0)