Skip to content

Ignore commented out code #599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ import { cssObjToAst } from '../util/cssObjToAst'
import { dset } from 'dset'
import selectorParser from 'postcss-selector-parser'
import { flatten } from '../util/array'
import { getTextWithoutComments } from '../util/doc'

export async function provideInvalidApplyCodeActions(
state: State,
params: CodeActionParams,
diagnostic: InvalidApplyDiagnostic
): Promise<CodeAction[]> {
let document = state.editor.documents.get(params.textDocument.uri)
let documentText = document.getText()
let documentText = getTextWithoutComments(document, 'css')
let cssRange: Range
let cssText = documentText
const { postcss } = state.modules
Expand All @@ -47,7 +48,7 @@ export async function provideInvalidApplyCodeActions(
.filter((b) => b.type === 'css')
.find(({ range }) => isWithinRange(diagnostic.range.start, range))?.range
if (!cssRange) return []
cssText = document.getText(cssRange)
cssText = getTextWithoutComments(document, 'css', cssRange)
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { closest } from '../util/closest'
import { absoluteRange } from '../util/absoluteRange'
import { combinations } from '../util/combinations'
import dlv from 'dlv'
import { getTextWithoutComments } from '../util/doc'

function pathToString(path: string | string[]): string {
if (typeof path === 'string') return path
Expand Down Expand Up @@ -177,7 +178,7 @@ export function getInvalidConfigPathDiagnostics(
}

ranges.forEach((range) => {
let text = document.getText(range)
let text = getTextWithoutComments(document, 'css', range)
let matches = findAll(
/(?<prefix>\s|^)(?<helper>config|theme)\((?<quote>['"])(?<key>[^)]+)\k<quote>[^)]*\)/g,
text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { findAll, indexToPosition } from '../util/find'
import { closest } from '../util/closest'
import { absoluteRange } from '../util/absoluteRange'
import dlv from 'dlv'
import { getTextWithoutComments } from '../util/doc'

export function getInvalidScreenDiagnostics(
state: State,
Expand All @@ -28,7 +29,7 @@ export function getInvalidScreenDiagnostics(
}

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

matches.forEach((match) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { findAll, indexToPosition } from '../util/find'
import * as semver from '../util/semver'
import { closest } from '../util/closest'
import { absoluteRange } from '../util/absoluteRange'
import { getTextWithoutComments } from '../util/doc'

export function getInvalidTailwindDirectiveDiagnostics(
state: State,
Expand Down Expand Up @@ -42,7 +43,7 @@ export function getInvalidTailwindDirectiveDiagnostics(
let hasVariantsDirective = state.jit && semver.gte(state.version, '2.1.99')

ranges.forEach((range) => {
let text = document.getText(range)
let text = getTextWithoutComments(document, 'css', range)
let matches = findAll(regex, text)

let valid = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { findAll, indexToPosition } from '../util/find'
import { closest } from '../util/closest'
import { absoluteRange } from '../util/absoluteRange'
import * as semver from '../util/semver'
import { getTextWithoutComments } from '../util/doc'

export function getInvalidVariantDiagnostics(
state: State,
Expand Down Expand Up @@ -38,7 +39,7 @@ export function getInvalidVariantDiagnostics(
}

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

matches.forEach((match) => {
Expand Down
6 changes: 2 additions & 4 deletions packages/tailwindcss-language-service/src/hoverProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { validateApply } from './util/validateApply'
import { getClassNameParts } from './util/getClassNameAtPosition'
import * as jit from './util/jit'
import { validateConfigPath } from './diagnostics/getInvalidConfigPathDiagnostics'
import { getTextWithoutComments } from './util/doc'

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

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

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

Expand Down
29 changes: 29 additions & 0 deletions packages/tailwindcss-language-service/src/util/doc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { TextDocument, Range } from 'vscode-languageserver'

export function getTextWithoutComments(
doc: TextDocument,
type: 'html' | 'js' | 'jsx' | 'css',
range?: Range
): string
export function getTextWithoutComments(text: string, type: 'html' | 'js' | 'jsx' | 'css'): string
export function getTextWithoutComments(
docOrText: TextDocument | string,
type: 'html' | 'js' | 'jsx' | 'css',
range?: Range
): string {
let text = typeof docOrText === 'string' ? docOrText : docOrText.getText(range)

if (type === 'js' || type === 'jsx') {
return text.replace(/\/\*.*?\*\//gs, replace).replace(/\/\/.*?$/gms, replace)
}

if (type === 'css') {
return text.replace(/\/\*.*?\*\//gs, replace)
}

return text.replace(/<!--.*?-->/gs, replace)
}

function replace(match: string): string {
return match.replace(/./gs, (char) => (char === '\n' ? '\n' : ' '))
}
22 changes: 14 additions & 8 deletions packages/tailwindcss-language-service/src/util/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { resolveRange } from './resolveRange'
import dlv from 'dlv'
import { rangesEqual } from './rangesEqual'
import Regex from 'becke-ch--regex--s0-0-v1--base--pl--lib'
import { getTextWithoutComments } from './doc'

export function findAll(re: RegExp, str: string): RegExpMatchArray[] {
let match: RegExpMatchArray
Expand Down Expand Up @@ -74,7 +75,7 @@ export async function findClassNamesInRange(
state: State,
doc: TextDocument,
range?: Range,
mode?: 'html' | 'css',
mode?: 'html' | 'css' | 'jsx',
includeCustom: boolean = true
): Promise<DocumentClassName[]> {
const classLists = await findClassListsInRange(state, doc, range, mode, includeCustom)
Expand All @@ -90,7 +91,7 @@ export async function findClassNamesInDocument(
}

export function findClassListsInCssRange(doc: TextDocument, range?: Range): DocumentClassList[] {
const text = doc.getText(range)
const text = getTextWithoutComments(doc, 'css', range)
const matches = findAll(
/(@apply\s+)(?<classList>[^;}]+?)(?<important>\s*!important)?\s*[;}]/g,
text
Expand Down Expand Up @@ -184,9 +185,10 @@ export function matchClassAttributes(text: string, attributes: string[]): RegExp
export async function findClassListsInHtmlRange(
state: State,
doc: TextDocument,
type: 'html' | 'js' | 'jsx',
range?: Range
): Promise<DocumentClassList[]> {
const text = doc.getText(range)
const text = getTextWithoutComments(doc, type, range)

const matches = matchClassAttributes(
text,
Expand Down Expand Up @@ -291,14 +293,14 @@ export async function findClassListsInRange(
state: State,
doc: TextDocument,
range?: Range,
mode?: 'html' | 'css',
mode?: 'html' | 'css' | 'jsx',
includeCustom: boolean = true
): Promise<DocumentClassList[]> {
let classLists: DocumentClassList[]
if (mode === 'css') {
classLists = findClassListsInCssRange(doc, range)
} else {
classLists = await findClassListsInHtmlRange(state, doc, range)
classLists = await findClassListsInHtmlRange(state, doc, mode, range)
}
return dedupeClassLists([
...classLists,
Expand All @@ -322,7 +324,9 @@ export async function findClassListsInDocument(
...(await Promise.all(
boundaries
.filter((b) => b.type === 'html' || b.type === 'jsx')
.map(({ range }) => findClassListsInHtmlRange(state, doc, range))
.map(({ type, range }) =>
findClassListsInHtmlRange(state, doc, type === 'html' ? 'html' : 'jsx', range)
)
)),
...boundaries
.filter((b) => b.type === 'css')
Expand Down Expand Up @@ -354,7 +358,7 @@ export function findHelperFunctionsInRange(
doc: TextDocument,
range?: Range
): DocumentHelperFunction[] {
const text = doc.getText(range)
const text = getTextWithoutComments(doc, 'css', range)
const matches = findAll(
/(?<before>^|\s)(?<helper>theme|config)\((?:(?<single>')([^']+)'|(?<double>")([^"]+)")[^)]*\)/gm,
text
Expand Down Expand Up @@ -408,8 +412,10 @@ export async function findClassNameAtPosition(

if (isCssContext(state, doc, position)) {
classNames = await findClassNamesInRange(state, doc, searchRange, 'css')
} else if (isHtmlContext(state, doc, position) || isJsxContext(state, doc, position)) {
} else if (isHtmlContext(state, doc, position)) {
classNames = await findClassNamesInRange(state, doc, searchRange, 'html')
} else if (isJsxContext(state, doc, position)) {
classNames = await findClassNamesInRange(state, doc, searchRange, 'jsx')
}

if (classNames.length === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { indexToPosition } from './find'
import { isJsDoc } from './js'
import moo from 'moo'
import Cache from 'tmp-cache'
import { getTextWithoutComments } from './doc'

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

Expand Down Expand Up @@ -113,17 +114,23 @@ export function getLanguageBoundaries(
return cachedBoundaries
}

let isJs = isJsDoc(state, doc)

let defaultType = isVueDoc(doc)
? 'none'
: isHtmlDoc(state, doc) || isJsDoc(state, doc) || isSvelteDoc(doc)
: isHtmlDoc(state, doc) || isSvelteDoc(doc)
? 'html'
: isJs
? 'jsx'
: null

if (defaultType === null) {
cache.set(cacheKey, null)
return null
}

text = getTextWithoutComments(text, isJs ? 'js' : 'html')

let lexer = defaultType === 'none' ? vueLexer : defaultLexer
lexer.reset(text)

Expand Down