Skip to content

Commit

Permalink
🏷️(worker) Prefer "import type" over raw "import" (#4420)
Browse files Browse the repository at this point in the history
While raw "import" works well, it does carry an extra cost:

- bundlers may not optimize it as much as they can and may require to import the linked file even if they don't need to assess typings
- it may produce less optimized bundles (we may refer to unneeded files and thus delay some operations)
  • Loading branch information
dubzzz authored Nov 9, 2023
1 parent 65f81d9 commit be9f8ef
Show file tree
Hide file tree
Showing 20 changed files with 27 additions and 33 deletions.
3 changes: 3 additions & 0 deletions .yarn/versions/7b36cbca.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
releases:
"@fast-check/jest": patch
"@fast-check/worker": minor
2 changes: 1 addition & 1 deletion packages/jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"fast-check": "^3.0.0"
},
"peerDependencies": {
"@fast-check/worker": "~0.0.7",
"@fast-check/worker": "~0.0.7 || ~0.1.0",
"@jest/expect": ">=28.0.0",
"@jest/globals": ">=25.5.2"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/worker/src/internals/MainThreadRunner.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fc from 'fast-check';
import { type PropertyArbitraries, type WorkerProperty } from './SharedTypes.js';
import type { PropertyArbitraries, WorkerProperty } from './SharedTypes.js';
import { BasicPool } from './worker-pool/BasicPool.js';
import { Lock } from './lock/Lock.js';
import { IWorkerPool, PooledWorker } from './worker-pool/IWorkerPool.js';
import type { IWorkerPool, PooledWorker } from './worker-pool/IWorkerPool.js';
import { OneTimePool } from './worker-pool/OneTimePool.js';
import { GlobalPool } from './worker-pool/GlobalPool.js';

Expand Down
10 changes: 2 additions & 8 deletions packages/worker/src/internals/NoopWorkerProperty.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import {
type IAsyncPropertyWithHooks,
type Value,
type Stream,
type PreconditionFailure,
type PropertyFailure,
} from 'fast-check';
import { type WorkerProperty } from './SharedTypes.js';
import type { IAsyncPropertyWithHooks, Value, Stream, PreconditionFailure, PropertyFailure } from 'fast-check';
import type { WorkerProperty } from './SharedTypes.js';

/**
* NoopWorkerProperty is a placeholder instance of property returned
Expand Down
2 changes: 1 addition & 1 deletion packages/worker/src/internals/SharedTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Arbitrary, IAsyncPropertyWithHooks } from 'fast-check';
import { type PoolToWorkerMessage, type WorkerToPoolMessage } from './worker-pool/IWorkerPool.js';
import type { PoolToWorkerMessage, WorkerToPoolMessage } from './worker-pool/IWorkerPool.js';

export type PropertyArbitraries<Ts extends unknown[]> = {
[K in keyof Ts]: Arbitrary<Ts[K]>;
Expand Down
2 changes: 1 addition & 1 deletion packages/worker/src/internals/worker-pool/GlobalPool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BasicPool } from './BasicPool.js';
import { IWorkerPool, PooledWorker } from './IWorkerPool.js';
import type { IWorkerPool, PooledWorker } from './IWorkerPool.js';

const poolPerFile = new Map<string, BasicPool<unknown, unknown>>();
const pendingTerminationPerFile = new Map<string, ReturnType<typeof setTimeout>>();
Expand Down
2 changes: 1 addition & 1 deletion packages/worker/src/internals/worker-pool/OneTimePool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BasicPool } from './BasicPool.js';
import { IWorkerPool, PooledWorker } from './IWorkerPool.js';
import type { IWorkerPool, PooledWorker } from './IWorkerPool.js';

/**
* Pool never re-using already spawned worker.
Expand Down
4 changes: 2 additions & 2 deletions packages/worker/src/internals/worker-runner/NoWorkerRunner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type MessagePort } from 'node:worker_threads';
import { type MainThreadToWorkerMessage, type WorkerToMainThreadMessage } from '../SharedTypes.js';
import type { MessagePort } from 'node:worker_threads';
import type { MainThreadToWorkerMessage, WorkerToMainThreadMessage } from '../SharedTypes.js';

/**
* Setup the fallback worker listening to all predicates and rejecting any that has never been registered
Expand Down
8 changes: 2 additions & 6 deletions packages/worker/src/internals/worker-runner/WorkerRunner.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { type MessagePort } from 'node:worker_threads';
import {
type MainThreadToWorkerMessage,
type PropertyPredicate,
type WorkerToMainThreadMessage,
} from '../SharedTypes.js';
import type { MessagePort } from 'node:worker_threads';
import type { MainThreadToWorkerMessage, PropertyPredicate, WorkerToMainThreadMessage } from '../SharedTypes.js';

/**
* Setup a worker listening to parentPort and able to run a single time for a given predicate
Expand Down
2 changes: 1 addition & 1 deletion packages/worker/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { assert as fcAssert, type IAsyncProperty, type IProperty, type Parameter
import { runWorker } from './internals/worker-runner/WorkerRunner.js';
import { runMainThread } from './internals/MainThreadRunner.js';
import { NoopWorkerProperty } from './internals/NoopWorkerProperty.js';
import { type PropertyArbitraries, type PropertyPredicate, type WorkerProperty } from './internals/SharedTypes.js';
import type { PropertyArbitraries, PropertyPredicate, WorkerProperty } from './internals/SharedTypes.js';
import { runNoWorker } from './internals/worker-runner/NoWorkerRunner.js';

let lastPredicateId = 0;
Expand Down
2 changes: 1 addition & 1 deletion packages/worker/test/e2e/blockEventLoop.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isMainThread } from 'node:worker_threads';
import { type Parameters } from 'fast-check';
import type { Parameters } from 'fast-check';
import { assert } from '@fast-check/worker';

/* eslint-disable @typescript-eslint/ban-ts-comment */
Expand Down
2 changes: 1 addition & 1 deletion packages/worker/test/e2e/concurrentAssert.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isMainThread } from 'node:worker_threads';
import { type Parameters } from 'fast-check';
import type { Parameters } from 'fast-check';
import { assert } from '@fast-check/worker';

/* eslint-disable @typescript-eslint/ban-ts-comment */
Expand Down
2 changes: 1 addition & 1 deletion packages/worker/test/e2e/failing.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isMainThread } from 'node:worker_threads';
import { type Parameters } from 'fast-check';
import type { Parameters } from 'fast-check';
import { assert } from '@fast-check/worker';

/* eslint-disable @typescript-eslint/ban-ts-comment */
Expand Down
2 changes: 1 addition & 1 deletion packages/worker/test/e2e/passing.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isMainThread } from 'node:worker_threads';
import { type Parameters } from 'fast-check';
import type { Parameters } from 'fast-check';
import { assert } from '@fast-check/worker';

/* eslint-disable @typescript-eslint/ban-ts-comment */
Expand Down
2 changes: 1 addition & 1 deletion packages/worker/test/e2e/predicateIsolation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isMainThread } from 'node:worker_threads';
import { type Parameters } from 'fast-check';
import type { Parameters } from 'fast-check';
import { assert } from '@fast-check/worker';

/* eslint-disable @typescript-eslint/ban-ts-comment */
Expand Down
2 changes: 1 addition & 1 deletion packages/worker/test/e2e/propertyIsolation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isMainThread } from 'node:worker_threads';
import { type Parameters } from 'fast-check';
import type { Parameters } from 'fast-check';
import { assert } from '@fast-check/worker';

/* eslint-disable @typescript-eslint/ban-ts-comment */
Expand Down
2 changes: 1 addition & 1 deletion packages/worker/test/e2e/unregistered.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isMainThread } from 'node:worker_threads';
import { type Parameters } from 'fast-check';
import type { Parameters } from 'fast-check';
import { assert } from '@fast-check/worker';

/* eslint-disable @typescript-eslint/ban-ts-comment */
Expand Down
3 changes: 2 additions & 1 deletion packages/worker/test/internals/lock/Lock.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AcquiredLock, Lock } from '../../../src/internals/lock/Lock.js';
import type { AcquiredLock } from '../../../src/internals/lock/Lock.js';
import { Lock } from '../../../src/internals/lock/Lock.js';

describe('Lock', () => {
it('should be able to take the first lock', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PoolToWorkerMessage, WorkerToPoolMessage } from '../../../src/internals/worker-pool/IWorkerPool.js';
import type { PoolToWorkerMessage, WorkerToPoolMessage } from '../../../src/internals/worker-pool/IWorkerPool.js';
import { BasicPool } from '../../../src/internals/worker-pool/BasicPool.js';
// @ts-expect-error - It should normally be "* as WorkerThreadsMock" but it does not work anymore since we switched to babel (instead of ts-jest)
import WorkerThreadsMock from 'node:worker_threads';
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2797,7 +2797,7 @@ __metadata:
jest-jasmine2: ^29.7.0
typescript: ~5.2.2
peerDependencies:
"@fast-check/worker": ~0.0.7
"@fast-check/worker": ~0.0.7 || ~0.1.0
"@jest/expect": ">=28.0.0"
"@jest/globals": ">=25.5.2"
peerDependenciesMeta:
Expand Down

0 comments on commit be9f8ef

Please sign in to comment.