1- import { OptionManager } from '@babel/core'
2- import { find , keys , replace , some , startsWith } from '@dword-design/functions'
3- import { resolvePath as defaultResolvePath } from 'babel-plugin-module-resolver'
4- import deepmerge from 'deepmerge'
5- import P from 'path'
1+ import { OptionManager } from '@babel/core' ;
2+ import { find , keys , replace , some , startsWith } from '@dword-design/functions' ;
3+ import { resolvePath as defaultResolvePath } from 'babel-plugin-module-resolver' ;
4+ import deepmerge from 'deepmerge' ;
5+ import P from 'path' ;
66
7- const isParentImport = path => / ^ ( \. \/ ) ? \. \. \/ / . test ( path )
7+ const isParentImport = path => / ^ ( \. \/ ) ? \. \. \/ / . test ( path ) ;
88
99const findMatchingAlias = ( sourcePath , currentFile , options ) => {
10- const resolvePath = options . resolvePath || defaultResolvePath
10+ const resolvePath = options . resolvePath || defaultResolvePath ;
11+ const absoluteSourcePath = P . resolve ( P . dirname ( currentFile ) , sourcePath ) ;
1112
12- const absoluteSourcePath = P . resolve ( P . dirname ( currentFile ) , sourcePath )
1313 for ( const aliasName of options . alias | > keys ) {
1414 const path = P . resolve (
1515 P . dirname ( currentFile ) ,
1616 resolvePath ( `${ aliasName } /` , currentFile , options ) ,
17- )
17+ ) ;
18+
1819 if ( absoluteSourcePath | > startsWith ( path ) ) {
19- return { name : aliasName , path }
20+ return { name : aliasName , path } ;
2021 }
2122 }
2223
23- return undefined
24- }
24+ return undefined ;
25+ } ;
2526
2627export default {
2728 create : context => {
28- const currentFile = context . getFilename ( )
29-
30- const folder = P . dirname ( currentFile )
29+ const currentFile = context . getFilename ( ) ;
30+ const folder = P . dirname ( currentFile ) ;
3131 // can't check a non-file
32- if ( currentFile === '<text>' ) return { }
33-
34- const manager = new OptionManager ( )
32+ if ( currentFile === '<text>' ) return { } ;
33+ const manager = new OptionManager ( ) ;
3534
3635 const babelConfig = manager . init ( {
3736 filename : currentFile ,
3837 rootMode : 'upward-optional' ,
39- } )
38+ } ) ;
4039
41- const plugin = babelConfig . plugins | > find ( { key : 'module-resolver' } )
40+ const plugin = babelConfig . plugins | > find ( { key : 'module-resolver' } ) ;
4241
4342 const options = deepmerge . all ( [
4443 { alias : [ ] } ,
4544 plugin ?. options || { } ,
4645 context . options [ 0 ] || { } ,
47- ] )
46+ ] ) ;
47+
4848 if ( options . alias . length === 0 ) {
4949 throw new Error (
5050 'No alias configured. You have to define aliases by either passing them to the babel-plugin-module-resolver plugin in your Babel config, or directly to the prefer-alias rule.' ,
51- )
51+ ) ;
5252 }
5353
54- const resolvePath = options . resolvePath || defaultResolvePath
55-
54+ const resolvePath = options . resolvePath || defaultResolvePath ;
5655 return {
5756 ImportDeclaration : node => {
58- const sourcePath = node . source . value
57+ const sourcePath = node . source . value ;
5958
6059 const hasAlias =
6160 options . alias
6261 | > keys
63- | > some ( alias => sourcePath | > startsWith ( `${ alias } /` ) )
62+ | > some ( alias => sourcePath | > startsWith ( `${ alias } /` ) ) ;
63+
6464 // relative parent
6565 if ( sourcePath | > isParentImport ) {
6666 const matchingAlias = findMatchingAlias (
6767 sourcePath ,
6868 currentFile ,
6969 options ,
70- )
70+ ) ;
71+
7172 if ( ! matchingAlias ) {
72- return undefined
73+ return undefined ;
7374 }
7475
75- const absoluteImportPath = P . resolve ( folder , sourcePath )
76+ const absoluteImportPath = P . resolve ( folder , sourcePath ) ;
7677
7778 const rewrittenImport = `${ matchingAlias . name } /${
7879 P . relative ( matchingAlias . path , absoluteImportPath )
7980 | > replace ( / \\ / g, '/' )
80- } `
81+ } `;
8182
8283 return context . report ( {
8384 fix : fixer =>
@@ -87,10 +88,15 @@ export default {
8788 ) ,
8889 message : `Unexpected parent import '${ sourcePath } '. Use '${ rewrittenImport } ' instead` ,
8990 node,
90- } )
91+ } ) ;
9192 }
9293
93- const importWithoutAlias = resolvePath ( sourcePath , currentFile , options )
94+ const importWithoutAlias = resolvePath (
95+ sourcePath ,
96+ currentFile ,
97+ options ,
98+ ) ;
99+
94100 if ( ! ( importWithoutAlias | > isParentImport ) && hasAlias ) {
95101 return context . report ( {
96102 fix : fixer =>
@@ -100,26 +106,22 @@ export default {
100106 ) ,
101107 message : `Unexpected subpath import via alias '${ sourcePath } '. Use '${ importWithoutAlias } ' instead` ,
102108 node,
103- } )
109+ } ) ;
104110 }
105111
106- return undefined
112+ return undefined ;
107113 } ,
108- }
114+ } ;
109115 } ,
110116 meta : {
111117 fixable : true ,
112118 schema : [
113119 {
114120 additionalProperties : false ,
115- properties : {
116- alias : {
117- type : 'object' ,
118- } ,
119- } ,
121+ properties : { alias : { type : 'object' } } ,
120122 type : 'object' ,
121123 } ,
122124 ] ,
123125 type : 'suggestion' ,
124126 } ,
125- }
127+ } ;
0 commit comments