-
-
Notifications
You must be signed in to change notification settings - Fork 364
/
doctor.ts
228 lines (202 loc) · 8.73 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
import * as vscode from 'vscode';
import * as semver from 'semver';
import { BaseLanguageClient } from 'vscode-languageclient';
import { GetMatchTsConfigRequest, ParseSFCRequest, GetVueCompilerOptionsRequest } from '@volar/vue-language-server';
const scheme = 'vue-doctor';
const knownValidSyntanxHighlightExtensions = {
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 = 'volar.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 question about the report message? You can see how it judge by inspecting the [source code](https://github.com/johnsoncodehk/volar/blob/master/extensions/vscode-vue-language-features/src/features/doctor.ts).\n\n`;
return content.trim();
}
},
));
context.subscriptions.push(vscode.commands.registerCommand('volar.action.doctor', () => {
const doc = vscode.window.activeTextEditor?.document;
if (doc?.languageId === '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 (
vscode.workspace.getConfiguration('volar').get<boolean>('doctor.statusBarItem')
&& editor
&& editor.document.languageId === '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 [
tsconfig,
vueOptions,
sfc,
] = await Promise.all([
client.sendRequest(GetMatchTsConfigRequest.type, { uri: fileUri.toString() }),
client.sendRequest(GetVueCompilerOptionsRequest.type, { uri: fileUri.toString() }),
vueDoc ? client.sendRequest(ParseSFCRequest.type, vueDoc.getText()) : undefined,
]);
const vueMod = getWorkspacePackageJson(fileUri.fsPath, 'vue');
const problems: {
title: string;
message: string;
}[] = [];
// check vue module exist
if (!vueMod) {
problems.push({
title: '`vue` module not found',
message: 'Vue module not found from workspace, you may have not install `node_modules` yet.',
});
}
// check vue version < 3 but missing vueCompilerOptions.target
if (vueMod) {
const vueVersionNumber = semver.gte(vueMod.json.version, '3.0.0') ? 3 : semver.gte(vueMod.json.version, '2.7.0') ? 2.7 : 2;
const targetVersionNumber = vueOptions?.target ?? 3;
const lines = [
`Target version not match, you can specify the target version in \`vueCompilerOptions.target\` in tsconfig.json / jsconfig.json. (expected \`"target": ${vueVersionNumber}\`)`,
'',
'- Vue version: ' + vueMod.json.version,
'- tsconfig target: ' + targetVersionNumber + (vueOptions?.target !== undefined ? '' : ' (default)'),
'- Vue module: ' + vueMod.path,
'- tsconfig: ' + (tsconfig?.fileName ?? 'Not found'),
'- vueCompilerOptions:',
' ```json',
JSON.stringify(vueOptions, undefined, 2).split('\n').map(line => ' ' + line).join('\n'),
' ```',
];
if (vueVersionNumber !== targetVersionNumber) {
problems.push({
title: 'Incorrect Target',
message: lines.join('\n'),
});
}
}
// check vue version < 2.7 but @vue/compiler-dom missing
if (vueMod && semver.lt(vueMod.json.version, '2.7.0') && !getWorkspacePackageJson(fileUri.fsPath, '@vue/compiler-dom')) {
problems.push({
title: '`@vue/compiler-dom` missing for Vue 2',
message: 'Vue 2 do not have JSX types definition, so template type checkinng cannot working correctly, you can install `@vue/compiler-dom` by add it to `devDependencies` to resolve this problem.',
});
}
// check vue version >= 2.7 and < 3 but installed @vue/compiler-dom
if (vueMod && semver.gte(vueMod.json.version, '2.7.0') && semver.lt(vueMod.json.version, '3.0.0') && getWorkspacePackageJson(fileUri.fsPath, '@vue/compiler-dom')) {
problems.push({
title: 'Do not need `@vue/compiler-dom`',
message: 'Vue 2.7 already included JSX types definition, you can remove `@vue/compiler-dom` depend from package.json.',
});
}
// check vue-tsc version same with extension version
const vueTscMod = getWorkspacePackageJson(fileUri.fsPath, 'vue-tsc');
if (vueTscMod && vueTscMod.json.version !== context.extension.packageJSON.version) {
problems.push({
title: '`vue-tsc` version different',
message: [
`The \`${context.extension.packageJSON.displayName}\` version is \`${context.extension.packageJSON.version}\`, but workspace \`vue-tsc\` version is \`${vueTscMod.json.version}\`, there may have different type checking behavior.`,
'',
'vue-tsc module: ' + vueTscMod.path,
].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-plugins/vetur instead of Vetur',
message: 'Detected Vetur enabled, you might consider disabling it and use [@volar-plugins/vetur](https://github.com/johnsoncodehk/volar-plugins/tree/master/packages/vetur) instead of.',
});
}
// check using pug but don't install @volar/vue-language-plugin-pug
if (
sfc?.descriptor.template?.lang === 'pug'
&& !vueOptions?.plugins?.includes('@volar/vue-language-plugin-pug')
) {
problems.push({
title: '`@volar/vue-language-plugin-pug` missing',
message: [
'For `<template lang="pug">`, you need add plugin via `$ npm install -D @volar/vue-language-plugin-pug` and add it to `vueCompilerOptions.plugins` to support TypeScript intellisense in Pug template.',
'',
'- tsconfig.json / jsconfig.json',
'```jsonc',
JSON.stringify({ vueCompilerOptions: { plugins: ["@volar/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 knownValidSyntanxHighlightExtensions) {
const validExts = knownValidSyntanxHighlightExtensions[block.lang as keyof typeof knownValidSyntanxHighlightExtensions];
const someInstalled = validExts.some(ext => !!vscode.extensions.getExtension(ext));
if (!someInstalled) {
problems.push({
title: 'Syntax Highlight for ' + block.lang,
message: `Not found valid syntax highlight extension for ${block.lang} langauge block, you can choose to install one of the following:\n\n`
+ validExts.map(ext => `- [${ext}](https://marketplace.visualstudio.com/items?itemName=${ext})\n`),
});
}
}
}
}
// check outdated language services plugins
// check outdated vue language plugins
// check node_modules has more than one vue versions
// check ESLint, Prettier...
return problems;
}
}
function getWorkspacePackageJson(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 { }
}