Skip to content

Commit d35cb82

Browse files
authored
fix(language-core): optimize matching of scoped class and v-bind() (#4679)
1 parent c7fe96f commit d35cb82

File tree

3 files changed

+17
-48
lines changed

3 files changed

+17
-48
lines changed
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { clearComments } from './parseCssVars';
22

3-
const cssClassNameReg = /(?=([\.]{1}[a-zA-Z_]+[\w\_\-]*)[\s\.\,\+\{\>#\:]{1})/g;
3+
const cssClassNameReg = /(?=(\.[a-z_][-\w]*)[\s.,+~>:#[{])/gi;
44

55
export function* parseCssClassNames(styleContent: string) {
66
styleContent = clearComments(styleContent);
77
const matches = styleContent.matchAll(cssClassNameReg);
88
for (const match of matches) {
9-
if (match.index !== undefined) {
10-
const matchText = match[1];
11-
if (matchText !== undefined) {
12-
yield { offset: match.index, text: matchText };
13-
}
9+
const matchText = match[1];
10+
if (matchText) {
11+
yield { offset: match.index, text: matchText };
1412
}
1513
}
1614
}

packages/language-core/lib/utils/parseCssVars.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
// https://github.com/vuejs/core/blob/main/packages/compiler-sfc/src/cssVars.ts#L47-L61
22

3-
const vBindCssVarReg = /\bv-bind\(\s*(?:'([^']+)'|"([^"]+)"|([^'"][^)]*))\s*\)/g;
3+
const vBindCssVarReg = /\bv-bind\(\s*(?:'([^']+)'|"([^"]+)"|([a-z_]\w*))\s*\)/gi;
44
const commentReg1 = /\/\*([\s\S]*?)\*\//g;
55
const commentReg2 = /\/\/([\s\S]*?)\n/g;
66

77
export function* parseCssVars(styleContent: string) {
88
styleContent = clearComments(styleContent);
99
const matchs = styleContent.matchAll(vBindCssVarReg);
1010
for (const match of matchs) {
11-
if (match.index !== undefined) {
12-
const matchText = match[1] ?? match[2] ?? match[3];
13-
if (matchText !== undefined) {
14-
const offset = match.index + styleContent.slice(match.index).indexOf(matchText);
15-
yield { offset, text: matchText };
16-
}
11+
const matchText = match.slice(1).find(t => t);
12+
if (matchText) {
13+
const offset = match.index + styleContent.slice(match.index).indexOf(matchText);
14+
yield { offset, text: matchText };
1715
}
1816
}
1917
}

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

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ describe('Renaming', async () => {
185185
.bar { color: v-bind(foo|); }
186186
.bar { color: v-bind('foo'); }
187187
.bar { color: v-bind("foo"); }
188-
.bar { color: v-bind(foo + foo); }
189188
.bar { color: v-bind('foo + foo'); }
190189
.bar { color: v-bind("foo + foo"); }
191190
.bar { color: v-bind(); }
@@ -199,32 +198,6 @@ describe('Renaming', async () => {
199198
{
200199
"changes": {
201200
"file://\${testWorkspacePath}/fixture.vue": [
202-
{
203-
"newText": "bar",
204-
"range": {
205-
"end": {
206-
"character": 34,
207-
"line": 10,
208-
},
209-
"start": {
210-
"character": 31,
211-
"line": 10,
212-
},
213-
},
214-
},
215-
{
216-
"newText": "bar",
217-
"range": {
218-
"end": {
219-
"character": 28,
220-
"line": 10,
221-
},
222-
"start": {
223-
"character": 25,
224-
"line": 10,
225-
},
226-
},
227-
},
228201
{
229202
"newText": "bar",
230203
"range": {
@@ -256,11 +229,11 @@ describe('Renaming', async () => {
256229
"range": {
257230
"end": {
258231
"character": 35,
259-
"line": 12,
232+
"line": 11,
260233
},
261234
"start": {
262235
"character": 32,
263-
"line": 12,
236+
"line": 11,
264237
},
265238
},
266239
},
@@ -269,11 +242,11 @@ describe('Renaming', async () => {
269242
"range": {
270243
"end": {
271244
"character": 29,
272-
"line": 12,
245+
"line": 11,
273246
},
274247
"start": {
275248
"character": 26,
276-
"line": 12,
249+
"line": 11,
277250
},
278251
},
279252
},
@@ -282,11 +255,11 @@ describe('Renaming', async () => {
282255
"range": {
283256
"end": {
284257
"character": 35,
285-
"line": 11,
258+
"line": 10,
286259
},
287260
"start": {
288261
"character": 32,
289-
"line": 11,
262+
"line": 10,
290263
},
291264
},
292265
},
@@ -295,11 +268,11 @@ describe('Renaming', async () => {
295268
"range": {
296269
"end": {
297270
"character": 29,
298-
"line": 11,
271+
"line": 10,
299272
},
300273
"start": {
301274
"character": 26,
302-
"line": 11,
275+
"line": 10,
303276
},
304277
},
305278
},

0 commit comments

Comments
 (0)