@@ -14,6 +14,7 @@ export const registerCodeActions = () => {
14
14
vscode . languages . registerCodeActionsProvider (
15
15
getExtensionSetting ( 'codeActions.enableLanguages' ) . map ( language => ( { language, scheme : 'file' } ) ) ,
16
16
{
17
+ // eslint-disable-next-line complexity
17
18
async provideCodeActions ( document , range , { diagnostics } ) {
18
19
const problem = diagnostics [ 0 ]
19
20
const hasMissingImport = problem && problem . source === 'ts' && problem . code === 2307
@@ -31,50 +32,56 @@ export const registerCodeActions = () => {
31
32
if ( builtinModules . includes ( moduleName ) || moduleName . startsWith ( './' ) ) return
32
33
33
34
const codeActions : vscode . CodeAction [ ] = [ ]
34
- // TODO check for existence
35
- codeActions . push (
36
- ...( hasMissingImport
37
- ? [ ]
38
- : [
39
- {
40
- title : 'Open README to side' ,
41
- command : {
42
- command : getExtensionCommandId ( 'openPackageReadmePreview' ) ,
43
- // TODO investigate on titles
44
- title : '' ,
45
- arguments : [ moduleName ] ,
46
- } ,
47
- kind : vscode . CodeActionKind . Empty ,
48
- } ,
49
- {
50
- title : 'Open Repository' ,
51
- command : {
52
- command : getExtensionCommandId ( 'openPackageRepository' ) ,
53
- title : '' ,
54
- arguments : [ moduleName ] ,
55
- } ,
56
- kind : vscode . CodeActionKind . Empty ,
57
- } ,
58
- ] ) ,
59
- {
60
- title : 'Open on NPM' ,
61
- command : {
62
- command : getExtensionCommandId ( 'openOnNpm' ) ,
63
- title : '' ,
64
- arguments : [ moduleName ] ,
35
+ if ( hasMissingImport ) {
36
+ const { packageJson = { } } = await readPackageJsonWithMetadata ( { type : 'closest' } ) . catch ( ( ) => ( { } ) )
37
+ let foundType
38
+ for ( const depType of [ 'dependencies' , 'devDependencies' , 'optionalDependencies' ] )
39
+ if ( moduleName in packageJson [ depType ] ?? { } ) {
40
+ foundType = depType
41
+ break
42
+ }
43
+
44
+ // TODO-low check for existence
45
+ codeActions . push (
46
+ {
47
+ title : 'Open README to side' ,
48
+ command : {
49
+ command : getExtensionCommandId ( 'openPackageReadmePreview' ) ,
50
+ title : '' ,
51
+ arguments : [ moduleName ] ,
52
+ } ,
53
+ kind : vscode . CodeActionKind . Empty ,
54
+ } ,
55
+ {
56
+ title : 'Open Repository' ,
57
+ command : {
58
+ command : getExtensionCommandId ( 'openPackageRepository' ) ,
59
+ title : '' ,
60
+ arguments : [ moduleName ] ,
61
+ } ,
62
+ kind : vscode . CodeActionKind . Empty ,
65
63
} ,
66
- kind : vscode . CodeActionKind . Empty ,
67
- } ,
68
- {
69
- title : 'Remove with PNPM' ,
70
- command : {
71
- command : getExtensionCommandId ( 'removePackages' ) ,
72
- title : '' ,
73
- arguments : [ [ moduleName ] ] ,
64
+ {
65
+ title : 'Open on NPM' ,
66
+ command : {
67
+ command : getExtensionCommandId ( 'openOnNpm' ) ,
68
+ title : '' ,
69
+ arguments : [ moduleName ] ,
70
+ } ,
71
+ kind : vscode . CodeActionKind . Empty ,
74
72
} ,
75
- kind : vscode . CodeActionKind . Empty ,
76
- } ,
77
- )
73
+ {
74
+ // todo use multiple find up packageJson and disable this code action if not found
75
+ title : `Remove ${ foundType ? `from ${ foundType } ` : 'module' } ` ,
76
+ command : {
77
+ command : getExtensionCommandId ( 'removePackages' ) ,
78
+ title : '' ,
79
+ arguments : [ [ moduleName ] ] ,
80
+ } ,
81
+ kind : vscode . CodeActionKind . Empty ,
82
+ } ,
83
+ )
84
+ }
78
85
79
86
if ( hasMissingImport || hasMissingTypes ) {
80
87
const addModuleFix = ( module : string , type : 'dependency' | 'devDependency' , isPreferred = true ) => {
0 commit comments