File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments