Skip to content

Commit b6c761c

Browse files
committed
Share all identical logic between rules
1 parent 3085a2f commit b6c761c

File tree

3 files changed

+62
-84
lines changed

3 files changed

+62
-84
lines changed

lib/rules/no-cross-imports.js

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

3-
const getPackages = require('get-monorepo-packages');
4-
const { relative, resolve, sep, isAbsolute } = require('path');
3+
const { isSubPath, packages, getImportValue } = require('../utils');
4+
const { resolve } = require('path');
55

66
module.exports.meta = {
77
type: 'problem',
@@ -32,55 +32,26 @@ module.exports.schema = [
3232
},
3333
];
3434

35-
const isSubPath = (parent, path) => {
36-
const relativePath = relative(parent, path);
37-
return relativePath.split(sep)[0] !== '..' && !isAbsolute(relativePath);
38-
};
39-
40-
const reportForbiddenPackage = (forbidden, context, node, value) => {
41-
forbidden.forEach(({ name, location }) => {
42-
if (
43-
isSubPath(name, value) ||
44-
isSubPath(location, resolve(context.getFilename(), value))
45-
) {
46-
context.report({
47-
node,
48-
message: 'Import from package "{{name}}" is not allowed',
49-
data: { name },
50-
});
51-
}
52-
});
53-
};
54-
5535
module.exports.create = context => {
56-
const packages = getPackages(process.cwd()).map(
57-
({ package: { name }, location }) => ({ name, location }),
58-
);
59-
6036
const {
6137
options: [{ allow = [] } = {}],
6238
} = context;
6339

6440
const allowed = typeof allow === 'string' ? [allow] : allow;
6541
const forbidden = packages.filter(({ name }) => !allowed.includes(name));
6642

67-
return {
68-
ImportDeclaration: node => {
69-
if (node.source.type === 'Literal') {
70-
reportForbiddenPackage(forbidden, context, node, node.source.value);
71-
}
72-
},
73-
ExpressionStatement: node => {
43+
return getImportValue(context, (value, node) => {
44+
forbidden.forEach(({ name, location }) => {
7445
if (
75-
node.expression.type === 'CallExpression' &&
76-
node.expression.arguments[0].type === 'Literal' &&
77-
(node.expression.callee.type === 'Import' ||
78-
(node.expression.callee.type === 'Identifier' &&
79-
node.expression.callee.name === 'require'))
46+
isSubPath(name, value) ||
47+
isSubPath(location, resolve(context.getFilename(), value))
8048
) {
81-
const { value } = node.expression.arguments[0];
82-
reportForbiddenPackage(forbidden, context, node, value);
49+
context.report({
50+
node,
51+
message: 'Import from package "{{name}}" is not allowed',
52+
data: { name },
53+
});
8354
}
84-
},
85-
};
55+
});
56+
});
8657
};

lib/rules/no-relative-imports.js

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

3-
const getPackages = require('get-monorepo-packages');
4-
const { relative, resolve, sep, isAbsolute } = require('path');
3+
const { isSubPath, packages, getImportValue } = require('../utils');
4+
const { resolve } = require('path');
55

66
module.exports.meta = {
77
type: 'problem',
@@ -13,44 +13,16 @@ module.exports.meta = {
1313

1414
module.exports.schema = [];
1515

16-
const isSubPath = (parent, path) => {
17-
const relativePath = relative(parent, path);
18-
return relativePath.split(sep)[0] !== '..' && !isAbsolute(relativePath);
19-
};
20-
21-
const isRelativeImport = (packages, context, node, value) => {
22-
packages.forEach(location => {
23-
if (
24-
!isSubPath(location, context.getFilename()) &&
25-
isSubPath(location, resolve(context.getFilename(), value))
26-
)
27-
context.report({
28-
node,
29-
message: 'Relative imports of other packages are not allowed',
30-
});
31-
});
32-
};
33-
34-
module.exports.create = context => {
35-
const packages = getPackages(process.cwd()).map(({ location }) => location);
36-
37-
return {
38-
ImportDeclaration: node => {
39-
if (node.source.type === 'Literal') {
40-
isRelativeImport(packages, context, node, node.source.value);
41-
}
42-
},
43-
ExpressionStatement: node => {
16+
module.exports.create = context =>
17+
getImportValue(context, (value, node) => {
18+
packages.forEach(({ location }) => {
4419
if (
45-
node.expression.type === 'CallExpression' &&
46-
node.expression.arguments[0].type === 'Literal' &&
47-
(node.expression.callee.type === 'Import' ||
48-
(node.expression.callee.type === 'Identifier' &&
49-
node.expression.callee.name === 'require'))
50-
) {
51-
const { value } = node.expression.arguments[0];
52-
isRelativeImport(packages, context, node, value);
53-
}
54-
},
55-
};
56-
};
20+
!isSubPath(location, context.getFilename()) &&
21+
isSubPath(location, resolve(context.getFilename(), value))
22+
)
23+
context.report({
24+
node,
25+
message: 'Relative imports of other packages are not allowed',
26+
});
27+
});
28+
});

lib/utils.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const getPackages = require('get-monorepo-packages');
4+
const { relative, sep, isAbsolute } = require('path');
5+
6+
const isSubPath = (parent, path) => {
7+
const relativePath = relative(parent, path);
8+
return relativePath.split(sep)[0] !== '..' && !isAbsolute(relativePath);
9+
};
10+
11+
const packages = getPackages(process.cwd()).map(
12+
({ package: { name }, location }) => ({ name, location }),
13+
);
14+
15+
const getImportValue = (context, callback) => ({
16+
ImportDeclaration: node => {
17+
if (node.source.type === 'Literal') {
18+
callback(node.source.value, node);
19+
}
20+
},
21+
ExpressionStatement: node => {
22+
if (
23+
node.expression.type === 'CallExpression' &&
24+
node.expression.arguments[0].type === 'Literal' &&
25+
(node.expression.callee.type === 'Import' ||
26+
(node.expression.callee.type === 'Identifier' &&
27+
node.expression.callee.name === 'require'))
28+
) {
29+
const { value } = node.expression.arguments[0];
30+
callback(value, node);
31+
}
32+
},
33+
});
34+
35+
module.exports = { isSubPath, packages, getImportValue };

0 commit comments

Comments
 (0)