Skip to content

Commit

Permalink
feat(lib)!: [resolver] synchronous methods
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Dec 29, 2024
1 parent 156b3d2 commit 2f922e5
Show file tree
Hide file tree
Showing 18 changed files with 635 additions and 145 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,22 @@ import {
isImportsSubpath,
isRelativeSpecifier,
legacyMainResolve,
legacyMainResolveSync,
lookupPackageScope,
moduleResolve,
moduleResolveSync,
packageExportsResolve,
packageExportsResolveSync,
packageImportsExportsResolve,
packageImportsExportsResolveSync,
packageImportsResolve,
packageImportsResolveSync,
packageResolve,
packageResolveSync,
packageSelfResolve,
packageSelfResolveSync,
packageTargetResolve,
packageTargetResolveSync,
patternKeyCompare,
patternMatch,
readPackageJson,
Expand Down Expand Up @@ -124,13 +132,21 @@ This package exports the following identifiers:
- [`resolveModule`](./src/lib/resolve-module.mts)
- [`resolver`](./src/lib/resolver.mts)
- `legacyMainResolve`
- `legacyMainResolveSync`
- `moduleResolve`
- `moduleResolveSync`
- `packageExportsResolve`
- `packageExportsResolveSync`
- `packageImportsExportsResolve`
- `packageImportsExportsResolveSync`
- `packageImportsResolve`
- `packageImportsResolveSync`
- `packageResolve`
- `packageResolveSync`
- `packageSelfResolve`
- `packageSelfResolveSync`
- `packageTargetResolve`
- `packageTargetResolveSync`
- [`root`](./src/lib/root.mts)
- [`toRelativeSpecifier`](./src/lib/to-relative-specifier.mts)
- [`toUrl`](./src/lib/to-url.mts)
Expand Down
81 changes: 45 additions & 36 deletions __tests__/reporters/verbose.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,9 @@
import type { Task, TaskResultPack } from '@vitest/runner'
import { getNames, getTests } from '@vitest/runner/utils'
import colors, { type Colors } from 'tinyrainbow'
import type { RunnerTask } from 'vitest'
import type { RunnerTask, RunnerTestFile } from 'vitest'
import { DefaultReporter, type Reporter } from 'vitest/reporters'

/**
* Verbose reporter options.
*/
interface Options {
/**
* Enable summary reporter?
*
* @default true
*/
summary?: boolean | null | undefined
}

/**
* Verbose reporter.
*
Expand All @@ -43,13 +31,12 @@ class VerboseReporter extends DefaultReporter implements Reporter {

/**
* Create a new verbose reporter.
*
* @param {Options | null | undefined} [options]
* Reporter options
*/
constructor(options?: Options | null | undefined) {
super({ summary: options?.summary ?? true })
constructor() {
super({ summary: true })

this.colors = colors
this.renderSucceed = true
this.verbose = true
}

Expand Down Expand Up @@ -113,6 +100,29 @@ class VerboseReporter extends DefaultReporter implements Reporter {
/**
* Print tasks.
*
* @see {@linkcode RunnerTestFile}
*
* @public
* @override
* @instance
*
* @param {RunnerTestFile[] | undefined} [files]
* List of test files
* @param {unknown[] | undefined} [errors]
* List of unhandled errors
* @return {undefined}
*/
public override onFinished(
files?: RunnerTestFile[] | undefined,
errors?: unknown[] | undefined
): undefined {
if (files) { for (const task of files) this.printTask(task, true) }
return void super.onFinished(files, errors)
}

/**
* Handle task updates.
*
* @see {@linkcode TaskResultPack}
*
* @public
Expand All @@ -124,21 +134,7 @@ class VerboseReporter extends DefaultReporter implements Reporter {
* @return {undefined}
*/
public override onTaskUpdate(packs: TaskResultPack[]): undefined {
for (const pack of packs) {
/**
* Current task.
*
* @const {Task | undefined} task
*/
const task: Task | undefined = this.ctx.state.idMap.get(pack[0])

// print top-level suite task and recursively print tests
if (task && task.type === 'suite' && 'filepath' in task) {
void this.printTask(task)
}
}

return void packs
return void (this.isTTY && void super.onTaskUpdate(packs))
}

/**
Expand All @@ -152,10 +148,20 @@ class VerboseReporter extends DefaultReporter implements Reporter {
*
* @param {Task | null | undefined} task
* The task to handle
* @param {boolean | null | undefined} [force]
* Print `task` even when {@linkcode isTTY} is `false`?
* @return {undefined}
*/
protected override printTask(task: Task | null | undefined): undefined {
if (task && task.result?.state !== 'run') {
protected override printTask(
task: Task | null | undefined,
force?: boolean | null | undefined
): undefined {
if (
(!this.isTTY || force) &&
task?.result?.state &&
task.result.state !== 'queued' &&
task.result.state !== 'run'
) {
/**
* Task skipped?
*
Expand Down Expand Up @@ -197,7 +203,10 @@ class VerboseReporter extends DefaultReporter implements Reporter {
state += skip ? this.colors.blackBright(suite) : suite

this.log(state)
if (!skip) { for (const tsk of task.tasks) void this.printTask(tsk) }

if (!skip) {
for (const subtask of task.tasks) void this.printTask(subtask, force)
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@
"mlly": "./src/internal/fs.browser.mts",
"default": "./dist/internal/fs.browser.mjs"
},
"node": "fs/promises",
"default": "fs/promises"
"node": "fs",
"default": "fs"
},
"#internal/process": {
"types": {
Expand Down
8 changes: 8 additions & 0 deletions src/__snapshots__/index.e2e.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,21 @@ exports[`e2e:mlly > should expose public api 1`] = `
"resolveAlias",
"resolveModule",
"legacyMainResolve",
"legacyMainResolveSync",
"moduleResolve",
"moduleResolveSync",
"packageExportsResolve",
"packageExportsResolveSync",
"packageImportsExportsResolve",
"packageImportsExportsResolveSync",
"packageImportsResolve",
"packageImportsResolveSync",
"packageResolve",
"packageResolveSync",
"packageSelfResolve",
"packageSelfResolveSync",
"packageTargetResolve",
"packageTargetResolveSync",
"resolver",
"root",
"toRelativeSpecifier",
Expand Down
28 changes: 13 additions & 15 deletions src/interfaces/__tests__/file-system.spec-d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
*/

import type TestSubject from '#interfaces/file-system'
import type { Awaitable, ModuleId, Stats } from '@flex-development/mlly'
import type { ModuleId, Stats } from '@flex-development/mlly'

describe('unit-d:interfaces/FileSystem', () => {
describe('readFile', () => {
type Subject = TestSubject['readFile']
describe('readFileSync', () => {
type Subject = TestSubject['readFileSync']

it('should match [this: void]', () => {
expectTypeOf<Subject>().thisParameter.toEqualTypeOf<void>()
Expand All @@ -21,16 +21,14 @@ describe('unit-d:interfaces/FileSystem', () => {
})

describe('returns', () => {
it('should return Awaitable<Buffer | string>', () => {
expectTypeOf<Subject>()
.returns
.toEqualTypeOf<Awaitable<Buffer | string>>()
it('should return Buffer | string', () => {
expectTypeOf<Subject>().returns.toEqualTypeOf<Buffer | string>()
})
})
})

describe('realpath', () => {
type Subject = TestSubject['realpath']
describe('realpathSync', () => {
type Subject = TestSubject['realpathSync']

it('should match [this: void]', () => {
expectTypeOf<Subject>().thisParameter.toEqualTypeOf<void>()
Expand All @@ -43,14 +41,14 @@ describe('unit-d:interfaces/FileSystem', () => {
})

describe('returns', () => {
it('should return Awaitable<string>', () => {
expectTypeOf<Subject>().returns.toEqualTypeOf<Awaitable<string>>()
it('should return string', () => {
expectTypeOf<Subject>().returns.toEqualTypeOf<string>()
})
})
})

describe('stat', () => {
type Subject = TestSubject['stat']
describe('statSync', () => {
type Subject = TestSubject['statSync']

it('should match [this: void]', () => {
expectTypeOf<Subject>().thisParameter.toEqualTypeOf<void>()
Expand All @@ -63,8 +61,8 @@ describe('unit-d:interfaces/FileSystem', () => {
})

describe('returns', () => {
it('should return Awaitable<Stats>', () => {
expectTypeOf<Subject>().returns.toEqualTypeOf<Awaitable<Stats>>()
it('should return Stats', () => {
expectTypeOf<Subject>().returns.toEqualTypeOf<Stats>()
})
})
})
Expand Down
17 changes: 7 additions & 10 deletions src/interfaces/file-system.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @module mlly/interfaces/FileSystem
*/

import type { Awaitable, ModuleId, Stats } from '@flex-development/mlly'
import type { ModuleId, Stats } from '@flex-development/mlly'

/**
* File system API.
Expand All @@ -12,7 +12,6 @@ interface FileSystem {
/**
* Get the contents of `id`.
*
* @see {@linkcode Awaitable}
* @see {@linkcode Buffer}
* @see {@linkcode ModuleId}
* @see https://nodejs.org/api/fs.html#fsreadfilepath-options-callback
Expand All @@ -21,42 +20,40 @@ interface FileSystem {
*
* @param {ModuleId} id
* The path or `file:` URL to handle
* @return {Awaitable<Buffer | string>}
* @return {Buffer | string}
* File contents
*/
readFile(this: void, id: ModuleId): Awaitable<Buffer | string>
readFileSync(this: void, id: ModuleId): Buffer | string

/**
* Get the resolved pathname for `id`.
*
* @see {@linkcode Awaitable}
* @see {@linkcode ModuleId}
* @see https://nodejs.org/api/fs.html#fsrealpathpath-options-callback
*
* @this {void}
*
* @param {ModuleId} id
* The path or `file:` URL to handle
* @return {Awaitable<string>}
* @return {string}
* Resolved pathname
*/
realpath(this: void, id: ModuleId): Awaitable<string>
realpathSync(this: void, id: ModuleId): string

/**
* Get information about a directory or file.
*
* @see {@linkcode Awaitable}
* @see {@linkcode ModuleId}
* @see {@linkcode Stats}
*
* @this {void}
*
* @param {ModuleId} id
* The path or `file:` URL to handle
* @return {Awaitable<Stats>}
* @return {Stats}
* Info about `id`
*/
stat(this: void, id: ModuleId): Awaitable<Stats>
statSync(this: void, id: ModuleId): Stats
}

export type { FileSystem as default }
12 changes: 6 additions & 6 deletions src/internal/fs.browser.mts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const fs: FileSystem = {
* Never; not implemented
* @throws {Error}
*/
readFile(): never {
throw new Error('[readFile] not implemented')
readFileSync(): never {
throw new Error('[readFileSync] not implemented')
},

/**
Expand All @@ -29,8 +29,8 @@ const fs: FileSystem = {
* Never; not implemented
* @throws {Error}
*/
realpath(): never {
throw new Error('[realpath] not implemented')
realpathSync(): never {
throw new Error('[realpathSync] not implemented')
},

/**
Expand All @@ -40,8 +40,8 @@ const fs: FileSystem = {
* Never; not implemented
* @throws {Error}
*/
stat(): never {
throw new Error('[stat] not implemented')
statSync(): never {
throw new Error('[statSync] not implemented')
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/__snapshots__/resolve-module.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`unit:lib/resolveModule > should return resolved URL ("#internal/fs") 1`] = `"node:fs/promises"`;
exports[`unit:lib/resolveModule > should return resolved URL ("#internal/fs") 1`] = `"node:fs"`;

exports[`unit:lib/resolveModule > should return resolved URL ("../../../src") 1`] = `file://\${process.cwd()}/src/index.mts`;

Expand Down
2 changes: 1 addition & 1 deletion src/lib/__snapshots__/resolver.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`unit:lib/resolver > legacyMainResolve > should throw if main entry poin

exports[`unit:lib/resolver > moduleResolve > should return resolved URL (0) 1`] = `file://\${process.cwd()}/__fixtures__/tsconfig.json`;

exports[`unit:lib/resolver > moduleResolve > should return resolved URL (1) 1`] = `"node:fs/promises"`;
exports[`unit:lib/resolver > moduleResolve > should return resolved URL (1) 1`] = `"node:fs"`;

exports[`unit:lib/resolver > moduleResolve > should return resolved URL (2) 1`] = `file://\${process.cwd()}/src/internal/fs.browser.mts`;

Expand Down
Loading

0 comments on commit 2f922e5

Please sign in to comment.