Skip to content

Commit 86d93aa

Browse files
authored
Ignore commented out code (#599)
1 parent 92410c1 commit 86d93aa

File tree

9 files changed

+64
-19
lines changed

9 files changed

+64
-19
lines changed

packages/tailwindcss-language-service/src/codeActions/provideInvalidApplyCodeActions.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ import { cssObjToAst } from '../util/cssObjToAst'
1717
import { dset } from 'dset'
1818
import selectorParser from 'postcss-selector-parser'
1919
import { flatten } from '../util/array'
20+
import { getTextWithoutComments } from '../util/doc'
2021

2122
export async function provideInvalidApplyCodeActions(
2223
state: State,
2324
params: CodeActionParams,
2425
diagnostic: InvalidApplyDiagnostic
2526
): Promise<CodeAction[]> {
2627
let document = state.editor.documents.get(params.textDocument.uri)
27-
let documentText = document.getText()
28+
let documentText = getTextWithoutComments(document, 'css')
2829
let cssRange: Range
2930
let cssText = documentText
3031
const { postcss } = state.modules
@@ -47,7 +48,7 @@ export async function provideInvalidApplyCodeActions(
4748
.filter((b) => b.type === 'css')
4849
.find(({ range }) => isWithinRange(diagnostic.range.start, range))?.range
4950
if (!cssRange) return []
50-
cssText = document.getText(cssRange)
51+
cssText = getTextWithoutComments(document, 'css', cssRange)
5152
}
5253

5354
try {

packages/tailwindcss-language-service/src/diagnostics/getInvalidConfigPathDiagnostics.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { closest } from '../util/closest'
1010
import { absoluteRange } from '../util/absoluteRange'
1111
import { combinations } from '../util/combinations'
1212
import dlv from 'dlv'
13+
import { getTextWithoutComments } from '../util/doc'
1314

1415
function pathToString(path: string | string[]): string {
1516
if (typeof path === 'string') return path
@@ -177,7 +178,7 @@ export function getInvalidConfigPathDiagnostics(
177178
}
178179

179180
ranges.forEach((range) => {
180-
let text = document.getText(range)
181+
let text = getTextWithoutComments(document, 'css', range)
181182
let matches = findAll(
182183
/(?<prefix>\s|^)(?<helper>config|theme)\((?<quote>['"])(?<key>[^)]+)\k<quote>[^)]*\)/g,
183184
text

packages/tailwindcss-language-service/src/diagnostics/getInvalidScreenDiagnostics.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { findAll, indexToPosition } from '../util/find'
77
import { closest } from '../util/closest'
88
import { absoluteRange } from '../util/absoluteRange'
99
import dlv from 'dlv'
10+
import { getTextWithoutComments } from '../util/doc'
1011

1112
export function getInvalidScreenDiagnostics(
1213
state: State,
@@ -28,7 +29,7 @@ export function getInvalidScreenDiagnostics(
2829
}
2930

3031
ranges.forEach((range) => {
31-
let text = document.getText(range)
32+
let text = getTextWithoutComments(document, 'css', range)
3233
let matches = findAll(/(?:\s|^)@screen\s+(?<screen>[^\s{]+)/g, text)
3334

3435
matches.forEach((match) => {

packages/tailwindcss-language-service/src/diagnostics/getInvalidTailwindDirectiveDiagnostics.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { findAll, indexToPosition } from '../util/find'
77
import * as semver from '../util/semver'
88
import { closest } from '../util/closest'
99
import { absoluteRange } from '../util/absoluteRange'
10+
import { getTextWithoutComments } from '../util/doc'
1011

1112
export function getInvalidTailwindDirectiveDiagnostics(
1213
state: State,
@@ -42,7 +43,7 @@ export function getInvalidTailwindDirectiveDiagnostics(
4243
let hasVariantsDirective = state.jit && semver.gte(state.version, '2.1.99')
4344

4445
ranges.forEach((range) => {
45-
let text = document.getText(range)
46+
let text = getTextWithoutComments(document, 'css', range)
4647
let matches = findAll(regex, text)
4748

4849
let valid = [

packages/tailwindcss-language-service/src/diagnostics/getInvalidVariantDiagnostics.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { findAll, indexToPosition } from '../util/find'
77
import { closest } from '../util/closest'
88
import { absoluteRange } from '../util/absoluteRange'
99
import * as semver from '../util/semver'
10+
import { getTextWithoutComments } from '../util/doc'
1011

1112
export function getInvalidVariantDiagnostics(
1213
state: State,
@@ -38,7 +39,7 @@ export function getInvalidVariantDiagnostics(
3839
}
3940

4041
ranges.forEach((range) => {
41-
let text = document.getText(range)
42+
let text = getTextWithoutComments(document, 'css', range)
4243
let matches = findAll(/(?:\s|^)@variants\s+(?<variants>[^{]+)/g, text)
4344

4445
matches.forEach((match) => {

packages/tailwindcss-language-service/src/hoverProvider.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { validateApply } from './util/validateApply'
88
import { getClassNameParts } from './util/getClassNameAtPosition'
99
import * as jit from './util/jit'
1010
import { validateConfigPath } from './diagnostics/getInvalidConfigPathDiagnostics'
11+
import { getTextWithoutComments } from './util/doc'
1112

1213
export async function doHover(
1314
state: State,
@@ -23,10 +24,7 @@ export async function doHover(
2324
function provideCssHelperHover(state: State, document: TextDocument, position: Position): Hover {
2425
if (!isCssContext(state, document, position)) return null
2526

26-
const line = document.getText({
27-
start: { line: position.line, character: 0 },
28-
end: { line: position.line + 1, character: 0 },
29-
})
27+
const line = getTextWithoutComments(document, 'css').split('\n')[position.line]
3028

3129
const match = line.match(/(?<helper>theme|config)\((?<quote>['"])(?<key>[^)]+)\k<quote>[^)]*\)/)
3230

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { TextDocument, Range } from 'vscode-languageserver'
2+
3+
export function getTextWithoutComments(
4+
doc: TextDocument,
5+
type: 'html' | 'js' | 'jsx' | 'css',
6+
range?: Range
7+
): string
8+
export function getTextWithoutComments(text: string, type: 'html' | 'js' | 'jsx' | 'css'): string
9+
export function getTextWithoutComments(
10+
docOrText: TextDocument | string,
11+
type: 'html' | 'js' | 'jsx' | 'css',
12+
range?: Range
13+
): string {
14+
let text = typeof docOrText === 'string' ? docOrText : docOrText.getText(range)
15+
16+
if (type === 'js' || type === 'jsx') {
17+
return text.replace(/\/\*.*?\*\//gs, replace).replace(/\/\/.*?$/gms, replace)
18+
}
19+
20+
if (type === 'css') {
21+
return text.replace(/\/\*.*?\*\//gs, replace)
22+
}
23+
24+
return text.replace(/<!--.*?-->/gs, replace)
25+
}
26+
27+
function replace(match: string): string {
28+
return match.replace(/./gs, (char) => (char === '\n' ? '\n' : ' '))
29+
}

packages/tailwindcss-language-service/src/util/find.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { resolveRange } from './resolveRange'
1212
import dlv from 'dlv'
1313
import { rangesEqual } from './rangesEqual'
1414
import Regex from 'becke-ch--regex--s0-0-v1--base--pl--lib'
15+
import { getTextWithoutComments } from './doc'
1516

1617
export function findAll(re: RegExp, str: string): RegExpMatchArray[] {
1718
let match: RegExpMatchArray
@@ -74,7 +75,7 @@ export async function findClassNamesInRange(
7475
state: State,
7576
doc: TextDocument,
7677
range?: Range,
77-
mode?: 'html' | 'css',
78+
mode?: 'html' | 'css' | 'jsx',
7879
includeCustom: boolean = true
7980
): Promise<DocumentClassName[]> {
8081
const classLists = await findClassListsInRange(state, doc, range, mode, includeCustom)
@@ -90,7 +91,7 @@ export async function findClassNamesInDocument(
9091
}
9192

9293
export function findClassListsInCssRange(doc: TextDocument, range?: Range): DocumentClassList[] {
93-
const text = doc.getText(range)
94+
const text = getTextWithoutComments(doc, 'css', range)
9495
const matches = findAll(
9596
/(@apply\s+)(?<classList>[^;}]+?)(?<important>\s*!important)?\s*[;}]/g,
9697
text
@@ -184,9 +185,10 @@ export function matchClassAttributes(text: string, attributes: string[]): RegExp
184185
export async function findClassListsInHtmlRange(
185186
state: State,
186187
doc: TextDocument,
188+
type: 'html' | 'js' | 'jsx',
187189
range?: Range
188190
): Promise<DocumentClassList[]> {
189-
const text = doc.getText(range)
191+
const text = getTextWithoutComments(doc, type, range)
190192

191193
const matches = matchClassAttributes(
192194
text,
@@ -291,14 +293,14 @@ export async function findClassListsInRange(
291293
state: State,
292294
doc: TextDocument,
293295
range?: Range,
294-
mode?: 'html' | 'css',
296+
mode?: 'html' | 'css' | 'jsx',
295297
includeCustom: boolean = true
296298
): Promise<DocumentClassList[]> {
297299
let classLists: DocumentClassList[]
298300
if (mode === 'css') {
299301
classLists = findClassListsInCssRange(doc, range)
300302
} else {
301-
classLists = await findClassListsInHtmlRange(state, doc, range)
303+
classLists = await findClassListsInHtmlRange(state, doc, mode, range)
302304
}
303305
return dedupeClassLists([
304306
...classLists,
@@ -322,7 +324,9 @@ export async function findClassListsInDocument(
322324
...(await Promise.all(
323325
boundaries
324326
.filter((b) => b.type === 'html' || b.type === 'jsx')
325-
.map(({ range }) => findClassListsInHtmlRange(state, doc, range))
327+
.map(({ type, range }) =>
328+
findClassListsInHtmlRange(state, doc, type === 'html' ? 'html' : 'jsx', range)
329+
)
326330
)),
327331
...boundaries
328332
.filter((b) => b.type === 'css')
@@ -354,7 +358,7 @@ export function findHelperFunctionsInRange(
354358
doc: TextDocument,
355359
range?: Range
356360
): DocumentHelperFunction[] {
357-
const text = doc.getText(range)
361+
const text = getTextWithoutComments(doc, 'css', range)
358362
const matches = findAll(
359363
/(?<before>^|\s)(?<helper>theme|config)\((?:(?<single>')([^']+)'|(?<double>")([^"]+)")[^)]*\)/gm,
360364
text
@@ -408,8 +412,10 @@ export async function findClassNameAtPosition(
408412

409413
if (isCssContext(state, doc, position)) {
410414
classNames = await findClassNamesInRange(state, doc, searchRange, 'css')
411-
} else if (isHtmlContext(state, doc, position) || isJsxContext(state, doc, position)) {
415+
} else if (isHtmlContext(state, doc, position)) {
412416
classNames = await findClassNamesInRange(state, doc, searchRange, 'html')
417+
} else if (isJsxContext(state, doc, position)) {
418+
classNames = await findClassNamesInRange(state, doc, searchRange, 'jsx')
413419
}
414420

415421
if (classNames.length === 0) {

packages/tailwindcss-language-service/src/util/getLanguageBoundaries.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { indexToPosition } from './find'
55
import { isJsDoc } from './js'
66
import moo from 'moo'
77
import Cache from 'tmp-cache'
8+
import { getTextWithoutComments } from './doc'
89

910
export type LanguageBoundary = { type: 'html' | 'js' | 'css' | string; range: Range }
1011

@@ -113,17 +114,23 @@ export function getLanguageBoundaries(
113114
return cachedBoundaries
114115
}
115116

117+
let isJs = isJsDoc(state, doc)
118+
116119
let defaultType = isVueDoc(doc)
117120
? 'none'
118-
: isHtmlDoc(state, doc) || isJsDoc(state, doc) || isSvelteDoc(doc)
121+
: isHtmlDoc(state, doc) || isSvelteDoc(doc)
119122
? 'html'
123+
: isJs
124+
? 'jsx'
120125
: null
121126

122127
if (defaultType === null) {
123128
cache.set(cacheKey, null)
124129
return null
125130
}
126131

132+
text = getTextWithoutComments(text, isJs ? 'js' : 'html')
133+
127134
let lexer = defaultType === 'none' ? vueLexer : defaultLexer
128135
lexer.reset(text)
129136

0 commit comments

Comments
 (0)