1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
diff --git a/packages/tailwindcss-language-service/src/util/doc.ts b/packages/tailwindcss-language-service/src/util/doc.ts
index 810f79dd537907593beff2046a23834e2d54e042..7c291ff933bdaf3eed03de42a1c2ef8d6c85c214 100644
--- a/packages/tailwindcss-language-service/src/util/doc.ts
+++ b/packages/tailwindcss-language-service/src/util/doc.ts
@@ -1,4 +1,5 @@
import type { TextDocument, Range } from 'vscode-languageserver'
+import moo from 'moo'
export function getTextWithoutComments(
doc: TextDocument,
@@ -14,7 +15,7 @@ ): string {
let text = typeof docOrText === 'string' ? docOrText : docOrText.getText(range)
if (type === 'js' || type === 'jsx') {
- return text.replace(/\/\*.*?\*\//gs, replace).replace(/\/\/.*?$/gms, replace)
+ return getJsWithoutComments(text)
}
if (type === 'css') {
@@ -27,3 +28,35 @@
function replace(match: string): string {
return match.replace(/./gs, (char) => (char === '\n' ? '\n' : ' '))
}
+
+let jsLexer: moo.Lexer
+
+function getJsWithoutComments(text: string): string {
+ if (!jsLexer) {
+ jsLexer = moo.states({
+ main: {
+ commentLine: /\/\/.*?$/,
+ commentBlock: { match: /\/\*[^]*?\*\//, lineBreaks: true },
+ stringDouble: /"(?:[^"\\]|\\.)*"/,
+ stringSingle: /'(?:[^'\\]|\\.)*'/,
+ stringBacktick: /`(?:[^`\\]|\\.)*`/,
+ other: { match: /[^]/, lineBreaks: true },
+ },
+ })
+ }
+
+ let str = ''
+ jsLexer.reset(text)
+
+ for (let token of jsLexer) {
+ if (token.type === 'commentLine') {
+ str += ' '.repeat(token.value.length)
+ } else if (token.type === 'commentBlock') {
+ str += token.value.replace(/./g, ' ')
+ } else {
+ str += token.value
+ }
+ }
+
+ return str
+}
|