Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tests] no-restricted-paths: import type tests #2459

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
- [Refactor] [`no-extraneous-dependencies`] improve performance using cache ([#2374], thanks [@meowtec])
- [meta] `CONTRIBUTING.md`: mention inactive PRs ([#2546], thanks [@stropho])
- [readme] make json for setting groups multiline ([#2570], thanks [@bertyhell])
- [Tests] [`no-restricted-paths`]: Tests for `import type` statements ([#2459], thanks [@golergka])
- [Tests] [`no-restricted-paths`]: fix one failing `import type` test case, submitted by [@golergka], thanks [@azyzz228]

## [2.26.0] - 2022-04-05

Expand Down Expand Up @@ -1028,6 +1030,7 @@ for info on changes for earlier releases.
[#2490]: https://github.com/import-js/eslint-plugin-import/pull/2490
[#2473]: https://github.com/import-js/eslint-plugin-import/pull/2473
[#2466]: https://github.com/import-js/eslint-plugin-import/pull/2466
[#2459]: https://github.com/import-js/eslint-plugin-import/pull/2459
[#2440]: https://github.com/import-js/eslint-plugin-import/pull/2440
[#2438]: https://github.com/import-js/eslint-plugin-import/pull/2438
[#2436]: https://github.com/import-js/eslint-plugin-import/pull/2436
Expand Down Expand Up @@ -1554,6 +1557,7 @@ for info on changes for earlier releases.
[@atav32]: https://github.com/atav32
[@atikenny]: https://github.com/atikenny
[@atos1990]: https://github.com/atos1990
[@azyzz228]: https://github.com/azyzz228
[@barbogast]: https://github.com/barbogast
[@be5invis]: https://github.com/be5invis
[@beatrizrezener]: https://github.com/beatrizrezener
Expand Down Expand Up @@ -1609,6 +1613,7 @@ for info on changes for earlier releases.
[@gavriguy]: https://github.com/gavriguy
[@georeith]: https://github.com/georeith
[@giodamelio]: https://github.com/giodamelio
[@golergka]: https://github.com/golergka
[@golopot]: https://github.com/golopot
[@GoodForOneFare]: https://github.com/GoodForOneFare
[@graingert]: https://github.com/graingert
Expand Down
Empty file.
274 changes: 271 additions & 3 deletions tests/src/rules/no-restricted-paths.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RuleTester } from 'eslint';
import rule from 'rules/no-restricted-paths';

import { test, testFilePath } from '../utils';
import { getTSParsers, test, testFilePath } from '../utils';

const ruleTester = new RuleTester();

Expand Down Expand Up @@ -474,8 +474,7 @@ ruleTester.run('no-restricted-paths', rule, {
],
errors: [
{
message: 'Restricted path exceptions must be descendants of the configured ' +
'`from` path for that zone.',
message: 'Restricted path exceptions must be descendants of the configured `from` path for that zone.',
line: 1,
column: 15,
},
Expand Down Expand Up @@ -712,3 +711,272 @@ ruleTester.run('no-restricted-paths', rule, {
}),
),
});

context('Typescript', function () {
getTSParsers().forEach(parser => {
const settings = {
'import/parsers': { [parser]: ['.ts'] },
'import/resolver': { 'eslint-import-resolver-typescript': true },
};
ruleTester.run('no-restricted-paths', rule, {
valid: [
test({
code: 'import type a from "../client/a.ts"',
filename: testFilePath('./restricted-paths/server/b.ts'),
options: [{
zones: [{ target: './tests/files/restricted-paths/server', from: './tests/files/restricted-paths/other' }],
}],
parser,
settings,
}),
test({
code: 'import type a from "../client/a.ts"',
filename: testFilePath('./restricted-paths/server/b.ts'),
options: [{
zones: [{ target: '**/*', from: './tests/files/restricted-paths/other' }],
}],
parser,
settings,
}),
test({
code: 'import type a from "../client/a.ts"',
filename: testFilePath('./restricted-paths/client/b.ts'),
options: [{
zones: [{
target: './tests/files/restricted-paths/!(client)/**/*',
from: './tests/files/restricted-paths/client/**/*',
}],
}],
parser,
settings,
}),
test({
code: 'import type b from "../server/b.ts"',
filename: testFilePath('./restricted-paths/client/a.ts'),
options: [{
zones: [{ target: './tests/files/restricted-paths/client', from: './tests/files/restricted-paths/other' }],
}],
parser,
settings,
}),
test({
code: 'import type a from "./a.ts"',
filename: testFilePath('./restricted-paths/server/one/a.ts'),
options: [{
zones: [{
target: './tests/files/restricted-paths/server/one',
from: './tests/files/restricted-paths/server',
except: ['./one'],
}],
}],
parser,
settings,
}),
test({
code: 'import type a from "../two/a.ts"',
filename: testFilePath('./restricted-paths/server/one/a.ts'),
options: [{
zones: [{
target: './tests/files/restricted-paths/server/one',
from: './tests/files/restricted-paths/server',
except: ['./two'],
}],
}],
parser,
settings,
}),
test({
code: 'import type a from "../one/a.ts"',
filename: testFilePath('./restricted-paths/server/two-new/a.ts'),
options: [{
zones: [{
target: './tests/files/restricted-paths/server/two',
from: './tests/files/restricted-paths/server',
except: [],
}],
}],
parser,
settings,
}),
test({
code: 'import type A from "../two/a.ts"',
filename: testFilePath('./restricted-paths/server/one/a.ts'),
options: [{
zones: [{
target: '**/*',
from: './tests/files/restricted-paths/server/**/*',
except: ['**/a.js'],
}],
}],
parser,
settings,
}),
// no config
test({ code: 'import type b from "../server/b.js"', parser, settings }),
test({ code: 'import type * as b from "../server/b.js"', parser, settings }),
],
invalid: [
test({
code: 'import type b from "../server/b"',
filename: testFilePath('./restricted-paths/client/a.ts'),
options: [{
zones: [{ target: './tests/files/restricted-paths/client', from: './tests/files/restricted-paths/server' }],
}],
errors: [{
message: 'Unexpected path "../server/b" imported in restricted zone.',
line: 1,
column: 20,
}],
parser,
settings,
}),
test({
code: 'import type b from "../server/b"',
filename: testFilePath('./restricted-paths/client/a.ts'),
options: [{
zones: [{ target: './tests/files/restricted-paths/client/**/*', from: './tests/files/restricted-paths/server' }],
}],
errors: [{
message: 'Unexpected path "../server/b" imported in restricted zone.',
line: 1,
column: 20,
}],
parser,
settings,
}),
test({
code: 'import type a from "../client/a"\nimport type c from "./c.ts"',
filename: testFilePath('./restricted-paths/server/b.ts'),
options: [{
zones: [
{
target: './tests/files/restricted-paths/server',
from: ['./tests/files/restricted-paths/client', './tests/files/restricted-paths/server/c.ts'],
},
],
}],
errors: [
{
message: 'Unexpected path "../client/a" imported in restricted zone.',
line: 1,
column: 20,
},
{
message: 'Unexpected path "./c.ts" imported in restricted zone.',
line: 2,
column: 20,
},
],
parser,
settings,
}),
test({
code: 'import type b from "../server/b"',
filename: testFilePath('./restricted-paths/client/a'),
options: [{
zones: [{ target: './client', from: './server' }],
basePath: testFilePath('./restricted-paths'),
}],
errors: [{
message: 'Unexpected path "../server/b" imported in restricted zone.',
line: 1,
column: 20,
}],
parser,
settings,
}),
test({
code: 'import type b from "../two/a"',
filename: testFilePath('./restricted-paths/server/one/a.ts'),
options: [{
zones: [{
target: './tests/files/restricted-paths/server/one',
from: './tests/files/restricted-paths/server',
except: ['./one'],
}],
}],
errors: [{
message: 'Unexpected path "../two/a" imported in restricted zone.',
line: 1,
column: 20,
}],
parser,
settings,
}),
test({
code: 'import type b from "../two/a"',
filename: testFilePath('./restricted-paths/server/one/a'),
options: [{
zones: [{
target: './tests/files/restricted-paths/server/one',
from: './tests/files/restricted-paths/server',
except: ['./one'],
message: 'Custom message',
}],
}],
errors: [{
message: 'Unexpected path "../two/a" imported in restricted zone. Custom message',
line: 1,
column: 20,
}],
parser,
settings,
}),
test({
code: 'import type b from "../two/a"',
filename: testFilePath('./restricted-paths/server/one/a.ts'),
options: [{
zones: [{
target: './tests/files/restricted-paths/server/one',
from: './tests/files/restricted-paths/server',
except: ['../client/a'],
}],
}],
errors: [{
message: 'Restricted path exceptions must be descendants of the configured ' +
'`from` path for that zone.',
line: 1,
column: 20,
}],
parser,
settings,
}),
test({
code: 'import type A from "../two/a"',
filename: testFilePath('./restricted-paths/server/one/a.ts'),
options: [{
zones: [{
target: '**/*',
from: './tests/files/restricted-paths/server/**/*',
}],
}],
errors: [{
message: 'Unexpected path "../two/a" imported in restricted zone.',
line: 1,
column: 20,
}],
parser,
settings,
}),
test({
code: 'import type A from "../two/a"',
filename: testFilePath('./restricted-paths/server/one/a.ts'),
options: [{
zones: [{
target: '**/*',
from: './tests/files/restricted-paths/server/**/*',
except: ['a.ts'],
}],
}],
errors: [{
message: 'Restricted path exceptions must be glob patterns when `from` contains glob patterns',
line: 1,
column: 20,
}],
parser,
settings,
}),
],
});
});
});