Skip to content

Commit

Permalink
fix(language-core): escape \ and ' in className avoid type error
Browse files Browse the repository at this point in the history
  • Loading branch information
uee chart bot committed Jul 29, 2024
1 parent dc6f943 commit b68b34e
Showing 1 changed file with 44 additions and 6 deletions.
50 changes: 44 additions & 6 deletions packages/language-core/lib/codegen/template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,47 @@ export function* generateTemplate(options: TemplateCodegenOptions): Generator<Co
yield `}`;
}

function* escapeString(className: string, offset: number, escapeTargets: string[]): Generator<Code> {
let count = 0;

const currentEscapeTargets = [...escapeTargets];

const firstEscapeTarget = currentEscapeTargets.shift()!;

const splitted = className.split(firstEscapeTarget);

for (let i = 0; i < splitted.length; i++) {
const part = splitted[i];
const partLength = part.length;

if (escapeTargets.length > 0) {
yield* escapeString(part, offset + count, [...currentEscapeTargets]);
} else {
yield [
part,
'template',
offset + count,
ctx.codeFeatures.navigationAndAdditionalCompletion,
];
}

if (i !== splitted.length - 1) {
yield '\\';

yield [
firstEscapeTarget,
'template',
offset + count + partLength,
ctx.codeFeatures.navigationAndAdditionalCompletion,
];

count += partLength + 1;
} else {
count += partLength;
}
}
}

function* generateStyleScopedClasses(): Generator<Code> {
yield `if (typeof __VLS_styleScopedClasses === 'object' && !Array.isArray(__VLS_styleScopedClasses)) {${newLine}`;
for (const offset of ctx.emptyClassOffsets) {
Expand All @@ -99,12 +140,9 @@ export function* generateTemplate(options: TemplateCodegenOptions): Generator<Co
ctx.codeFeatures.navigationWithoutRename,
];
yield `'`;
yield [
className,
'template',
offset,
ctx.codeFeatures.navigationAndAdditionalCompletion,
];

// fix https://github.com/vuejs/language-tools/issues/4537
yield* escapeString(className, offset, ['\\', '\'']);
yield `'`;
yield [
'',
Expand Down

0 comments on commit b68b34e

Please sign in to comment.