Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support using keyword #232

Merged
merged 7 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion samples/basic/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
],
"[typescript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
}
},
"vitest.commandLine": "npx vitest"
}
43 changes: 43 additions & 0 deletions samples/basic/test/using.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { describe, expect, it } from 'vitest';

(Symbol as any).dispose ??= Symbol('Symbol.dispose');
(Symbol as any).asyncDispose ??= Symbol('Symbol.asyncDispose')

describe('using keyword', () => {
it('dispose', () => {
function getDisposableResource() {
using resource = new SomeDisposableResource()
return resource
}

const resource = getDisposableResource()
expect(resource.isDisposed).toBe(true)
})

it('asyncDispose', async () => {
async function getAsyncDisposableResource() {
await using resource = new SomeAsyncDisposableResource()
return resource
}

const resource = await getAsyncDisposableResource()
expect(resource.isDisposed).toBe(true)
})
})

class SomeDisposableResource implements Disposable {
public isDisposed = false;

[Symbol.dispose](): void {
this.isDisposed = true
}
}

class SomeAsyncDisposableResource implements AsyncDisposable {
public isDisposed = false

async [Symbol.asyncDispose](): Promise<void> {
await new Promise<void>(resolve => setTimeout(resolve, 0))
this.isDisposed = true
}
}
11 changes: 7 additions & 4 deletions samples/basic/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

// Configure Vitest (https://vitest.dev/config)

import { defineConfig } from "vite";
import { defineConfig } from 'vite'

export default defineConfig({
esbuild: {
target: 'es2020',
},
test: {
include: ["src/should_included_test.ts", "test/**/*.test.ts"],
exclude: ["test/ignored.test.ts"],
include: ['src/should_included_test.ts', 'test/**/*.test.ts'],
exclude: ['test/ignored.test.ts'],
},
});
})
1 change: 1 addition & 0 deletions src/pure/parsers/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const commonPlugins: ParserPlugin[] = [
'optionalChaining',
'partialApplication',
'throwExpressions',
'explicitResourceManagement', // allows use of the EcmaScript `using` keyword: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#using-declarations-and-explicit-resource-management
'topLevelAwait',
['pipelineOperator', { proposal: 'smart' }],
]
Expand Down
53 changes: 53 additions & 0 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,59 @@ describe('parse', () => {
expect(out.describeBlocks.length).toBe(1)
})

it('parse using keyword', () => {
const out = parse(
'x.ts',
`
import { describe, expect, it } from 'vitest';

(Symbol as any).dispose ??= Symbol('Symbol.dispose');
(Symbol as any).asyncDispose ??= Symbol('Symbol.asyncDispose')

describe('using keyword', () => {
it('dispose', () => {
function getDisposableResource() {
using resource = new SomeDisposableResource()
return resource
}

const resource = getDisposableResource()
expect(resource.isDisposed).toBe(true)
})

it('asyncDispose', async () => {
async function getAsyncDisposableResource() {
await using resource = new SomeAsyncDisposableResource()
return resource
}

const resource = await getAsyncDisposableResource()
expect(resource.isDisposed).toBe(true)
})
})

class SomeDisposableResource implements Disposable {
public isDisposed = false;

[Symbol.dispose](): void {
this.isDisposed = true
}
}

class SomeAsyncDisposableResource implements AsyncDisposable {
public isDisposed = false

async [Symbol.asyncDispose](): Promise<void> {
await new Promise<void>(resolve => setTimeout(resolve, 0))
this.isDisposed = true
}
}
`,
)

expect(out.describeBlocks.length).toBe(1)
})

it('parse satisfies keyword', () => {
const out = parse(
'x.ts',
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"module": "commonJS",
"target": "ES2020",
"outDir": "out",
"lib": ["ES2020", "DOM"],
"lib": ["ES2020", "ESNext.Disposable", "DOM"],
"sourceMap": true,
"rootDir": "src",
"esModuleInterop": true,
Expand Down
96 changes: 50 additions & 46 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -890,44 +890,44 @@
resolved "https://registry.yarnpkg.com/@ungap/structured-clone/-/structured-clone-1.2.0.tgz#756641adb587851b5ccb3e095daf27ae581c8406"
integrity sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==

"@vitest/expect@1.2.2":
version "1.2.2"
resolved "https://registry.yarnpkg.com/@vitest/expect/-/expect-1.2.2.tgz#39ea22e849bbf404b7e5272786551aa99e2663d0"
integrity sha512-3jpcdPAD7LwHUUiT2pZTj2U82I2Tcgg2oVPvKxhn6mDI2On6tfvPQTjAI4628GUGDZrCm4Zna9iQHm5cEexOAg==
"@vitest/expect@1.3.0":
version "1.3.0"
resolved "https://registry.yarnpkg.com/@vitest/expect/-/expect-1.3.0.tgz#09b374357b51be44f4fba9336d59024756f902dc"
integrity sha512-7bWt0vBTZj08B+Ikv70AnLRicohYwFgzNjFqo9SxxqHHxSlUJGSXmCRORhOnRMisiUryKMdvsi1n27Bc6jL9DQ==
dependencies:
"@vitest/spy" "1.2.2"
"@vitest/utils" "1.2.2"
"@vitest/spy" "1.3.0"
"@vitest/utils" "1.3.0"
chai "^4.3.10"

"@vitest/runner@1.2.2":
version "1.2.2"
resolved "https://registry.yarnpkg.com/@vitest/runner/-/runner-1.2.2.tgz#8b060a56ecf8b3d607b044d79f5f50d3cd9fee2f"
integrity sha512-JctG7QZ4LSDXr5CsUweFgcpEvrcxOV1Gft7uHrvkQ+fsAVylmWQvnaAr/HDp3LAH1fztGMQZugIheTWjaGzYIg==
"@vitest/runner@1.3.0":
version "1.3.0"
resolved "https://registry.yarnpkg.com/@vitest/runner/-/runner-1.3.0.tgz#1fabbe8d13642473e1acc3450b4df67cf40ae9d1"
integrity sha512-1Jb15Vo/Oy7mwZ5bXi7zbgszsdIBNjc4IqP8Jpr/8RdBC4nF1CTzIAn2dxYvpF1nGSseeL39lfLQ2uvs5u1Y9A==
dependencies:
"@vitest/utils" "1.2.2"
"@vitest/utils" "1.3.0"
p-limit "^5.0.0"
pathe "^1.1.1"

"@vitest/snapshot@1.2.2":
version "1.2.2"
resolved "https://registry.yarnpkg.com/@vitest/snapshot/-/snapshot-1.2.2.tgz#f56fd575569774968f3eeba9382a166c26201042"
integrity sha512-SmGY4saEw1+bwE1th6S/cZmPxz/Q4JWsl7LvbQIky2tKE35US4gd0Mjzqfr84/4OD0tikGWaWdMja/nWL5NIPA==
"@vitest/snapshot@1.3.0":
version "1.3.0"
resolved "https://registry.yarnpkg.com/@vitest/snapshot/-/snapshot-1.3.0.tgz#016b34289d87ef0c64f4cdb9173086c2edf1db7b"
integrity sha512-swmktcviVVPYx9U4SEQXLV6AEY51Y6bZ14jA2yo6TgMxQ3h+ZYiO0YhAHGJNp0ohCFbPAis1R9kK0cvN6lDPQA==
dependencies:
magic-string "^0.30.5"
pathe "^1.1.1"
pretty-format "^29.7.0"

"@vitest/spy@1.2.2":
version "1.2.2"
resolved "https://registry.yarnpkg.com/@vitest/spy/-/spy-1.2.2.tgz#8fc2aeccb96cecbbdd192c643729bd5f97a01c86"
integrity sha512-k9Gcahssw8d7X3pSLq3e3XEu/0L78mUkCjivUqCQeXJm9clfXR/Td8+AP+VC1O6fKPIDLcHDTAmBOINVuv6+7g==
"@vitest/spy@1.3.0":
version "1.3.0"
resolved "https://registry.yarnpkg.com/@vitest/spy/-/spy-1.3.0.tgz#1faab30e364324e9826887d479e71c03299fe77b"
integrity sha512-AkCU0ThZunMvblDpPKgjIi025UxR8V7MZ/g/EwmAGpjIujLVV2X6rGYGmxE2D4FJbAy0/ijdROHMWa2M/6JVMw==
dependencies:
tinyspy "^2.2.0"

"@vitest/utils@1.2.2":
version "1.2.2"
resolved "https://registry.yarnpkg.com/@vitest/utils/-/utils-1.2.2.tgz#94b5a1bd8745ac28cf220a99a8719efea1bcfc83"
integrity sha512-WKITBHLsBHlpjnDQahr+XK6RE7MiAsgrIkr0pGhQ9ygoxBfUeG0lUG5iLlzqjmKSlBv3+j5EGsriBzh+C3Tq9g==
"@vitest/utils@1.3.0":
version "1.3.0"
resolved "https://registry.yarnpkg.com/@vitest/utils/-/utils-1.3.0.tgz#7d46aa00617c1720b075eaeb4c4979a712d86c8e"
integrity sha512-/LibEY/fkaXQufi4GDlQZhikQsPO2entBKtfuyIpr1jV4DpaeasqkeHjhdOhU24vSHshcSuEyVlWdzvv2XmYCw==
dependencies:
diff-sequences "^29.6.3"
estree-walker "^3.0.3"
Expand Down Expand Up @@ -997,7 +997,7 @@ acorn-walk@^8.3.2:
resolved "https://registry.yarnpkg.com/acorn-walk/-/acorn-walk-8.3.2.tgz#7703af9415f1b6db9315d6895503862e231d34aa"
integrity sha512-cjkyv4OtNCIeqhHrfS81QWXoCBPExR/J62oyEqepVw8WaQeSqpW2uhuLPh1m9eWhDuOo/jUXVTlifvesOWp/4A==

acorn@^8.10.0, acorn@^8.11.3, acorn@^8.5.0, acorn@^8.9.0:
acorn@^8.11.3, acorn@^8.5.0, acorn@^8.9.0:
version "8.11.3"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.11.3.tgz#71e0b14e13a4ec160724b38fb7b0f233b1b81d7a"
integrity sha512-Y9rRfJG5jcKOE0CLisYbojUjIrIEE7AGMzA/Sm4BslANhbS+cDMpgBdcPT91oJ7OuJ9hYJBx59RjbhxVnrF8Xg==
Expand Down Expand Up @@ -2757,6 +2757,11 @@ joycon@^3.0.1:
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==

js-tokens@^8.0.2:
version "8.0.3"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-8.0.3.tgz#1c407ec905643603b38b6be6977300406ec48775"
integrity sha512-UfJMcSJc+SEXEl9lH/VLHSZbThQyLpw1vLO1Lb+j4RWDvG3N2f7yj3PVQA3cmkTBNldJ9eFnM+xEXxHIXrYiJw==

js-yaml@4.1.0, js-yaml@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-4.1.0.tgz#c1fb65f8f5017901cdd2c951864ba18458a10602"
Expand Down Expand Up @@ -3945,12 +3950,12 @@ strip-json-comments@3.1.1, strip-json-comments@^3.1.1:
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-3.1.1.tgz#31f1281b3832630434831c310c01cccda8cbe006"
integrity sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==

strip-literal@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/strip-literal/-/strip-literal-1.3.0.tgz#db3942c2ec1699e6836ad230090b84bb458e3a07"
integrity sha512-PugKzOsyXpArk0yWmUwqOZecSO0GH0bPoctLcqNDH9J04pVW3lflYE0ujElBGTloevcxF5MofAOZ7C5l2b+wLg==
strip-literal@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/strip-literal/-/strip-literal-2.0.0.tgz#5d063580933e4e03ebb669b12db64d2200687527"
integrity sha512-f9vHgsCWBq2ugHAkGMiiYY+AYG0D/cbloKKg0nhaaaSNsujdGIpVXCNsrJpCKr5M0f4aI31mr13UjY6GAuXCKA==
dependencies:
acorn "^8.10.0"
js-tokens "^8.0.2"

sucrase@^3.20.3:
version "3.35.0"
Expand Down Expand Up @@ -4224,10 +4229,10 @@ validate-npm-package-license@^3.0.1:
spdx-correct "^3.0.0"
spdx-expression-parse "^3.0.0"

vite-node@1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/vite-node/-/vite-node-1.2.2.tgz#f6d329b06f9032130ae6eac1dc773f3663903c25"
integrity sha512-1as4rDTgVWJO3n1uHmUYqq7nsFgINQ9u+mRcXpjeOMJUmviqNKjcZB7UfRZrlM7MjYXMKpuWp5oGkjaFLnjawg==
vite-node@1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/vite-node/-/vite-node-1.3.0.tgz#618cc26d83545cfbd4e2c3014678257496d2bd00"
integrity sha512-D/oiDVBw75XMnjAXne/4feCkCEwcbr2SU1bjAhCcfI5Bq3VoOHji8/wCPAfUkDIeohJ5nSZ39fNxM3dNZ6OBOA==
dependencies:
cac "^6.7.14"
debug "^4.3.4"
Expand All @@ -4246,18 +4251,17 @@ vite@^5.0.0:
optionalDependencies:
fsevents "~2.3.3"

vitest@^1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/vitest/-/vitest-1.2.2.tgz#9e29ad2a74a5df553c30c5798c57a062d58ce299"
integrity sha512-d5Ouvrnms3GD9USIK36KG8OZ5bEvKEkITFtnGv56HFaSlbItJuYr7hv2Lkn903+AvRAgSixiamozUVfORUekjw==
dependencies:
"@vitest/expect" "1.2.2"
"@vitest/runner" "1.2.2"
"@vitest/snapshot" "1.2.2"
"@vitest/spy" "1.2.2"
"@vitest/utils" "1.2.2"
vitest@latest:
version "1.3.0"
resolved "https://registry.yarnpkg.com/vitest/-/vitest-1.3.0.tgz#3b04e2a8270b2db0929829cb8f03df7bffd1b5a2"
integrity sha512-V9qb276J1jjSx9xb75T2VoYXdO1UKi+qfflY7V7w93jzX7oA/+RtYE6TcifxksxsZvygSSMwu2Uw6di7yqDMwg==
dependencies:
"@vitest/expect" "1.3.0"
"@vitest/runner" "1.3.0"
"@vitest/snapshot" "1.3.0"
"@vitest/spy" "1.3.0"
"@vitest/utils" "1.3.0"
acorn-walk "^8.3.2"
cac "^6.7.14"
chai "^4.3.10"
debug "^4.3.4"
execa "^8.0.1"
Expand All @@ -4266,11 +4270,11 @@ vitest@^1.2.2:
pathe "^1.1.1"
picocolors "^1.0.0"
std-env "^3.5.0"
strip-literal "^1.3.0"
strip-literal "^2.0.0"
tinybench "^2.5.1"
tinypool "^0.8.2"
vite "^5.0.0"
vite-node "1.2.2"
vite-node "1.3.0"
why-is-node-running "^2.2.2"

vue-demi@*:
Expand Down
Loading