Skip to content

Commit

Permalink
fix: use typescript to lookup files, config file options
Browse files Browse the repository at this point in the history
  • Loading branch information
sastan committed Mar 21, 2021
1 parent 59d6640 commit ce3d3d7
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 95 deletions.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,7 @@
"dependencies": {
"cssbeautify": "^0.3.1",
"esbuild": "^0.9.3",
"import-from": "^3.0.0",
"locate-path": "^6.0.0",
"match-sorter": "^6.3.0",
"resolve-from": "^5.0.0",
"twind": "^0.16.6",
"typescript": "^4.1.0",
"typescript-template-language-service-decorator": "^2.2.0",
Expand Down
14 changes: 10 additions & 4 deletions src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface TwindPluginConfiguration {
readonly tags: ReadonlyArray<string>
readonly configFile?: string;
readonly debug: boolean
readonly configFile?: string
readonly debug?: boolean
// Readonly validate: boolean;
// readonly lint: { [key: string]: any };
// readonly emmet: { [key: string]: any };
Expand All @@ -24,18 +24,24 @@ export class ConfigurationManager {
return this._configuration
}

private _configuration: TwindPluginConfiguration = ConfigurationManager.defaultConfiguration
private _configuration: TwindPluginConfiguration = {
...ConfigurationManager.defaultConfiguration,
tags: [...ConfigurationManager.defaultConfiguration.tags],
}

public updateFromPluginConfig(config: Partial<TwindPluginConfiguration> = {}): void {
const mergedConfig = {
const { tags, ...mergedConfig } = {
...ConfigurationManager.defaultConfiguration,
...config,
}

this._configuration = {
...mergedConfig,
debug: 'true' == String(mergedConfig.debug),
tags: this._configuration.tags,
}
;(this._configuration.tags as string[]).length = 0
;(this._configuration.tags as string[]).push(...tags)

for (const listener of this._configUpdatedListeners) {
listener()
Expand Down
12 changes: 8 additions & 4 deletions src/language-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ export class TwindLanguageService implements TemplateLanguageService {
): ts.QuickInfo | undefined {
const rules = parse(context.text, context.toOffset(position))

const rule = rules.map((rule) => rule.value).join(' ')
const rule = rules
.filter((rule) => !/\${x*}/.test(rule.value))
.map((rule) => rule.value)
.join(' ')

if (!rule) {
return undefined
Expand Down Expand Up @@ -264,8 +267,8 @@ export class TwindLanguageService implements TemplateLanguageService {
? undefined
: {
messageText: `Missing utility class`,
start: rule.loc.start,
length: rule.loc.end - rule.loc.start,
start: rule.spans[0].start,
length: rule.spans[rule.spans.length - 1].end - rule.spans[0].start,
file: context.node.getSourceFile(),
category: this.typescript.DiagnosticCategory.Error,
code: ErrorCodes.UNKNOWN_DIRECTIVE,
Expand All @@ -278,7 +281,8 @@ export class TwindLanguageService implements TemplateLanguageService {
(variant) =>
!(
this._twind.completions.variants.has(variant.value) ||
(variant.value[0] == '[' && variant.value[variant.value.length - 2] == ']')
(variant.value[0] == '[' && variant.value[variant.value.length - 2] == ']') ||
/\${x*}/.test(variant.value)
),
)
.map(
Expand Down
25 changes: 16 additions & 9 deletions src/load.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type * as ts from 'typescript/lib/tsserverlibrary'

import * as Path from 'path'
import Module from 'module'

import { buildSync } from 'esbuild'
import findUp from 'find-up'
import locatePath from 'locate-path'

import type { Configuration } from 'twind'

Expand All @@ -24,12 +24,18 @@ const TAILWIND_CONFIG_FILES = [
// TODO use typescript to check files
// this.typescript.server.toNormalizedPath(fileName)
// info.project.containsFile()
export const findConfig = (cwd = process.cwd()): string | undefined =>
locatePath.sync(TWIND_CONFIG_FILES.map((file) => Path.resolve(cwd, 'config', file))) ||
locatePath.sync(TWIND_CONFIG_FILES.map((file) => Path.resolve(cwd, 'src', file))) ||
locatePath.sync(TWIND_CONFIG_FILES.map((file) => Path.resolve(cwd, 'public', file))) ||
findUp.sync(TWIND_CONFIG_FILES, { cwd }) ||
findUp.sync(TAILWIND_CONFIG_FILES, { cwd })
export const findConfig = (project: ts.server.Project, cwd = process.cwd()): string | undefined => {
const locatePath = (files: string[]) =>
files.map((file) => Path.resolve(cwd, file)).find((file) => project.fileExists(file))

return (
locatePath(TWIND_CONFIG_FILES.map((file) => Path.join('config', file))) ||
locatePath(TWIND_CONFIG_FILES.map((file) => Path.join('src', file))) ||
locatePath(TWIND_CONFIG_FILES.map((file) => Path.join('public', file))) ||
locatePath(TWIND_CONFIG_FILES) ||
locatePath(TAILWIND_CONFIG_FILES)
)
}

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

export const getConfig = (
project: ts.server.Project,
cwd = process.cwd(),
configFile?: string,
): Configuration & { configFile: string | undefined } => {
configFile ??= findConfig(cwd)
configFile ??= findConfig(project, cwd)

return {
...(configFile ? loadConfig(Path.resolve(cwd, configFile), cwd) : {}),
Expand Down
13 changes: 4 additions & 9 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { ConfigurationManager, TwindPluginConfiguration } from './configuration'
import { TwindLanguageService } from './language-service'
import { StandardTemplateSourceHelper } from './source-helper'
import { LanguageServiceLogger } from './logger'
import { getSubstitutions } from './substituter'
import { getSourceMatchers } from './source-matcher'

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

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

const templateSettings = getTemplateSettings(this._configManager, this._logger)
const templateSettings = getTemplateSettings(this._configManager)

const helper = new StandardTemplateSourceHelper(
this.typescript,
Expand Down Expand Up @@ -163,18 +162,14 @@ export class TwindPlugin {
}
}

export function getTemplateSettings(
configManager: ConfigurationManager,
logger: LanguageServiceLogger,
): TemplateSettings {
export function getTemplateSettings(configManager: ConfigurationManager): TemplateSettings {
return {
get tags() {
return configManager.config.tags
},
enableForStringWithSubstitutions: true,
getSubstitutions(templateString, spans): string {
logger.log(`getSubstitutions: ${JSON.stringify(templateString)} (${JSON.stringify(spans)})`)
return getSubstitutions(/* templateString, spans */)
getSubstitution(templateString, start, end) {
return `\${${'x'.repeat(end - start - 3)}}`
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/source-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
import type ScriptSourceHelper from 'typescript-template-language-service-decorator/lib/script-source-helper'
import type TemplateSourceHelper from 'typescript-template-language-service-decorator/lib/template-source-helper'
import { relative } from 'typescript-template-language-service-decorator/lib/nodes'
import { match, Matcher, Predicates } from './match'
import { match, Matcher } from './match'

class PlaceholderSubstituter {
public static replacePlaceholders(
Expand Down
7 changes: 0 additions & 7 deletions src/substituter.ts

This file was deleted.

Loading

0 comments on commit ce3d3d7

Please sign in to comment.