Skip to content

Commit dfce548

Browse files
Enable isolated declarations for @tailwindcss/language-service (#1348)
Just some code cleanup (also a bugfix revealed by doing this)
1 parent 03ad23d commit dfce548

27 files changed

+146
-136
lines changed

packages/tailwindcss-language-server/src/testing/index.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as os from 'node:os'
33
import * as fs from 'node:fs/promises'
44
import * as path from 'node:path'
55
import * as proc from 'node:child_process'
6-
import dedent from 'dedent'
6+
import dedent, { type Dedent } from 'dedent'
77

88
export interface TestUtils<TestInput extends Record<string, any>> {
99
/** The "cwd" for this test */
@@ -160,8 +160,8 @@ async function installDependenciesIn(dir: string) {
160160
})
161161
}
162162

163-
export const css = dedent
164-
export const scss = dedent
165-
export const html = dedent
166-
export const js = dedent
167-
export const json = dedent
163+
export const css: Dedent = dedent
164+
export const scss: Dedent = dedent
165+
export const html: Dedent = dedent
166+
export const js: Dedent = dedent
167+
export const json: Dedent = dedent

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ function classNameToAst(
212212
obj = rule
213213
}
214214

215-
return cssObjToAst(obj, state.modules.postcss)
215+
return cssObjToAst(obj, state.modules.postcss.module)
216216
}
217217

218218
function appendPseudosToSelector(selector: string, pseudos: string[]): string | null {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2230,7 +2230,7 @@ export async function doComplete(
22302230
document: TextDocument,
22312231
position: Position,
22322232
context?: CompletionContext,
2233-
) {
2233+
): Promise<CompletionList | null> {
22342234
if (state === null) return { items: [], isIncomplete: false }
22352235

22362236
const result =

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export function visit(
239239
nodes: postcss.AnyNode[],
240240
cb: (node: postcss.AnyNode, path: postcss.AnyNode[]) => void,
241241
path: postcss.AnyNode[] = [],
242-
) {
242+
): void {
243243
for (let child of nodes) {
244244
path = [...path, child]
245245
cb(child, path)

packages/tailwindcss-language-service/src/metadata/extensions.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,5 @@ let templateExtensions = [
7272
'rs',
7373
]
7474

75-
export const IS_SCRIPT_SOURCE = new RegExp(`\\.(${scriptExtensions.join('|')})$`)
76-
export const IS_TEMPLATE_SOURCE = new RegExp(`\\.(${templateExtensions.join('|')})$`)
75+
export const IS_SCRIPT_SOURCE: RegExp = new RegExp(`\\.(${scriptExtensions.join('|')})$`)
76+
export const IS_TEMPLATE_SOURCE: RegExp = new RegExp(`\\.(${templateExtensions.join('|')})$`)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Range } from 'vscode-languageserver'
22

3-
export function absoluteRange(range: Range, reference?: Range) {
3+
export function absoluteRange(range: Range, reference?: Range): Range {
44
return {
55
start: {
66
line: (reference?.start.line || 0) + range.start.line,

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export function braceLevel(text: string) {
1+
export function braceLevel(text: string): number {
22
let count = 0
33

44
for (let i = text.length - 1; i >= 0; i--) {
@@ -10,7 +10,7 @@ export function braceLevel(text: string) {
1010
return count
1111
}
1212

13-
export function parenLevel(text: string) {
13+
export function parenLevel(text: string): number {
1414
let count = 0
1515

1616
for (let i = text.length - 1; i >= 0; i--) {
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Plugin } from 'postcss'
1+
import type { Plugin, PluginCreator } from 'postcss'
22
import parseValue from 'postcss-value-parser'
33
import { inGamut } from 'culori'
44
import { formatColor, getColorFromValue } from './color'
@@ -16,51 +16,55 @@ export function getEquivalentColor(value: string): string {
1616
return formatColor(color)
1717
}
1818

19-
export function equivalentColorValues({ comments }: { comments: Comment[] }): Plugin {
20-
return {
21-
postcssPlugin: 'plugin',
22-
Declaration(decl) {
23-
if (!allowedFunctions.some((fn) => decl.value.includes(fn))) {
24-
return
25-
}
26-
27-
parseValue(decl.value).walk((node) => {
28-
if (node.type !== 'function') {
29-
return true
19+
export const equivalentColorValues: PluginCreator<any> = Object.assign(
20+
({ comments }: { comments: Comment[] }): Plugin => {
21+
return {
22+
postcssPlugin: 'plugin',
23+
Declaration(decl) {
24+
if (!allowedFunctions.some((fn) => decl.value.includes(fn))) {
25+
return
3026
}
3127

32-
if (node.value === 'var') {
33-
return true
34-
}
28+
parseValue(decl.value).walk((node) => {
29+
if (node.type !== 'function') {
30+
return true
31+
}
3532

36-
if (!allowedFunctions.includes(node.value)) {
37-
return false
38-
}
33+
if (node.value === 'var') {
34+
return true
35+
}
3936

40-
const values = node.nodes.filter((n) => n.type === 'word').map((n) => n.value)
41-
if (values.length < 3) {
42-
return false
43-
}
37+
if (!allowedFunctions.includes(node.value)) {
38+
return false
39+
}
4440

45-
let color = `${node.value}(${values.join(' ')})`
41+
const values = node.nodes.filter((n) => n.type === 'word').map((n) => n.value)
42+
if (values.length < 3) {
43+
return false
44+
}
4645

47-
let equivalent = getEquivalentColor(color)
46+
let color = `${node.value}(${values.join(' ')})`
4847

49-
if (equivalent === color) {
50-
return false
51-
}
48+
let equivalent = getEquivalentColor(color)
5249

53-
comments.push({
54-
index:
55-
decl.source.start.offset +
56-
`${decl.prop}${decl.raws.between}`.length +
57-
node.sourceEndIndex,
58-
value: equivalent,
59-
})
50+
if (equivalent === color) {
51+
return false
52+
}
6053

61-
return false
62-
})
63-
},
64-
}
65-
}
66-
equivalentColorValues.postcss = true
54+
comments.push({
55+
index:
56+
decl.source.start.offset +
57+
`${decl.prop}${decl.raws.between}`.length +
58+
node.sourceEndIndex,
59+
value: equivalent,
60+
})
61+
62+
return false
63+
})
64+
},
65+
}
66+
},
67+
{
68+
postcss: true as const,
69+
},
70+
)

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import type { State } from './state'
66
import { cssLanguages } from './languages'
77
import { getLanguageBoundaries } from './getLanguageBoundaries'
88

9-
function getCssLanguages(state: State) {
9+
function getCssLanguages(state: State): string[] {
1010
const userCssLanguages = Object.keys(state.editor.userLanguages).filter((lang) =>
1111
cssLanguages.includes(state.editor.userLanguages[lang]),
1212
)
1313

1414
return [...cssLanguages, ...userCssLanguages]
1515
}
1616

17-
export function isCssLanguage(state: State, lang: string) {
17+
export function isCssLanguage(state: State, lang: string): boolean {
1818
return getCssLanguages(state).indexOf(lang) !== -1
1919
}
2020

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ function parse(obj, parent, postcss) {
120120
}
121121
}
122122

123-
export function cssObjToAst(obj, postcss) {
123+
import type { Postcss, Root } from 'postcss'
124+
125+
export function cssObjToAst(obj: any, postcss: Postcss): Root {
124126
var root = postcss.root()
125127
parse(obj, root, postcss)
126128
return root

packages/tailwindcss-language-service/src/util/estimated-class-size.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { segment } from './segment'
66
* This is meant to be a lower bound, as the actual size of a class can vary
77
* depending on the actual CSS properties and values, configured theme, etc…
88
*/
9-
export function estimatedClassSize(className: string) {
9+
export function estimatedClassSize(className: string): number {
1010
let size = 0
1111

1212
// We estimate the size using the following structure which gives a reasonable

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { State } from './state'
22
import dlv from 'dlv'
33

4-
export function flagEnabled(state: State, flag: string) {
4+
export function flagEnabled(state: State, flag: string): boolean {
55
if (state.featureFlags.future.includes(flag)) {
66
return state.config.future === 'all' || dlv(state.config, ['future', flag], false)
77
}

packages/tailwindcss-language-service/src/util/format-bytes.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const UNITS = ['byte', 'kilobyte', 'megabyte', 'gigabyte', 'terabyte', 'petabyte']
22

3-
export function formatBytes(n: number) {
3+
export function formatBytes(n: number): string {
44
let i = n == 0 ? 0 : Math.floor(Math.log(n) / Math.log(1000))
55
return new Intl.NumberFormat('en', {
66
notation: 'compact',

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ let vueLexer = moo.states(vueStates)
139139

140140
let cache = new Cache<string, LanguageBoundary[] | null>({ max: 25, maxAge: 1000 })
141141

142-
export function clearLanguageBoundariesCache() {
142+
export function clearLanguageBoundariesCache(): void {
143143
cache.clear()
144144
}
145145

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { addPixelEquivalentsToValue } from './pixelEquivalents'
44
import { addEquivalents } from './equivalents'
55
import { addThemeValues, inlineThemeValues } from './rewriting'
66

7-
export function bigSign(bigIntValue) {
7+
export function bigSign(bigIntValue: number | bigint): number {
88
// @ts-ignore
99
return (bigIntValue > 0n) - (bigIntValue < 0n)
1010
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { State } from '../util/state'
2-
import { type Range } from 'vscode-languageserver'
2+
import type { Range } from 'vscode-languageserver'
33
import type { TextDocument } from 'vscode-languageserver-textdocument'
44
import { getLanguageBoundaries } from '../util/getLanguageBoundaries'
55
import { isCssDoc } from '../util/css'

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

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { EditorState } from './state'
22

3-
export const htmlLanguages = [
3+
export const htmlLanguages: string[] = [
44
'aspnetcorerazor',
55
'astro',
66
'astro-markdown',
@@ -36,7 +36,7 @@ export const htmlLanguages = [
3636
'twig',
3737
]
3838

39-
export const cssLanguages = [
39+
export const cssLanguages: string[] = [
4040
'css',
4141
'less',
4242
'postcss',
@@ -47,7 +47,7 @@ export const cssLanguages = [
4747
'tailwindcss',
4848
]
4949

50-
export const jsLanguages = [
50+
export const jsLanguages: string[] = [
5151
'javascript',
5252
'javascriptreact',
5353
'reason',
@@ -58,16 +58,21 @@ export const jsLanguages = [
5858
'glimmer-ts',
5959
]
6060

61-
export const specialLanguages = ['vue', 'svelte']
61+
export const specialLanguages: string[] = ['vue', 'svelte']
6262

63-
export const languages = [...cssLanguages, ...htmlLanguages, ...jsLanguages, ...specialLanguages]
63+
export const languages: string[] = [
64+
...cssLanguages,
65+
...htmlLanguages,
66+
...jsLanguages,
67+
...specialLanguages,
68+
]
6469

6570
const semicolonlessLanguages = ['sass', 'sugarss', 'stylus']
6671

6772
export function isSemicolonlessCssLanguage(
6873
languageId: string,
6974
userLanguages: EditorState['userLanguages'] = {},
70-
) {
75+
): boolean {
7176
return (
7277
semicolonlessLanguages.includes(languageId) ||
7378
semicolonlessLanguages.includes(userLanguages[languageId])

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import moo from 'moo'
2-
import { lazy } from './lazy'
2+
import { Lazy, lazy } from './lazy'
33

44
const classAttributeStates: () => { [x: string]: moo.Rules } = () => ({
55
doubleClassList: {
@@ -66,7 +66,7 @@ const simpleClassAttributeStates: { [x: string]: moo.Rules } = {
6666
},
6767
}
6868

69-
export const getClassAttributeLexer = lazy(() => {
69+
export const getClassAttributeLexer: Lazy<moo.Lexer> = lazy(() => {
7070
let supportsNegativeLookbehind = true
7171
try {
7272
new RegExp('(?<!)')
@@ -90,7 +90,7 @@ export const getClassAttributeLexer = lazy(() => {
9090
return moo.states(simpleClassAttributeStates)
9191
})
9292

93-
export const getComputedClassAttributeLexer = lazy(() => {
93+
export const getComputedClassAttributeLexer: Lazy<moo.Lexer> = lazy(() => {
9494
let supportsNegativeLookbehind = true
9595
try {
9696
new RegExp('(?<!)')

0 commit comments

Comments
 (0)