From d23da898673bc461c113e8746ae70d1031f54fb3 Mon Sep 17 00:00:00 2001 From: Alexander Arvidsson <2972103+AlexanderArvidsson@users.noreply.github.com> Date: Fri, 19 Jul 2024 20:54:40 +0200 Subject: [PATCH] fix: support Typescript 5 --- package.json | 2 +- src/language-service.ts | 16 +++++++++++----- src/load.ts | 2 +- src/twind.ts | 25 ++++++++++++++----------- src/watch.ts | 3 ++- yarn.lock | 8 ++++---- 6 files changed, 33 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index 8b5a409..a4d2411 100644 --- a/package.json +++ b/package.json @@ -102,7 +102,7 @@ "match-sorter": "^6.3.0", "sucrase": "^3.17.1", "twind": "^0.16.10", - "typescript": "^4.1.0", + "typescript": "^5.5.3", "typescript-template-language-service-decorator": "^2.2.0", "vscode-languageserver-types": "^3.13.0" }, diff --git a/src/language-service.ts b/src/language-service.ts index 3a994ea..54c623e 100644 --- a/src/language-service.ts +++ b/src/language-service.ts @@ -463,11 +463,17 @@ export class TwindLanguageService implements TemplateLanguageService { if (selfRef) { matched.unshift( - Object.assign(Object.defineProperties({}, Object.getOwnPropertyDescriptors(selfRef)), { - value: '&', - raw: '&', - label: '&', - }), + Object.assign( + Object.defineProperties( + Object.assign({}, selfRef), + Object.getOwnPropertyDescriptors(selfRef), + ), + { + value: '&', + raw: '&', + label: '&', + }, + ), ) } } diff --git a/src/load.ts b/src/load.ts index 7a60d25..d1dc126 100644 --- a/src/load.ts +++ b/src/load.ts @@ -52,7 +52,7 @@ export const loadFile = (file: string, cwd = process.cwd()): T => { // eslint-disable-next-line @typescript-eslint/no-var-requires return require(moduleId) as T } catch (error) { - console.error(`Failed to load ${moduleId}: ${error.stack}`) + console.error(`Failed to load ${moduleId}: ${(error as Error).stack}`) return {} as T } } diff --git a/src/twind.ts b/src/twind.ts index 324f796..a608a91 100644 --- a/src/twind.ts +++ b/src/twind.ts @@ -3,7 +3,7 @@ import Module from 'module' import { fileURLToPath } from 'url' import type { Logger } from 'typescript-template-language-service-decorator' -import type * as TS from 'typescript/lib/tsserverlibrary' +import type TS from 'typescript/lib/typescript' import cssbeautify from 'cssbeautify' import stringify from 'fast-json-stable-stringify' @@ -65,7 +65,7 @@ const detailsFromThemeValue =
( ? '≥' + screen : ((Array.isArray(screen) ? (screen as ThemeScreenValue[]) - : [screen as undefined]) as ThemeScreenValue[]) + : [screen as unknown]) as ThemeScreenValue[]) .filter(Boolean) .map((value) => { if (typeof value == 'string') { @@ -212,7 +212,7 @@ export class Twind { constructor( private readonly typescript: typeof TS, - private readonly info: ts.server.PluginCreateInfo, + private readonly info: TS.server.PluginCreateInfo, private readonly configurationManager: ConfigurationManager, private readonly logger: Logger, ) { @@ -267,9 +267,12 @@ export class Twind { }) // Prefer project twind and fallback to bundled twind - let twindDTSFile = this.info.project - .resolveModuleNames(['twind'], program.getRootFileNames()[0]) - .map((moduleName) => moduleName?.resolvedFileName)[0] + const resolvedModule = this.info.languageServiceHost.getResolvedModuleWithFailedLookupLocationsFromCache?.( + 'twind', + program.getRootFileNames()[0], + ) + + let twindDTSFile = resolvedModule?.resolvedModule?.resolvedFileName if (twindDTSFile) { this.logger.log(`found local twind dts at ${twindDTSFile}`) @@ -364,7 +367,7 @@ export class Twind { sheet, mode: { ...silent, - report: (info) => { + report: (info: ReportInfo) => { // Ignore error from substitions if ( !( @@ -387,8 +390,8 @@ export class Twind { }) let context: Context - tw((_) => { - context = _ + tw((ctx: Context) => { + context = ctx return '' }) @@ -511,10 +514,10 @@ export class Twind { const type = checker.getTypeAtLocation(node) // (type.flags & ts.TypeFlags.Union) | (type.flags & ts.TypeFlags.Intersection) - const { types } = type as ts.UnionOrIntersectionType + const { types } = type as TS.UnionOrIntersectionType // (type.flags & ts.TypeFlags.StringLiteral) - tokens = types.map((type) => (type as ts.StringLiteralType).value) + tokens = types.map((type) => (type as TS.StringLiteralType).value) } else { ts.forEachChild(node, visit) } diff --git a/src/watch.ts b/src/watch.ts index 721abca..f0efd8c 100644 --- a/src/watch.ts +++ b/src/watch.ts @@ -20,7 +20,8 @@ export function watch( return () => watcher.close() } catch (error) { - if (['ERR_FEATURE_UNAVAILABLE_ON_PLATFORM', 'ENOENT'].includes(error.code)) { + const errno = error as NodeJS.ErrnoException + if (errno.code && ['ERR_FEATURE_UNAVAILABLE_ON_PLATFORM', 'ENOENT'].includes(errno.code)) { return () => { /* no-op*/ } diff --git a/yarn.lock b/yarn.lock index 442659c..d9716c5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1366,10 +1366,10 @@ typescript-template-language-service-decorator@^2.2.0: resolved "https://registry.npmjs.org/typescript-template-language-service-decorator/-/typescript-template-language-service-decorator-2.2.0.tgz#4ee6d580f307fb9239978e69626f2775b8a59b2a" integrity sha512-xiolqt1i7e22rpqMaprPgSFVgU64u3b9n6EJlAaUYE61jumipKAdI1+O5khPlWslpTUj80YzjUKjJ2jxT0D74w== -typescript@^4.1.0: - version "4.1.2" - resolved "https://registry.npmjs.org/typescript/-/typescript-4.1.2.tgz#6369ef22516fe5e10304aae5a5c4862db55380e9" - integrity sha512-thGloWsGH3SOxv1SoY7QojKi0tc+8FnOmiarEGMbd/lar7QOEd3hvlx3Fp5y6FlDUGl9L+pd4n2e+oToGMmhRQ== +typescript@^5.5.3: + version "5.5.3" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.5.3.tgz#e1b0a3c394190838a0b168e771b0ad56a0af0faa" + integrity sha512-/hreyEujaB0w76zKo6717l3L0o/qEUtRgdvUBvlkhoWeOVMjMuHNHk0BRBzikzuGDqNmPQbg5ifMEqsHLiIUcQ== uri-js@^4.2.2: version "4.4.0"