Skip to content

Commit 1b25cb6

Browse files
authored
fix(language-service): do not provide required props inlay hints for intrinsic elements (#5258)
1 parent f256f36 commit 1b25cb6

File tree

8 files changed

+54
-13
lines changed

8 files changed

+54
-13
lines changed

packages/language-server/node.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ connection.onInitialize(params => {
6262
getElementAttrs(...args) {
6363
return connection.sendRequest(options.typescript.requestForwardingCommand!, ['vue:getElementAttrs', args]);
6464
},
65+
getElementNames(...args) {
66+
return connection.sendRequest(options.typescript.requestForwardingCommand!, ['vue:getElementNames', args]);
67+
},
6568
getImportPathForFile(...args) {
6669
return connection.sendRequest(options.typescript.requestForwardingCommand!, ['vue:getImportPathForFile', args]);
6770
},

packages/language-service/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { getComponentEvents } from '@vue/typescript-plugin/lib/requests/getCompo
3838
import { getComponentNames } from '@vue/typescript-plugin/lib/requests/getComponentNames';
3939
import { getComponentProps } from '@vue/typescript-plugin/lib/requests/getComponentProps';
4040
import { getElementAttrs } from '@vue/typescript-plugin/lib/requests/getElementAttrs';
41+
import { getElementNames } from '@vue/typescript-plugin/lib/requests/getElementNames';
4142
import { getImportPathForFile } from '@vue/typescript-plugin/lib/requests/getImportPathForFile';
4243
import { getPropertiesAtLocation } from '@vue/typescript-plugin/lib/requests/getPropertiesAtLocation';
4344
import type { RequestContext } from '@vue/typescript-plugin/lib/requests/types';
@@ -131,6 +132,9 @@ export function getFullLanguageServicePlugins(ts: typeof import('typescript')) {
131132
async getElementAttrs(...args) {
132133
return await getElementAttrs.apply(requestContext, args);
133134
},
135+
async getElementNames(...args) {
136+
return await getElementNames.apply(requestContext, args);
137+
},
134138
async getQuickInfoAtPosition(fileName, position) {
135139
const languageService = context.getLanguageService();
136140
const uri = context.project.typescript!.uriConverter.asUri(fileName);

packages/language-service/lib/plugins/vue-missing-props-hints.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export function create(
1717
},
1818
create(context) {
1919
const tsPluginClient = getTsPluginClient?.(context);
20+
let intrinsicElementNames: Set<string>;
2021

2122
return {
2223

@@ -59,6 +60,10 @@ export function create(
5960
const components = await tsPluginClient?.getComponentNames(root.fileName) ?? [];
6061
const componentProps: Record<string, string[]> = {};
6162

63+
intrinsicElementNames ??= new Set(
64+
await tsPluginClient?.getElementNames(root.fileName) ?? []
65+
);
66+
6267
let token: html.TokenType;
6368
let current: {
6469
unburnedRequiredProps: string[];
@@ -69,6 +74,10 @@ export function create(
6974
while ((token = scanner.scan()) !== html.TokenType.EOS) {
7075
if (token === html.TokenType.StartTag) {
7176
const tagName = scanner.getTokenText();
77+
if (intrinsicElementNames.has(tagName)) {
78+
continue;
79+
}
80+
7281
const checkTag = tagName.includes('.')
7382
? tagName
7483
: components.find(component => component === tagName || hyphenateTag(component) === tagName);

packages/typescript-plugin/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { getComponentEvents } from './lib/requests/getComponentEvents';
88
import { getComponentNames } from './lib/requests/getComponentNames';
99
import { getComponentProps } from './lib/requests/getComponentProps';
1010
import { getElementAttrs } from './lib/requests/getElementAttrs';
11+
import { getElementNames } from './lib/requests/getElementNames';
1112
import { getImportPathForFile } from './lib/requests/getImportPathForFile';
1213
import { getPropertiesAtLocation } from './lib/requests/getPropertiesAtLocation';
1314
import { getQuickInfoAtPosition } from './lib/requests/getQuickInfoAtPosition';
@@ -120,6 +121,11 @@ const plugin = createLanguageServicePlugin(
120121
response: getElementAttrs.apply(getRequestContext(args[0]), args),
121122
};
122123
});
124+
session.addProtocolHandler('vue:getElementNames', ({ arguments: args }) => {
125+
return {
126+
response: getElementNames.apply(getRequestContext(args[0]), args),
127+
};
128+
});
123129

124130
projectService.logger.info('Vue specific commands are successfully added.');
125131
}

packages/typescript-plugin/lib/common.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { forEachElementNode, hyphenateTag, Language, VueCompilerOptions, VueVirtualCode } from '@vue/language-core';
22
import { capitalize } from '@vue/shared';
33
import type * as ts from 'typescript';
4-
import { _getComponentNames, _getElementNames } from './requests/getComponentNames';
4+
import { _getComponentNames } from './requests/getComponentNames';
5+
import { _getElementNames } from './requests/getElementNames';
56
import type { RequestContext } from './requests/types';
67

78
const windowsPathReg = /\\/g;

packages/typescript-plugin/lib/requests/getComponentNames.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,3 @@ export function _getComponentNames(
3131
names.push(getSelfComponentName(vueCode.fileName));
3232
return names;
3333
}
34-
35-
export function _getElementNames(
36-
ts: typeof import('typescript'),
37-
tsLs: ts.LanguageService,
38-
vueCode: VueVirtualCode
39-
) {
40-
return getVariableType(ts, tsLs, vueCode, '__VLS_elements')
41-
?.type
42-
?.getProperties()
43-
.map(c => c.name)
44-
?? [];
45-
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { VueVirtualCode } from '@vue/language-core';
2+
import type * as ts from 'typescript';
3+
import type { RequestContext } from './types';
4+
import { getVariableType } from './utils';
5+
6+
export function getElementNames(
7+
this: RequestContext,
8+
fileName: string
9+
) {
10+
const { typescript: ts, language, languageService, getFileId } = this;
11+
const volarFile = language.scripts.get(getFileId(fileName));
12+
if (!(volarFile?.generated?.root instanceof VueVirtualCode)) {
13+
return;
14+
}
15+
const vueCode = volarFile.generated.root;
16+
return _getElementNames(ts, languageService, vueCode);
17+
}
18+
19+
export function _getElementNames(
20+
ts: typeof import('typescript'),
21+
tsLs: ts.LanguageService,
22+
vueCode: VueVirtualCode
23+
) {
24+
return getVariableType(ts, tsLs, vueCode, '__VLS_elements')
25+
?.type
26+
?.getProperties()
27+
.map(c => c.name)
28+
?? [];
29+
}

packages/typescript-plugin/lib/requests/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ export type Requests = {
1010
getComponentEvents: ToRequest<typeof import('./getComponentEvents.js')['getComponentEvents']>;
1111
getComponentDirectives: ToRequest<typeof import('./getComponentDirectives.js')['getComponentDirectives']>;
1212
getElementAttrs: ToRequest<typeof import('./getElementAttrs.js')['getElementAttrs']>;
13+
getElementNames: ToRequest<typeof import('./getElementNames.js')['getElementNames']>;
1314
};

0 commit comments

Comments
 (0)