Skip to content

Commit 669af0f

Browse files
authored
feat: no-parent-barrel-import
1 parent 328064a commit 669af0f

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/rules/no-parent-barrel-import.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @fileOverview Forbids a module from importing from parent barrel file
3+
* @author jonioni
4+
*/
5+
6+
import { parse } from "path";
7+
import resolve from 'eslint-module-utils/resolve';
8+
import moduleVisitor from 'eslint-module-utils/moduleVisitor';
9+
import docsUrl from '../docsUrl';
10+
11+
function isImportingFromParentBarrel(context, node, requireName) {
12+
const filePath = context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename();
13+
const importPath = resolve(requireName, context);
14+
const importDetails = parse(importPath);
15+
if (importDetails.name === "index" && filePath.startsWith(importDetails.dir)) {
16+
context.report({
17+
node,
18+
message: 'Module imports from parent barrel file.',
19+
});
20+
}
21+
}
22+
23+
module.exports = {
24+
meta: {
25+
type: 'problem',
26+
docs: {
27+
category: 'Static analysis',
28+
description: 'Forbid a module from importing from parent barrel file.',
29+
recommended: true,
30+
url: docsUrl('no-parent-barrel-import'),
31+
},
32+
schema: [],
33+
},
34+
create(context) {
35+
return isImportingFromParentBarrel((source, node) => {
36+
isImportingSelf(context, node, source.value);
37+
}, { commonjs: true });
38+
},
39+
};

0 commit comments

Comments
 (0)