Skip to content

Commit 710127d

Browse files
committed
feat(macros/define-style): support navigation for css-modules
1 parent 617eb2f commit 710127d

File tree

3 files changed

+42
-37
lines changed

3 files changed

+42
-37
lines changed

packages/macros/src/volar/define-style.ts

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { replaceRange } from 'ts-macro'
1+
import { allCodeFeatures, replaceRange, type Code } from 'ts-macro'
22
import type { DefineStyle, TransformOptions } from '.'
33

44
export function transformDefineStyle(
@@ -20,14 +20,45 @@ export function transformDefineStyle(
2020
`<{`,
2121
...parseCssClassNames(
2222
expression.arguments[0].getText(ast).slice(1, -1),
23-
).map(({ text }) => `\n'${text.slice(1)}': string`),
23+
).flatMap(
24+
({ text, offset }) =>
25+
[
26+
`\n`,
27+
[
28+
`'${text.slice(1)}'`,
29+
`style_${index}`,
30+
expression.arguments.pos + offset + 1,
31+
{ navigation: true },
32+
],
33+
`: string`,
34+
] as Code[],
35+
),
2436
'\n}>',
2537
)
2638
}
2739

2840
addEmbeddedCode(expression, index, options)
2941
}
3042

43+
const commentReg = /(?<=\/\*)[\s\S]*?(?=\*\/)|(?<=\/\/)[\s\S]*?(?=\n)/g
44+
const cssClassNameReg = /(?=(\.[a-z_][-\w]*)[\s.,+~>:#)[{])/gi
45+
const fragmentReg = /(?<=\{)[^{]*(?=(?<!\\);)/g
46+
47+
function parseCssClassNames(css: string) {
48+
for (const reg of [commentReg, fragmentReg]) {
49+
css = css.replace(reg, (match) => ' '.repeat(match.length))
50+
}
51+
const matches = css.matchAll(cssClassNameReg)
52+
const result = []
53+
for (const match of matches) {
54+
const matchText = match[1]
55+
if (matchText) {
56+
result.push({ offset: match.index, text: matchText })
57+
}
58+
}
59+
return result
60+
}
61+
3162
function addEmbeddedCode(
3263
expression: import('typescript').CallExpression,
3364
index: number,
@@ -57,35 +88,9 @@ function addEmbeddedCode(
5788
sourceOffsets: [style.getStart(ast) + 1],
5889
generatedOffsets: [0],
5990
lengths: [styleText.length],
60-
data: {
61-
completion: true,
62-
format: true,
63-
navigation: true,
64-
semantic: true,
65-
structure: true,
66-
verification: true,
67-
},
91+
data: allCodeFeatures,
6892
},
6993
],
7094
embeddedCodes: [],
7195
})
7296
}
73-
74-
const commentReg = /(?<=\/\*)[\s\S]*?(?=\*\/)|(?<=\/\/)[\s\S]*?(?=\n)/g
75-
const cssClassNameReg = /(?=(\.[a-z_][-\w]*)[\s.,+~>:#)[{])/gi
76-
const fragmentReg = /(?<=\{)[^{]*(?=(?<!\\);)/g
77-
78-
function parseCssClassNames(css: string) {
79-
for (const reg of [commentReg, fragmentReg]) {
80-
css = css.replace(reg, (match) => ' '.repeat(match.length))
81-
}
82-
const matches = css.matchAll(cssClassNameReg)
83-
const result = []
84-
for (const match of matches) {
85-
const matchText = match[1]
86-
if (matchText) {
87-
result.push({ offset: match.index, text: matchText })
88-
}
89-
}
90-
return result
91-
}

packages/macros/tests/__snapshots__/fixtures.spec.ts.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ export default function (__MACROS_props) {
250250
`;
251251
252252
exports[`fixtures > ./fixtures/define-style.tsx 1`] = `
253-
"import "vue-jsx-vapor/macros/define-style/0?scopeId=4e9d5cd0&scoped=true&lang.css";import style1 from "vue-jsx-vapor/macros/define-style/1?scopeId=426a859d&scoped=true&lang.module.scss";
253+
"import "vue-jsx-vapor/macros/define-style/0?scopeId=4e9d5cd0&scoped=true&lang.css";import style1 from "vue-jsx-vapor/macros/define-style/1?scopeId=13f8dd3a&scoped=true&lang.module.scss";
254254
import { useSlots as __MACROS_useSlots } from "vue";import "vue-jsx-vapor/macros/define-style/2?scopeId=63a7910c&scoped=false&lang.scss";import { defineComponent, ref } from 'vue'
255255
256256
export const Comp = (__MACROS_props) => {
@@ -265,13 +265,13 @@ export default defineComponent((__MACROS_props) => {
265265
const { default: Default, ...slots } = Object.assign({}, __MACROS_useSlots())
266266
return () => (
267267
<>
268-
<div {...{style:{'--426a859d-color-value': color.value}}} data-v-426a859d="" class={styles.bar}>foo</div>
269-
<div {...{style:{'--426a859d-color-value': color.value}}} data-v-426a859d="" class="bar">
270-
<span data-v-426a859d="">bar</span>
268+
<div {...{style:{'--13f8dd3a-color-value': color.value}}} data-v-13f8dd3a="" class={styles.bar}>foo</div>
269+
<div {...{style:{'--13f8dd3a-color-value': color.value}}} data-v-13f8dd3a="" class="bar">
270+
<span data-v-13f8dd3a="" class={styles['bar-baz']}>bar</span>
271271
</div>
272272
273-
<Default {...{style:{'--426a859d-color-value': color.value}}} data-v-426a859d-s="" />
274-
<slots.title {...{style:{'--426a859d-color-value': color.value}}} data-v-426a859d-s="" />
273+
<Default {...{style:{'--13f8dd3a-color-value': color.value}}} data-v-13f8dd3a-s="" />
274+
<slots.title {...{style:{'--13f8dd3a-color-value': color.value}}} data-v-13f8dd3a-s="" />
275275
</>
276276
)
277277
})

packages/macros/tests/fixtures/define-style.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default defineComponent(() => {
1515
const styles = defineStyle.scss(`
1616
.bar {
1717
color: ${color.value};
18-
.barBaz {
18+
.bar-baz {
1919
background: red;
2020
}
2121
}
@@ -25,7 +25,7 @@ export default defineComponent(() => {
2525
<>
2626
<div class={styles.bar}>foo</div>
2727
<div class="bar">
28-
<span>bar</span>
28+
<span class={styles['bar-baz']}>bar</span>
2929
</div>
3030

3131
<Default />

0 commit comments

Comments
 (0)