Skip to content

Commit 36af0a9

Browse files
committed
Also lint in variable declarations
1 parent cdd92d3 commit 36af0a9

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

lib/utils.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,38 @@ const packages = getPackages(process.cwd()).map(
2020
}),
2121
);
2222

23+
const isImportExpression = expression =>
24+
expression.type === 'CallExpression' &&
25+
expression.arguments.length > 0 &&
26+
expression.arguments[0].type === 'Literal' &&
27+
(expression.callee.type === 'Import' ||
28+
(expression.callee.type === 'Identifier' &&
29+
expression.callee.name === 'require'));
30+
31+
const resolveImport = (context, node, { value, range }) => {
32+
const path = resolve(dirname(context.getFilename()), value);
33+
const [start, end] = range;
34+
return { node, value, path, start, end };
35+
};
36+
2337
const getImport = (context, callback) => ({
2438
ImportDeclaration: node => {
2539
if (node.source.type === 'Literal') {
26-
const { value, range } = node.source;
27-
const path = resolve(dirname(context.getFilename()), value);
28-
const [start, end] = range;
29-
callback({ node, value, path, start, end });
40+
callback(resolveImport(context, node, node.source));
3041
}
3142
},
3243
ExpressionStatement: node => {
33-
if (
34-
node.expression.type === 'CallExpression' &&
35-
node.expression.arguments.length > 0 &&
36-
node.expression.arguments[0].type === 'Literal' &&
37-
(node.expression.callee.type === 'Import' ||
38-
(node.expression.callee.type === 'Identifier' &&
39-
node.expression.callee.name === 'require'))
40-
) {
41-
const { value, range } = node.expression.arguments[0];
42-
const path = resolve(dirname(context.getFilename()), value);
43-
const [start, end] = range;
44-
callback({ node, value, path, start, end });
44+
if (isImportExpression(node.expression)) {
45+
callback(resolveImport(context, node, node.expression.arguments[0]));
4546
}
4647
},
48+
VariableDeclaration: node => {
49+
node.declarations.forEach(({ init }) => {
50+
if (isImportExpression(init)) {
51+
callback(resolveImport(context, node, init.arguments[0]));
52+
}
53+
});
54+
},
4755
});
4856

4957
module.exports = { isSubPath, packages, getImport };

tests/rules/no-cross-imports.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ ruleTester.run('no-cross-imports', rule, {
5050
},
5151
],
5252
},
53+
{
54+
code: "const test = import('@test/workspace');",
55+
filename: '/some/path.js',
56+
errors: [
57+
{
58+
message: 'Import from package "@test/workspace" is not allowed',
59+
},
60+
],
61+
},
5362
{
5463
code: "require('@test/workspace');",
5564
filename: '/some/path.js',
@@ -59,6 +68,15 @@ ruleTester.run('no-cross-imports', rule, {
5968
},
6069
],
6170
},
71+
{
72+
code: "const test = require('@test/workspace');",
73+
filename: '/some/path.js',
74+
errors: [
75+
{
76+
message: 'Import from package "@test/workspace" is not allowed',
77+
},
78+
],
79+
},
6280
{
6381
code: "import workspace from '@test/workspace';",
6482
filename: '/some/path.js',

0 commit comments

Comments
 (0)