Skip to content

Commit 0b81052

Browse files
malykhinviljharb
authored andcommitted
[New] no-restricted-paths: Add custom message support
1 parent 0b585a1 commit 0b81052

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1111
- [`no-cycle`]: add `ignoreExternal` option ([#1681], thanks [@sveyret])
1212
- [`order`]: Add support for TypeScript's "import equals"-expressions ([#1785], thanks [@manuth])
1313
- [`import/default`]: support default export in TSExportAssignment ([#1689], thanks [@Maxim-Mazurok])
14+
- [`no-restricted-paths`]: add custom message support ([#1802], thanks [@malykhinvi])
1415

1516
### Fixed
1617
- [`group-exports`]: Flow type export awareness ([#1702], thanks [@ernestostifano])
@@ -688,6 +689,7 @@ for info on changes for earlier releases.
688689

689690
[`memo-parser`]: ./memo-parser/README.md
690691

692+
[#1802]: https://github.com/benmosher/eslint-plugin-import/pull/1802
691693
[#1788]: https://github.com/benmosher/eslint-plugin-import/pull/1788
692694
[#1786]: https://github.com/benmosher/eslint-plugin-import/pull/1786
693695
[#1785]: https://github.com/benmosher/eslint-plugin-import/pull/1785
@@ -1192,3 +1194,4 @@ for info on changes for earlier releases.
11921194
[@adamborowski]: https://github.com/adamborowski
11931195
[@adjerbetian]: https://github.com/adjerbetian
11941196
[@Maxim-Mazurok]: https://github.com/Maxim-Mazurok
1197+
[@malykhinvi]: https://github.com/malykhinvi

docs/rules/no-restricted-paths.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ In order to prevent such scenarios this rule allows you to define restricted zon
1010
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.
1111
The default value for `basePath` is the current working directory.
1212
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.
13+
You may also specify an optional `message` for a zone, which will be displayed in case of the rule violation.
1314

1415
### Examples
1516

src/rules/no-restricted-paths.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module.exports = {
3232
},
3333
uniqueItems: true,
3434
},
35+
message: { type: 'string' },
3536
},
3637
additionalProperties: false,
3738
},
@@ -102,7 +103,7 @@ module.exports = {
102103

103104
context.report({
104105
node,
105-
message: `Unexpected path "{{importPath}}" imported in restricted zone.`,
106+
message: `Unexpected path "{{importPath}}" imported in restricted zone.${zone.message ? ` ${zone.message}` : ''}`,
106107
data: { importPath },
107108
})
108109
})

tests/src/rules/no-restricted-paths.js

+17
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,23 @@ ruleTester.run('no-restricted-paths', rule, {
145145
column: 15,
146146
} ],
147147
}),
148+
test({
149+
code: 'import b from "../two/a.js"',
150+
filename: testFilePath('./restricted-paths/server/one/a.js'),
151+
options: [ {
152+
zones: [ {
153+
target: './tests/files/restricted-paths/server/one',
154+
from: './tests/files/restricted-paths/server',
155+
except: ['./one'],
156+
message: 'Custom message',
157+
} ],
158+
} ],
159+
errors: [ {
160+
message: 'Unexpected path "../two/a.js" imported in restricted zone. Custom message',
161+
line: 1,
162+
column: 15,
163+
} ],
164+
}),
148165
test({
149166
code: 'import b from "../two/a.js"',
150167
filename: testFilePath('./restricted-paths/server/one/a.js'),

0 commit comments

Comments
 (0)