Skip to content

Commit

Permalink
Also lint in variable declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
jj-ivx committed Oct 11, 2019
1 parent cdd92d3 commit 36af0a9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
40 changes: 24 additions & 16 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,38 @@ const packages = getPackages(process.cwd()).map(
}),
);

const isImportExpression = expression =>
expression.type === 'CallExpression' &&
expression.arguments.length > 0 &&
expression.arguments[0].type === 'Literal' &&
(expression.callee.type === 'Import' ||
(expression.callee.type === 'Identifier' &&
expression.callee.name === 'require'));

const resolveImport = (context, node, { value, range }) => {
const path = resolve(dirname(context.getFilename()), value);
const [start, end] = range;
return { node, value, path, start, end };
};

const getImport = (context, callback) => ({
ImportDeclaration: node => {
if (node.source.type === 'Literal') {
const { value, range } = node.source;
const path = resolve(dirname(context.getFilename()), value);
const [start, end] = range;
callback({ node, value, path, start, end });
callback(resolveImport(context, node, node.source));
}
},
ExpressionStatement: node => {
if (
node.expression.type === 'CallExpression' &&
node.expression.arguments.length > 0 &&
node.expression.arguments[0].type === 'Literal' &&
(node.expression.callee.type === 'Import' ||
(node.expression.callee.type === 'Identifier' &&
node.expression.callee.name === 'require'))
) {
const { value, range } = node.expression.arguments[0];
const path = resolve(dirname(context.getFilename()), value);
const [start, end] = range;
callback({ node, value, path, start, end });
if (isImportExpression(node.expression)) {
callback(resolveImport(context, node, node.expression.arguments[0]));
}
},
VariableDeclaration: node => {
node.declarations.forEach(({ init }) => {
if (isImportExpression(init)) {
callback(resolveImport(context, node, init.arguments[0]));
}
});
},
});

module.exports = { isSubPath, packages, getImport };
18 changes: 18 additions & 0 deletions tests/rules/no-cross-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ ruleTester.run('no-cross-imports', rule, {
},
],
},
{
code: "const test = import('@test/workspace');",
filename: '/some/path.js',
errors: [
{
message: 'Import from package "@test/workspace" is not allowed',
},
],
},
{
code: "require('@test/workspace');",
filename: '/some/path.js',
Expand All @@ -59,6 +68,15 @@ ruleTester.run('no-cross-imports', rule, {
},
],
},
{
code: "const test = require('@test/workspace');",
filename: '/some/path.js',
errors: [
{
message: 'Import from package "@test/workspace" is not allowed',
},
],
},
{
code: "import workspace from '@test/workspace';",
filename: '/some/path.js',
Expand Down

0 comments on commit 36af0a9

Please sign in to comment.