Skip to content

Commit d826110

Browse files
committed
Merge branch 'master' into pr/4813
2 parents 83c45a7 + acddb02 commit d826110

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+426
-296
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,15 @@ lspconfig.tsserver.setup {
9797
},
9898
},
9999
},
100+
}
100101

101102
lspconfig.volar.setup {
102103
init_options = {
103104
vue = {
104105
hybridMode = false,
105106
},
106107
},
107-
},
108+
}
108109
```
109110

110111
### nvim-cmp integration

packages/component-meta/lib/base.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ function readVueComponentDefaultProps(
721721

722722
function scriptSetupWorker() {
723723

724-
const descriptor = vueSourceFile.sfc;
724+
const descriptor = vueSourceFile._sfc;
725725
const scriptSetupRanges = descriptor.scriptSetup ? vue.parseScriptSetupRanges(ts, descriptor.scriptSetup.ast, vueCompilerOptions) : undefined;
726726

727727
if (descriptor.scriptSetup && scriptSetupRanges?.props.withDefaults?.arg) {
@@ -772,7 +772,7 @@ function readVueComponentDefaultProps(
772772

773773
function scriptWorker() {
774774

775-
const descriptor = vueSourceFile.sfc;
775+
const descriptor = vueSourceFile._sfc;
776776

777777
if (descriptor.script) {
778778
const scriptResult = readTsComponentDefaultProps(descriptor.script.lang, descriptor.script.content, 'default', printer, ts);

packages/component-meta/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"**/*.js",
77
"**/*.d.ts"
88
],
9+
"sideEffects": false,
910
"repository": {
1011
"type": "git",
1112
"url": "https://github.com/vuejs/language-tools.git",

packages/component-type-helpers/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"**/*.js",
77
"**/*.d.ts"
88
],
9+
"sideEffects": false,
910
"repository": {
1011
"type": "git",
1112
"url": "https://github.com/vuejs/language-tools.git",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type * as CompilerDOM from '@vue/compiler-dom';
2+
3+
export interface InlayHintInfo {
4+
blockName: string;
5+
offset: number;
6+
setting: string;
7+
label: string;
8+
tooltip?: string;
9+
paddingRight?: boolean;
10+
paddingLeft?: boolean;
11+
}
12+
13+
export function createVBindShorthandInlayHintInfo(loc: CompilerDOM.SourceLocation, variableName: string): InlayHintInfo {
14+
return {
15+
blockName: 'template',
16+
offset: loc.end.offset,
17+
setting: 'vue.inlayHints.vBindShorthand',
18+
label: `="${variableName}"`,
19+
tooltip: [
20+
`This is a shorthand for \`${loc.source}="${variableName}"\`.`,
21+
'To hide this hint, set `vue.inlayHints.vBindShorthand` to `false` in IDE settings.',
22+
'[More info](https://github.com/vuejs/core/pull/9451)',
23+
].join('\n\n'),
24+
};
25+
}

packages/language-core/lib/codegen/script/context.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { InlayHintInfo } from '../types';
1+
import { InlayHintInfo } from '../inlayHints';
22
import { getLocalTypesGenerator } from '../localTypes';
33
import type { ScriptCodegenOptions } from './index';
44

packages/language-core/lib/codegen/script/index.ts

+15-8
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import type { Code, Sfc, VueCodeInformation, VueCompilerOptions } from '../../ty
66
import { endOfLine, generateSfcBlockSection, newLine } from '../common';
77
import { generateGlobalTypes } from '../globalTypes';
88
import type { TemplateCodegenContext } from '../template/context';
9-
import { createScriptCodegenContext, ScriptCodegenContext } from './context';
109
import { generateComponentSelf } from './componentSelf';
10+
import { createScriptCodegenContext, ScriptCodegenContext } from './context';
1111
import { generateScriptSetup, generateScriptSetupImports } from './scriptSetup';
1212
import { generateSrc } from './src';
1313
import { generateStyleModulesType } from './styleModulesType';
@@ -79,6 +79,7 @@ export function* generateScript(options: ScriptCodegenOptions): Generator<Code,
7979
}
8080
else {
8181
yield generateSfcBlockSection(options.sfc.script, 0, options.sfc.script.content.length, codeFeatures.all);
82+
yield* generateScriptSectionPartiallyEnding(options.sfc.script.name, options.sfc.script.content.length, '#3632/both.vue');
8283
yield* generateScriptSetup(options, ctx, options.sfc.scriptSetup, options.scriptSetupRanges);
8384
}
8485
}
@@ -114,8 +115,8 @@ export function* generateScript(options: ScriptCodegenOptions): Generator<Code,
114115
}
115116
else {
116117
yield generateSfcBlockSection(options.sfc.script, 0, classBlockEnd, codeFeatures.all);
117-
yield `__VLS_template = () => {`;
118-
const templateCodegenCtx = yield* generateTemplate(options, ctx, true);
118+
yield `__VLS_template = () => {${newLine}`;
119+
const templateCodegenCtx = yield* generateTemplate(options, ctx);
119120
yield* generateComponentSelf(options, ctx, templateCodegenCtx);
120121
yield `},${newLine}`;
121122
yield generateSfcBlockSection(options.sfc.script, classBlockEnd, options.sfc.script.content.length, codeFeatures.all);
@@ -131,16 +132,16 @@ export function* generateScript(options: ScriptCodegenOptions): Generator<Code,
131132
yield* generateScriptSetup(options, ctx, options.sfc.scriptSetup, options.scriptSetupRanges);
132133
}
133134

134-
yield `;`;
135+
if (options.sfc.script) {
136+
yield* generateScriptSectionPartiallyEnding(options.sfc.script.name, options.sfc.script.content.length, '#3632/script.vue');
137+
}
135138
if (options.sfc.scriptSetup) {
136-
// #4569
137-
yield ['', 'scriptSetup', options.sfc.scriptSetup.content.length, codeFeatures.verification];
139+
yield* generateScriptSectionPartiallyEnding(options.sfc.scriptSetup.name, options.sfc.scriptSetup.content.length, '#4569/main.vue');
138140
}
139-
yield newLine;
140141

141142
if (!ctx.generatedTemplate) {
142143
yield `function __VLS_template() {${newLine}`;
143-
const templateCodegenCtx = yield* generateTemplate(options, ctx, false);
144+
const templateCodegenCtx = yield* generateTemplate(options, ctx);
144145
yield `}${endOfLine}`;
145146
yield* generateComponentSelf(options, ctx, templateCodegenCtx);
146147
}
@@ -163,6 +164,12 @@ export function* generateScript(options: ScriptCodegenOptions): Generator<Code,
163164
return ctx;
164165
}
165166

167+
export function* generateScriptSectionPartiallyEnding(source: string, end: number, mark: string): Generator<Code> {
168+
yield `;`;
169+
yield ['', source, end, codeFeatures.verification];
170+
yield `/* PartiallyEnd: ${mark} */${newLine}`;
171+
}
172+
166173
function* generateDefineProp(
167174
options: ScriptCodegenOptions,
168175
scriptSetup: NonNullable<Sfc['scriptSetup']>

packages/language-core/lib/codegen/script/scriptSetup.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges';
22
import type { Code, Sfc, TextRange } from '../../types';
33
import { endOfLine, generateSfcBlockSection, newLine } from '../common';
44
import { generateComponent, generateEmitsOption } from './component';
5-
import type { ScriptCodegenContext } from './context';
6-
import { ScriptCodegenOptions, codeFeatures } from './index';
75
import { generateComponentSelf } from './componentSelf';
6+
import type { ScriptCodegenContext } from './context';
7+
import { ScriptCodegenOptions, codeFeatures, generateScriptSectionPartiallyEnding } from './index';
88
import { generateTemplate } from './template';
99

1010
export function* generateScriptSetupImports(
@@ -278,6 +278,8 @@ function* generateSetupFunction(
278278
yield generateSfcBlockSection(scriptSetup, scriptSetupRanges.importSectionEndOffset, scriptSetup.content.length, codeFeatures.all);
279279
}
280280

281+
yield* generateScriptSectionPartiallyEnding(scriptSetup.name, scriptSetup.content.length, '#3632/scriptSetup.vue');
282+
281283
if (scriptSetupRanges.props.define?.typeArg && scriptSetupRanges.props.withDefaults?.arg) {
282284
// fix https://github.com/vuejs/language-tools/issues/1187
283285
yield `const __VLS_withDefaultsArg = (function <T>(t: T) { return t })(`;
@@ -288,7 +290,7 @@ function* generateSetupFunction(
288290
yield* generateComponentProps(options, ctx, scriptSetup, scriptSetupRanges, definePropMirrors);
289291
yield* generateModelEmits(options, scriptSetup, scriptSetupRanges);
290292
yield `function __VLS_template() {${newLine}`;
291-
const templateCodegenCtx = yield* generateTemplate(options, ctx, false);
293+
const templateCodegenCtx = yield* generateTemplate(options, ctx);
292294
yield `}${endOfLine}`;
293295
yield* generateComponentSelf(options, ctx, templateCodegenCtx);
294296
yield `type __VLS_TemplateResult = ReturnType<typeof __VLS_template>${endOfLine}`;

packages/language-core/lib/codegen/script/template.ts

+5-13
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,11 @@ import { generateStyleScopedClasses } from '../template/styleScopedClasses';
88
import type { ScriptCodegenContext } from './context';
99
import { codeFeatures, type ScriptCodegenOptions } from './index';
1010

11-
function* generateTemplateCtx(
12-
options: ScriptCodegenOptions,
13-
isClassComponent: boolean
14-
): Generator<Code> {
11+
function* generateTemplateCtx(options: ScriptCodegenOptions): Generator<Code> {
1512
const exps = [];
1613

17-
if (isClassComponent) {
18-
exps.push(`this`);
19-
}
20-
else {
21-
exps.push(`{} as InstanceType<__VLS_PickNotAny<typeof __VLS_self, new () => {}>>`);
22-
}
14+
exps.push(`{} as InstanceType<__VLS_PickNotAny<typeof __VLS_self, new () => {}>>`);
15+
2316
if (options.vueCompilerOptions.petiteVueExtensions.some(ext => options.fileBaseName.endsWith(ext))) {
2417
exps.push(`globalThis`);
2518
}
@@ -116,16 +109,15 @@ export function* generateTemplateDirectives(options: ScriptCodegenOptions): Gene
116109

117110
export function* generateTemplate(
118111
options: ScriptCodegenOptions,
119-
ctx: ScriptCodegenContext,
120-
isClassComponent: boolean
112+
ctx: ScriptCodegenContext
121113
): Generator<Code, TemplateCodegenContext> {
122114
ctx.generatedTemplate = true;
123115

124116
const templateCodegenCtx = createTemplateCodegenContext({
125117
scriptSetupBindingNames: new Set(),
126118
edited: options.edited,
127119
});
128-
yield* generateTemplateCtx(options, isClassComponent);
120+
yield* generateTemplateCtx(options);
129121
yield* generateTemplateComponents(options);
130122
yield* generateTemplateDirectives(options);
131123
yield* generateTemplateBody(options, templateCodegenCtx);

packages/language-core/lib/codegen/template/context.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type * as CompilerDOM from '@vue/compiler-dom';
22
import type { Code, VueCodeInformation } from '../../types';
33
import { endOfLine, newLine, wrapWith } from '../common';
44
import type { TemplateCodegenOptions } from './index';
5-
import { InlayHintInfo } from '../types';
5+
import { InlayHintInfo } from '../inlayHints';
66

77
const _codeFeatures = {
88
all: {

packages/language-core/lib/codegen/template/element.ts

+17-8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { generateInterpolation } from './interpolation';
1515
import { generatePropertyAccess } from './propertyAccess';
1616
import { generateTemplateChild } from './templateChild';
1717
import { generateObjectProperty } from './objectProperty';
18+
import { createVBindShorthandInlayHintInfo } from '../inlayHints';
1819
import { getNodeText } from '../../parsers/scriptSetupRanges';
1920

2021
const colonReg = /:/g;
@@ -48,16 +49,24 @@ export function* generateComponent(
4849

4950
let props = node.props;
5051
let dynamicTagInfo: {
51-
exp: string;
52+
tag: string;
5253
offsets: [number, number | undefined];
53-
astHolder: any;
54+
astHolder: CompilerDOM.SourceLocation;
5455
} | undefined;
5556

5657
if (isComponentTag) {
5758
for (const prop of node.props) {
58-
if (prop.type === CompilerDOM.NodeTypes.DIRECTIVE && prop.name === 'bind' && prop.arg?.loc.source === 'is' && prop.exp) {
59+
if (
60+
prop.type === CompilerDOM.NodeTypes.DIRECTIVE
61+
&& prop.name === 'bind'
62+
&& prop.arg?.loc.source === 'is'
63+
&& prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
64+
) {
65+
if (prop.arg.loc.end.offset === prop.exp.loc.end.offset) {
66+
ctx.inlayHints.push(createVBindShorthandInlayHintInfo(prop.exp.loc, 'is'));
67+
}
5968
dynamicTagInfo = {
60-
exp: prop.exp.loc.source,
69+
tag: prop.exp.content,
6170
offsets: [prop.exp.loc.start.offset, undefined],
6271
astHolder: prop.exp.loc,
6372
};
@@ -69,9 +78,9 @@ export function* generateComponent(
6978
else if (node.tag.includes('.')) {
7079
// namespace tag
7180
dynamicTagInfo = {
72-
exp: node.tag,
73-
astHolder: node.loc,
81+
tag: node.tag,
7482
offsets: [startTagOffset, endTagOffset],
83+
astHolder: node.loc,
7584
};
7685
}
7786

@@ -110,7 +119,7 @@ export function* generateComponent(
110119
yield* generateInterpolation(
111120
options,
112121
ctx,
113-
dynamicTagInfo.exp,
122+
dynamicTagInfo.tag,
114123
dynamicTagInfo.astHolder,
115124
dynamicTagInfo.offsets[0],
116125
ctx.codeFeatures.all,
@@ -122,7 +131,7 @@ export function* generateComponent(
122131
yield* generateInterpolation(
123132
options,
124133
ctx,
125-
dynamicTagInfo.exp,
134+
dynamicTagInfo.tag,
126135
dynamicTagInfo.astHolder,
127136
dynamicTagInfo.offsets[1],
128137
{

packages/language-core/lib/codegen/template/elementProps.ts

+2-11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { generateEventArg, generateEventExpression } from './elementEvents';
1111
import type { TemplateCodegenOptions } from './index';
1212
import { generateInterpolation } from './interpolation';
1313
import { generateObjectProperty } from './objectProperty';
14+
import { createVBindShorthandInlayHintInfo } from '../inlayHints';
1415

1516
export function* generateElementProps(
1617
options: TemplateCodegenOptions,
@@ -334,17 +335,7 @@ function* generatePropExp(
334335
features
335336
);
336337
if (enableCodeFeatures) {
337-
ctx.inlayHints.push({
338-
blockName: 'template',
339-
offset: prop.loc.end.offset,
340-
setting: 'vue.inlayHints.vBindShorthand',
341-
label: `="${propVariableName}"`,
342-
tooltip: [
343-
`This is a shorthand for \`${prop.loc.source}="${propVariableName}"\`.`,
344-
'To hide this hint, set `vue.inlayHints.vBindShorthand` to `false` in IDE settings.',
345-
'[More info](https://github.com/vuejs/core/pull/9451)',
346-
].join('\n\n'),
347-
});
338+
ctx.inlayHints.push(createVBindShorthandInlayHintInfo(prop.loc, propVariableName));
348339
}
349340
}
350341
}

packages/language-core/lib/codegen/template/interpolation.ts

-10
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ export function* generateInterpolation(
1818
): Generator<Code> {
1919
const code = prefix + _code + suffix;
2020
const ast = createTsAst(options.ts, astHolder, code);
21-
const vars: {
22-
text: string,
23-
isShorthand: boolean,
24-
offset: number,
25-
}[] = [];
2621
for (let [section, offset, type] of forEachInterpolationSegment(
2722
options.ts,
2823
options.destructuredPropNames,
@@ -70,11 +65,6 @@ export function* generateInterpolation(
7065
yield addSuffix;
7166
}
7267
}
73-
if (start !== undefined) {
74-
for (const v of vars) {
75-
v.offset = start + v.offset - prefix.length;
76-
}
77-
}
7868
}
7969

8070
export function* forEachInterpolationSegment(

packages/language-core/lib/codegen/template/slotOutlet.ts

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { generateElementChildren } from './elementChildren';
66
import { generateElementProps } from './elementProps';
77
import type { TemplateCodegenOptions } from './index';
88
import { generateInterpolation } from './interpolation';
9+
import { createVBindShorthandInlayHintInfo } from '../inlayHints';
910

1011
export function* generateSlotOutlet(
1112
options: TemplateCodegenOptions,
@@ -80,6 +81,10 @@ export function* generateSlotOutlet(
8081
nameProp?.type === CompilerDOM.NodeTypes.DIRECTIVE
8182
&& nameProp.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
8283
) {
84+
const isShortHand = nameProp.arg?.loc.start.offset === nameProp.exp.loc.start.offset;
85+
if (isShortHand) {
86+
ctx.inlayHints.push(createVBindShorthandInlayHintInfo(nameProp.exp.loc, 'name'));
87+
}
8388
const slotExpVar = ctx.getInternalVariable();
8489
yield `var ${slotExpVar} = `;
8590
yield* generateInterpolation(

packages/language-core/lib/codegen/types.ts

-9
This file was deleted.

0 commit comments

Comments
 (0)