Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/vuejs/language-tools into…
Browse files Browse the repository at this point in the history
… fix/resolve-components
  • Loading branch information
KazariEX committed Dec 20, 2024
2 parents a45d76d + 4fff892 commit 1dc739e
Show file tree
Hide file tree
Showing 60 changed files with 1,985 additions and 26,202 deletions.
1 change: 0 additions & 1 deletion .github/CODEOWNERS

This file was deleted.

3 changes: 3 additions & 0 deletions .github/workflows/update-html-data.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: update-html-data

on:
push:
branches:
- 'master'
workflow_dispatch:
schedule:
- cron: '0 0 * * *'
Expand Down
16 changes: 8 additions & 8 deletions extensions/vscode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,13 @@ Finally you need to make VS Code recognize your new extension and automatically

<!-- commands -->

| Command | Title |
| ------------------------------ | ------------------------------------------------- |
| `vue.action.restartServer` | Vue: Restart Vue and TS servers |
| `vue.action.doctor` | Vue: Doctor |
| `vue.action.writeVirtualFiles` | Vue (Debug): Write Virtual Files |
| `vue.action.splitEditors` | Vue: Split <script>, <template>, <style> Editors |
| `vue.findAllFileReferences` | Vue: Find File References via Vue Language Server |
| Command | Title |
| ------------------------------ | ------------------------------------------------------ |
| `vue.action.restartServer` | Vue: Restart Vue and TS servers |
| `vue.action.doctor` | Vue: Doctor |
| `vue.action.writeVirtualFiles` | Vue (Debug): Write Virtual Files |
| `vue.action.splitEditors` | Vue: Split &lt;script&gt;, <template>, <style> Editors |
| `vue.findAllFileReferences` | Vue: Find File References via Vue Language Server |

<!-- commands -->

Expand All @@ -292,7 +292,7 @@ Finally you need to make VS Code recognize your new extension and automatically
| `vue.complete.casing.props` | Preferred attr name case. | `string` | `"autoKebab"` |
| `vue.complete.defineAssignment` | Auto add `const props = ` before `defineProps` when selecting the completion item `props`. (also `emit` and `slots`) | `boolean` | `true` |
| `vue.autoInsert.dotValue` | Auto-complete Ref value with `.value`. | `boolean` | `false` |
| `vue.autoInsert.bracketSpacing` | Auto add space between double curly brackets: {{|}} -> {{ | }} | `boolean` | `true` |
| `vue.autoInsert.bracketSpacing` | Auto add space between double curly brackets: {{&vert;}} -&gt; {{ | }} | `boolean` | `true` |
| `vue.inlayHints.destructuredProps` | Show inlay hints for destructured props. | `boolean` | `false` |
| `vue.inlayHints.missingProps` | Show inlay hints for missing required props. | `boolean` | `false` |
| `vue.inlayHints.inlineHandlerLeading` | Show inlay hints for event argument in inline handlers. | `boolean` | `false` |
Expand Down
3 changes: 2 additions & 1 deletion packages/language-core/lib/codegen/globalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function generateGlobalTypes(lib: string, target: number, strictTemplates
const __VLS_intrinsicElements: __VLS_IntrinsicElements;
const __VLS_directiveBindingRestFields: { instance: null, oldValue: null, modifiers: any, dir: any };
const __VLS_unref: typeof import('${lib}').unref;
const __VLS_placeholder: any;
const __VLS_nativeElements = {
...{} as SVGElementTagNameMap,
Expand Down Expand Up @@ -67,7 +68,7 @@ export function generateGlobalTypes(lib: string, target: number, strictTemplates
: __VLS_IsFunction<Events, CamelizedEvent> extends true
? { [K in onEvent]?: Events[CamelizedEvent] }
: Props
)${ strictTemplates ? '' : ' & Record<string, unknown>' };
)${strictTemplates ? '' : ' & Record<string, unknown>'};
// fix https://github.com/vuejs/language-tools/issues/926
type __VLS_UnionToIntersection<U> = (U extends unknown ? (arg: U) => unknown : never) extends ((arg: infer P) => unknown) ? P : never;
type __VLS_OverloadUnionInner<T, U = unknown> = U & T extends (...args: infer A) => infer R
Expand Down
23 changes: 0 additions & 23 deletions packages/language-core/lib/codegen/script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export function* generateScript(options: ScriptCodegenOptions): Generator<Code,
&& options.sfc.script.content[exportDefault.expression.start] === '{';
if (options.sfc.scriptSetup && options.scriptSetupRanges) {
yield* generateScriptSetupImports(options.sfc.scriptSetup, options.scriptSetupRanges);
yield* generateDefineProp(options, options.sfc.scriptSetup);
if (exportDefault) {
yield generateSfcBlockSection(options.sfc.script, 0, exportDefault.expression.start, codeFeatures.all);
yield* generateScriptSetup(options, ctx, options.sfc.scriptSetup, options.scriptSetupRanges);
Expand Down Expand Up @@ -141,7 +140,6 @@ export function* generateScript(options: ScriptCodegenOptions): Generator<Code,
}
else if (options.sfc.scriptSetup && options.scriptSetupRanges) {
yield* generateScriptSetupImports(options.sfc.scriptSetup, options.scriptSetupRanges);
yield* generateDefineProp(options, options.sfc.scriptSetup);
yield* generateScriptSetup(options, ctx, options.sfc.scriptSetup, options.scriptSetupRanges);
}

Expand Down Expand Up @@ -182,24 +180,3 @@ export function* generateScriptSectionPartiallyEnding(source: string, end: numbe
yield ['', source, end, codeFeatures.verification];
yield `/* PartiallyEnd: ${mark} */${newLine}`;
}

function* generateDefineProp(
options: ScriptCodegenOptions,
scriptSetup: NonNullable<Sfc['scriptSetup']>
): Generator<Code> {
const definePropProposalA = scriptSetup.content.trimStart().startsWith('// @experimentalDefinePropProposal=kevinEdition') || options.vueCompilerOptions.experimentalDefinePropProposal === 'kevinEdition';
const definePropProposalB = scriptSetup.content.trimStart().startsWith('// @experimentalDefinePropProposal=johnsonEdition') || options.vueCompilerOptions.experimentalDefinePropProposal === 'johnsonEdition';

if (definePropProposalA || definePropProposalB) {
yield `type __VLS_PropOptions<T> = Exclude<import('${options.vueCompilerOptions.lib}').Prop<T>, import('${options.vueCompilerOptions.lib}').PropType<T>>${endOfLine}`;
if (definePropProposalA) {
yield `declare function defineProp<T>(name: string, options: ({ required: true } | { default: T }) & __VLS_PropOptions<T>): import('${options.vueCompilerOptions.lib}').ComputedRef<T>${endOfLine}`;
yield `declare function defineProp<T>(name?: string, options?: __VLS_PropOptions<T>): import('${options.vueCompilerOptions.lib}').ComputedRef<T | undefined>${endOfLine}`;
}
if (definePropProposalB) {
yield `declare function defineProp<T>(value: T | (() => T), required?: boolean, options?: __VLS_PropOptions<T>): import('${options.vueCompilerOptions.lib}').ComputedRef<T>${endOfLine}`;
yield `declare function defineProp<T>(value: T | (() => T) | undefined, required: true, options?: __VLS_PropOptions<T>): import('${options.vueCompilerOptions.lib}').ComputedRef<T>${endOfLine}`;
yield `declare function defineProp<T>(value?: T | (() => T), required?: boolean, options?: __VLS_PropOptions<T>): import('${options.vueCompilerOptions.lib}').ComputedRef<T | undefined>${endOfLine}`;
}
}
}
126 changes: 82 additions & 44 deletions packages/language-core/lib/codegen/script/scriptSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export function* generateScriptSetupImports(
0,
codeFeatures.all,
];
yield newLine;
}

export function* generateScriptSetup(
Expand Down Expand Up @@ -66,7 +65,7 @@ export function* generateScriptSetup(
}

yield `return {} as {${newLine}`
+ ` props: ${ctx.localTypes.PrettifyLocal}<typeof __VLS_functionalComponentProps & __VLS_TemplateResult['attrs'] & __VLS_PublicProps> & __VLS_BuiltInPublicProps,${newLine}`
+ ` props: ${ctx.localTypes.PrettifyLocal}<__VLS_OwnProps & __VLS_PublicProps & __VLS_TemplateResult['attrs']> & __VLS_BuiltInPublicProps,${newLine}`
+ ` expose(exposed: import('${options.vueCompilerOptions.lib}').ShallowUnwrapRef<${scriptSetupRanges.defineExpose ? 'typeof __VLS_exposed' : '{}'}>): void,${newLine}`
+ ` attrs: any,${newLine}`
+ ` slots: __VLS_TemplateResult['slots'],${newLine}`
Expand Down Expand Up @@ -96,16 +95,6 @@ function* generateSetupFunction(
scriptSetupRanges: ScriptSetupRanges,
syntax: 'return' | 'export default' | undefined
): Generator<Code> {
if (options.vueCompilerOptions.target >= 3.3) {
yield `const { `;
for (const macro of Object.keys(options.vueCompilerOptions.macros)) {
if (!ctx.bindingNames.has(macro) && macro !== 'templateRef') {
yield macro + `, `;
}
}
yield `} = await import('${options.vueCompilerOptions.lib}')${endOfLine}`;
}

ctx.scriptSetupGeneratedOffset = options.getGeneratedLength() - scriptSetupRanges.importSectionEndOffset;

let setupCodeModifies: [Code[], number, number][] = [];
Expand Down Expand Up @@ -226,17 +215,19 @@ function* generateSetupFunction(
}
const isTs = options.lang !== 'js' && options.lang !== 'jsx';
for (const { callExp, exp, arg } of scriptSetupRanges.useTemplateRef) {
const templateRefType = arg ? [
`__VLS_TemplateResult['refs'][`,
generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.navigation),
`]`
] : [`unknown`];
const templateRefType = arg
? [
`__VLS_TemplateResult['refs'][`,
generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.all),
`]`
]
: [`unknown`];
if (isTs) {
setupCodeModifies.push([
[
`<`,
...templateRefType,
`, keyof __VLS_TemplateResult['refs']>`
`>`
],
exp.end,
exp.end
Expand All @@ -257,6 +248,13 @@ function* generateSetupFunction(
callExp.end
]);
}
if (arg) {
setupCodeModifies.push([
[`(__VLS_placeholder)`],
arg.start,
arg.end
]);
}
}
setupCodeModifies = setupCodeModifies.sort((a, b) => a[1] - b[1]);

Expand All @@ -271,6 +269,8 @@ function* generateSetupFunction(
yield generateSfcBlockSection(scriptSetup, nextStart, scriptSetup.content.length, codeFeatures.all);

yield* generateScriptSectionPartiallyEnding(scriptSetup.name, scriptSetup.content.length, '#3632/scriptSetup.vue');
yield* generateMacros(options, ctx);
yield* generateDefineProp(options, scriptSetup);

if (scriptSetupRanges.defineProps?.typeArg && scriptSetupRanges.withDefaults?.arg) {
// fix https://github.com/vuejs/language-tools/issues/1187
Expand Down Expand Up @@ -308,6 +308,42 @@ function* generateSetupFunction(
}
}

function* generateMacros(
options: ScriptCodegenOptions,
ctx: ScriptCodegenContext
): Generator<Code> {
if (options.vueCompilerOptions.target >= 3.3) {
yield `declare const { `;
for (const macro of Object.keys(options.vueCompilerOptions.macros)) {
if (!ctx.bindingNames.has(macro)) {
yield `${macro}, `;
}
}
yield `}: typeof import('${options.vueCompilerOptions.lib}')${endOfLine}`;
}
}

function* generateDefineProp(
options: ScriptCodegenOptions,
scriptSetup: NonNullable<Sfc['scriptSetup']>
): Generator<Code> {
const definePropProposalA = scriptSetup.content.trimStart().startsWith('// @experimentalDefinePropProposal=kevinEdition') || options.vueCompilerOptions.experimentalDefinePropProposal === 'kevinEdition';
const definePropProposalB = scriptSetup.content.trimStart().startsWith('// @experimentalDefinePropProposal=johnsonEdition') || options.vueCompilerOptions.experimentalDefinePropProposal === 'johnsonEdition';

if (definePropProposalA || definePropProposalB) {
yield `type __VLS_PropOptions<T> = Exclude<import('${options.vueCompilerOptions.lib}').Prop<T>, import('${options.vueCompilerOptions.lib}').PropType<T>>${endOfLine}`;
if (definePropProposalA) {
yield `declare function defineProp<T>(name: string, options: ({ required: true } | { default: T }) & __VLS_PropOptions<T>): import('${options.vueCompilerOptions.lib}').ComputedRef<T>${endOfLine}`;
yield `declare function defineProp<T>(name?: string, options?: __VLS_PropOptions<T>): import('${options.vueCompilerOptions.lib}').ComputedRef<T | undefined>${endOfLine}`;
}
if (definePropProposalB) {
yield `declare function defineProp<T>(value: T | (() => T), required?: boolean, options?: __VLS_PropOptions<T>): import('${options.vueCompilerOptions.lib}').ComputedRef<T>${endOfLine}`;
yield `declare function defineProp<T>(value: T | (() => T) | undefined, required: true, options?: __VLS_PropOptions<T>): import('${options.vueCompilerOptions.lib}').ComputedRef<T>${endOfLine}`;
yield `declare function defineProp<T>(value?: T | (() => T), required?: boolean, options?: __VLS_PropOptions<T>): import('${options.vueCompilerOptions.lib}').ComputedRef<T | undefined>${endOfLine}`;
}
}
}

function* generateDefineWithType(
scriptSetup: NonNullable<Sfc['scriptSetup']>,
statement: TextRange,
Expand Down Expand Up @@ -359,36 +395,38 @@ function* generateComponentProps(
scriptSetup: NonNullable<Sfc['scriptSetup']>,
scriptSetupRanges: ScriptSetupRanges
): Generator<Code> {
yield `const __VLS_fnComponent = (await import('${options.vueCompilerOptions.lib}')).defineComponent({${newLine}`;

if (scriptSetupRanges.defineProps?.arg) {
yield `props: `;
yield generateSfcBlockSection(
scriptSetup,
scriptSetupRanges.defineProps.arg.start,
scriptSetupRanges.defineProps.arg.end,
codeFeatures.navigation
);
yield `,${newLine}`;
}
if (scriptSetup.generic) {
yield `const __VLS_fnComponent = (await import('${options.vueCompilerOptions.lib}')).defineComponent({${newLine}`;

if (scriptSetupRanges.defineProps?.arg) {
yield `props: `;
yield generateSfcBlockSection(
scriptSetup,
scriptSetupRanges.defineProps.arg.start,
scriptSetupRanges.defineProps.arg.end,
codeFeatures.navigation
);
yield `,${newLine}`;
}

yield* generateEmitsOption(options, scriptSetupRanges);
yield* generateEmitsOption(options, scriptSetupRanges);

yield `})${endOfLine}`;
yield `})${endOfLine}`;

yield `type __VLS_BuiltInPublicProps = ${options.vueCompilerOptions.target >= 3.4
? `import('${options.vueCompilerOptions.lib}').PublicProps`
: options.vueCompilerOptions.target >= 3.0
? `import('${options.vueCompilerOptions.lib}').VNodeProps`
+ ` & import('${options.vueCompilerOptions.lib}').AllowedComponentProps`
+ ` & import('${options.vueCompilerOptions.lib}').ComponentCustomProps`
: `globalThis.JSX.IntrinsicAttributes`
}`;
yield endOfLine;
yield `type __VLS_BuiltInPublicProps = ${options.vueCompilerOptions.target >= 3.4
? `import('${options.vueCompilerOptions.lib}').PublicProps`
: options.vueCompilerOptions.target >= 3.0
? `import('${options.vueCompilerOptions.lib}').VNodeProps`
+ ` & import('${options.vueCompilerOptions.lib}').AllowedComponentProps`
+ ` & import('${options.vueCompilerOptions.lib}').ComponentCustomProps`
: `globalThis.JSX.IntrinsicAttributes`
}`;
yield endOfLine;

yield `let __VLS_functionalComponentProps!: `;
yield `${ctx.localTypes.OmitKeepDiscriminatedUnion}<InstanceType<typeof __VLS_fnComponent>['$props'], keyof __VLS_BuiltInPublicProps>`;
yield endOfLine;
yield `type __VLS_OwnProps = `;
yield `${ctx.localTypes.OmitKeepDiscriminatedUnion}<InstanceType<typeof __VLS_fnComponent>['$props'], keyof __VLS_BuiltInPublicProps>`;
yield endOfLine;
}

if (scriptSetupRanges.defineProp.length) {
yield `const __VLS_defaults = {${newLine}`;
Expand Down
Loading

0 comments on commit 1dc739e

Please sign in to comment.