Skip to content
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

fix: support Typescript 5 #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
16 changes: 11 additions & 5 deletions src/language-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '&',
},
),
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const loadFile = <T>(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
}
}
Expand Down
25 changes: 14 additions & 11 deletions src/twind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -65,7 +65,7 @@ const detailsFromThemeValue = <Section extends keyof Theme>(
? '≥' + 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') {
Expand Down Expand Up @@ -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,
) {
Expand Down Expand Up @@ -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}`)
Expand Down Expand Up @@ -364,7 +367,7 @@ export class Twind {
sheet,
mode: {
...silent,
report: (info) => {
report: (info: ReportInfo) => {
// Ignore error from substitions
if (
!(
Expand All @@ -387,8 +390,8 @@ export class Twind {
})

let context: Context
tw((_) => {
context = _
tw((ctx: Context) => {
context = ctx
return ''
})

Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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*/
}
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down