From 8b88aec1aa120a3ed02f632f190446e5be53f384 Mon Sep 17 00:00:00 2001 From: Ben Drucker Date: Tue, 11 May 2021 18:28:55 -0700 Subject: [PATCH 1/4] map libs to full filenames --- source/lib/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/lib/config.ts b/source/lib/config.ts index f478293c..e61335bc 100644 --- a/source/lib/config.ts +++ b/source/lib/config.ts @@ -34,7 +34,7 @@ export default (pkg: {tsd?: RawConfig}, cwd: string): Config => { compilerOptions: { strict: true, jsx: JsxEmit.React, - lib: ['es2017', 'dom', 'dom.iterable'], + lib: ['es2017', 'dom', 'dom.iterable'].map(lib => `lib.${lib}.d.ts`), module: ModuleKind.CommonJS, target: ScriptTarget.ES2017, esModuleInterop: true, From e1ecfd2ddb8719079903f91f30609f3ae43e52ee Mon Sep 17 00:00:00 2001 From: Ben Drucker Date: Tue, 11 May 2021 19:10:08 -0700 Subject: [PATCH 2/4] move tsconfig to avoid influencing tests --- package.json | 4 ++-- source/test/fixtures/dom/package.json | 7 +------ tsconfig.json => tsconfig.tsd.json | 0 3 files changed, 3 insertions(+), 8 deletions(-) rename tsconfig.json => tsconfig.tsd.json (100%) diff --git a/package.json b/package.json index 149c7574..02cc7a9d 100644 --- a/package.json +++ b/package.json @@ -19,9 +19,9 @@ "prepublishOnly": "npm run build", "pretest": "npm run build && cpy \"./**/**\" \"../../../dist/test/fixtures/\" --parents --cwd=source/test/fixtures", "test": "npm run lint && ava", - "build": "npm run clean && tsc && chmod +x dist/cli.js", + "build": "npm run clean && tsc --project tsconfig.tsd.json && chmod +x dist/cli.js", "clean": "del-cli dist", - "lint": "tslint -p . --format stylish" + "lint": "tslint -p tsconfig.tsd.json --format stylish" }, "files": [ "dist/index.js", diff --git a/source/test/fixtures/dom/package.json b/source/test/fixtures/dom/package.json index d88f069d..de6dc1db 100644 --- a/source/test/fixtures/dom/package.json +++ b/source/test/fixtures/dom/package.json @@ -1,8 +1,3 @@ { - "name": "foo", - "tsd": { - "compilerOptions": { - "lib": ["dom"] - } - } + "name": "foo" } diff --git a/tsconfig.json b/tsconfig.tsd.json similarity index 100% rename from tsconfig.json rename to tsconfig.tsd.json From a0ec843e6fc7e7be74a82106ed79362ccf10f5ef Mon Sep 17 00:00:00 2001 From: Ben Drucker Date: Tue, 11 May 2021 19:15:00 -0700 Subject: [PATCH 3/4] remove missing lib test --- source/test/test.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/source/test/test.ts b/source/test/test.ts index 97de449d..f1139327 100644 --- a/source/test/test.ts +++ b/source/test/test.ts @@ -76,15 +76,6 @@ test('overridden config defaults to `strict` if `strict` is not explicitly overr ]); }); -test('fail if types are used from a lib that was not explicitly specified', async t => { - const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/lib-config/failure-missing-lib')}); - - verify(t, diagnostics, [ - [1, 22, 'error', 'Cannot find name \'Window\'.', /failure-missing-lib\/index.d.ts$/], - [4, 11, 'error', 'Cannot find name \'Window\'.', /failure-missing-lib\/index.test-d.ts$/] - ]); -}); - test('allow specifying a lib as a triple-slash-reference', async t => { const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/lib-config/lib-as-triple-slash-reference')}); From 5f28a84f593ded0b059ca97808cea066d1389516 Mon Sep 17 00:00:00 2001 From: Dimitri B Date: Mon, 24 May 2021 11:56:41 +0200 Subject: [PATCH 4/4] Let ts compiler derive the correct `lib` config option values for us - extend default `dom` lib tests - restore the missing lib test --- source/lib/config.ts | 6 +++++- source/test/fixtures/dom/index.test-d.ts | 4 ++++ .../fixtures/lib-config/failure-missing-lib/package.json | 7 ++++++- source/test/test.ts | 9 +++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/source/lib/config.ts b/source/lib/config.ts index e61335bc..dc565ab9 100644 --- a/source/lib/config.ts +++ b/source/lib/config.ts @@ -34,7 +34,7 @@ export default (pkg: {tsd?: RawConfig}, cwd: string): Config => { compilerOptions: { strict: true, jsx: JsxEmit.React, - lib: ['es2017', 'dom', 'dom.iterable'].map(lib => `lib.${lib}.d.ts`), + lib: parseRawLibs(['es2017', 'dom', 'dom.iterable'], cwd), module: ModuleKind.CommonJS, target: ScriptTarget.ES2017, esModuleInterop: true, @@ -67,3 +67,7 @@ function parseCompilerConfigObject(compilerOptions: RawCompilerOptions, cwd: str cwd ).options; } + +function parseRawLibs(libs: string[], cwd: string): string[] { + return parseCompilerConfigObject({lib: libs}, cwd).lib || []; +} diff --git a/source/test/fixtures/dom/index.test-d.ts b/source/test/fixtures/dom/index.test-d.ts index ade5c04c..59ff367d 100644 --- a/source/test/fixtures/dom/index.test-d.ts +++ b/source/test/fixtures/dom/index.test-d.ts @@ -5,3 +5,7 @@ const parent = document.createElement('div'); const child = document.createElement('p'); expectType(append(parent, child)); + +const elementsCollection = document.getElementsByClassName('foo'); +expectType>(elementsCollection); +expectType<() => IterableIterator>(elementsCollection[Symbol.iterator]); diff --git a/source/test/fixtures/lib-config/failure-missing-lib/package.json b/source/test/fixtures/lib-config/failure-missing-lib/package.json index de6dc1db..6e90f8d8 100644 --- a/source/test/fixtures/lib-config/failure-missing-lib/package.json +++ b/source/test/fixtures/lib-config/failure-missing-lib/package.json @@ -1,3 +1,8 @@ { - "name": "foo" + "name": "foo", + "tsd": { + "compilerOptions": { + "lib": [] + } + } } diff --git a/source/test/test.ts b/source/test/test.ts index f1139327..97de449d 100644 --- a/source/test/test.ts +++ b/source/test/test.ts @@ -76,6 +76,15 @@ test('overridden config defaults to `strict` if `strict` is not explicitly overr ]); }); +test('fail if types are used from a lib that was not explicitly specified', async t => { + const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/lib-config/failure-missing-lib')}); + + verify(t, diagnostics, [ + [1, 22, 'error', 'Cannot find name \'Window\'.', /failure-missing-lib\/index.d.ts$/], + [4, 11, 'error', 'Cannot find name \'Window\'.', /failure-missing-lib\/index.test-d.ts$/] + ]); +}); + test('allow specifying a lib as a triple-slash-reference', async t => { const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/lib-config/lib-as-triple-slash-reference')});