Skip to content

Commit ce3d3d7

Browse files
committed
fix: use typescript to lookup files, config file options
1 parent 59d6640 commit ce3d3d7

10 files changed

+122
-95
lines changed

package.json

-3
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,7 @@
9797
"dependencies": {
9898
"cssbeautify": "^0.3.1",
9999
"esbuild": "^0.9.3",
100-
"import-from": "^3.0.0",
101-
"locate-path": "^6.0.0",
102100
"match-sorter": "^6.3.0",
103-
"resolve-from": "^5.0.0",
104101
"twind": "^0.16.6",
105102
"typescript": "^4.1.0",
106103
"typescript-template-language-service-decorator": "^2.2.0",

src/configuration.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export interface TwindPluginConfiguration {
22
readonly tags: ReadonlyArray<string>
3-
readonly configFile?: string;
4-
readonly debug: boolean
3+
readonly configFile?: string
4+
readonly debug?: boolean
55
// Readonly validate: boolean;
66
// readonly lint: { [key: string]: any };
77
// readonly emmet: { [key: string]: any };
@@ -24,18 +24,24 @@ export class ConfigurationManager {
2424
return this._configuration
2525
}
2626

27-
private _configuration: TwindPluginConfiguration = ConfigurationManager.defaultConfiguration
27+
private _configuration: TwindPluginConfiguration = {
28+
...ConfigurationManager.defaultConfiguration,
29+
tags: [...ConfigurationManager.defaultConfiguration.tags],
30+
}
2831

2932
public updateFromPluginConfig(config: Partial<TwindPluginConfiguration> = {}): void {
30-
const mergedConfig = {
33+
const { tags, ...mergedConfig } = {
3134
...ConfigurationManager.defaultConfiguration,
3235
...config,
3336
}
3437

3538
this._configuration = {
3639
...mergedConfig,
3740
debug: 'true' == String(mergedConfig.debug),
41+
tags: this._configuration.tags,
3842
}
43+
;(this._configuration.tags as string[]).length = 0
44+
;(this._configuration.tags as string[]).push(...tags)
3945

4046
for (const listener of this._configUpdatedListeners) {
4147
listener()

src/language-service.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,10 @@ export class TwindLanguageService implements TemplateLanguageService {
194194
): ts.QuickInfo | undefined {
195195
const rules = parse(context.text, context.toOffset(position))
196196

197-
const rule = rules.map((rule) => rule.value).join(' ')
197+
const rule = rules
198+
.filter((rule) => !/\${x*}/.test(rule.value))
199+
.map((rule) => rule.value)
200+
.join(' ')
198201

199202
if (!rule) {
200203
return undefined
@@ -264,8 +267,8 @@ export class TwindLanguageService implements TemplateLanguageService {
264267
? undefined
265268
: {
266269
messageText: `Missing utility class`,
267-
start: rule.loc.start,
268-
length: rule.loc.end - rule.loc.start,
270+
start: rule.spans[0].start,
271+
length: rule.spans[rule.spans.length - 1].end - rule.spans[0].start,
269272
file: context.node.getSourceFile(),
270273
category: this.typescript.DiagnosticCategory.Error,
271274
code: ErrorCodes.UNKNOWN_DIRECTIVE,
@@ -278,7 +281,8 @@ export class TwindLanguageService implements TemplateLanguageService {
278281
(variant) =>
279282
!(
280283
this._twind.completions.variants.has(variant.value) ||
281-
(variant.value[0] == '[' && variant.value[variant.value.length - 2] == ']')
284+
(variant.value[0] == '[' && variant.value[variant.value.length - 2] == ']') ||
285+
/\${x*}/.test(variant.value)
282286
),
283287
)
284288
.map(

src/load.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import type * as ts from 'typescript/lib/tsserverlibrary'
2+
13
import * as Path from 'path'
24
import Module from 'module'
35

46
import { buildSync } from 'esbuild'
5-
import findUp from 'find-up'
6-
import locatePath from 'locate-path'
77

88
import type { Configuration } from 'twind'
99

@@ -24,12 +24,18 @@ const TAILWIND_CONFIG_FILES = [
2424
// TODO use typescript to check files
2525
// this.typescript.server.toNormalizedPath(fileName)
2626
// info.project.containsFile()
27-
export const findConfig = (cwd = process.cwd()): string | undefined =>
28-
locatePath.sync(TWIND_CONFIG_FILES.map((file) => Path.resolve(cwd, 'config', file))) ||
29-
locatePath.sync(TWIND_CONFIG_FILES.map((file) => Path.resolve(cwd, 'src', file))) ||
30-
locatePath.sync(TWIND_CONFIG_FILES.map((file) => Path.resolve(cwd, 'public', file))) ||
31-
findUp.sync(TWIND_CONFIG_FILES, { cwd }) ||
32-
findUp.sync(TAILWIND_CONFIG_FILES, { cwd })
27+
export const findConfig = (project: ts.server.Project, cwd = process.cwd()): string | undefined => {
28+
const locatePath = (files: string[]) =>
29+
files.map((file) => Path.resolve(cwd, file)).find((file) => project.fileExists(file))
30+
31+
return (
32+
locatePath(TWIND_CONFIG_FILES.map((file) => Path.join('config', file))) ||
33+
locatePath(TWIND_CONFIG_FILES.map((file) => Path.join('src', file))) ||
34+
locatePath(TWIND_CONFIG_FILES.map((file) => Path.join('public', file))) ||
35+
locatePath(TWIND_CONFIG_FILES) ||
36+
locatePath(TAILWIND_CONFIG_FILES)
37+
)
38+
}
3339

3440
export const loadFile = <T>(file: string, cwd = process.cwd()): T => {
3541
const result = buildSync({
@@ -107,10 +113,11 @@ export const loadConfig = (configFile: string, cwd = process.cwd()): Configurati
107113
}
108114

109115
export const getConfig = (
116+
project: ts.server.Project,
110117
cwd = process.cwd(),
111118
configFile?: string,
112119
): Configuration & { configFile: string | undefined } => {
113-
configFile ??= findConfig(cwd)
120+
configFile ??= findConfig(project, cwd)
114121

115122
return {
116123
...(configFile ? loadConfig(Path.resolve(cwd, configFile), cwd) : {}),

src/plugin.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { ConfigurationManager, TwindPluginConfiguration } from './configuration'
1010
import { TwindLanguageService } from './language-service'
1111
import { StandardTemplateSourceHelper } from './source-helper'
1212
import { LanguageServiceLogger } from './logger'
13-
import { getSubstitutions } from './substituter'
1413
import { getSourceMatchers } from './source-matcher'
1514

1615
// https://github.com/microsoft/typescript-template-language-service-decorator/blob/main/src/standard-template-source-helper.ts#L75
@@ -68,7 +67,7 @@ export class TwindPlugin {
6867

6968
const ttls = new TwindLanguageService(this.typescript, info, this._configManager, this._logger)
7069

71-
const templateSettings = getTemplateSettings(this._configManager, this._logger)
70+
const templateSettings = getTemplateSettings(this._configManager)
7271

7372
const helper = new StandardTemplateSourceHelper(
7473
this.typescript,
@@ -163,18 +162,14 @@ export class TwindPlugin {
163162
}
164163
}
165164

166-
export function getTemplateSettings(
167-
configManager: ConfigurationManager,
168-
logger: LanguageServiceLogger,
169-
): TemplateSettings {
165+
export function getTemplateSettings(configManager: ConfigurationManager): TemplateSettings {
170166
return {
171167
get tags() {
172168
return configManager.config.tags
173169
},
174170
enableForStringWithSubstitutions: true,
175-
getSubstitutions(templateString, spans): string {
176-
logger.log(`getSubstitutions: ${JSON.stringify(templateString)} (${JSON.stringify(spans)})`)
177-
return getSubstitutions(/* templateString, spans */)
171+
getSubstitution(templateString, start, end) {
172+
return `\${${'x'.repeat(end - start - 3)}}`
178173
},
179174
}
180175
}

src/source-helper.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type {
99
import type ScriptSourceHelper from 'typescript-template-language-service-decorator/lib/script-source-helper'
1010
import type TemplateSourceHelper from 'typescript-template-language-service-decorator/lib/template-source-helper'
1111
import { relative } from 'typescript-template-language-service-decorator/lib/nodes'
12-
import { match, Matcher, Predicates } from './match'
12+
import { match, Matcher } from './match'
1313

1414
class PlaceholderSubstituter {
1515
public static replacePlaceholders(

src/substituter.ts

-7
This file was deleted.

0 commit comments

Comments
 (0)