Skip to content

Commit 53e4090

Browse files
author
Tobias Lengsholz
committed
Make scopes configurable
1 parent fedecb7 commit 53e4090

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

lib/rules/no-cross-imports.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,59 @@ module.exports.meta = {
2626
},
2727
],
2828
},
29+
scopes: {
30+
anyOf: [
31+
{ type: 'boolean', additionalProperties: false },
32+
{
33+
type: 'object',
34+
additionalProperties: false,
35+
properties: {
36+
enable: { type: 'boolean' },
37+
folderName: { type: 'string' },
38+
},
39+
},
40+
],
41+
},
2942
},
3043
},
3144
],
3245
};
3346

34-
const filterSharedPackagesInCurrentScope = ({ location: currentLocation }) => ({
35-
location,
36-
}) => {
47+
const filterSharedPackagesInCurrentScope = (
48+
{ location: currentLocation },
49+
scopedEnabled,
50+
scopedSharingFolderName,
51+
) => ({ location }) => {
52+
if (!scopedEnabled) return true;
3753
const locationArray = location.split('/');
3854
const forbiddenPackageParent = locationArray.slice(0, -1).join('/');
3955
if (!isSubPath(forbiddenPackageParent, currentLocation)) {
4056
return true;
4157
}
4258

43-
return locationArray[locationArray.length - 1] !== 'shared';
59+
return locationArray[locationArray.length - 1] !== scopedSharingFolderName;
4460
};
4561

4662
module.exports.create = (context) => {
4763
const {
48-
options: [{ allow = [] } = {}],
64+
options: [{ allow = [], scopes = { enable: false } } = {}],
4965
} = context;
5066

5167
const allowed = typeof allow === 'string' ? [allow] : allow;
68+
const scopedEnabled = scopes === true || !!scopes.enable;
69+
const scopedSharingFolderName = scopes.folderName || 'shared';
70+
5271
const forbidden = packages.filter(({ name }) => !allowed.includes(name));
5372

5473
return getImport(context, ({ node, value, path, currentPackage }) => {
5574
forbidden
56-
.filter(filterSharedPackagesInCurrentScope(currentPackage))
75+
.filter(
76+
filterSharedPackagesInCurrentScope(
77+
currentPackage,
78+
scopedEnabled,
79+
scopedSharingFolderName,
80+
),
81+
)
5782
.forEach(({ name, location }) => {
5883
if (
5984
name !== currentPackage.name &&

tests/rules/no-cross-imports.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ ruleTester.run('no-cross-imports', rule, {
3939
code: "import '@test/workspace';",
4040
},
4141
{
42+
options: [{ scopes: true }],
43+
filename: '/test/scope/workspace/file.js',
44+
code: "import '@test/shared-in-scope';",
45+
},
46+
{
47+
options: [{ scopes: { enable: true } }],
48+
filename: '/test/scope/workspace/file.js',
49+
code: "import '@test/shared-in-scope';",
50+
},
51+
{
52+
options: [{ scopes: { enable: true, folderName: 'shared' } }],
4253
filename: '/test/scope/workspace/file.js',
4354
code: "import '@test/shared-in-scope';",
4455
},
@@ -189,6 +200,7 @@ ruleTester.run('no-cross-imports', rule, {
189200
],
190201
},
191202
{
203+
options: [{ scopes: true }],
192204
filename: '/test/scope/workspace/file.js',
193205
code: "import '@test/shared-outside-scope';",
194206
errors: [
@@ -198,5 +210,24 @@ ruleTester.run('no-cross-imports', rule, {
198210
},
199211
],
200212
},
213+
{
214+
filename: '/test/scope/workspace/file.js',
215+
code: "import '@test/shared-in-scope';",
216+
errors: [
217+
{
218+
message: 'Import from package "@test/shared-in-scope" is not allowed',
219+
},
220+
],
221+
},
222+
{
223+
options: [{ scopes: { enable: true, folderName: 'something-else' } }],
224+
filename: '/test/scope/workspace/file.js',
225+
code: "import '@test/shared-in-scope';",
226+
errors: [
227+
{
228+
message: 'Import from package "@test/shared-in-scope" is not allowed',
229+
},
230+
],
231+
},
201232
],
202233
});

0 commit comments

Comments
 (0)