@@ -16,63 +16,71 @@ const fs = require('fs');
16
16
const Module = require ( 'module' ) ;
17
17
18
18
module . exports = function ( context ) {
19
- return {
20
- ImportDeclaration ( node ) {
21
- let source = node . source . value . replace ( / ^ [ a - z ] + : / , '' ) ;
22
- if ( source . startsWith ( '.' ) || Module . builtinModules . includes ( source ) ) {
23
- return ;
24
- }
25
-
26
- // Split the import specifier on slashes. If it starts with an @ then it's
27
- // a scoped package, otherwise just take the first part.
28
- let parts = source . split ( '/' ) ;
29
- let pkgName = source . startsWith ( '@' ) ? parts . slice ( 0 , 2 ) . join ( '/' ) : parts [ 0 ] ;
30
-
31
- // Search for a package.json starting from the current filename
32
- let pkgPath = findUp . sync ( 'package.json' , { cwd : path . dirname ( context . getFilename ( ) ) } ) ;
33
- if ( ! pkgPath ) {
34
- return ;
35
- }
36
-
37
- let pkg = JSON . parse ( fs . readFileSync ( pkgPath , 'utf8' ) ) ;
38
-
39
- // The only dev dependency should be spectrum-css.
40
- if ( exists ( pkg . devDependencies , pkgName ) && pkgName === '@adobe/spectrum-css-temp' ) {
41
- return ;
42
- }
43
-
44
- if ( ! exists ( pkg . dependencies , pkgName ) && ! exists ( pkg . peerDependencies , pkgName ) ) {
45
- context . report ( {
46
- node,
47
- message : `Missing dependency on ${ pkgName } .` ,
48
- fix ( fixer ) {
49
- // Attempt to find a package in the monorepo. If the dep is for an external library,
50
- // then we cannot auto fix it because we don't know the version to add.
51
- let depPath = __dirname + '/../packages/' + pkgName + '/package.json' ;
52
- if ( ! fs . existsSync ( depPath ) ) {
53
- return ;
54
- }
55
-
56
- let depPkg = JSON . parse ( fs . readFileSync ( depPath , 'utf8' ) ) ;
57
-
58
- if ( pkgName === '@react-spectrum/provider' ) {
59
- pkg . peerDependencies = insertObject ( pkg . peerDependencies , pkgName , depPkg . version ) ;
60
- } else {
61
- pkg . dependencies = insertObject ( pkg . dependencies , pkgName , depPkg . version ) ;
62
- }
63
-
64
- fs . writeFileSync ( pkgPath , JSON . stringify ( pkg , false , 2 ) + '\n' ) ;
65
-
66
- // Fake fix so eslint doesn't show the error.
67
- return {
68
- range : [ 0 , 0 ] ,
69
- text : ''
70
- } ;
19
+ let processNode = ( node ) => {
20
+ if ( ! node . source ) {
21
+ return ;
22
+ }
23
+
24
+ let source = node . source . value . replace ( / ^ [ a - z ] + : / , '' ) ;
25
+ if ( source . startsWith ( '.' ) || Module . builtinModules . includes ( source ) ) {
26
+ return ;
27
+ }
28
+
29
+ // Split the import specifier on slashes. If it starts with an @ then it's
30
+ // a scoped package, otherwise just take the first part.
31
+ let parts = source . split ( '/' ) ;
32
+ let pkgName = source . startsWith ( '@' ) ? parts . slice ( 0 , 2 ) . join ( '/' ) : parts [ 0 ] ;
33
+
34
+ // Search for a package.json starting from the current filename
35
+ let pkgPath = findUp . sync ( 'package.json' , { cwd : path . dirname ( context . getFilename ( ) ) } ) ;
36
+ if ( ! pkgPath ) {
37
+ return ;
38
+ }
39
+
40
+ let pkg = JSON . parse ( fs . readFileSync ( pkgPath , 'utf8' ) ) ;
41
+
42
+ // The only dev dependency should be spectrum-css.
43
+ if ( exists ( pkg . devDependencies , pkgName ) && pkgName === '@adobe/spectrum-css-temp' ) {
44
+ return ;
45
+ }
46
+
47
+ if ( ! exists ( pkg . dependencies , pkgName ) && ! exists ( pkg . peerDependencies , pkgName ) ) {
48
+ context . report ( {
49
+ node,
50
+ message : `Missing dependency on ${ pkgName } .` ,
51
+ fix ( fixer ) {
52
+ // Attempt to find a package in the monorepo. If the dep is for an external library,
53
+ // then we cannot auto fix it because we don't know the version to add.
54
+ let depPath = __dirname + '/../packages/' + pkgName + '/package.json' ;
55
+ if ( ! fs . existsSync ( depPath ) ) {
56
+ return ;
71
57
}
72
- } ) ;
73
- }
58
+
59
+ let depPkg = JSON . parse ( fs . readFileSync ( depPath , 'utf8' ) ) ;
60
+
61
+ if ( pkgName === '@react-spectrum/provider' ) {
62
+ pkg . peerDependencies = insertObject ( pkg . peerDependencies , pkgName , depPkg . version ) ;
63
+ } else {
64
+ pkg . dependencies = insertObject ( pkg . dependencies , pkgName , depPkg . version ) ;
65
+ }
66
+
67
+ fs . writeFileSync ( pkgPath , JSON . stringify ( pkg , false , 2 ) + '\n' ) ;
68
+
69
+ // Fake fix so eslint doesn't show the error.
70
+ return {
71
+ range : [ 0 , 0 ] ,
72
+ text : ''
73
+ } ;
74
+ }
75
+ } ) ;
74
76
}
75
77
} ;
78
+
79
+ return {
80
+ ImportDeclaration : processNode ,
81
+ ExportNamedDeclaration : processNode ,
82
+ ExportAllDeclaration : processNode
83
+ } ;
76
84
} ;
77
85
78
86
function exists ( deps , name ) {
0 commit comments