Skip to content

Commit fedecb7

Browse files
author
Tobias Lengsholz
committed
Enable imports from workspaces in a "shared" folder if it shares a parent
1 parent c606209 commit fedecb7

File tree

3 files changed

+58
-12
lines changed

3 files changed

+58
-12
lines changed

lib/rules/no-cross-imports.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ module.exports.meta = {
3131
],
3232
};
3333

34+
const filterSharedPackagesInCurrentScope = ({ location: currentLocation }) => ({
35+
location,
36+
}) => {
37+
const locationArray = location.split('/');
38+
const forbiddenPackageParent = locationArray.slice(0, -1).join('/');
39+
if (!isSubPath(forbiddenPackageParent, currentLocation)) {
40+
return true;
41+
}
42+
43+
return locationArray[locationArray.length - 1] !== 'shared';
44+
};
45+
3446
module.exports.create = (context) => {
3547
const {
3648
options: [{ allow = [] } = {}],
@@ -40,17 +52,19 @@ module.exports.create = (context) => {
4052
const forbidden = packages.filter(({ name }) => !allowed.includes(name));
4153

4254
return getImport(context, ({ node, value, path, currentPackage }) => {
43-
forbidden.forEach(({ name, location }) => {
44-
if (
45-
name !== currentPackage.name &&
46-
(isSubPath(name, value) || isSubPath(location, path))
47-
) {
48-
context.report({
49-
node,
50-
message: 'Import from package "{{name}}" is not allowed',
51-
data: { name },
52-
});
53-
}
54-
});
55+
forbidden
56+
.filter(filterSharedPackagesInCurrentScope(currentPackage))
57+
.forEach(({ name, location }) => {
58+
if (
59+
name !== currentPackage.name &&
60+
(isSubPath(name, value) || isSubPath(location, path))
61+
) {
62+
context.report({
63+
node,
64+
message: 'Import from package "{{name}}" is not allowed',
65+
data: { name },
66+
});
67+
}
68+
});
5569
});
5670
};

tests/rules/no-cross-imports.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ ruleTester.run('no-cross-imports', rule, {
3838
filename: '/some/file.js',
3939
code: "import '@test/workspace';",
4040
},
41+
{
42+
filename: '/test/scope/workspace/file.js',
43+
code: "import '@test/shared-in-scope';",
44+
},
4145
],
4246

4347
invalid: [
@@ -184,5 +188,15 @@ ruleTester.run('no-cross-imports', rule, {
184188
},
185189
],
186190
},
191+
{
192+
filename: '/test/scope/workspace/file.js',
193+
code: "import '@test/shared-outside-scope';",
194+
errors: [
195+
{
196+
message:
197+
'Import from package "@test/shared-outside-scope" is not allowed',
198+
},
199+
],
200+
},
187201
],
188202
});

tests/setup.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,22 @@ mock('get-monorepo-packages', () => [
2222
name: '@test/third-workspace',
2323
},
2424
},
25+
{
26+
location: '/test/scope/shared',
27+
package: {
28+
name: '@test/shared-in-scope',
29+
},
30+
},
31+
{
32+
location: '/test/other-scope/shared',
33+
package: {
34+
name: '@test/shared-outside-scope',
35+
},
36+
},
37+
{
38+
location: '/test/scope/workspace',
39+
package: {
40+
name: '@test/scoped-workspace',
41+
},
42+
},
2543
]);

0 commit comments

Comments
 (0)