Skip to content

Commit

Permalink
[New] no-restricted-paths: Add custom message support
Browse files Browse the repository at this point in the history
  • Loading branch information
malykhinvi authored and ljharb committed Jun 6, 2020
1 parent 0b585a1 commit 0dfd086
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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])
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions docs/rules/no-restricted-paths.md
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion src/rules/no-restricted-paths.js
Expand Up @@ -32,6 +32,7 @@ module.exports = {
},
uniqueItems: true,
},
message: { type: 'string' },
},
additionalProperties: false,
},
Expand Down Expand Up @@ -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 },
})
})
Expand Down
18 changes: 18 additions & 0 deletions tests/src/rules/no-restricted-paths.js
Expand Up @@ -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'),
Expand Down

0 comments on commit 0dfd086

Please sign in to comment.