Skip to content

Commit

Permalink
fix(language-core): avoid generating zero-length mappings for interpo…
Browse files Browse the repository at this point in the history
…lation edges

Refs: vue-vine/vue-vine#149 (comment)
  • Loading branch information
johnsoncodehk committed Sep 13, 2024
1 parent 814b30f commit d8ed4c0
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions packages/language-core/lib/codegen/template/interpolation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function* generateInterpolation(
isShorthand: boolean,
offset: number,
}[] = [];
for (let [section, offset, onlyError] of forEachInterpolationSegment(
for (let [section, offset, type] of forEachInterpolationSegment(
options.ts,
options.destructuredPropNames,
options.templateRefNames,
Expand All @@ -48,18 +48,24 @@ export function* generateInterpolation(
section = section.substring(-offset);
offset = 0;
}
if (start !== undefined && data !== undefined) {
yield [
section,
'template',
start + offset,
onlyError
? ctx.codeFeatures.verification
: typeof data === 'function' ? data(start + offset) : data,
];
}
else {
yield section;
const shouldSkip = section.length === 0 && (type === 'startText' || type === 'endText');
if (!shouldSkip) {
if (
start !== undefined
&& data
) {
yield [
section,
'template',
start + offset,
type === 'errorMappingOnly'
? ctx.codeFeatures.verification
: typeof data === 'function' ? data(start + offset) : data,
];
}
else {
yield section;
}
}
yield addSuffix;
}
Expand All @@ -79,7 +85,7 @@ export function* forEachInterpolationSegment(
code: string,
offset: number | undefined,
ast: ts.SourceFile
): Generator<[fragment: string, offset: number | undefined, errorMappingOnly?: boolean]> {
): Generator<[fragment: string, offset: number | undefined, type?: 'errorMappingOnly' | 'startText' | 'endText']> {
let ctxVars: {
text: string,
isShorthand: boolean,
Expand Down Expand Up @@ -124,8 +130,8 @@ export function* forEachInterpolationSegment(
yield [code.substring(0, ctxVars[0].offset + ctxVars[0].text.length), 0];
yield [': ', undefined];
}
else {
yield [code.substring(0, ctxVars[0].offset), 0];
else if (ctxVars[0].offset > 0) {
yield [code.substring(0, ctxVars[0].offset), 0, 'startText'];
}

for (let i = 0; i < ctxVars.length - 1; i++) {
Expand All @@ -145,7 +151,9 @@ export function* forEachInterpolationSegment(

const lastVar = ctxVars.at(-1)!;
yield* generateVar(code, destructuredPropNames, templateRefNames, lastVar);
yield [code.substring(lastVar.offset + lastVar.text.length), lastVar.offset + lastVar.text.length];
if (lastVar.offset + lastVar.text.length < code.length) {
yield [code.substring(lastVar.offset + lastVar.text.length), lastVar.offset + lastVar.text.length, 'endText'];
}
}
else {
yield [code, 0];
Expand All @@ -166,10 +174,10 @@ function* generateVar(
isShorthand: boolean,
offset: number,
} = curVar
): Generator<[fragment: string, offset: number | undefined, errorMappingOnly?: boolean]> {
): Generator<[fragment: string, offset: number | undefined, type?: 'errorMappingOnly']> {
// fix https://github.com/vuejs/language-tools/issues/1205
// fix https://github.com/vuejs/language-tools/issues/1264
yield ['', nextVar.offset, true];
yield ['', nextVar.offset, 'errorMappingOnly'];

const isDestructuredProp = destructuredPropNames?.has(curVar.text) ?? false;
const isTemplateRef = templateRefNames?.has(curVar.text) ?? false;
Expand Down

0 comments on commit d8ed4c0

Please sign in to comment.