From be9f8ef04e7b00d1439d5d456a4ba9afe6c92d6a Mon Sep 17 00:00:00 2001 From: Nicolas DUBIEN Date: Thu, 9 Nov 2023 10:21:02 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=B7=EF=B8=8F(worker)=20Prefer=20"impor?= =?UTF-8?q?t=20type"=20over=20raw=20"import"=20(#4420)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- .yarn/versions/7b36cbca.yml | 3 +++ packages/jest/package.json | 2 +- packages/worker/src/internals/MainThreadRunner.ts | 4 ++-- packages/worker/src/internals/NoopWorkerProperty.ts | 10 ++-------- packages/worker/src/internals/SharedTypes.ts | 2 +- .../worker/src/internals/worker-pool/GlobalPool.ts | 2 +- .../worker/src/internals/worker-pool/OneTimePool.ts | 2 +- .../src/internals/worker-runner/NoWorkerRunner.ts | 4 ++-- .../worker/src/internals/worker-runner/WorkerRunner.ts | 8 ++------ packages/worker/src/main.ts | 2 +- packages/worker/test/e2e/blockEventLoop.spec.ts | 2 +- packages/worker/test/e2e/concurrentAssert.spec.ts | 2 +- packages/worker/test/e2e/failing.spec.ts | 2 +- packages/worker/test/e2e/passing.spec.ts | 2 +- packages/worker/test/e2e/predicateIsolation.spec.ts | 2 +- packages/worker/test/e2e/propertyIsolation.spec.ts | 2 +- packages/worker/test/e2e/unregistered.spec.ts | 2 +- packages/worker/test/internals/lock/Lock.spec.ts | 3 ++- .../test/internals/worker-pool/BasicPool.spec.ts | 2 +- yarn.lock | 2 +- 20 files changed, 27 insertions(+), 33 deletions(-) create mode 100644 .yarn/versions/7b36cbca.yml diff --git a/.yarn/versions/7b36cbca.yml b/.yarn/versions/7b36cbca.yml new file mode 100644 index 00000000..9afa524f --- /dev/null +++ b/.yarn/versions/7b36cbca.yml @@ -0,0 +1,3 @@ +releases: + "@fast-check/jest": patch + "@fast-check/worker": minor diff --git a/packages/jest/package.json b/packages/jest/package.json index 2d1977a9..54ad8820 100644 --- a/packages/jest/package.json +++ b/packages/jest/package.json @@ -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" }, diff --git a/packages/worker/src/internals/MainThreadRunner.ts b/packages/worker/src/internals/MainThreadRunner.ts index 002a886a..67aef412 100644 --- a/packages/worker/src/internals/MainThreadRunner.ts +++ b/packages/worker/src/internals/MainThreadRunner.ts @@ -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'; diff --git a/packages/worker/src/internals/NoopWorkerProperty.ts b/packages/worker/src/internals/NoopWorkerProperty.ts index 36a484b2..dabd2599 100644 --- a/packages/worker/src/internals/NoopWorkerProperty.ts +++ b/packages/worker/src/internals/NoopWorkerProperty.ts @@ -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 diff --git a/packages/worker/src/internals/SharedTypes.ts b/packages/worker/src/internals/SharedTypes.ts index ee75ae86..a50a1b6f 100644 --- a/packages/worker/src/internals/SharedTypes.ts +++ b/packages/worker/src/internals/SharedTypes.ts @@ -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 = { [K in keyof Ts]: Arbitrary; diff --git a/packages/worker/src/internals/worker-pool/GlobalPool.ts b/packages/worker/src/internals/worker-pool/GlobalPool.ts index c0f4a2cf..31467e3a 100644 --- a/packages/worker/src/internals/worker-pool/GlobalPool.ts +++ b/packages/worker/src/internals/worker-pool/GlobalPool.ts @@ -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>(); const pendingTerminationPerFile = new Map>(); diff --git a/packages/worker/src/internals/worker-pool/OneTimePool.ts b/packages/worker/src/internals/worker-pool/OneTimePool.ts index f2cc219a..030f0c44 100644 --- a/packages/worker/src/internals/worker-pool/OneTimePool.ts +++ b/packages/worker/src/internals/worker-pool/OneTimePool.ts @@ -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. diff --git a/packages/worker/src/internals/worker-runner/NoWorkerRunner.ts b/packages/worker/src/internals/worker-runner/NoWorkerRunner.ts index 6220d4c2..fa2cab5d 100644 --- a/packages/worker/src/internals/worker-runner/NoWorkerRunner.ts +++ b/packages/worker/src/internals/worker-runner/NoWorkerRunner.ts @@ -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 diff --git a/packages/worker/src/internals/worker-runner/WorkerRunner.ts b/packages/worker/src/internals/worker-runner/WorkerRunner.ts index 1cfe890c..58db46fc 100644 --- a/packages/worker/src/internals/worker-runner/WorkerRunner.ts +++ b/packages/worker/src/internals/worker-runner/WorkerRunner.ts @@ -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 diff --git a/packages/worker/src/main.ts b/packages/worker/src/main.ts index 1056b971..1351bf91 100644 --- a/packages/worker/src/main.ts +++ b/packages/worker/src/main.ts @@ -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; diff --git a/packages/worker/test/e2e/blockEventLoop.spec.ts b/packages/worker/test/e2e/blockEventLoop.spec.ts index a0693830..d901da8d 100644 --- a/packages/worker/test/e2e/blockEventLoop.spec.ts +++ b/packages/worker/test/e2e/blockEventLoop.spec.ts @@ -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 */ diff --git a/packages/worker/test/e2e/concurrentAssert.spec.ts b/packages/worker/test/e2e/concurrentAssert.spec.ts index cadcc6ae..febcf083 100644 --- a/packages/worker/test/e2e/concurrentAssert.spec.ts +++ b/packages/worker/test/e2e/concurrentAssert.spec.ts @@ -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 */ diff --git a/packages/worker/test/e2e/failing.spec.ts b/packages/worker/test/e2e/failing.spec.ts index 8652666c..07ab8837 100644 --- a/packages/worker/test/e2e/failing.spec.ts +++ b/packages/worker/test/e2e/failing.spec.ts @@ -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 */ diff --git a/packages/worker/test/e2e/passing.spec.ts b/packages/worker/test/e2e/passing.spec.ts index 06681c40..ecc876ef 100644 --- a/packages/worker/test/e2e/passing.spec.ts +++ b/packages/worker/test/e2e/passing.spec.ts @@ -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 */ diff --git a/packages/worker/test/e2e/predicateIsolation.spec.ts b/packages/worker/test/e2e/predicateIsolation.spec.ts index 10e1912d..bd45948c 100644 --- a/packages/worker/test/e2e/predicateIsolation.spec.ts +++ b/packages/worker/test/e2e/predicateIsolation.spec.ts @@ -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 */ diff --git a/packages/worker/test/e2e/propertyIsolation.spec.ts b/packages/worker/test/e2e/propertyIsolation.spec.ts index 7ea40186..ec8a3dc0 100644 --- a/packages/worker/test/e2e/propertyIsolation.spec.ts +++ b/packages/worker/test/e2e/propertyIsolation.spec.ts @@ -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 */ diff --git a/packages/worker/test/e2e/unregistered.spec.ts b/packages/worker/test/e2e/unregistered.spec.ts index 2a357989..16971f47 100644 --- a/packages/worker/test/e2e/unregistered.spec.ts +++ b/packages/worker/test/e2e/unregistered.spec.ts @@ -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 */ diff --git a/packages/worker/test/internals/lock/Lock.spec.ts b/packages/worker/test/internals/lock/Lock.spec.ts index 30822bb3..b35d16a9 100644 --- a/packages/worker/test/internals/lock/Lock.spec.ts +++ b/packages/worker/test/internals/lock/Lock.spec.ts @@ -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 () => { diff --git a/packages/worker/test/internals/worker-pool/BasicPool.spec.ts b/packages/worker/test/internals/worker-pool/BasicPool.spec.ts index 94906f1d..578ff911 100644 --- a/packages/worker/test/internals/worker-pool/BasicPool.spec.ts +++ b/packages/worker/test/internals/worker-pool/BasicPool.spec.ts @@ -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'; diff --git a/yarn.lock b/yarn.lock index f07653ca..7ff2212f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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: -- 2.51.2