-
-
Notifications
You must be signed in to change notification settings - Fork 91
fix: support TypeScript 7 and Bun for locale file transpilation #795
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
Open
shadoworion
wants to merge
2
commits into
codingcommons:main
Choose a base branch
from
shadoworion:feat/typescript-7-and-bun-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // aliased because the esbuild banner of the CLI bundle already declares 'createRequire' | ||
| import { createRequire as createNodeRequire } from 'module' | ||
| import { resolve } from 'path' | ||
| import type ts from 'typescript' | ||
| import { parseTypescriptVersion, type TypescriptVersion } from './generator.utils.mjs' | ||
| import { logger } from './logger.mjs' | ||
|
|
||
| type TypescriptModule = typeof ts | ||
|
|
||
| // starting with TypeScript 7 the package's main entry only exposes version information, | ||
| // so the module needs to be imported lazily and each API feature-detected before use | ||
| const importTypescript = async (): Promise<Partial<TypescriptModule> | undefined> => { | ||
| try { | ||
| const tsModule = (await import('typescript')) as { default?: TypescriptModule } & TypescriptModule | ||
| return tsModule.default ?? tsModule | ||
| } catch (ignore) { | ||
| return undefined | ||
| } | ||
| } | ||
|
|
||
| let compilerPromise: Promise<TypescriptModule | undefined> | undefined | ||
|
|
||
| export const getTypescriptCompiler = (): Promise<TypescriptModule | undefined> => | ||
| (compilerPromise ??= importTypescript().then((tsModule) => | ||
| typeof tsModule?.createProgram === 'function' ? (tsModule as TypescriptModule) : undefined, | ||
| )) | ||
|
|
||
| // resolve from the user's project instead of `import.meta.url`; the importer and exporter | ||
| // are also shipped as CJS bundles where `import.meta` is empty | ||
| export const createRequireFromProject = () => createNodeRequire(resolve(process.cwd(), 'noop.js')) | ||
|
|
||
| const FALLBACK_VERSION: TypescriptVersion = { major: 5, minor: 5 } | ||
|
|
||
| const detectTypescriptVersion = async (): Promise<TypescriptVersion> => { | ||
| const tsModule = await importTypescript() | ||
| if (typeof tsModule?.versionMajorMinor === 'string') { | ||
| return parseTypescriptVersion(tsModule.versionMajorMinor) | ||
| } | ||
|
|
||
| try { | ||
| const { version } = createRequireFromProject()('typescript/package.json') as { version?: string } | ||
| if (version) return parseTypescriptVersion(version) | ||
| } catch (ignore) { | ||
| // 'typescript' is not installed | ||
| } | ||
|
|
||
| logger.info( | ||
| `could not detect the installed TypeScript version, assuming version >= ${FALLBACK_VERSION.major}.${FALLBACK_VERSION.minor}`, | ||
| ) | ||
| return FALLBACK_VERSION | ||
| } | ||
|
|
||
| let versionPromise: Promise<TypescriptVersion> | undefined | ||
|
|
||
| export const getTypescriptVersion = (): Promise<TypescriptVersion> => (versionPromise ??= detectTypescriptVersion()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { suite } from 'uvu' | ||
| import * as assert from 'uvu/assert' | ||
| import { parseTypescriptVersion, type TypescriptVersion } from './generator.utils.mjs' | ||
| import { getTypescriptVersion } from './typescript.utils.mjs' | ||
|
|
||
| const test = suite('typescript-version') | ||
|
|
||
| const cases: [string, TypescriptVersion][] = [ | ||
| ['3.5', { major: 3, minor: 5 }], | ||
| ['5.1', { major: 5, minor: 1 }], | ||
| ['7.0', { major: 7, minor: 0 }], | ||
| ['', { major: 0, minor: 0 }], | ||
| ] | ||
|
|
||
| cases.forEach(([version, expected]) => | ||
| test(`parseTypescriptVersion: '${version}'`, () => assert.equal(parseTypescriptVersion(version), expected)), | ||
| ) | ||
|
|
||
| test('getTypescriptVersion detects the installed version', async () => { | ||
| const version = await getTypescriptVersion() | ||
| assert.ok(version.major >= 3) | ||
| }) | ||
|
|
||
| test.run() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also needs
--ignoreConfig. With TypeScript 7.0.2, passing a source file while the project contains atsconfig.jsonproduces TS5112 and emits nothing. I confirmed that adding the flag makes the same command emit the expected files.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, thank you! Reproduced it — with a
tsconfig.jsonpresent,tsc7.0.2 aborts withTS5112and emits nothing, so the fallback reported "No usable TypeScript compiler was found".Fixed in 6f65ac0 by adding
--ignoreConfigto the spawned arguments. The flag is passed unconditionally because this code path is only reachable on TypeScript >= 7 (older versions still provide the compiler API and take the programmaticts.createProgrambranch, which never read the user'stsconfig.jsoneither — so behavior stays consistent).Verified with TypeScript 7.0.2 +
tsconfig.json(previously failing, now generates correctly) and re-ran the TypeScript 5 and Bun scenarios as regression.