Skip to content

Commit

Permalink
fix: detection of slots in script-less SFC (#2113)
Browse files Browse the repository at this point in the history
Co-authored-by: johnsoncodehk <johnsoncodehk@gmail.com>
  • Loading branch information
elevatebart and johnsoncodehk committed Jan 5, 2023
1 parent a21f38d commit b6dac62
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
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>

0 comments on commit b6dac62

Please sign in to comment.