-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathplugin.ts
561 lines (483 loc) · 16.8 KB
/
plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
import * as path from "path";
import merge from "deepmerge";
import typescript, {
CodeFixAction,
FormatCodeSettings,
UserPreferences,
} from "typescript/lib/tsserverlibrary";
import { Logger } from "./logger";
import { Configuration, ConfigurationField } from "../../core/configuration";
import { getDenoDts } from "../../core/deno";
import { ModuleResolver } from "../../core/module_resolver";
import { CacheModule } from "../../core/deno_cache";
import { normalizeFilepath, isUntitledDocument } from "../../core/util";
import { normalizeImportStatement } from "../../core/deno_normalize_import_statement";
import { getImportModules } from "../../core/deno_deps";
const ignoredCompilerOptions: readonly string[] = [
"allowSyntheticDefaultImports",
"baseUrl",
"build",
"composite",
"declaration",
"declarationDir",
"declarationMap",
"diagnostics",
"downlevelIteration",
"emitBOM",
"emitDeclarationOnly",
"esModuleInterop",
"extendedDiagnostics",
"forceConsistentCasingInFileNames",
"help",
"importHelpers",
"incremental",
"inlineSourceMap",
"inlineSources",
"init",
"isolatedModules",
"listEmittedFiles",
"listFiles",
"mapRoot",
"maxNodeModuleJsDepth",
"module",
"moduleResolution",
"newLine",
"noEmit",
"noEmitHelpers",
"noEmitOnError",
"noLib",
"noResolve",
"out",
"outDir",
"outFile",
"paths",
"preserveSymlinks",
"preserveWatchOutput",
"pretty",
"rootDir",
"rootDirs",
"showConfig",
"skipDefaultLibCheck",
"skipLibCheck",
"sourceMap",
"sourceRoot",
"stripInternal",
"target",
"traceResolution",
"tsBuildInfoFile",
"types",
"typeRoots",
"version",
"watch",
];
export class DenoPlugin implements typescript.server.PluginModule {
// plugin name
static readonly PLUGIN_NAME = "typescript-deno-plugin";
private readonly configurationManager = new Configuration();
// see https://github.com/denoland/deno/blob/2debbdacb935cfe1eb7bb8d1f40a5063b339d90b/js/compiler.ts#L159-L170
private readonly DEFAULT_OPTIONS: typescript.CompilerOptions = {
allowJs: true,
checkJs: false,
strict: true,
esModuleInterop: true,
jsx: this.ts.JsxEmit.React,
module: this.ts.ModuleKind.ESNext,
moduleResolution: this.ts.ModuleResolutionKind.NodeJs,
outDir: "$deno$",
resolveJsonModule: true,
sourceMap: true,
stripComments: true,
target: this.ts.ScriptTarget.ESNext,
noEmit: true,
noEmitHelpers: true,
};
// No matter how tsconfig.json is set in the working directory
// It will always overwrite the configuration
private readonly MUST_OVERWRITE_OPTIONS: typescript.CompilerOptions = {
jsx: this.DEFAULT_OPTIONS.jsx,
module: this.DEFAULT_OPTIONS.module,
moduleResolution: this.DEFAULT_OPTIONS.moduleResolution,
resolveJsonModule: this.DEFAULT_OPTIONS.resolveJsonModule,
strict: this.DEFAULT_OPTIONS.strict,
noEmit: this.DEFAULT_OPTIONS.noEmit,
noEmitHelpers: this.DEFAULT_OPTIONS.noEmitHelpers,
target: this.ts.ScriptTarget.ESNext,
};
private logger!: Logger;
constructor(private readonly ts: typeof typescript) {}
create(info: typescript.server.PluginCreateInfo): typescript.LanguageService {
const { project, languageService, languageServiceHost } = info;
const projectDirectory = project.getCurrentDirectory();
function getRealPath(filepath: string): string {
return project.realpath ? project.realpath(filepath) : filepath;
}
// TypeScript plugins have a `cwd` of `/`, which causes issues with import resolution.
process.chdir(projectDirectory);
this.logger = Logger.forPlugin(DenoPlugin.PLUGIN_NAME, info);
this.logger.info(`Create typescript-deno-plugin`);
const getCompilationSettings = languageServiceHost.getCompilationSettings.bind(
languageServiceHost
);
const getScriptFileNames = languageServiceHost.getScriptFileNames.bind(
languageServiceHost
);
// ref https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#customizing-module-resolution
const resolveModuleNames = languageServiceHost.resolveModuleNames?.bind(
languageServiceHost
);
const getSemanticDiagnostics = languageService.getSemanticDiagnostics.bind(
languageService
);
const resolveTypeReferenceDirectives = languageServiceHost.resolveTypeReferenceDirectives?.bind(
languageServiceHost
);
const getCompletionEntryDetails = languageService.getCompletionEntryDetails.bind(
languageService
);
const originGetCodeFixesAtPosition = languageService.getCodeFixesAtPosition.bind(
languageService
);
languageServiceHost.getCompilationSettings = () => {
const projectConfig = getCompilationSettings();
if (!this.configurationManager.config.enable) {
return projectConfig;
}
// delete the option which ignore by Deno
// see https://github.com/denoland/deno/blob/bced52505f/cli/js/compiler/host.ts#L65-L121
for (const option in projectConfig) {
if (ignoredCompilerOptions.includes(option)) {
delete projectConfig[option];
}
}
const compilationSettings = merge(
merge(this.DEFAULT_OPTIONS, projectConfig),
this.MUST_OVERWRITE_OPTIONS
);
return compilationSettings;
};
languageServiceHost.getScriptFileNames = () => {
const scriptFileNames = getScriptFileNames();
if (!this.configurationManager.config.enable) {
return scriptFileNames;
}
// Get typescript declaration File
const dtsFiles = [
getDenoDts(!!this.configurationManager.config.unstable),
];
const iterator = new Set(dtsFiles).entries();
for (const [, filepath] of iterator) {
scriptFileNames.push(filepath);
}
return scriptFileNames;
};
if (resolveTypeReferenceDirectives) {
languageServiceHost.resolveTypeReferenceDirectives = (
typeDirectiveNames: string[],
containingFile: string,
...rest
) => {
if (!this.configurationManager.config.enable) {
return resolveTypeReferenceDirectives(
typeDirectiveNames,
containingFile,
...rest
);
}
// containingFile may be `untitled: ^ Untitled-1`
const realContainingFile = isUntitledDocument(containingFile)
? path.join(project.getCurrentDirectory(), "untitled")
: // in Windows.
// containingFile may be a unix-like style
// eg. c:/Users/admin/path/to/file.ts
// This is not a legal file path in Windows
// It will cause a series of bugs, so here we get the real file path
getRealPath(containingFile);
if (!this.ts.sys.fileExists(realContainingFile)) {
return [];
}
const importMapsFilepath = this.configurationManager.config.import_map
? path.isAbsolute(this.configurationManager.config.import_map)
? this.configurationManager.config.import_map
: path.resolve(
project.getCurrentDirectory(),
this.configurationManager.config.import_map
)
: undefined;
const resolver = ModuleResolver.create(
realContainingFile,
importMapsFilepath
);
const result: (
| typescript.ResolvedTypeReferenceDirective
| undefined
)[] = [];
for (const typeDirectiveName of typeDirectiveNames) {
const [resolvedModule] = resolver.resolveModules([typeDirectiveName]);
// if module found. then return the module file
if (resolvedModule) {
const target: typescript.ResolvedTypeReferenceDirective = {
primary: false,
resolvedFileName: resolvedModule.filepath,
};
result.push(target);
continue;
}
// If the module does not exist, then apply the native reference method
const [target] = resolveTypeReferenceDirectives(
[typeDirectiveName],
containingFile,
...rest
);
result.push(target);
}
return result;
};
}
languageService.getSemanticDiagnostics = (filename: string) => {
const diagnostics = getSemanticDiagnostics(filename);
if (!this.configurationManager.config.enable) {
return diagnostics;
}
// ref: https://github.com/denoland/deno/blob/da8cb408c878aa6e90542e26173f1f14b5254d29/cli/js/compiler/util.ts#L262
const ignoredDiagnostics = [
// TS2306: File 'file:///Users/rld/src/deno/cli/tests/subdir/amd_like.js' is
// not a module.
2306,
// TS1375: 'await' expressions are only allowed at the top level of a file
// when that file is a module, but this file has no imports or exports.
// Consider adding an empty 'export {}' to make this file a module.
1375,
// TS1103: 'for-await-of' statement is only allowed within an async function
// or async generator.
1103,
// TS2691: An import path cannot end with a '.ts' extension. Consider
// importing 'bad-module' instead.
2691,
// TS5009: Cannot find the common subdirectory path for the input files.
5009,
// TS5055: Cannot write file
// 'http://localhost:4545/cli/tests/subdir/mt_application_x_javascript.j4.js'
// because it would overwrite input file.
5055,
// TypeScript is overly opinionated that only CommonJS modules kinds can
// support JSON imports. Allegedly this was fixed in
// Microsoft/TypeScript#26825 but that doesn't seem to be working here,
// so we will ignore complaints about this compiler setting.
5070,
// TS7016: Could not find a declaration file for module '...'. '...'
// implicitly has an 'any' type. This is due to `allowJs` being off by
// default but importing of a JavaScript module.
7016,
];
return diagnostics.filter((v) => !ignoredDiagnostics.includes(v.code));
};
languageService.getCodeFixesAtPosition = (
fileName: string,
start: number,
end: number,
errorCodes: readonly number[],
formatOptions: FormatCodeSettings,
preferences: UserPreferences
): readonly CodeFixAction[] => {
const fixActions = originGetCodeFixesAtPosition(
fileName,
start,
end,
errorCodes,
formatOptions,
preferences
);
if (!this.configurationManager.config.enable) {
return fixActions;
}
for (const action of fixActions) {
if (action.fixName === "import") {
for (const change of action.changes) {
for (const tc of change.textChanges) {
tc.newText = normalizeImportStatement(
fileName,
tc.newText,
this.logger
);
}
}
}
}
return fixActions;
};
languageService.getCompletionEntryDetails = (
fileName: string,
position: number,
name: string,
formatOptions?:
| typescript.FormatCodeOptions
| typescript.FormatCodeSettings,
source?: string,
preferences?: typescript.UserPreferences
) => {
const details = getCompletionEntryDetails(
fileName,
position,
name,
formatOptions,
source,
preferences
);
if (!this.configurationManager.config.enable) {
return details;
}
if (details) {
// modifiers maybe contain multiple values. eg `export,declare`
const modifiers = details.kindModifiers.split(",") || [];
if (
modifiers.includes("export") &&
details.codeActions &&
details.codeActions.length
) {
for (const ca of details.codeActions) {
for (const change of ca.changes) {
if (!change.isNewFile) {
for (const tc of change.textChanges) {
tc.newText = normalizeImportStatement(
fileName,
tc.newText,
this.logger
);
}
}
}
}
}
if (details.source && details.source.length) {
for (const source of details.source) {
if (source.kind === "text") {
// text is always unix style
const text = source.text;
const absoluteFilepath = path.resolve(
normalizeFilepath(path.dirname(fileName)),
normalizeFilepath(text)
);
if (path.isAbsolute(absoluteFilepath)) {
const denoCache = CacheModule.create(
absoluteFilepath,
this.logger
);
if (denoCache) {
source.text = denoCache.meta.url.href;
}
}
}
}
}
}
return details;
};
if (resolveModuleNames) {
languageServiceHost.resolveModuleNames = (
moduleNames: string[],
containingFile: string,
...rest
): (
| typescript.ResolvedModule
| typescript.ResolvedModuleFull
| undefined
)[] => {
if (!this.configurationManager.config.enable) {
return resolveModuleNames(moduleNames, containingFile, ...rest);
}
// containingFile may be `untitled: ^ Untitled-1`
const realContainingFile = isUntitledDocument(containingFile)
? path.join(project.getCurrentDirectory(), "untitled")
: // in Windows.
// containingFile may be a unix-like style
// eg. c:/Users/admin/path/to/file.ts
// This is not a legal file path in Windows
// It will cause a series of bugs, so here we get the real file path
getRealPath(containingFile);
const importMapsFilepath = this.configurationManager.config.import_map
? path.isAbsolute(this.configurationManager.config.import_map)
? this.configurationManager.config.import_map
: path.resolve(
project.getCurrentDirectory(),
this.configurationManager.config.import_map
)
: undefined;
const resolver = ModuleResolver.create(
realContainingFile,
importMapsFilepath
);
const content = this.ts.sys.readFile(containingFile, "utf8");
// handle @deno-types
if (content && content.indexOf("// @deno-types=") >= 0) {
const sourceFile = this.ts.createSourceFile(
containingFile,
content,
this.ts.ScriptTarget.ESNext,
true
);
const modules = getImportModules(this.ts)(sourceFile);
for (const m of modules) {
if (m.hint) {
const index = moduleNames.findIndex((v) => v === m.moduleName);
moduleNames[index] = m.hint.text;
}
}
}
const resolvedModules = resolver.resolveModules(moduleNames);
// try resolve typeReferenceDirectives
for (const resolvedModule of resolvedModules) {
if (!resolvedModule) {
continue;
}
const content = this.ts.sys.readFile(resolvedModule.filepath);
if (!content) {
continue;
}
const { typeReferenceDirectives } = this.ts.preProcessFile(
content,
true,
true
);
if (!typeReferenceDirectives.length) {
continue;
}
const _resolver = ModuleResolver.create(
resolvedModule.filepath,
importMapsFilepath
);
const modules = _resolver.resolveModules(
typeReferenceDirectives.map((v) => v.fileName)
);
for (const m of modules) {
if (m) {
resolvedModule.origin = m.origin;
resolvedModule.filepath = m.filepath;
}
}
}
return resolvedModules.map((v) => {
if (!v) {
return v;
}
const result: typescript.ResolvedModuleFull = {
extension: v.extension as typescript.Extension,
isExternalLibraryImport: false,
resolvedFileName: v.filepath,
};
return result;
});
};
}
this.configurationManager.resolveFromVscode(projectDirectory);
this.configurationManager.onUpdatedConfig(() => {
project.refreshDiagnostics();
project.updateGraph();
languageService.getProgram()?.emit();
});
return languageService;
}
onConfigurationChanged(c: ConfigurationField): void {
this.logger.info(`onConfigurationChanged: ${JSON.stringify(c)}`);
this.configurationManager.update(c);
}
}