Skip to content

Commit

Permalink
fix: createApp() initialData type missing in template
Browse files Browse the repository at this point in the history
close #1444
  • Loading branch information
johnsoncodehk committed Jun 13, 2022
1 parent 0b1ec91 commit d708323
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 19 deletions.
6 changes: 3 additions & 3 deletions packages/vue-test-workspace/typeChecks/petite-vue.html
Expand Up @@ -3,6 +3,7 @@

createApp({ foo: 1, exactType })

// @ts-expect-error
const bar = 123;

function createApp(_ = {}) { }
Expand All @@ -15,9 +16,8 @@
}}

<div id="app" v-scope="{ open: true, elseOpen: true }">
{{ exactType(open, {} as boolean) }}
<!-- do not fix for now -->
<!-- {{ exactType(open, {} as boolean) }} -->
{{ exactType(elseOpen, {} as boolean) }}
<button @click="elseOpen = !elseOpen">toggle else</button>
</div>

<button @click="elseOpen = !elseOpen">toggle else</button>
6 changes: 1 addition & 5 deletions packages/vue-typescript/src/plugins/file-html.ts
Expand Up @@ -20,11 +20,7 @@ export default function (): VueLanguagePlugin {
// style block intellisense support by vscode-html-language-features
if (matchText.startsWith('<script') && matchText.indexOf('src=') === -1) {
// monkey fix replace `<script type="module">` to `<script setup>`
codeGen.addCode2(matchText
.replace('type="module"', 'setup ')
.replace('type=\'module\'', 'setup ')
.replace('type=module', 'setup ')
, match.index, undefined);
codeGen.addCode2(matchText, match.index, undefined);
}
codeGen.addText('\n\n');
content = content.substring(0, match.index) + ' '.repeat(matchText.length) + content.substring(match.index + matchText.length);
Expand Down
71 changes: 71 additions & 0 deletions packages/vue-typescript/src/plugins/petite-vue-script.ts
@@ -0,0 +1,71 @@
import { CodeGen } from '@volar/code-gen';
import { EmbeddedFileMappingData } from '@volar/vue-code-gen';
import { EmbeddedFile, VueLanguagePlugin } from '../sourceFile';

export default function (): VueLanguagePlugin {

return {

getEmbeddedFilesCount(fileName, sfc) {
return fileName.endsWith('.html') ? 1 : 0;
},

getEmbeddedFile(fileName, sfc, i) {

if (sfc.script) {

const createVueArg = sfc.script.content.match(/createApp\s*\(([\s\S]*?)\)/);
const codeGen = new CodeGen<EmbeddedFileMappingData>();

codeGen.addCode2(sfc.script.content, 0, {
vueTag: 'script',
capabilities: {
basic: true,
references: true,
definitions: true,
diagnostic: true,
rename: true,
completion: true,
semanticTokens: true,
},
});

codeGen.addText('\n\n');
codeGen.addText(`const __VLS_scope = `);
if (createVueArg && createVueArg.index !== undefined) {
codeGen.addCode2(createVueArg[1], createVueArg.index + createVueArg[0].indexOf(createVueArg[1]), {
vueTag: 'script',
capabilities: {
references: true,
definitions: true,
rename: true,
},
});
}
else {
codeGen.addText('{}');
}
codeGen.addText(';\n');
codeGen.addText('declare const __VLS_export: new () => typeof __VLS_scope;\n');
codeGen.addText('export default __VLS_export;\n');

const file: EmbeddedFile = {
fileName: fileName + '.__VLS_script.' + sfc.script.lang,
content: codeGen.getText(),
capabilities: {
diagnostics: true,
foldingRanges: false,
formatting: false,
documentSymbol: false,
codeActions: true,
inlayHints: true,
},
isTsHostFile: true,
mappings: codeGen.getMappings(),
};

return file;
}
},
};
}
Expand Up @@ -5,7 +5,7 @@ export default function (): VueLanguagePlugin {

return {

getEmbeddedFilesCount(sfc) {
getEmbeddedFilesCount(fileName, sfc) {
return sfc.customBlocks.length;
},

Expand Down
2 changes: 1 addition & 1 deletion packages/vue-typescript/src/plugins/vue-sfc-scripts.ts
Expand Up @@ -5,7 +5,7 @@ export default function (): VueLanguagePlugin {

return {

getEmbeddedFilesCount(sfc) {
getEmbeddedFilesCount(fileName, sfc) {
return 2;
},

Expand Down
4 changes: 2 additions & 2 deletions packages/vue-typescript/src/plugins/vue-sfc-styles.ts
Expand Up @@ -5,8 +5,8 @@ export default function (): VueLanguagePlugin {

return {

getEmbeddedFilesCount(sfc) {
return sfc.styles.length;
getEmbeddedFilesCount(fileName, sfc) {
return fileName.endsWith('.html') ? 0 : sfc.styles.length;
},

getEmbeddedFile(fileName, sfc, i) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-typescript/src/plugins/vue-sfc-template.ts
Expand Up @@ -5,7 +5,7 @@ export default function (): VueLanguagePlugin {

return {

getEmbeddedFilesCount(sfc) {
getEmbeddedFilesCount(fileName, sfc) {
return sfc.template ? 1 : 0;
},

Expand Down
4 changes: 2 additions & 2 deletions packages/vue-typescript/src/plugins/vue-typescript-scripts.ts
Expand Up @@ -17,8 +17,8 @@ export default function (

return {

getEmbeddedFilesCount(sfc) {
return 2;
getEmbeddedFilesCount(fileName, sfc) {
return fileName.endsWith('.html') ? 0 : 2;
},

getEmbeddedFile(fileName, sfc, i) {
Expand Down
Expand Up @@ -25,7 +25,7 @@ export default function (

return {

getEmbeddedFilesCount(sfc) {
getEmbeddedFilesCount(fileName, sfc) {
return 3;
},

Expand Down
8 changes: 5 additions & 3 deletions packages/vue-typescript/src/sourceFile.ts
Expand Up @@ -15,6 +15,7 @@ import { parseCssVars } from './utils/parseCssVars';
import useVueFilePlugin from './plugins/file-vue';
import useMdFilePlugin from './plugins/file-md';
import useHtmlFilePlugin from './plugins/file-html';
import usePetiteVueScriptPlugin from './plugins/petite-vue-script';
import useHtmlPlugin from './plugins/vue-template-html';
import usePugPlugin from './plugins/vue-template-pug';
import useVueSfcStyles from './plugins/vue-sfc-styles';
Expand Down Expand Up @@ -45,7 +46,7 @@ export interface VueLanguagePlugin {

// TODO: compileHtmlTemplateToAst

getEmbeddedFilesCount?(sfc: Sfc): number;
getEmbeddedFilesCount?(fileName: string, sfc: Sfc): number;

getEmbeddedFile?(fileName: string, sfc: Sfc, i: number): EmbeddedFile | undefined;
}
Expand Down Expand Up @@ -231,6 +232,7 @@ export function createSourceFile(
useVueFilePlugin(),
useMdFilePlugin(),
useHtmlFilePlugin(),
usePetiteVueScriptPlugin(),
useHtmlPlugin(),
usePugPlugin(),
useVueSfcStyles(),
Expand Down Expand Up @@ -276,7 +278,7 @@ export function createSourceFile(
});
const pluginEmbeddeds = plugins.map(plugin => {
if (plugin.getEmbeddedFilesCount && plugin.getEmbeddedFile) {
const embeddedsCount = computed(() => plugin.getEmbeddedFilesCount!(sfc));
const embeddedsCount = computed(() => plugin.getEmbeddedFilesCount!(fileName, sfc));
const embeddeds = computed(() => {
const computeds: ComputedRef<Embedded | undefined>[] = [];
for (let i = 0; i < embeddedsCount.value; i++) {
Expand Down Expand Up @@ -518,7 +520,7 @@ export function createSourceFile(
getCompiledVue: untrack(() => file2VueSourceMap.value),
getSfcTemplateLanguageCompiled: untrack(() => computedHtmlTemplate.value),
getSfcVueTemplateCompiled: untrack(() => templateAstCompiled.value),
getScriptFileName: untrack(() => fileName + '.' + scriptLang.value),
getScriptFileName: untrack(() => fileName.endsWith('.html') ? fileName + '.__VLS_script.' + scriptLang.value : fileName + '.' + scriptLang.value),
getDescriptor: untrack(() => unref(sfc)),
getScriptAst: untrack(() => scriptAst.value),
getScriptSetupAst: untrack(() => scriptSetupAst.value),
Expand Down

0 comments on commit d708323

Please sign in to comment.