Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(language-core): correct type and completion support of vue: event #4969

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 35 additions & 18 deletions packages/language-core/lib/codegen/template/elementEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ export function* generateElementEvents(
propsVar = ctx.getInternalVariable();
yield `let ${propsVar}!: __VLS_FunctionalComponentProps<typeof ${componentVar}, typeof ${componentInstanceVar}>${endOfLine}`;
}
const originalPropName = camelize('on-' + prop.arg.loc.source);
const isVNodeEvent = prop.arg.loc.source.startsWith('vue:');
const name = isVNodeEvent
? 'vnode-' + prop.arg.loc.source.slice('vue:'.length)
: prop.arg.loc.source;
const originalPropName = camelize('on-' + name);
const originalPropNameObjectKey = variableNameRegex.test(originalPropName)
? originalPropName
: `'${originalPropName}'`;
Expand All @@ -44,21 +48,21 @@ export function* generateElementEvents(
yield `(${newLine}`;
yield `__VLS_IsFunction<typeof ${propsVar}, '${originalPropName}'> extends true${newLine}`;
yield `? typeof ${propsVar}${newLine}`;
yield `: __VLS_IsFunction<typeof ${eventsVar}, '${prop.arg.loc.source}'> extends true${newLine}`;
yield `: __VLS_IsFunction<typeof ${eventsVar}, '${name}'> extends true${newLine}`;
yield `? {${newLine}`;
yield `/**__VLS_emit,${emitVar},${prop.arg.loc.source}*/${newLine}`;
yield `${originalPropNameObjectKey}?: typeof ${eventsVar}['${prop.arg.loc.source}']${newLine}`;
yield `/**__VLS_emit,${emitVar},${name}*/${newLine}`;
yield `${originalPropNameObjectKey}?: typeof ${eventsVar}['${name}']${newLine}`;
yield `}${newLine}`;
if (prop.arg.loc.source !== camelize(prop.arg.loc.source)) {
yield `: __VLS_IsFunction<typeof ${eventsVar}, '${camelize(prop.arg.loc.source)}'> extends true${newLine}`;
if (name !== camelize(name)) {
yield `: __VLS_IsFunction<typeof ${eventsVar}, '${camelize(name)}'> extends true${newLine}`;
yield `? {${newLine}`;
yield `/**__VLS_emit,${emitVar},${camelize(prop.arg.loc.source)}*/${newLine}`;
yield `${originalPropNameObjectKey}?: typeof ${eventsVar}['${camelize(prop.arg.loc.source)}']${newLine}`;
yield `/**__VLS_emit,${emitVar},${camelize(name)}*/${newLine}`;
yield `${originalPropNameObjectKey}?: typeof ${eventsVar}['${camelize(name)}']${newLine}`;
yield `}${newLine}`;
}
yield `: typeof ${propsVar}${newLine}`;
yield `) = {${newLine}`;
yield* generateEventArg(ctx, prop.arg, true);
yield* generateEventArg(ctx, prop.arg, name, isVNodeEvent);
yield `: `;
yield* generateEventExpression(options, ctx, prop);
yield `}${endOfLine}`;
Expand Down Expand Up @@ -87,19 +91,32 @@ const eventArgFeatures: VueCodeInformation = {
export function* generateEventArg(
ctx: TemplateCodegenContext,
arg: CompilerDOM.SimpleExpressionNode,
enableHover: boolean
name: string = arg.loc.source,
isVNodeEvent: boolean = false
): Generator<Code> {
const features = enableHover
? {
if (isVNodeEvent) {
yield ['', 'template', arg.loc.start.offset, {
...ctx.codeFeatures.withoutHighlightAndCompletion,
...eventArgFeatures,
}
: eventArgFeatures;
if (variableNameRegex.test(camelize(arg.loc.source))) {
...ctx.codeFeatures.navigationWithoutRename
}];
yield `onVnode`;
yield* generateCamelized(
capitalize(name.slice('vnode-'.length)),
arg.loc.start.offset + 'vue:'.length,
combineLastMapping
);
return;
}

const features = {
...ctx.codeFeatures.withoutHighlightAndCompletion,
...eventArgFeatures,
};
if (variableNameRegex.test(camelize(name))) {
yield ['', 'template', arg.loc.start.offset, features];
yield `on`;
yield* generateCamelized(
capitalize(arg.loc.source),
capitalize(name),
arg.loc.start.offset,
combineLastMapping
);
Expand All @@ -113,7 +130,7 @@ export function* generateEventArg(
['', 'template', arg.loc.start.offset, combineLastMapping],
'on',
...generateCamelized(
capitalize(arg.loc.source),
capitalize(name),
arg.loc.start.offset,
combineLastMapping
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function* generateElementProps(
) {
if (!isComponent) {
yield `...{ `;
yield* generateEventArg(ctx, prop.arg, true);
yield* generateEventArg(ctx, prop.arg);
yield `: `;
yield* generateEventExpression(options, ctx, prop);
yield `}, `;
Expand Down
8 changes: 6 additions & 2 deletions packages/language-service/lib/plugins/vue-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,11 @@ export function create(
}

const { attrs, propsInfo, events } = tagInfo;
const props = propsInfo.map(prop => prop.name);
const props = propsInfo.map(prop =>
hyphenateTag(prop.name).startsWith('on-vnode-')
? 'onVue:' + prop.name.slice('onVnode'.length)
: prop.name
);
const attributes: html.IAttributeData[] = [];
const _tsCodegen = tsCodegen.get(vueCode._sfc);

Expand Down Expand Up @@ -871,7 +875,7 @@ export function create(
}
else if (isEvent) {
item.kind = 23 satisfies typeof vscode.CompletionItemKind.Event;
if (propName.startsWith('vnode-')) {
if (propName.startsWith('vue:')) {
tokens.push('\u0004');
}
}
Expand Down