Skip to content

Commit dca97f6

Browse files
committed
Add 'require-dependency' rule
1 parent 0d8a02d commit dca97f6

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

lib/rules/require-dependency.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const { isSubPath, packages, getImport } = require('../utils');
4+
5+
module.exports.meta = {
6+
type: 'problem',
7+
docs: {
8+
url:
9+
'https://github.com/joshuajaco/eslint-plugin-workspaces/docs/rules/require-dependency.md',
10+
},
11+
};
12+
13+
module.exports.create = context =>
14+
getImport(context, ({ node, value, path }) => {
15+
const { dependencies, name: currentPackage } = packages.find(
16+
({ location }) => isSubPath(location, context.getFilename()),
17+
);
18+
19+
packages.forEach(({ name, location }) => {
20+
if (
21+
name !== currentPackage &&
22+
(isSubPath(name, value) || isSubPath(location, path)) &&
23+
!Object.keys(dependencies).includes(name)
24+
) {
25+
context.report({
26+
node,
27+
message:
28+
'Importing packages without listing them as dependency is not allowed',
29+
});
30+
}
31+
});
32+
});

lib/utils.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ const isSubPath = (parent, path) => {
99
};
1010

1111
const packages = getPackages(process.cwd()).map(
12-
({ package: { name }, location }) => ({ name, location }),
12+
({ package: { name, dependencies = {} }, location }) => ({
13+
name,
14+
location,
15+
dependencies,
16+
}),
1317
);
1418

1519
const getImport = (context, callback) => ({

tests/rules/require-dependency.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
const ruleTester = require('../ruleTester');
4+
const rule = require('../../lib/rules/require-dependency');
5+
6+
ruleTester.run('require-dependency', rule, {
7+
valid: [
8+
{
9+
filename: '/test/another-workspace/index.js',
10+
code: "import '../workspace';",
11+
},
12+
],
13+
invalid: [
14+
{
15+
filename: '/test/workspace/index.js',
16+
code: "import '../another-workspace';",
17+
errors: [
18+
{
19+
message:
20+
'Importing packages without listing them as dependency is not allowed',
21+
},
22+
],
23+
},
24+
],
25+
});

tests/setup.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@
33
const mock = require('mock-require');
44

55
mock('get-monorepo-packages', () => [
6-
{ location: '/test/workspace', package: { name: '@test/workspace' } },
6+
{
7+
location: '/test/workspace',
8+
package: {
9+
name: '@test/workspace',
10+
},
11+
},
712
{
813
location: '/test/another-workspace',
9-
package: { name: '@test/another-workspace' },
14+
package: {
15+
name: '@test/another-workspace',
16+
dependencies: { '@test/workspace': '^1.0.0' },
17+
},
1018
},
1119
]);

0 commit comments

Comments
 (0)