forked from vuejs/language-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doctor.ts
290 lines (262 loc) · 10.7 KB
/
doctor.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
import { getTsdk } from '@volar/vscode';
import { GetConnectedNamedPipeServerRequest, ParseSFCRequest } from '@vue/language-server';
import * as semver from 'semver';
import * as vscode from 'vscode';
import type { BaseLanguageClient } from 'vscode-languageclient';
import { config } from '../config';
const scheme = 'vue-doctor';
const knownValidSyntaxHighlightExtensions = {
postcss: ['cpylua.language-postcss', 'vunguyentuan.vscode-postcss', 'csstools.postcss'],
stylus: ['sysoev.language-stylus'],
sass: ['Syler.sass-indented'],
};
export async function register(context: vscode.ExtensionContext, client: BaseLanguageClient) {
const item = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right);
item.backgroundColor = new vscode.ThemeColor('statusBarItem.warningBackground');
item.command = 'vue.action.doctor';
const docChangeEvent = new vscode.EventEmitter<vscode.Uri>();
updateStatusBar(vscode.window.activeTextEditor);
context.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(updateStatusBar));
context.subscriptions.push(vscode.workspace.registerTextDocumentContentProvider(
scheme,
{
onDidChange: docChangeEvent.event,
async provideTextDocumentContent(doctorUri: vscode.Uri): Promise<string | undefined> {
const fileUri = doctorUri.with({
scheme: 'file',
path: doctorUri.path.substring(0, doctorUri.path.length - '/Doctor.md'.length),
});
const problems = await getProblems(fileUri);
let content = `# ${fileUri.path.split('/').pop()} Doctor\n\n`;
for (const problem of problems) {
content += '## ❗ ' + problem.title + '\n\n';
content += problem.message + '\n\n';
}
content += '---\n\n';
content += `> Have any questions about the report message? You can see how it is composed by inspecting the [source code](https://github.com/vuejs/language-tools/blob/master/extensions/vscode/src/features/doctor.ts).\n\n`;
return content.trim();
}
},
));
context.subscriptions.push(vscode.commands.registerCommand('vue.action.doctor', () => {
const doc = vscode.window.activeTextEditor?.document;
if (
doc
&& (doc.languageId === 'vue' || doc.uri.toString().endsWith('.vue'))
&& doc.uri.scheme === 'file'
) {
vscode.commands.executeCommand('markdown.showPreviewToSide', getDoctorUri(doc.uri));
}
}));
function getDoctorUri(fileUri: vscode.Uri) {
return fileUri.with({ scheme, path: fileUri.path + '/Doctor.md' });
}
async function updateStatusBar(editor: vscode.TextEditor | undefined) {
if (
config.doctor.status
&& editor
&& (editor.document.languageId === 'vue' || editor.document.uri.toString().endsWith('.vue'))
&& editor.document.uri.scheme === 'file'
) {
const problems = await getProblems(editor.document.uri);
if (problems.length && vscode.window.activeTextEditor?.document === editor.document) {
item.show();
item.text = problems.length + (problems.length === 1 ? ' known issue' : ' known issues');
docChangeEvent.fire(getDoctorUri(editor.document.uri));
}
}
else {
item.hide();
}
}
async function getProblems(fileUri: vscode.Uri) {
const vueDoc = vscode.workspace.textDocuments.find(doc => doc.fileName === fileUri.fsPath);
const sfc = await (vueDoc ? client.sendRequest(ParseSFCRequest.type, vueDoc.getText()) : undefined);
const vueMod = getPackageJsonOfWorkspacePackage(fileUri.fsPath, 'vue');
const domMod = getPackageJsonOfWorkspacePackage(fileUri.fsPath, '@vue/runtime-dom');
const problems: {
title: string;
message: string;
}[] = [];
if (vueMod && semver.lt(vueMod.json.version, '2.7.0') && !domMod) {
// check vue version < 2.7 but @vue/runtime-dom missing
problems.push({
title: '`@vue/runtime-dom` missing for Vue 2',
message: [
'Vue 2 does not have JSX types definitions, so template type checking will not work correctly. You can resolve this problem by installing `@vue/runtime-dom` and adding it to your project\'s `devDependencies`.',
'',
'- vue: ' + vueMod.path,
].join('\n'),
});
}
else if (vueMod && semver.gte(vueMod.json.version, '2.7.0') && semver.lt(vueMod.json.version, '3.0.0') && domMod) {
// check vue version >= 2.7 and < 3 but installed @vue/runtime-dom
problems.push({
title: 'Unnecessary `@vue/runtime-dom`',
message: [
'Vue 2.7 already includes JSX type definitions. You can remove the `@vue/runtime-dom` dependency from package.json.',
'',
'- vue: ' + vueMod.path,
'- @vue/runtime-dom: ' + domMod.path,
].join('\n'),
});
}
// check @types/node > 18.8.0 && < 18.11.1
const typesNodeMod = getPackageJsonOfWorkspacePackage(fileUri.fsPath, '@types/node');
if (typesNodeMod && semver.gte(typesNodeMod.json.version, '18.8.1') && semver.lte(typesNodeMod.json.version, '18.11.0')) {
problems.push({
title: 'Incompatible `@types/node` version',
message: [
'`@types/node`\'s version `' + typesNodeMod.json.version + '` is incompatible with Vue. It will cause broken DOM event types in template.',
'',
'You can update `@types/node` to `18.11.1` or later to resolve.',
'',
'- @types/node: ' + typesNodeMod.path,
'- Issue: https://github.com/vuejs/language-tools/issues/1985',
].join('\n'),
});
}
// check should use @volar-plugins/vetur instead of vetur
const vetur = vscode.extensions.getExtension('octref.vetur');
if (vetur?.isActive) {
problems.push({
title: 'Use volar-service-vetur instead of Vetur',
message: 'Detected Vetur enabled. Consider disabling Vetur and use [volar-service-vetur](https://github.com/volarjs/services/tree/master/packages/vetur) instead.',
});
}
// check using pug but don't install @vue/language-plugin-pug
if (
sfc?.descriptor.template?.lang === 'pug'
&& !await getPackageJsonOfWorkspacePackage(fileUri.fsPath, '@vue/language-plugin-pug')
) {
problems.push({
title: '`@vue/language-plugin-pug` missing',
message: [
'For `<template lang="pug">`, the `@vue/language-plugin-pug` plugin is required. Install it using `$ npm install -D @vue/language-plugin-pug` and add it to `vueCompilerOptions.plugins` to support TypeScript intellisense in Pug templates.',
'',
'- package.json',
'```json',
JSON.stringify({ devDependencies: { "@vue/language-plugin-pug": "latest" } }, undefined, 2),
'```',
'',
'- tsconfig.json / jsconfig.json',
'```jsonc',
JSON.stringify({ vueCompilerOptions: { plugins: ["@vue/language-plugin-pug"] } }, undefined, 2),
'```',
].join('\n'),
});
}
// check syntax highlight extension installed
if (sfc) {
const blocks = [
sfc.descriptor.template,
sfc.descriptor.script,
sfc.descriptor.scriptSetup,
...sfc.descriptor.styles,
...sfc.descriptor.customBlocks,
];
for (const block of blocks) {
if (!block) continue;
if (block.lang && block.lang in knownValidSyntaxHighlightExtensions) {
const validExts = knownValidSyntaxHighlightExtensions[block.lang as keyof typeof knownValidSyntaxHighlightExtensions];
const someInstalled = validExts.some(ext => !!vscode.extensions.getExtension(ext));
if (!someInstalled) {
problems.push({
title: 'Syntax Highlighting for ' + block.lang,
message: `Did not find a valid syntax highlighter extension for ${block.lang} language block; you can choose to install one of the following:\n\n`
+ validExts.map(ext => `- [${ext}](https://marketplace.visualstudio.com/items?itemName=${ext})\n`),
});
}
}
}
}
// emmet.includeLanguages
const emmetIncludeLanguages = vscode.workspace.getConfiguration('emmet').get<{ [lang: string]: string; }>('includeLanguages');
if (emmetIncludeLanguages?.['vue']) {
problems.push({
title: 'Unnecessary `emmet.includeLanguages.vue`',
message: 'Vue language server already supports Emmet. You can remove `emmet.includeLanguages.vue` from `.vscode/settings.json`.',
});
}
// files.associations
const filesAssociations = vscode.workspace.getConfiguration('files').get<{ [pattern: string]: string; }>('associations');
if (filesAssociations?.['*.vue'] === 'html') {
problems.push({
title: 'Unnecessary `files.associations["*.vue"]`',
message: 'With `"files.associations": { "*.vue": html }`, language server cannot to recognize Vue files. You can remove `files.associations["*.vue"]` from `.vscode/settings.json`.',
});
}
/*
// check outdated language services plugins
const knownPlugins = [
// '@volar-plugins/css',
// '@volar-plugins/emmet',
'@volar-plugins/eslint',
// '@volar-plugins/html',
// '@volar-plugins/json',
'@volar-plugins/prettier',
'@volar-plugins/prettyhtml',
'@volar-plugins/pug-beautify',
'@volar-plugins/sass-formatter',
'@volar-plugins/tslint',
// '@volar-plugins/typescript',
// '@volar-plugins/typescript-twoslash-queries',
'@volar-plugins/vetur',
];
for (const plugin of knownPlugins) {
const pluginMod = await getPackageJsonOfWorkspacePackage(fileUri.fsPath, plugin);
if (!pluginMod) continue;
if (!pluginMod.json.version.startsWith('2.')) {
problems.push({
title: `Outdated ${plugin}`,
message: [
`The ${plugin} plugin is outdated. Please update it to the latest version.`,
'',
'- plugin package.json: ' + pluginMod.path,
'- plugin version: ' + pluginMod.json.version,
'- expected version: >= 2.0.0',
].join('\n'),
});
}
}
*/
// check tsdk version should not be 4.9
const tsdk = await getTsdk(context);
if (tsdk?.version?.startsWith('4.9')) {
problems.push({
title: 'Bad TypeScript version',
message: [
'TS 4.9 has a bug that will cause auto import to fail. Please downgrade to TS 4.8 or upgrade to TS 5.0+.',
'',
'Issue: https://github.com/vuejs/language-tools/issues/2190',
].join('\n'),
});
}
// #3942
const namedPipe = await client.sendRequest(GetConnectedNamedPipeServerRequest.type, fileUri.fsPath.replace(/\\/g, '/'));
if (namedPipe?.serverKind === 0) {
problems.push({
title: 'Missing jsconfig/tsconfig',
message: [
'The current file does not have a matching tsconfig/jsconfig, and extension version 2.0 will not work properly for this at the moment.',
'To avoid this problem, you can create a jsconfig in the project root, or downgrade to 1.8.27.',
'',
'Issue: https://github.com/vuejs/language-tools/issues/3942',
].join('\n'),
});
}
// check outdated vue language plugins
// check node_modules has more than one vue versions
// check ESLint, Prettier...
return problems;
}
}
function getPackageJsonOfWorkspacePackage(folder: string, pkg: string): { path: string, json: { version: string; }; } | undefined {
try {
const path = require.resolve(pkg + '/package.json', { paths: [folder] });
return {
path,
json: require(path),
};
} catch { }
}