Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Nov 28, 2023
1 parent 610c01c commit 8f292c4
Show file tree
Hide file tree
Showing 16 changed files with 143 additions and 91 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export class DaemonClient extends SockDaemonClient<
ServiceRequest,
ServiceResolveResult | ServiceCompileResult
> {

/**
* Always 'tsimp'. Used by SockDaemon to know where to put stuff.
*/
Expand Down
3 changes: 2 additions & 1 deletion src/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
12 changes: 8 additions & 4 deletions src/equivalents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 4 additions & 1 deletion src/service/resolve-type-reference-directive-references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
7 changes: 5 additions & 2 deletions src/service/transpile-only.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}"}`
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion test/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
13 changes: 8 additions & 5 deletions test/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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'
)
})
}
})
Expand Down
11 changes: 10 additions & 1 deletion test/equivalents.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
5 changes: 4 additions & 1 deletion test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
6 changes: 1 addition & 5 deletions test/service/diagnostic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion test/service/get-output-typecheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}
})
Expand Down
2 changes: 1 addition & 1 deletion test/service/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}
})
Expand Down
150 changes: 87 additions & 63 deletions test/service/service.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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',
Expand All @@ -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': '',
Expand All @@ -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
)
})
6 changes: 4 additions & 2 deletions test/service/transpile-only.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down
5 changes: 4 additions & 1 deletion test/ts-sys-cached.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down

0 comments on commit 8f292c4

Please sign in to comment.