From 180c3b5c9c772f3ee973d02c3c064289d9d2eeef Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 19 May 2022 15:02:28 +0400 Subject: [PATCH] [Tests] `no-restricted-paths`: `import type` tests --- CHANGELOG.md | 2 + tests/src/rules/no-restricted-paths.js | 270 ++++++++++++++++++++++++- 2 files changed, 270 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1bbceb50f..b7c4bd0d58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange - [Performance] `ExportMap`: add caching after parsing for an ambiguous module ([#2531], thanks [@stenin-nikita]) - [Docs] [`no-useless-path-segments`]: fix paths ([#2424], thanks [@s-h-a-d-o-w]) - [Tests] [`no-cycle`]: add passing test cases ([#2438], thanks [@georeith]) +- [Tests] [`no-restricted-paths`]: Tests for `import type` statements, thanks [@golergka] ## [2.26.0] - 2022-04-05 @@ -1588,6 +1589,7 @@ for info on changes for earlier releases. [@georeith]: https://github.com/georeith [@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 diff --git a/tests/src/rules/no-restricted-paths.js b/tests/src/rules/no-restricted-paths.js index d782a14472..ec62556f0f 100644 --- a/tests/src/rules/no-restricted-paths.js +++ b/tests/src/rules/no-restricted-paths.js @@ -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, { @@ -712,3 +711,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` contains glob patterns', + line: 1, + column: 20, + } ], + parser, + settings, + }), + ], + }); + }); +});