From 0dfd08661aa03d5af15ed1d42851e551325cdc38 Mon Sep 17 00:00:00 2001 From: Vasily Malykhin Date: Sat, 6 Jun 2020 08:59:23 +0300 Subject: [PATCH] [New] `no-restricted-paths`: Add custom message support --- CHANGELOG.md | 3 +++ docs/rules/no-restricted-paths.md | 1 + src/rules/no-restricted-paths.js | 4 +++- tests/src/rules/no-restricted-paths.js | 18 ++++++++++++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88a32dade4..9b4cb56088 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel - [`no-cycle`]: add `ignoreExternal` option ([#1681], thanks [@sveyret]) - [`order`]: Add support for TypeScript's "import equals"-expressions ([#1785], thanks [@manuth]) - [`import/default`]: support default export in TSExportAssignment ([#1689], thanks [@Maxim-Mazurok]) +- [`no-restricted-paths`]: add custom message support ([#1802], thanks [@malykhinvi]) ### Fixed - [`group-exports`]: Flow type export awareness ([#1702], thanks [@ernestostifano]) @@ -688,6 +689,7 @@ for info on changes for earlier releases. [`memo-parser`]: ./memo-parser/README.md +[#1802]: https://github.com/benmosher/eslint-plugin-import/pull/1802 [#1788]: https://github.com/benmosher/eslint-plugin-import/pull/1788 [#1786]: https://github.com/benmosher/eslint-plugin-import/pull/1786 [#1785]: https://github.com/benmosher/eslint-plugin-import/pull/1785 @@ -1192,3 +1194,4 @@ for info on changes for earlier releases. [@adamborowski]: https://github.com/adamborowski [@adjerbetian]: https://github.com/adjerbetian [@Maxim-Mazurok]: https://github.com/Maxim-Mazurok +[@malykhinvi]: https://github.com/malykhinvi diff --git a/docs/rules/no-restricted-paths.md b/docs/rules/no-restricted-paths.md index 3776699836..bfcb9af237 100644 --- a/docs/rules/no-restricted-paths.md +++ b/docs/rules/no-restricted-paths.md @@ -10,6 +10,7 @@ In order to prevent such scenarios this rule allows you to define restricted zon This rule has one option. The option is an object containing the definition of all restricted `zones` and the optional `basePath` which is used to resolve relative paths within. The default value for `basePath` is the current working directory. Each zone consists of the `target` path and a `from` path. The `target` is the path where the restricted imports should be applied. The `from` path defines the folder that is not allowed to be used in an import. An optional `except` may be defined for a zone, allowing exception paths that would otherwise violate the related `from`. Note that `except` is relative to `from` and cannot backtrack to a parent directory. +You may also specify an optional `message` for a zone, which will be displayed in case of the rule violation. ### Examples diff --git a/src/rules/no-restricted-paths.js b/src/rules/no-restricted-paths.js index 221457b1c9..1627d533e6 100644 --- a/src/rules/no-restricted-paths.js +++ b/src/rules/no-restricted-paths.js @@ -32,6 +32,7 @@ module.exports = { }, uniqueItems: true, }, + message: { type: 'string' }, }, additionalProperties: false, }, @@ -102,7 +103,8 @@ module.exports = { context.report({ node, - message: `Unexpected path "{{importPath}}" imported in restricted zone.`, + message: `Unexpected path "{{importPath}}" imported in restricted zone.` + + (zone.message ? ` ${zone.message}` : ''), data: { importPath }, }) }) diff --git a/tests/src/rules/no-restricted-paths.js b/tests/src/rules/no-restricted-paths.js index 1c3edb3dae..0a729a246e 100644 --- a/tests/src/rules/no-restricted-paths.js +++ b/tests/src/rules/no-restricted-paths.js @@ -145,6 +145,24 @@ ruleTester.run('no-restricted-paths', rule, { column: 15, } ], }), + test({ + code: 'import b from "../two/a.js"', + filename: testFilePath('./restricted-paths/server/one/a.js'), + 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.js" imported in restricted zone. ' + + 'Custom message', + line: 1, + column: 15, + } ], + }), test({ code: 'import b from "../two/a.js"', filename: testFilePath('./restricted-paths/server/one/a.js'),