forked from graphql/graphql-js
/
no-dir-import.js
36 lines (30 loc) · 894 Bytes
/
no-dir-import.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'use strict';
const fs = require('fs');
const path = require('path');
module.exports = function noDirImportRule(context) {
return {
ImportDeclaration: checkImportPath,
ExportNamedDeclaration: checkImportPath,
};
function checkImportPath(node) {
const { source } = node;
// bail if the declaration doesn't have a source, e.g. "export { foo };"
if (!source) {
return;
}
const importPath = source.value;
if (importPath.startsWith('./') || importPath.startsWith('../')) {
const baseDir = path.dirname(context.getFilename());
const resolvedPath = path.resolve(baseDir, importPath);
if (
fs.existsSync(resolvedPath) &&
fs.statSync(resolvedPath).isDirectory()
) {
context.report({
node: source,
message: 'It is not allowed to import from directory',
});
}
}
}
};