Skip to content

Commit

Permalink
[Tests] no-restricted-paths: import type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
golergka committed May 19, 2022
1 parent 3767479 commit 36e3f00
Show file tree
Hide file tree
Showing 2 changed files with 270 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

### Changed
- [Tests] `named`: Run all TypeScript test ([#2427], thanks [@ProdigySim])
- [Tests] `no-restricted-paths`: Tests for `import type` statements, thanks [@golergka]
- [readme] note use of typescript in readme `import/extensions` section ([#2440], thanks [@OutdatedVersion])
- [Docs] `order`: use correct default value ([#2392], thanks [@hyperupcall])
- [meta] replace git.io link in comments with the original URL ([#2444], thanks [@liby])
Expand Down Expand Up @@ -1557,6 +1558,7 @@ for info on changes for earlier releases.
[@gausie]: https://github.com/gausie
[@gavriguy]: https://github.com/gavriguy
[@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
270 changes: 268 additions & 2 deletions tests/src/rules/no-restricted-paths.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { RuleTester } from 'eslint';
import { getTSParsers, test, testFilePath } from '../utils';
import rule from 'rules/no-restricted-paths';

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

const ruleTester = new RuleTester();

ruleTester.run('no-restricted-paths', rule, {
Expand Down Expand Up @@ -262,3 +261,270 @@ 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' },
{ target: './tests/files/restricted-paths/server', from: './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" 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` is a glob pattern',
line: 1,
column: 20,
} ],
parser,
settings,
}),
],
});
});
});

0 comments on commit 36e3f00

Please sign in to comment.