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/typescript-semantic-renaming-first
  • Loading branch information
KazariEX committed Jan 22, 2025
2 parents 355bb2d + 07a1baa commit 6e745c4
Show file tree
Hide file tree
Showing 69 changed files with 2,517 additions and 1,174 deletions.
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: "\U0001F41E Bug report"
description: Create a report to help us improve
type: Bug
labels: ["pending triage"]
body:
- type: markdown
Expand Down
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/feature_request.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: "\U0001F680 New feature proposal"
description: Suggest an idea for this project
labels: ["feature request"]
type: Feature
body:
- type: markdown
attributes:
Expand Down
80 changes: 71 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,80 @@ To develop with upstream Volar.js modules, you can setup workspace with https://

---

<h3 align="center">Full-time Support by</h3>
<br />
<table>
<tbody>
<tr>
<td align="center" valign="middle" colspan="6">
<b>Special Sponsor</b>
</td>
</tr>
<tr>
<td align="center" valign="middle" colspan="6">
<br>
<a href="https://voidzero.dev/">
<img src="https://raw.githubusercontent.com/johnsoncodehk/sponsors/master/logos/VoidZero.svg" height="60" />
</a>
<h3>Next Generation Tooling</h3>
</td>
</tr>
<tr>
<td align="center" valign="middle" colspan="6">
<b>Platinum Sponsors</b>
</td>
</tr>
<tr>
<td align="center" valign="middle" width="50%" colspan="3">
<a href="https://vuejs.org/">
<img src="https://raw.githubusercontent.com/johnsoncodehk/sponsors/master/logos/Vue.svg" height="80" />
</a>
<p>An approachable, performant and versatile framework for building web user interfaces.</p>
</td>
<td align="center" valign="middle" width="50%" colspan="3">
<a href="https://astro.build/">
<!-- Expire: 2025-02-04 -->
<img src="https://raw.githubusercontent.com/johnsoncodehk/sponsors/master/logos/Astro.svg" width="200" />
</a>
<p>Astro powers the world's fastest websites, client-side web apps, dynamic API endpoints, and everything in-between.</p>
</td>
</tr>
<tr>
<td align="center" valign="middle" colspan="3">
<!-- Expire: 2025-02-04 -->
<a href="https://www.jetbrains.com/">
<img src="https://raw.githubusercontent.com/johnsoncodehk/sponsors/master/logos/JetBrains.svg" width="80" />
</a>
<p>Essential tools for software developers and teams.</p>
</td>
<td align="center" valign="middle" colspan="3">
<a href="https://stackblitz.com/">
<img src="https://raw.githubusercontent.com/johnsoncodehk/sponsors/master/logos/StackBlitz.svg" width="240" />
</a>
<p>Stay in the flow with instant dev experiences.<br>No more hours stashing/pulling/installing locally</p>
<p><b> — just click, and start coding.</b></p>
</td>
</tr>
<tr>
<td align="center" valign="middle" colspan="6">
<b>Silver Sponsors</b>
</td>
</tr>
<tr>
<td align="center" valign="middle" width="33.3%" colspan="2">
<a href="https://www.prefect.io/"><img src="https://raw.githubusercontent.com/johnsoncodehk/sponsors/master/logos/Prefect.svg" width="200" /></a>
</td>
<td align="center" valign="middle" width="33.3%" colspan="2">
<a href="https://www.techjobasia.com/"><img src="https://raw.githubusercontent.com/johnsoncodehk/sponsors/master/logos/TechJobAsia.svg" width="200" /></a>
</td>
<td align="center" valign="middle" width="33.3%" colspan="2">
<a href="https://haoqun.blog/"><img src="https://avatars.githubusercontent.com/u/3277634?v=4" height="80" /></a>
</td>
</tr>
</tbody>
</table>

<p align="center">
<span>
<a href="https://stackblitz.com/">
<img src="https://raw.githubusercontent.com/johnsoncodehk/sponsors/master/logos/StackBlitz.svg" height="80" />
<h4 align="center">Boot a fresh environment in milliseconds.</h4>
</a>
</span>
<a href="https://github.com/sponsors/johnsoncodehk">Become a sponsor</a>
</p>
<br />

<p align="center">
<a href="https://cdn.jsdelivr.net/gh/johnsoncodehk/sponsors/sponsors.svg">
Expand Down
2 changes: 1 addition & 1 deletion extensions/vscode/src/features/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ function getPackageJsonOfWorkspacePackage(folder: string, pkg: string) {
const path = require.resolve(pkg + '/package.json', { paths: [folder] });
return {
path,
json: require(path) as { version: string },
json: require(path) as { version: string; },
};
} catch { }
}
2 changes: 1 addition & 1 deletion extensions/vscode/src/hybridMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const enabledHybridMode = computed(() => {
return true;
}
return config.server.hybridMode;
})
});

export const enabledTypeScriptPlugin = computed(() => {
return (
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
"test": "vitest run",
"test:update": "vitest run --update",
"lint": "tsslint --projects {tsconfig.json,packages/*/tsconfig.json,extensions/*/tsconfig.json}",
"lint:fix": "pnpm run lint -- --fix",
"lint:fix": "pnpm run lint -- --fix --vscode-settings .vscode/settings.json",
"chrome": "vscode-test-web --browserType=chromium --extensionDevelopmentPath=./extensions/vscode ../volar-starter"
},
"devDependencies": {
"@lerna-lite/cli": "latest",
"@lerna-lite/publish": "latest",
"@tsslint/cli": "latest",
"@tsslint/config": "latest",
"@tsslint/eslint": "latest",
"@typescript-eslint/eslint-plugin": "latest",
"@vue/language-core": "2.2.0",
"typescript": "latest",
"vitest": "latest"
}
Expand Down
12 changes: 10 additions & 2 deletions packages/component-meta/lib/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ export function baseCreate(
const directoryExists = languageServiceHost.directoryExists?.bind(languageServiceHost);
const fileExists = languageServiceHost.fileExists.bind(languageServiceHost);
const getScriptSnapshot = languageServiceHost.getScriptSnapshot.bind(languageServiceHost);
const globalTypesName = `${commandLine.vueOptions.lib}_${commandLine.vueOptions.target}_${commandLine.vueOptions.strictTemplates}.d.ts`;
const globalTypesContents = `// @ts-nocheck\nexport {};\n` + vue.generateGlobalTypes(commandLine.vueOptions.lib, commandLine.vueOptions.target, commandLine.vueOptions.strictTemplates);
const globalTypesName = vue.getGlobalTypesFileName(commandLine.vueOptions);
const globalTypesContents = `// @ts-nocheck\nexport {};\n` + vue.generateGlobalTypes(commandLine.vueOptions);
const globalTypesSnapshot: ts.IScriptSnapshot = {
getText: (start, end) => globalTypesContents.slice(start, end),
getLength: () => globalTypesContents.length,
Expand Down Expand Up @@ -754,6 +754,14 @@ function readVueComponentDefaultProps(
...resolvePropsOption(ast, obj, printer, ts),
};
}
} else if (descriptor.scriptSetup && scriptSetupRanges?.defineProps?.destructured) {
const ast = descriptor.scriptSetup.ast;
for (const [prop, initializer] of scriptSetupRanges.defineProps.destructured) {
if (initializer) {
const expText = printer?.printNode(ts.EmitHint.Expression, initializer, ast) ?? initializer.getText(ast);
result[prop] = { default: expText };
}
}
}

function findObjectLiteralExpression(node: ts.Node) {
Expand Down
11 changes: 11 additions & 0 deletions packages/component-meta/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,17 @@ const worker = (checker: ComponentMetaChecker, withTsconfig: boolean) => describ
});
});

test('reference-type-props-destructured', () => {
const componentPath = path.resolve(__dirname, '../../../test-workspace/component-meta/reference-type-props/component-destructure.vue');
const meta = checker.getComponentMeta(componentPath);

expect(meta.type).toEqual(TypeMeta.Class);

const text = meta.props.find(prop => prop.name === 'text');

expect(text?.default).toEqual('"foobar"');
});

test('reference-type-props-js', () => {
const componentPath = path.resolve(__dirname, '../../../test-workspace/component-meta/reference-type-props/component-js.vue');
const meta = checker.getComponentMeta(componentPath);
Expand Down
1 change: 0 additions & 1 deletion packages/language-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export * from './lib/utils/parseSfc';
export * from './lib/utils/ts';
export * from './lib/virtualFile/vueFile';

export * as scriptRanges from './lib/parsers/scriptRanges';
export { tsCodegen } from './lib/plugins/vue-tsx';
export * from './lib/utils/shared';

Expand Down
43 changes: 29 additions & 14 deletions packages/language-core/lib/codegen/globalTypes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import type { VueCompilerOptions } from '../types';
import { getSlotsPropertyName } from '../utils/shared';

export function generateGlobalTypes(lib: string, target: number, strictTemplates: boolean) {
const fnPropsType = `(K extends { $props: infer Props } ? Props : any)${strictTemplates ? '' : ' & Record<string, unknown>'}`;
export function getGlobalTypesFileName(options: VueCompilerOptions) {
return [
options.lib,
options.target,
options.checkUnknownProps,
options.checkUnknownEvents,
options.checkUnknownComponents,
].map(v => {
if (typeof v === 'boolean') {
return v ? 1 : 0;
}
return v;
}).join('_') + '.d.ts';
}

export function generateGlobalTypes(options: VueCompilerOptions) {
const { lib, target, checkUnknownProps, checkUnknownEvents, checkUnknownComponents } = options;
const fnPropsType = `(K extends { $props: infer Props } ? Props : any)${checkUnknownProps ? '' : ' & Record<string, unknown>'}`;
let text = ``;
if (target < 3.5) {
text += `
Expand Down Expand Up @@ -41,14 +58,15 @@ export function generateGlobalTypes(lib: string, target: number, strictTemplates
type __VLS_IsAny<T> = 0 extends 1 & T ? true : false;
type __VLS_PickNotAny<A, B> = __VLS_IsAny<A> extends true ? B : A;
type __VLS_unknownDirective = (arg1: unknown, arg2: unknown, arg3: unknown, arg4: unknown) => void;
type __VLS_WithComponent<N0 extends string, LocalComponents, N1 extends string, N2 extends string, N3 extends string> =
type __VLS_WithComponent<N0 extends string, LocalComponents, Self, N1 extends string, N2 extends string, N3 extends string> =
N1 extends keyof LocalComponents ? N1 extends N0 ? Pick<LocalComponents, N0 extends keyof LocalComponents ? N0 : never> : { [K in N0]: LocalComponents[N1] } :
N2 extends keyof LocalComponents ? N2 extends N0 ? Pick<LocalComponents, N0 extends keyof LocalComponents ? N0 : never> : { [K in N0]: LocalComponents[N2] } :
N3 extends keyof LocalComponents ? N3 extends N0 ? Pick<LocalComponents, N0 extends keyof LocalComponents ? N0 : never> : { [K in N0]: LocalComponents[N3] } :
Self extends object ? { [K in N0]: Self } :
N1 extends keyof __VLS_GlobalComponents ? N1 extends N0 ? Pick<__VLS_GlobalComponents, N0 extends keyof __VLS_GlobalComponents ? N0 : never> : { [K in N0]: __VLS_GlobalComponents[N1] } :
N2 extends keyof __VLS_GlobalComponents ? N2 extends N0 ? Pick<__VLS_GlobalComponents, N0 extends keyof __VLS_GlobalComponents ? N0 : never> : { [K in N0]: __VLS_GlobalComponents[N2] } :
N3 extends keyof __VLS_GlobalComponents ? N3 extends N0 ? Pick<__VLS_GlobalComponents, N0 extends keyof __VLS_GlobalComponents ? N0 : never> : { [K in N0]: __VLS_GlobalComponents[N3] } :
${strictTemplates ? '{}' : '{ [K in N0]: unknown }'};
${checkUnknownComponents ? '{}' : '{ [K in N0]: unknown }'};
type __VLS_FunctionalComponentProps<T, K> =
'__ctx' extends keyof __VLS_PickNotAny<K, {}> ? K extends { __ctx?: { props?: infer P } } ? NonNullable<P> : never
: T extends (props: infer P, ...args: any) => any ? P :
Expand All @@ -68,7 +86,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>'};
)${checkUnknownEvents ? '' : ' & 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 Expand Up @@ -99,23 +117,20 @@ export function generateGlobalTypes(lib: string, target: number, strictTemplates
>>;
type __VLS_UseTemplateRef<T> = Readonly<import('${lib}').ShallowRef<T | null>>;
function __VLS_getVForSourceType(source: number): [number, number, number][];
function __VLS_getVForSourceType(source: string): [string, number, number][];
function __VLS_getVForSourceType(source: number): [number, number][];
function __VLS_getVForSourceType(source: string): [string, number][];
function __VLS_getVForSourceType<T extends any[]>(source: T): [
item: T[number],
key: number,
index: number,
][];
function __VLS_getVForSourceType<T extends { [Symbol.iterator](): Iterator<any> }>(source: T): [
item: T extends { [Symbol.iterator](): Iterator<infer T1> } ? T1 : never,
key: number,
index: undefined,
index: number,
][];
// #3845
function __VLS_getVForSourceType<T extends number | { [Symbol.iterator](): Iterator<any> }>(source: T): [
item: number | (Exclude<T, number> extends { [Symbol.iterator](): Iterator<infer T1> } ? T1 : never),
key: number,
index: undefined,
index: number,
][];
function __VLS_getVForSourceType<T>(source: T): [
item: T[keyof T],
Expand All @@ -142,8 +157,8 @@ export function generateGlobalTypes(lib: string, target: number, strictTemplates
} & { props?: ${fnPropsType}; expose?(exposed: K): void; } }
: T extends () => any ? (props: {}, ctx?: any) => ReturnType<T>
: T extends (...args: any) => any ? T
: (_: {}${strictTemplates ? '' : ' & Record<string, unknown>'}, ctx?: any) => { __ctx?: { attrs?: any, expose?: any, slots?: any, emit?: any, props?: {}${strictTemplates ? '' : ' & Record<string, unknown>'} } };
function __VLS_elementAsFunction<T>(tag: T, endTag?: T): (_: T${strictTemplates ? '' : ' & Record<string, unknown>'}) => void;
: (_: {}${checkUnknownProps ? '' : ' & Record<string, unknown>'}, ctx?: any) => { __ctx?: { attrs?: any, expose?: any, slots?: any, emit?: any, props?: {}${checkUnknownProps ? '' : ' & Record<string, unknown>'} } };
function __VLS_asFunctionalElement<T>(tag: T, endTag?: T): (_: T${checkUnknownComponents ? '' : ' & Record<string, unknown>'}) => void;
function __VLS_functionalComponentArgsRest<T extends (...args: any) => any>(t: T): 2 extends Parameters<T>['length'] ? [any] : [];
function __VLS_normalizeSlot<S>(s: S): S extends () => infer R ? (props: {}) => R : S;
function __VLS_tryAsConstant<const T>(t: T): T;
Expand Down
6 changes: 3 additions & 3 deletions packages/language-core/lib/codegen/script/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ export function* generateComponent(
yield generateSfcBlockSection(options.sfc.script, args.start + 1, args.end - 1, codeFeatures.all);
}
if (options.vueCompilerOptions.target >= 3.5 && options.templateCodegen?.templateRefs.size) {
yield `__typeRefs: {} as __VLS_TemplateResult['refs'],${newLine}`;
yield `__typeRefs: {} as __VLS_TemplateRefs,${newLine}`;
}
if (options.vueCompilerOptions.target >= 3.5 && options.templateCodegen?.singleRootElType) {
yield `__typeEl: {} as __VLS_TemplateResult['rootEl'],${newLine}`;
yield `__typeEl: {} as __VLS_TemplateEl,${newLine}`;
}
yield `})`;
}
Expand Down Expand Up @@ -154,7 +154,7 @@ export function* generatePropsOption(
});
}
if (inheritAttrs && options.templateCodegen?.inheritedAttrVars.size) {
let attrsType = `__VLS_TemplateResult['attrs']`;
let attrsType = `__VLS_TemplateAttrs`;
if (hasEmitsOption) {
attrsType = `Omit<${attrsType}, \`on\${string}\`>`;
}
Expand Down
8 changes: 3 additions & 5 deletions packages/language-core/lib/codegen/script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type * as ts from 'typescript';
import type { ScriptRanges } from '../../parsers/scriptRanges';
import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges';
import type { Code, Sfc, VueCodeInformation, VueCompilerOptions } from '../../types';
import { generateGlobalTypes } from '../globalTypes';
import { generateGlobalTypes, getGlobalTypesFileName } from '../globalTypes';
import type { TemplateCodegenContext } from '../template/context';
import { endOfLine, generateSfcBlockSection, newLine } from '../utils';
import { generateComponentSelf } from './componentSelf';
Expand Down Expand Up @@ -68,7 +68,7 @@ export function* generateScript(options: ScriptCodegenOptions): Generator<Code,
yield `/// <reference types="${relativePath}" />${newLine}`;
}
else {
yield `/// <reference types=".vue-global-types/${options.vueCompilerOptions.lib}_${options.vueCompilerOptions.target}_${options.vueCompilerOptions.strictTemplates}.d.ts" />${newLine}`;
yield `/// <reference types=".vue-global-types/${getGlobalTypesFileName(options.vueCompilerOptions)}" />${newLine}`;
}
}
else {
Expand Down Expand Up @@ -151,9 +151,7 @@ export function* generateScript(options: ScriptCodegenOptions): Generator<Code,
}

if (!ctx.generatedTemplate) {
yield `function __VLS_template() {${newLine}`;
const templateCodegenCtx = yield* generateTemplate(options, ctx);
yield `}${endOfLine}`;
yield* generateComponentSelf(options, ctx, templateCodegenCtx);
}

Expand All @@ -165,7 +163,7 @@ export function* generateScript(options: ScriptCodegenOptions): Generator<Code,
}
yield* ctx.localTypes.generate([...ctx.localTypes.getUsedNames()]);
if (options.appendGlobalTypes) {
yield generateGlobalTypes(options.vueCompilerOptions.lib, options.vueCompilerOptions.target, options.vueCompilerOptions.strictTemplates);
yield generateGlobalTypes(options.vueCompilerOptions);
}

if (options.sfc.scriptSetup) {
Expand Down
Loading

0 comments on commit 6e745c4

Please sign in to comment.