2
2
import * as fs from "fs" ;
3
3
import * as path from "path" ;
4
4
5
- import * as mockRequire from "mock-require" ;
6
- import * as ts_module from "typescript/lib/tsserverlibrary" ;
5
+ import merge from "merge-deep" ;
6
+ import mockRequire from "mock-require" ;
7
+ import ts_module from "typescript/lib/tsserverlibrary" ;
7
8
8
9
import { Logger } from "./logger" ;
9
10
import { getDenoDir } from "./shared" ;
10
11
11
12
let logger : Logger ;
12
13
13
- export = function init ( { typescript } : { typescript : typeof ts_module } ) {
14
+ module . exports = function init ( { typescript } : { typescript : typeof ts_module } ) {
14
15
// Make sure Deno imports the correct version of TS
15
16
mockRequire ( "typescript" , typescript ) ;
16
17
@@ -27,7 +28,7 @@ export = function init({ typescript }: { typescript: typeof ts_module }) {
27
28
resolveJsonModule : true ,
28
29
sourceMap : true ,
29
30
target : typescript . ScriptTarget . ESNext ,
30
- typeRoots : [ ]
31
+ typeRoots : [ ] ,
31
32
} ;
32
33
33
34
return {
@@ -47,36 +48,34 @@ export = function init({ typescript }: { typescript: typeof ts_module }) {
47
48
moduleNames : string [ ] ,
48
49
containingFile : string ,
49
50
reusedNames ?: string [ ] ,
50
- redirectedReference ?: ts_module . ResolvedProjectReference
51
+ redirectedReference ?: ts_module . ResolvedProjectReference ,
51
52
) => {
52
- moduleNames = moduleNames
53
- . map ( stripExtNameDotTs )
54
- . map ( convertRemoteToLocalCache ) ;
53
+ moduleNames = moduleNames . map ( stripExtNameDotTs ) . map ( convertRemoteToLocalCache ) ;
55
54
56
55
return resolveModuleNames . call (
57
56
info . languageServiceHost ,
58
57
moduleNames ,
59
58
containingFile ,
60
59
reusedNames ,
61
- redirectedReference
60
+ redirectedReference ,
62
61
) ;
63
62
} ;
64
63
65
- const projectSetting = info . project . getCompilationSettings ( ) ;
64
+ const getCompilationSettings = info . languageServiceHost . getCompilationSettings ;
65
+
66
66
info . languageServiceHost . getCompilationSettings = ( ) => {
67
- return { ...OPTIONS , ...projectSetting } ;
67
+ const projectConfig = getCompilationSettings . call ( info . languageServiceHost ) ;
68
+ const compilationSettings = merge ( OPTIONS , projectConfig ) ;
69
+ logger . info ( `compilationSettings:${ JSON . stringify ( compilationSettings ) } ` ) ;
70
+ return compilationSettings ;
68
71
} ;
69
72
70
73
const getScriptFileNames = info . languageServiceHost . getScriptFileNames ! ;
71
74
info . languageServiceHost . getScriptFileNames = ( ) => {
72
- const scriptFileNames = getScriptFileNames . call (
73
- info . languageServiceHost
74
- ) ;
75
+ const scriptFileNames = getScriptFileNames . call ( info . languageServiceHost ) ;
75
76
76
77
const denoDtsPath =
77
- getDtsPathForVscode ( info ) ||
78
- getGlobalDtsPath ( ) ||
79
- getLocalDtsPath ( info ) ;
78
+ getDtsPathForVscode ( info ) || getGlobalDtsPath ( ) || getLocalDtsPath ( info ) ;
80
79
81
80
if ( denoDtsPath ) {
82
81
scriptFileNames . push ( denoDtsPath ) ;
@@ -94,16 +93,16 @@ export = function init({ typescript }: { typescript: typeof ts_module }) {
94
93
name : string ,
95
94
formatOptions ?: ts_module . FormatCodeOptions | ts_module . FormatCodeSettings ,
96
95
source ?: string ,
97
- preferences ?: ts_module . UserPreferences ) => {
98
-
96
+ preferences ?: ts_module . UserPreferences ,
97
+ ) => {
99
98
const details = getCompletionEntryDetails . call (
100
99
info . languageService ,
101
100
fileName ,
102
101
position ,
103
102
name ,
104
103
formatOptions ,
105
104
source ,
106
- preferences
105
+ preferences ,
107
106
) ;
108
107
109
108
if ( details ) {
@@ -112,7 +111,10 @@ export = function init({ typescript }: { typescript: typeof ts_module }) {
112
111
for ( const change of ca . changes ) {
113
112
if ( ! change . isNewFile ) {
114
113
for ( const tc of change . textChanges ) {
115
- tc . newText = tc . newText . replace ( / ^ ( i m p o r t .* f r o m [ ' " ] ) ( \. .* ) ( [ ' " ] ; \n ) / i, "$1$2.ts$3" ) ;
114
+ tc . newText = tc . newText . replace (
115
+ / ^ ( i m p o r t .* f r o m [ ' " ] ) ( \. .* ) ( [ ' " ] ; \n ) / i,
116
+ "$1$2.ts$3" ,
117
+ ) ;
116
118
}
117
119
}
118
120
}
@@ -128,7 +130,7 @@ export = function init({ typescript }: { typescript: typeof ts_module }) {
128
130
129
131
onConfigurationChanged ( config : any ) {
130
132
logger . info ( `onConfigurationChanged: ${ JSON . stringify ( config ) } ` ) ;
131
- }
133
+ } ,
132
134
} ;
133
135
} ;
134
136
@@ -166,17 +168,15 @@ interface IDenoModuleHeaders {
166
168
* If moduleName is not found, recursively search for headers and "redirect_to" property.
167
169
*/
168
170
function fallbackHeader ( modulePath : string ) : string {
169
- const validPath = modulePath . endsWith ( ".ts" )
170
- ? modulePath
171
- : `${ modulePath } .ts` ;
171
+ const validPath = modulePath . endsWith ( ".ts" ) ? modulePath : `${ modulePath } .ts` ;
172
172
if ( fs . existsSync ( validPath ) ) {
173
173
return modulePath ;
174
174
}
175
175
176
176
const headersPath = `${ validPath } .headers.json` ;
177
177
if ( fs . existsSync ( headersPath ) ) {
178
178
const headers : IDenoModuleHeaders = JSON . parse (
179
- fs . readFileSync ( headersPath , { encoding : "utf-8" } )
179
+ fs . readFileSync ( headersPath , { encoding : "utf-8" } ) ,
180
180
) ;
181
181
logger . info ( `redirect "${ modulePath } " to "${ headers . redirect_to } ".` ) ;
182
182
// TODO: avoid Circular
@@ -185,9 +185,7 @@ function fallbackHeader(modulePath: string): string {
185
185
return modulePath ;
186
186
}
187
187
188
- function getDtsPathForVscode (
189
- info : ts_module . server . PluginCreateInfo
190
- ) : string | undefined {
188
+ function getDtsPathForVscode ( info : ts_module . server . PluginCreateInfo ) : string | undefined {
191
189
const bundledDtsPath = info . config . dtsPath ;
192
190
193
191
if ( bundledDtsPath && fs . existsSync ( bundledDtsPath ) ) {
@@ -214,7 +212,7 @@ function getLocalDtsPath(info: ts.server.PluginCreateInfo): string | undefined {
214
212
"node_modules" ,
215
213
"typescript-deno-plugin" ,
216
214
"lib" ,
217
- "lib.deno_runtime.d.ts"
215
+ "lib.deno_runtime.d.ts" ,
218
216
) ;
219
217
220
218
if ( fs . existsSync ( localDtsPath ) ) {
0 commit comments