Skip to content

Commit

Permalink
Merge pull request #113 from ryoppippi/feature/tagged-type-more
Browse files Browse the repository at this point in the history
 Refactor Data Type and Import Statements in unplugin-typia
  • Loading branch information
ryoppippi authored Jun 16, 2024
2 parents fb1258a + 00a8af4 commit ed1280d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 16 deletions.
9 changes: 3 additions & 6 deletions packages/unplugin-typia/src/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import { tmpdir } from 'node:os';
import process from 'node:process';
import { basename, dirname, join } from 'pathe';
import { readPackageJSON } from 'pkg-types';
import type { CacheKey, CachePath, FilePath, ID, Source } from './types.js';
import type { CacheKey, CachePath, Data, FilePath, ID, Source } from './types.js';
import { wrap } from './types.js';
import type { transformTypia } from './typia.js';
import type { ResolvedOptions } from './options.js';
import { isBun } from './utils.js';

type ResolvedCacheOptions = ResolvedOptions['cache'];

type Data = Awaited<ReturnType<typeof transformTypia>>;

let cacheDir: string | null = null;
let typiaVersion: string | undefined;

Expand All @@ -37,7 +34,7 @@ export async function getCache(
const key = getKey(id, source);
const path = getCachePath(key, option);

let data: Data | null = null;
let data: string | null = null;
if (isBun()) {
const file = Bun.file(path);
if (!(await file.exists())) {
Expand All @@ -55,7 +52,7 @@ export async function getCache(
const hashComment = await getHashComment(key);

if (data.endsWith(hashComment)) {
return data;
return wrap<Data>(data);
}

return null;
Expand Down
3 changes: 2 additions & 1 deletion packages/unplugin-typia/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { resolveOptions } from './options.js';
import { transformTypia } from './typia.js';
import { getCache, setCache } from './cache.js';
import { log } from './utils.js';
import { type ID, type Source, wrap } from './types.js';
import type { ID, Source } from './types.js';
import { wrap } from './types.js';

const name = `unplugin-typia`;

Expand Down
1 change: 1 addition & 0 deletions packages/unplugin-typia/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type CachePath = Tagged<string, 'cache-path'>;
export type ID = Tagged<string, 'id'>;
export type Source = Tagged<string, 'source'>;
export type FilePath = Tagged<string, 'file-path'>;
export type Data = Tagged<string, 'data'>;

export function wrap<T extends Tagged<PropertyKey, any>>(value: UnwrapTagged<T>): T {
return value as T;
Expand Down
8 changes: 5 additions & 3 deletions packages/unplugin-typia/src/core/typia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import type { UnpluginBuildContext, UnpluginContext } from 'unplugin';
import { transform as typiaTransform } from 'typia/lib/transform.js';

import type { ResolvedOptions } from './options.ts';
import type { ID, Source } from './types.js';
import type { Data, ID, Source } from './types.js';
import { wrap } from './types.js';

/** create a printer */
const printer = ts.createPrinter();
Expand Down Expand Up @@ -35,7 +36,7 @@ export async function transformTypia(
*/
unpluginContext: UnpluginBuildContext & UnpluginContext,
options: ResolvedOptions,
): Promise<string> {
): Promise<Data> {
/** Whether to enable cache */
const cacheEnable = options.cache.enable;

Expand All @@ -52,7 +53,8 @@ export async function transformTypia(

warnDiagnostic(diagnostics, transformed, unpluginContext);

return printer.printFile(file);
const data = printer.printFile(file);
return wrap<Data>(data);
}

/**
Expand Down
10 changes: 4 additions & 6 deletions packages/unplugin-typia/test/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import { test } from 'bun:test';
import { assertEquals, assertNotEquals } from '@std/assert';

import * as cache from '../src/core/cache.js';
import type { FilePath, ID, Source } from '../src/core/types.js';
import type { Data, FilePath, ID, Source } from '../src/core/types.js';
import { wrap } from '../src/core/types.js';

type Data = Parameters<typeof cache.setCache>[2];

const tmp = wrap<FilePath>(tmpdir());

function removeComments(data: string | null) {
Expand All @@ -32,7 +30,7 @@ test('set and get cache', async () => {
const random = Math.random().toString();
const source = wrap<Source>(random);
const filename = wrap<ID>(`${random}-${random}.json`);
const data = `${random};` as const satisfies Data;
const data = wrap<Data>(`${random};`);

/* set cache */
await cache.setCache(filename, source, data, option);
Expand All @@ -51,7 +49,7 @@ test('set and get null with different id', async () => {
const random = Math.random().toString();
const source = wrap<Source>(random);
const filename = wrap<ID>(`${random}-${random}.json`);
const data = `${random};` as const satisfies Data;
const data = wrap<Data>(`${random};`);

/* set cache */
await cache.setCache(filename, source, data, option);
Expand All @@ -71,7 +69,7 @@ test('set and get null with cache disabled', async () => {
const random = Math.random().toString();
const source = wrap<Source>(random);
const filename = wrap<ID>(`${random}-${random}.json`);
const data = `${random};` as const satisfies Data;
const data = wrap<Data>(`${random};`);

/* set cache */
await cache.setCache(filename, source, data, option);
Expand Down

0 comments on commit ed1280d

Please sign in to comment.