Skip to content

Commit fce47f0

Browse files
committed
refactor(language-core): simplify codegen of style class properties
1 parent ac5f446 commit fce47f0

File tree

9 files changed

+59
-97
lines changed

9 files changed

+59
-97
lines changed

packages/language-core/lib/codegen/script/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges';
66
import type { Code, Sfc, VueCompilerOptions } from '../../types';
77
import { codeFeatures } from '../codeFeatures';
88
import { generateGlobalTypes, getGlobalTypesFileName } from '../globalTypes';
9-
import { generateStyleModules } from '../style/modules';
109
import type { TemplateCodegenContext } from '../template/context';
1110
import { endOfLine, generateSfcBlockSection, newLine } from '../utils';
1211
import { generateComponentSelf } from './componentSelf';
@@ -134,9 +133,6 @@ export function* generateScript(options: ScriptCodegenOptions): Generator<Code,
134133
yield* generateComponentSelf(options, ctx, templateCodegenCtx);
135134
}
136135

137-
// #4788
138-
yield* generateStyleModules(options, ctx);
139-
140136
if (options.edited) {
141137
yield `type __VLS_IntrinsicElementsCompletion = __VLS_IntrinsicElements${endOfLine}`;
142138
}

packages/language-core/lib/codegen/script/template.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Code } from '../../types';
22
import { hyphenateTag } from '../../utils/shared';
33
import { codeFeatures } from '../codeFeatures';
4+
import { generateStyleModules } from '../style/modules';
45
import { generateStyleScopedClasses } from '../style/scopedClasses';
56
import { type TemplateCodegenContext, createTemplateCodegenContext } from '../template/context';
67
import { generateInterpolation } from '../template/interpolation';
@@ -116,6 +117,7 @@ function* generateTemplateBody(
116117
): Generator<Code> {
117118
yield* generateStyleScopedClasses(options, templateCodegenCtx);
118119
yield* generateStyleScopedClassReferences(templateCodegenCtx, true);
120+
yield* generateStyleModules(options);
119121
yield* generateCssVars(options, templateCodegenCtx);
120122

121123
if (options.templateCodegen) {

packages/language-core/lib/codegen/style/classProperty.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,21 @@ export function* generateClassProperty(
66
styleIndex: number,
77
classNameWithDot: string,
88
offset: number,
9-
propertyType: string,
10-
optional: boolean
9+
propertyType: string
1110
): Generator<Code> {
12-
yield `${newLine} & { `;
11+
yield `${newLine} & { '`;
1312
yield [
1413
'',
1514
'style_' + styleIndex,
1615
offset,
1716
codeFeatures.navigation,
1817
];
19-
yield `'`;
2018
yield [
2119
classNameWithDot.slice(1),
2220
'style_' + styleIndex,
2321
offset + 1,
2422
codeFeatures.navigation,
2523
];
26-
yield `'`;
27-
yield [
28-
'',
29-
'style_' + styleIndex,
30-
offset + classNameWithDot.length,
31-
codeFeatures.navigationWithoutRename,
32-
];
33-
yield `${optional ? '?' : ''}: ${propertyType}`;
24+
yield `': ${propertyType}`;
3425
yield ` }`;
3526
}

packages/language-core/lib/codegen/style/modules.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import type { Code } from '../../types';
22
import { codeFeatures } from '../codeFeatures';
33
import type { ScriptCodegenOptions } from '../script';
4-
import type { ScriptCodegenContext } from '../script/context';
54
import { endOfLine, newLine } from '../utils';
65
import { generateClassProperty } from './classProperty';
76

87
export function* generateStyleModules(
9-
options: ScriptCodegenOptions,
10-
ctx: ScriptCodegenContext
8+
options: ScriptCodegenOptions
119
): Generator<Code> {
1210
const styles = options.sfc.styles.map((style, i) => [style, i] as const).filter(([style]) => style.module);
1311
if (!styles.length && !options.scriptSetupRanges?.useCssModule.length) {
@@ -27,14 +25,13 @@ export function* generateStyleModules(
2725
codeFeatures.withoutHighlight
2826
];
2927
}
30-
yield `: Record<string, string> & ${ctx.localTypes.PrettifyLocal}<{}`;
28+
yield `: Record<string, string> & __VLS_PrettifyGlobal<{}`;
3129
for (const className of style.classNames) {
3230
yield* generateClassProperty(
3331
i,
3432
className.text,
3533
className.offset,
36-
'string',
37-
false
34+
'string'
3835
);
3936
}
4037
yield `>${endOfLine}`;

packages/language-core/lib/codegen/style/scopedClasses.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,33 @@ export function* generateStyleScopedClasses(
88
options: ScriptCodegenOptions,
99
ctx: TemplateCodegenContext
1010
): Generator<Code> {
11+
const option = options.vueCompilerOptions.experimentalResolveStyleCssClasses;
12+
const styles = options.sfc.styles
13+
.map((style, i) => [style, i] as const)
14+
.filter(([style]) => option === 'always' || (option === 'scoped' && style.scoped));
15+
if (!styles.length) {
16+
return;
17+
}
18+
1119
const firstClasses = new Set<string>();
1220
yield `type __VLS_StyleScopedClasses = {}`;
13-
for (let i = 0; i < options.sfc.styles.length; i++) {
14-
const style = options.sfc.styles[i];
15-
const option = options.vueCompilerOptions.experimentalResolveStyleCssClasses;
16-
if (option === 'always' || (option === 'scoped' && style.scoped)) {
17-
for (const className of style.classNames) {
18-
if (firstClasses.has(className.text)) {
19-
ctx.scopedClasses.push({
20-
source: 'style_' + i,
21-
className: className.text.slice(1),
22-
offset: className.offset + 1
23-
});
24-
continue;
25-
}
26-
firstClasses.add(className.text);
27-
yield* generateClassProperty(
28-
i,
29-
className.text,
30-
className.offset,
31-
'boolean',
32-
true
33-
);
21+
for (const [style, i] of styles) {
22+
for (const className of style.classNames) {
23+
if (firstClasses.has(className.text)) {
24+
ctx.scopedClasses.push({
25+
source: 'style_' + i,
26+
className: className.text.slice(1),
27+
offset: className.offset + 1
28+
});
29+
continue;
3430
}
31+
firstClasses.add(className.text);
32+
yield* generateClassProperty(
33+
i,
34+
className.text,
35+
className.offset,
36+
'boolean'
37+
);
3538
}
3639
}
3740
yield endOfLine;

packages/language-core/lib/codegen/template/styleScopedClasses.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,18 @@ export function* generateStyleScopedClassReferences(
2121
yield `']} */${endOfLine}`;
2222
}
2323
for (const { source, className, offset } of ctx.scopedClasses) {
24-
yield `/** @type {__VLS_StyleScopedClasses[`;
25-
yield [
26-
'',
27-
source,
28-
offset - (withDot ? 1 : 0),
29-
ctx.codeFeatures.navigation,
30-
];
31-
yield `'`;
32-
24+
yield `/** @type {__VLS_StyleScopedClasses['`;
25+
if (withDot) {
26+
yield [
27+
'',
28+
source,
29+
offset - 1,
30+
ctx.codeFeatures.navigation,
31+
];
32+
}
3333
// fix https://github.com/vuejs/language-tools/issues/4537
3434
yield* escapeString(source, className, offset, ['\\', '\'']);
35-
yield `'`;
36-
yield [
37-
'',
38-
source,
39-
offset + className.length,
40-
ctx.codeFeatures.navigationWithoutRename,
41-
];
42-
yield `]} */${endOfLine}`;
35+
yield `']} */${endOfLine}`;
4336
}
4437

4538
function* escapeString(source: string, className: string, offset: number, escapeTargets: string[]): Generator<Code> {

packages/language-server/tests/renaming.spec.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,25 +104,25 @@ describe('Renaming', async () => {
104104
"newText": "bar",
105105
"range": {
106106
"end": {
107-
"character": 8,
108-
"line": 7,
107+
"character": 28,
108+
"line": 2,
109109
},
110110
"start": {
111-
"character": 5,
112-
"line": 7,
111+
"character": 25,
112+
"line": 2,
113113
},
114114
},
115115
},
116116
{
117117
"newText": "bar",
118118
"range": {
119119
"end": {
120-
"character": 28,
121-
"line": 2,
120+
"character": 8,
121+
"line": 7,
122122
},
123123
"start": {
124-
"character": 25,
125-
"line": 2,
124+
"character": 5,
125+
"line": 7,
126126
},
127127
},
128128
},
@@ -743,25 +743,25 @@ describe('Renaming', async () => {
743743
"newText": "stylus",
744744
"range": {
745745
"end": {
746-
"character": 23,
747-
"line": 15,
746+
"character": 22,
747+
"line": 8,
748748
},
749749
"start": {
750-
"character": 19,
751-
"line": 15,
750+
"character": 18,
751+
"line": 8,
752752
},
753753
},
754754
},
755755
{
756756
"newText": "stylus",
757757
"range": {
758758
"end": {
759-
"character": 22,
760-
"line": 8,
759+
"character": 23,
760+
"line": 15,
761761
},
762762
"start": {
763-
"character": 18,
764-
"line": 8,
763+
"character": 19,
764+
"line": 15,
765765
},
766766
},
767767
},

test-workspace/tsc/passedFixtures/vue3/#3688/main.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<template>
44
{{ () => {
5-
exactType({} as __VLS_StyleScopedClasses, {} as { 'foo'?: boolean });
5+
exactType({} as __VLS_StyleScopedClasses, {} as { 'foo': boolean });
66
} }}
77
</template>
88

test-workspace/tsc/passedFixtures/vue3/#4788/main.vue

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)