@@ -19,13 +19,19 @@ import { readFileSync } from 'fs';
19
19
import { URL } from 'url' ;
20
20
21
21
let logger : Logger ;
22
+ let pluginInfo : ts_module . server . PluginCreateInfo ;
22
23
23
24
type DenoPluginConfig = {
24
25
enable : boolean ;
25
26
importmap ?: string ;
27
+ tsconfig ?: string ;
26
28
dtsPath ?: string ;
27
29
} ;
28
30
31
+ const config : DenoPluginConfig = {
32
+ enable : true ,
33
+ } ;
34
+
29
35
module . exports = function init (
30
36
{ typescript } : { typescript : typeof ts_module } ,
31
37
) {
@@ -68,32 +74,24 @@ module.exports = function init(
68
74
logger = Logger . forPlugin ( info ) ;
69
75
logger . info ( "plugin created." ) ;
70
76
77
+ pluginInfo = info ;
71
78
const tsLs = info . languageService ;
72
79
const tsLsHost = info . languageServiceHost ;
73
80
const project = info . project ;
74
- const config : DenoPluginConfig = {
75
- enable : true ,
76
- ...info . config ,
77
- } ;
81
+
82
+ Object . assign ( config , info . config ) ;
78
83
79
84
if ( ! config . enable ) {
80
85
logger . info ( "plugin disabled." ) ;
81
86
return tsLs ;
82
87
}
83
88
84
- logger . info ( 'config:\n' + JSON . stringify ( config , null , ' ' ) ) ;
85
-
86
89
const projectDirectory = project . getCurrentDirectory ( ) ;
87
90
// TypeScript plugins have a `cwd` of `/`, which causes issues with import resolution.
88
91
process . chdir ( projectDirectory ) ;
89
92
90
93
let parsedImportMap : ImportMaps | null = null ;
91
94
92
- if ( config . importmap != null ) {
93
- logger . info ( 'use import maps: ' + config . importmap ) ;
94
- parsedImportMap = parseImportMapFromFile ( projectDirectory , config . importmap ) ;
95
- }
96
-
97
95
const resolveTypeReferenceDirectives =
98
96
tsLsHost . resolveTypeReferenceDirectives ;
99
97
@@ -104,6 +102,7 @@ module.exports = function init(
104
102
redirectedReference : ts_module . ResolvedProjectReference | undefined ,
105
103
options : ts_module . CompilerOptions ,
106
104
) : ( ts_module . ResolvedTypeReferenceDirective | undefined ) [ ] => {
105
+
107
106
const ret = resolveTypeReferenceDirectives . call (
108
107
tsLsHost ,
109
108
typeDirectiveNames ,
@@ -112,6 +111,11 @@ module.exports = function init(
112
111
options ,
113
112
) ;
114
113
114
+ if ( ! config . enable ) {
115
+ logger . info ( "plugin disabled." ) ;
116
+ return ret ;
117
+ }
118
+
115
119
return ret ;
116
120
} ;
117
121
}
@@ -123,9 +127,19 @@ module.exports = function init(
123
127
tsLsHost . resolveModuleNames = (
124
128
moduleNames : string [ ] ,
125
129
containingFile : string ,
130
+ ...rest
126
131
) => {
132
+ if ( ! config . enable ) {
133
+ logger . info ( "plugin disabled." ) ;
134
+ return resolveModuleNames . call ( tsLsHost , moduleNames , containingFile , ...rest ) ;
135
+ }
136
+
127
137
const resolvedModules : ( ResolvedModuleFull | undefined ) [ ] = [ ] ;
128
138
139
+ if ( config . importmap != null ) {
140
+ parsedImportMap = parseImportMapFromFile ( projectDirectory , config . importmap ) ;
141
+ }
142
+
129
143
// try resolve typeReferenceDirectives
130
144
for ( let moduleName of moduleNames ) {
131
145
if ( parsedImportMap !== null ) {
@@ -190,6 +204,10 @@ module.exports = function init(
190
204
info . languageServiceHost . getCompilationSettings ;
191
205
192
206
info . languageServiceHost . getCompilationSettings = ( ) => {
207
+ if ( ! config . enable ) {
208
+ return getCompilationSettings . call ( tsLsHost ) ;
209
+ }
210
+
193
211
const projectConfig = getCompilationSettings . call (
194
212
info . languageServiceHost ,
195
213
) ;
@@ -203,6 +221,10 @@ module.exports = function init(
203
221
204
222
const getScriptFileNames = info . languageServiceHost . getScriptFileNames ! ;
205
223
info . languageServiceHost . getScriptFileNames = ( ) => {
224
+ if ( ! config . enable ) {
225
+ return getScriptFileNames . call ( tsLsHost ) ;
226
+ }
227
+
206
228
const scriptFileNames = getScriptFileNames . call (
207
229
info . languageServiceHost ,
208
230
) ;
@@ -240,6 +262,10 @@ module.exports = function init(
240
262
preferences ,
241
263
) ;
242
264
265
+ if ( ! config . enable ) {
266
+ return details ;
267
+ }
268
+
243
269
if ( details ) {
244
270
if ( details . codeActions && details . codeActions . length ) {
245
271
for ( const ca of details . codeActions ) {
@@ -263,6 +289,10 @@ module.exports = function init(
263
289
function getSemanticDiagnostics ( filename : string ) {
264
290
const diagnostics = tsLs . getSemanticDiagnostics ( filename ) ;
265
291
292
+ if ( ! config . enable ) {
293
+ return diagnostics ;
294
+ }
295
+
266
296
// ref: https://github.com/denoland/deno/blob/da8cb408c878aa6e90542e26173f1f14b5254d29/cli/js/compiler/util.ts#L262
267
297
const ignoredDiagnostics = [
268
298
// TS2306: File 'file:///Users/rld/src/deno/cli/tests/subdir/amd_like.js' is
@@ -311,6 +341,13 @@ module.exports = function init(
311
341
312
342
return proxy ;
313
343
} ,
344
+
345
+ onConfigurationChanged ( c : DenoPluginConfig ) {
346
+ Object . assign ( config , c ) ;
347
+ pluginInfo . project . markAsDirty ( ) ;
348
+ pluginInfo . project . refreshDiagnostics ( ) ;
349
+ pluginInfo . project . updateGraph ( ) ;
350
+ }
314
351
} ;
315
352
} ;
316
353
0 commit comments