From 8f292c4c3e6af7692fb760b7e719ba2b68366312 Mon Sep 17 00:00:00 2001 From: isaacs Date: Mon, 27 Nov 2023 20:09:06 -0800 Subject: [PATCH] formatting --- README.md | 2 +- src/client.ts | 1 - src/debug.ts | 3 +- src/equivalents.ts | 12 +- ...lve-type-reference-directive-references.ts | 5 +- src/service/transpile-only.ts | 7 +- test/bin.ts | 4 +- test/debug.ts | 13 +- test/equivalents.ts | 11 +- test/index.ts | 5 +- test/service/diagnostic.ts | 6 +- test/service/get-output-typecheck.ts | 2 +- test/service/load.ts | 2 +- test/service/service.ts | 150 ++++++++++-------- test/service/transpile-only.ts | 6 +- test/ts-sys-cached.ts | 5 +- 16 files changed, 143 insertions(+), 91 deletions(-) diff --git a/README.md b/README.md index dc53ecd..52792bf 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ In Node v20.6 and higher, you can also load `tsimp` in your program, and from that point forward, TypeScript modules will Just Work. -Note that `import` declarations happen in parallel *before* the +Note that `import` declarations happen in parallel _before_ the code is executed, so you'll need to split it up like this: ```js diff --git a/src/client.ts b/src/client.ts index a2dbca4..05fce0e 100644 --- a/src/client.ts +++ b/src/client.ts @@ -26,7 +26,6 @@ export class DaemonClient extends SockDaemonClient< ServiceRequest, ServiceResolveResult | ServiceCompileResult > { - /** * Always 'tsimp'. Used by SockDaemon to know where to put stuff. */ diff --git a/src/debug.ts b/src/debug.ts index a94852a..e748457 100644 --- a/src/debug.ts +++ b/src/debug.ts @@ -23,6 +23,7 @@ const getLogger = export const error = getLogger('error', red) export const warn = level > 0 ? getLogger('warn', yellow) : () => {} -export const debug = level > 1 ? getLogger('debug', magenta) : () => {} +export const debug = + level > 1 ? getLogger('debug', magenta) : () => {} export const info = level > 2 ? getLogger('info', blue) : () => {} export const trace = getLogger('trace', green) diff --git a/src/equivalents.ts b/src/equivalents.ts index 29ad517..4185373 100644 --- a/src/equivalents.ts +++ b/src/equivalents.ts @@ -36,14 +36,18 @@ export const map: ExtMap = { } as const satisfies MapBase export const allExts = Object.keys(map) -export const isExt = (e: any): e is TSExt | JSExt => allExts.includes(e) +export const isExt = (e: any): e is TSExt | JSExt => + allExts.includes(e) -export const equivalents = (path: string, extensionless = false): string[] => { +export const equivalents = ( + path: string, + extensionless = false +): string[] => { const { ext } = parse(path) const stem = path.substring(0, path.length - ext.length) - const equivs:string[] = [] + const equivs: string[] = [] if (isExt(ext)) { - equivs.push(...map[ext].map((e => stem+e))) + equivs.push(...map[ext].map(e => stem + e)) } if (extensionless) { equivs.push(...allExts.map(e => path + e)) diff --git a/src/service/resolve-type-reference-directive-references.ts b/src/service/resolve-type-reference-directive-references.ts index f379e1a..179faa2 100644 --- a/src/service/resolve-type-reference-directive-references.ts +++ b/src/service/resolve-type-reference-directive-references.ts @@ -63,7 +63,10 @@ export const getResolveTypeReferenceDirectiveReferences = ( const key = createModeAwareCacheKey(name, mode) let result = rtrdrInternalCache.get(key) if (!result) { - rtrdrInternalCache.set(key, (result = loader.resolve(name, mode))) + rtrdrInternalCache.set( + key, + (result = loader.resolve(name, mode)) + ) } resolutions.push(result) } diff --git a/src/service/transpile-only.ts b/src/service/transpile-only.ts index a8b4dd2..dfd0f23 100644 --- a/src/service/transpile-only.ts +++ b/src/service/transpile-only.ts @@ -106,7 +106,8 @@ const createTsTranspileModule = ({ /* c8 ignore next */ getNewLine: () => newLine, fileExists: (fileName): boolean => - relative(fileName, inputFileName) === '' || relative(fileName, packageJsonFileName) === '', + relative(fileName, inputFileName) === '' || + relative(fileName, packageJsonFileName) === '', readFile: fileName => relative(fileName, packageJsonFileName) === '' ? `{"type": "${packageJsonType}"}` @@ -135,7 +136,9 @@ const createTsTranspileModule = ({ const pj = catcher(() => { const json = readFile(d + '/package.json') if (!json) return undefined - const pj = JSON.parse(json) as { type?: 'commonjs' | 'module' } + const pj = JSON.parse(json) as { + type?: 'commonjs' | 'module' + } return pj }) if (pj?.type) { diff --git a/test/bin.ts b/test/bin.ts index dc3153f..5f3478e 100644 --- a/test/bin.ts +++ b/test/bin.ts @@ -22,7 +22,9 @@ t.test('help', async t => { t.test('stop', async t => { run(['--stop']) - t.throws(() => process.kill('SIGTERM', Number(readFileSync('.tsimp/daemon/pid')))) + t.throws(() => + process.kill('SIGTERM', Number(readFileSync('.tsimp/daemon/pid'))) + ) }) t.test('start', async t => { diff --git a/test/debug.ts b/test/debug.ts index d885571..1554442 100644 --- a/test/debug.ts +++ b/test/debug.ts @@ -19,10 +19,8 @@ for (const isTTY of [true, false]) { writable: true, value: isTTY, }) - if (TSIMP_DEBUG) - process.env.TSIMP_DEBUG = TSIMP_DEBUG - else - delete process.env.TSIMP_DEBUG + if (TSIMP_DEBUG) process.env.TSIMP_DEBUG = TSIMP_DEBUG + else delete process.env.TSIMP_DEBUG const d = (await t.mockImport('../dist/esm/debug.js', { 'node:util': { format: (...args: any[]) => JSON.stringify(args), @@ -33,7 +31,12 @@ for (const isTTY of [true, false]) { d.debug('debug') d.info('info') d.trace('trace') - t.matchSnapshot(errs().map(s => s.join('')).join('\n'), 'output') + t.matchSnapshot( + errs() + .map(s => s.join('')) + .join('\n'), + 'output' + ) }) } }) diff --git a/test/equivalents.ts b/test/equivalents.ts index b9644a6..3ed5a16 100644 --- a/test/equivalents.ts +++ b/test/equivalents.ts @@ -1,6 +1,15 @@ import t from 'tap' -import { jsExts, tsExts, map, allExts, isExt, equivalents, isTSExt, isJSExt } from '../src/equivalents.js' +import { + jsExts, + tsExts, + map, + allExts, + isExt, + equivalents, + isTSExt, + isJSExt, +} from '../src/equivalents.js' const files = [ '/x/y.ts', diff --git a/test/index.ts b/test/index.ts index 7e44a64..1912646 100644 --- a/test/index.ts +++ b/test/index.ts @@ -6,7 +6,10 @@ let registerCalled = false const TSIMP = (await t.mockImport('../src/index.js', { 'node:module': { register: (url: string, args: any) => { - t.equal(url.toLowerCase(), getUrl('./hooks/loader.mjs').toLowerCase()) + t.equal( + url.toLowerCase(), + getUrl('./hooks/loader.mjs').toLowerCase() + ) const { parentURL: pu } = args t.equal(pu.toLowerCase(), getUrl('./index.js').toLowerCase()) t.match(args, { diff --git a/test/service/diagnostic.ts b/test/service/diagnostic.ts index 92175cb..a283842 100644 --- a/test/service/diagnostic.ts +++ b/test/service/diagnostic.ts @@ -15,11 +15,7 @@ export const f: Foo = { bar: true, baz: 'xyz' } }) const f = resolve(dir, 'bad-foo.ts') -const { diagnostics } = compile( - readFileSync(f, 'utf8'), - f, - true -) +const { diagnostics } = compile(readFileSync(f, 'utf8'), f, true) const d = diagnostics[0] if (!d) throw new Error('no diagnostics') diff --git a/test/service/get-output-typecheck.ts b/test/service/get-output-typecheck.ts index c2bf7df..42c7f69 100644 --- a/test/service/get-output-typecheck.ts +++ b/test/service/get-output-typecheck.ts @@ -91,7 +91,7 @@ for (const tsconfigModule of ['commonjs', 'esnext', 'nodenext']) { t.matchSnapshot(d, 'diagnostics') }) } - t.test('chdir', async () => process.chdir(cwd)) + t.test('chdir', async () => process.chdir(cwd)) }) } }) diff --git a/test/service/load.ts b/test/service/load.ts index 910980a..8591c79 100644 --- a/test/service/load.ts +++ b/test/service/load.ts @@ -105,7 +105,7 @@ for (const tsconfigModule of ['commonjs', 'esnext', 'nodenext']) { fileName, 'load after config change not cached, but still same output' ) - t.test('chdir', async () => process.chdir(cwd)) + t.test('chdir', async () => process.chdir(cwd)) }) } }) diff --git a/test/service/service.ts b/test/service/service.ts index 2f34202..b24f2ba 100644 --- a/test/service/service.ts +++ b/test/service/service.ts @@ -1,7 +1,7 @@ -import {resolve} from 'path' +import { resolve } from 'path' import t from 'tap' -import {pathToFileURL} from 'url' -import {CompileResult} from '../../src/types.js' +import { pathToFileURL } from 'url' +import { CompileResult } from '../../src/types.js' t.test('preload', async t => { const { DaemonServer } = (await t.mockImport( @@ -38,58 +38,79 @@ t.test('resolve', async t => { )) as typeof import('../../src/service/service.js') t.equal(DaemonServer.serviceName, 'tsimp') const ds = new DaemonServer() - t.strictSame(ds.handle({ - id: 'id', - action: 'resolve', - url: String(pathToFileURL(resolve(dir, 'foo.ts'))), - }), { - fileName: String(pathToFileURL(resolve(dir, 'foo.ts'))), - }) - t.strictSame(ds.handle({ - id: 'id', - action: 'resolve', - url: String(pathToFileURL(resolve(dir, 'foo.js'))), - }), { - fileName: String(pathToFileURL(resolve(dir, 'foo.ts'))), - }) - t.strictSame(ds.handle({ - id: 'id', - action: 'resolve', - url: String(pathToFileURL(resolve(dir, 'bar.js'))), - }), { - fileName: String(pathToFileURL(resolve(dir, 'bar.tsx'))), - }) + t.strictSame( + ds.handle({ + id: 'id', + action: 'resolve', + url: String(pathToFileURL(resolve(dir, 'foo.ts'))), + }), + { + fileName: String(pathToFileURL(resolve(dir, 'foo.ts'))), + } + ) + t.strictSame( + ds.handle({ + id: 'id', + action: 'resolve', + url: String(pathToFileURL(resolve(dir, 'foo.js'))), + }), + { + fileName: String(pathToFileURL(resolve(dir, 'foo.ts'))), + } + ) + t.strictSame( + ds.handle({ + id: 'id', + action: 'resolve', + url: String(pathToFileURL(resolve(dir, 'bar.js'))), + }), + { + fileName: String(pathToFileURL(resolve(dir, 'bar.tsx'))), + } + ) - t.strictSame(ds.handle({ - id: 'id', - action: 'resolve', - url: './foo.ts', - parentURL: String(pathToFileURL(resolve(dir, 'bar.ts'))), - }), { - fileName: String(pathToFileURL(resolve(dir, 'foo.ts'))), - }) - t.strictSame(ds.handle({ - id: 'id', - action: 'resolve', - url: './foo.js', - parentURL: String(pathToFileURL(resolve(dir, 'bar.js'))), - }), { - fileName: String(pathToFileURL(resolve(dir, 'foo.ts'))), - }) - t.strictSame(ds.handle({ - id: 'id', - action: 'resolve', - url: './bar.js', - parentURL: String(pathToFileURL(resolve(dir, 'foo.js'))), - }), { - fileName: String(pathToFileURL(resolve(dir, 'bar.tsx'))), - }) + t.strictSame( + ds.handle({ + id: 'id', + action: 'resolve', + url: './foo.ts', + parentURL: String(pathToFileURL(resolve(dir, 'bar.ts'))), + }), + { + fileName: String(pathToFileURL(resolve(dir, 'foo.ts'))), + } + ) + t.strictSame( + ds.handle({ + id: 'id', + action: 'resolve', + url: './foo.js', + parentURL: String(pathToFileURL(resolve(dir, 'bar.js'))), + }), + { + fileName: String(pathToFileURL(resolve(dir, 'foo.ts'))), + } + ) + t.strictSame( + ds.handle({ + id: 'id', + action: 'resolve', + url: './bar.js', + parentURL: String(pathToFileURL(resolve(dir, 'foo.js'))), + }), + { + fileName: String(pathToFileURL(resolve(dir, 'bar.tsx'))), + } + ) - t.strictSame(ds.handle({ - id: 'id', - action: 'resolve', - url: String(pathToFileURL(resolve(dir, 'does-not-exist'))), - }), {}) + t.strictSame( + ds.handle({ + id: 'id', + action: 'resolve', + url: String(pathToFileURL(resolve(dir, 'does-not-exist'))), + }), + {} + ) t.strictSame( ds.handle({ id: 'id', @@ -102,7 +123,7 @@ t.test('resolve', async t => { t.test('compile', async t => { let compileResult: CompileResult = { fileName: 'some/file', - diagnostics: [] + diagnostics: [], } const dir = t.testdir({ 'foo.tsx': '', @@ -112,16 +133,19 @@ t.test('compile', async t => { '../../src/service/service.js', { '../../src/service/load.js': { - load: () => compileResult + load: () => compileResult, }, - }, + } )) as typeof import('../../src/service/service.js') const ds = new DaemonServer() - t.strictSame(ds.handle({ - id: 'id', - action: 'compile', - fileName, - diagMode: 'warn', - pretty: true, - }), compileResult) + t.strictSame( + ds.handle({ + id: 'id', + action: 'compile', + fileName, + diagMode: 'warn', + pretty: true, + }), + compileResult + ) }) diff --git a/test/service/transpile-only.ts b/test/service/transpile-only.ts index 24198e2..0a2cf00 100644 --- a/test/service/transpile-only.ts +++ b/test/service/transpile-only.ts @@ -66,8 +66,10 @@ for (const tsconfigModule of ['commonjs', 'esnext', 'nodenext']) { 'file.ts', ] as const) { t.test(file, async t => { - const content = file === 'mix/mixed.ts' - ? fixture.mix['mixed.ts'] : fixture[file] + const content = + file === 'mix/mixed.ts' + ? fixture.mix['mixed.ts'] + : fixture[file] const noForce = getOutputTranspileOnly( content, `${dir}/${file}` diff --git a/test/ts-sys-cached.ts b/test/ts-sys-cached.ts index 477a630..d71026e 100644 --- a/test/ts-sys-cached.ts +++ b/test/ts-sys-cached.ts @@ -27,7 +27,10 @@ t.equal(sys.getCurrentDirectory(), process.cwd()) t.equal(sys.normalizePath(import.meta.url), import.meta.url) t.equal( sys.normalizePath(fileURLToPath(import.meta.url) + '/../x/y/'), - resolve(fileURLToPath(import.meta.url), '../x/y').replace(/\\/g, '/') + '/' + resolve(fileURLToPath(import.meta.url), '../x/y').replace( + /\\/g, + '/' + ) + '/' ) t.equal(sys.normalizePath('x/y/z/../a/'), 'x/y/a/') const sysWin = await t.mockImport('../src/ts-sys-cached.js', {