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: detection of slots in script-less SFC #2113

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions vue-language-tools/vue-component-meta/tests/index.spec.ts
Expand Up @@ -550,6 +550,32 @@ const worker = (checker: ComponentMetaChecker, withTsconfig: boolean) => describ
expect(d).toBeDefined();
});

test('template-slots without a script block', () => {
const componentPath = path.resolve(__dirname, '../../vue-test-workspace/vue-component-meta/template-slots/component-no-script.vue');
const meta = checker.getComponentMeta(componentPath);

const a = meta.slots.find(slot =>
slot.name === 'default'
&& slot.type === '{ num: number; }'
);
const b = meta.slots.find(slot =>
slot.name === 'named-slot'
&& slot.type === '{ str: string; }'
);
const c = meta.slots.find(slot =>
slot.name === 'vbind'
&& slot.type === '{ num: number; str: string; }'
);
const d = meta.slots.find(slot =>
slot.name === 'no-bind'
);

expect(a).toBeDefined();
expect(b).toBeDefined();
expect(c).toBeDefined();
expect(d).toBeDefined();
});

test('class-slots', () => {
const componentPath = path.resolve(__dirname, '../../vue-test-workspace/vue-component-meta/class-slots/component.vue');
const meta = checker.getComponentMeta(componentPath);
Expand Down
37 changes: 36 additions & 1 deletion vue-language-tools/vue-language-core/src/generators/script.ts
Expand Up @@ -17,7 +17,7 @@ import { walkInterpolationFragment } from '../utils/transform';
export function generate(
ts: typeof import('typescript/lib/tsserverlibrary'),
fileName: string,
sfc: Sfc,
_sfc: Sfc,
lang: string,
scriptRanges: ScriptRanges | undefined,
scriptSetupRanges: ScriptSetupRanges | undefined,
Expand All @@ -31,6 +31,41 @@ export function generate(
mirrorBehaviorMappings: SourceMaps.Mapping<[MirrorBehaviorCapabilities, MirrorBehaviorCapabilities]>[] = [],
) {

// monkey fix for https://github.com/johnsoncodehk/volar/pull/2113
const sfc = {
script: _sfc.script,
scriptSetup: _sfc.scriptSetup,
};
if (!sfc.script && !sfc.scriptSetup) {
sfc.scriptSetup = {
content: '',
lang: 'ts',
name: '',
start: 0,
end: 0,
startTagEnd: 0,
endTagStart: 0,
generic: undefined,
genericOffset: 0,
};
scriptSetupRanges = {
bindings: [],
emitsAssignName: undefined,
emitsRuntimeArg: undefined,
emitsTypeArg: undefined,
emitsTypeNums: 0,
exposeRuntimeArg: undefined,
exposeTypeArg: undefined,
importSectionEndOffset: 0,
notOnTopTypeExports: [],
propsAssignName: undefined,
propsRuntimeArg: undefined,
propsTypeArg: undefined,
typeBindings: [],
withDefaultsArg: undefined,
};
}

const bypassDefineComponent = lang === 'js' || lang === 'jsx';
const vueVersion = vueCompilerOptions.target ?? 3;
const vueLibName = getVueLibraryName(vueVersion);
Expand Down
@@ -0,0 +1,6 @@
<template>
<slot name="no-bind"></slot>
<slot :num="123"></slot>
<slot name="named-slot" str="str"></slot>
<slot name="vbind" v-bind="{ num: 123, str: 'str' }"></slot>
</template>