Skip to content

Commit

Permalink
fix: template context properties error missing
Browse files Browse the repository at this point in the history
close #1205
  • Loading branch information
johnsoncodehk committed Apr 21, 2022
1 parent 8052316 commit 864ec76
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
22 changes: 21 additions & 1 deletion packages/vue-code-gen/src/generators/template.ts
Expand Up @@ -1758,7 +1758,7 @@ export function generate(
prefix: string,
suffix: string,
) {
walkInterpolationFragment(ts, prefix + mapCode + suffix, (frag, fragOffset) => {
walkInterpolationFragment(ts, prefix + mapCode + suffix, (frag, fragOffset, lastCtxAccess) => {
if (fragOffset === undefined) {
tsCodeGen.addText(frag);
}
Expand All @@ -1776,6 +1776,26 @@ export function generate(
fragOffset = 0;
}
if (sourceOffset !== undefined && data !== undefined) {
// fix https://github.com/johnsoncodehk/volar/issues/1205
if (lastCtxAccess && data.capabilities.diagnostic) {
tsCodeGen.addMapping2({
data: {
vueTag: data.vueTag,
capabilities: {
diagnostic: true,
},
},
mode: SourceMaps.Mode.Totally,
sourceRange: {
start: sourceOffset + fragOffset,
end: sourceOffset + fragOffset + lastCtxAccess.varLength,
},
mappedRange: {
start: tsCodeGen.getText().length - lastCtxAccess.ctxText.length,
end: tsCodeGen.getText().length + lastCtxAccess.varLength,
},
});
}
writeCode(
frag,
{
Expand Down
25 changes: 18 additions & 7 deletions packages/vue-code-gen/src/transform.ts
Expand Up @@ -4,7 +4,7 @@ import type * as ts from 'typescript/lib/tsserverlibrary';
export function walkInterpolationFragment(
ts: typeof import('typescript/lib/tsserverlibrary'),
code: string,
cb: (fragment: string, offset: number | undefined) => void,
cb: (fragment: string, offset: number | undefined, beforeCtxAccess?: { ctxText: string, varLength: number; }) => void,
localVars: Record<string, number>,
identifiers: Set<string>,
) {
Expand Down Expand Up @@ -40,6 +40,8 @@ export function walkInterpolationFragment(
ctxVars = ctxVars.sort((a, b) => a.offset - b.offset);
// localVarOffsets = localVarOffsets.sort((a, b) => a - b);

let lastCtxAccess: { ctxText: string, varLength: number; } | undefined;

if (ctxVars.length) {

if (ctxVars[0].isShorthand) {
Expand All @@ -52,23 +54,32 @@ export function walkInterpolationFragment(

for (let i = 0; i < ctxVars.length - 1; i++) {

cb('__VLS_ctx.', undefined);

writeCtxAccess(ctxVars[i].text.length);
if (ctxVars[i + 1].isShorthand) {
cb(code.substring(ctxVars[i].offset, ctxVars[i + 1].offset + ctxVars[i + 1].text.length), ctxVars[i].offset);
cb(code.substring(ctxVars[i].offset, ctxVars[i + 1].offset + ctxVars[i + 1].text.length), ctxVars[i].offset, lastCtxAccess);
cb(': ', undefined);
}
else {
cb(code.substring(ctxVars[i].offset, ctxVars[i + 1].offset), ctxVars[i].offset);
cb(code.substring(ctxVars[i].offset, ctxVars[i + 1].offset), ctxVars[i].offset, lastCtxAccess);
}
lastCtxAccess = undefined;
}

cb('__VLS_ctx.', undefined);
cb(code.substring(ctxVars[ctxVars.length - 1].offset), ctxVars[ctxVars.length - 1].offset);
writeCtxAccess(ctxVars[ctxVars.length - 1].text.length);
cb(code.substring(ctxVars[ctxVars.length - 1].offset), ctxVars[ctxVars.length - 1].offset, lastCtxAccess);
lastCtxAccess = undefined;
}
else {
cb(code, 0);
}

function writeCtxAccess(varLength: number) {
cb('__VLS_ctx.', undefined);
lastCtxAccess = {
ctxText: '__VLS_ctx.',
varLength,
};
}
}

function walkIdentifiers(
Expand Down
23 changes: 22 additions & 1 deletion packages/vue-typescript/src/use/useSfcTemplateScript.ts
Expand Up @@ -230,11 +230,32 @@ export function useSfcTemplateScript(
walkInterpolationFragment(
ts,
bindText,
(frag, fragOffset) => {
(frag, fragOffset, lastCtxAccess) => {
if (fragOffset === undefined) {
codeGen.addText(frag);
}
else {
// fix https://github.com/johnsoncodehk/volar/issues/1205
if (lastCtxAccess) {
codeGen.addMapping2({
data: {
vueTag: 'style',
vueTagIndex: i,
capabilities: {
diagnostic: true,
},
},
mode: SourceMaps.Mode.Totally,
sourceRange: {
start: cssBind.start + fragOffset,
end: cssBind.start + fragOffset + lastCtxAccess.varLength,
},
mappedRange: {
start: codeGen.getText().length - lastCtxAccess.ctxText.length,
end: codeGen.getText().length + lastCtxAccess.varLength,
},
});
}
codeGen.addCode(
frag,
{
Expand Down

0 comments on commit 864ec76

Please sign in to comment.