Skip to content

Commit

Permalink
feat: support scoped css auto-complete for string syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Jan 28, 2022
1 parent 3884d75 commit d15999d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,10 @@ export function useSfcTemplateScript(

/* Style Scoped */
codeGen.addText('/* Style Scoped */\n');
codeGen.addText('declare var __VLS_styleScopedClasses: {\n');
codeGen.addText('type __VLS_StyleScopedClasses = {\n');
const cssScopedMappings = writeCssClassProperties(cssScopedClasses.value, true, 'boolean', true);
codeGen.addText('} & (');
const classNames: string[] = [];
for (const [_, classes] of cssScopedClasses.value) {
for (const [className] of classes) {
classNames.push(className);
}
}
codeGen.addText(classNames.map(name => `'${name}'`).join(' | '));
codeGen.addText(')[];\n');
codeGen.addText(`{\n`);
codeGen.addText('};\n');
codeGen.addText('declare var __VLS_styleScopedClasses: __VLS_StyleScopedClasses | keyof __VLS_StyleScopedClasses | (keyof __VLS_StyleScopedClasses)[];\n');

/* Props */
codeGen.addText(`/* Props */\n`);
Expand All @@ -125,7 +117,6 @@ export function useSfcTemplateScript(
margeCodeGen(codeGen, templateCodeGens.value.codeGen);
}

codeGen.addText(`}\n`);
codeGen.addText(`export default __VLS_slots;\n`);

return {
Expand Down
10 changes: 9 additions & 1 deletion packages/vue-code-gen/src/generators/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,7 @@ export function generate(

let startOffset = prop.value.loc.start.offset;
let tempClassName = '';
let openedBlock = false;

for (const char of (prop.value.loc.source + ' ')) {
if (char.trim() === '' || char === '"' || char === "'") {
Expand All @@ -1435,8 +1436,15 @@ export function generate(
}
}

if (openedBlock) {
tsCodeGen.addText(`}\n`);
}

function addClass(className: string, offset: number) {
tsCodeGen.addText(`// @ts-ignore\n`);
if (!openedBlock) {
tsCodeGen.addText(`if (typeof __VLS_styleScopedClasses === 'object' && !Array.isArray(__VLS_styleScopedClasses)) {\n`);
openedBlock = true;
}
tsCodeGen.addText(`__VLS_styleScopedClasses[`);
writeCodeWithQuotes(
className,
Expand Down

0 comments on commit d15999d

Please sign in to comment.