From bb1e01f637f93d0a91a96a52bf689f40d98e5622 Mon Sep 17 00:00:00 2001 From: Sam Verschueren Date: Wed, 5 Sep 2018 21:00:31 +0200 Subject: [PATCH] Support top-level await - fixes #1 (#4) --- readme.md | 12 ++++++++++++ source/lib/compiler.ts | 7 ++++++- source/test/fixtures/top-level-await/index.d.ts | 6 ++++++ source/test/fixtures/top-level-await/index.js | 3 +++ source/test/fixtures/top-level-await/index.test-d.ts | 5 +++++ source/test/fixtures/top-level-await/package.json | 7 +++++++ source/test/test.ts | 6 ++++++ 7 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 source/test/fixtures/top-level-await/index.d.ts create mode 100644 source/test/fixtures/top-level-await/index.js create mode 100644 source/test/fixtures/top-level-await/index.test-d.ts create mode 100644 source/test/fixtures/top-level-await/package.json diff --git a/readme.md b/readme.md index 74362cc6..fe249e30 100644 --- a/readme.md +++ b/readme.md @@ -61,6 +61,18 @@ If we don't change the test file and we run the `tsd-check` command again, the t +### Top-level `await` + +If your method returns a `Promise`, you can use top-level `await` to resolve the value instead of wrapping it in an `async` [IIFE](https://developer.mozilla.org/en-US/docs/Glossary/IIFE). + +```ts +import {expectType} from 'tsd-check'; +import concat from '.'; + +expectType>(concat('foo', 'bar')); + +expectType(await concat('foo', 'bar')); +``` ## Assertions diff --git a/source/lib/compiler.ts b/source/lib/compiler.ts index d3779f22..57c23af9 100644 --- a/source/lib/compiler.ts +++ b/source/lib/compiler.ts @@ -2,6 +2,11 @@ import * as path from 'path'; import {createProgram, getPreEmitDiagnostics, ScriptTarget, ModuleResolutionKind, flattenDiagnosticMessageText} from 'typescript'; import {Diagnostic, Context} from './interfaces'; +// List of diagnostic codes that should be ignored +const ignoredDiagnostics = new Set([ + 1308 // Support top-level `await` +]); + const loadConfig = () => { return { moduleResolution: ModuleResolutionKind.NodeJs, @@ -28,7 +33,7 @@ export const getDiagnostics = (context: Context): Diagnostic[] => { const result: Diagnostic[] = []; for (const diagnostic of diagnostics) { - if (!diagnostic.file) { + if (!diagnostic.file || ignoredDiagnostics.has(diagnostic.code)) { continue; } diff --git a/source/test/fixtures/top-level-await/index.d.ts b/source/test/fixtures/top-level-await/index.d.ts new file mode 100644 index 00000000..cc34cfaa --- /dev/null +++ b/source/test/fixtures/top-level-await/index.d.ts @@ -0,0 +1,6 @@ +declare const one: { + (foo: string, bar: string): Promise; + (foo: number, bar: number): Promise; +}; + +export default one; diff --git a/source/test/fixtures/top-level-await/index.js b/source/test/fixtures/top-level-await/index.js new file mode 100644 index 00000000..f17717f5 --- /dev/null +++ b/source/test/fixtures/top-level-await/index.js @@ -0,0 +1,3 @@ +module.exports.default = (foo, bar) => { + return foo + bar; +}; diff --git a/source/test/fixtures/top-level-await/index.test-d.ts b/source/test/fixtures/top-level-await/index.test-d.ts new file mode 100644 index 00000000..db17fe37 --- /dev/null +++ b/source/test/fixtures/top-level-await/index.test-d.ts @@ -0,0 +1,5 @@ +import {expectType} from '../../..'; +import one from '.'; + +expectType(await one('foo', 'bar')); +expectType(await one(1, 2)); diff --git a/source/test/fixtures/top-level-await/package.json b/source/test/fixtures/top-level-await/package.json new file mode 100644 index 00000000..06ea168a --- /dev/null +++ b/source/test/fixtures/top-level-await/package.json @@ -0,0 +1,7 @@ +{ + "name": "foo", + "files": [ + "index.js", + "index.d.ts" + ] +} diff --git a/source/test/test.ts b/source/test/test.ts index 87017646..ab84f55e 100644 --- a/source/test/test.ts +++ b/source/test/test.ts @@ -36,3 +36,9 @@ test('return no diagnostics', async t => { t.true(diagnostics.length === 0); }); + +test('support top-level await', async t => { + const diagnostics = await m({cwd: path.join(__dirname, 'fixtures/top-level-await')}); + + t.true(diagnostics.length === 0); +});