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