From f1a2057521fead27b9ab6e2dae4fe09258a185dd Mon Sep 17 00:00:00 2001 From: Dimitri B Date: Tue, 1 Jun 2021 10:16:23 +0200 Subject: [PATCH] Allow omitting `types` property for non-barrel main (#112) Allow omitting `types` property for non-barrel main when `.d.ts` file name matches `main`. Fixes #84 --- source/lib/index.ts | 7 +++++- .../with-main-barrel/index.d.ts} | 0 .../with-main-barrel/index.js} | 0 .../with-main-barrel/index.test-d.ts | 6 +++++ .../with-main-barrel/package.json | 7 ++++++ .../with-main-other/foo.d.ts | 6 +++++ .../with-main-other/foo.js | 3 +++ .../with-main-other}/foo.test-d.ts | 3 ++- .../with-main-other}/package.json | 2 -- .../without-main/index.d.ts | 6 +++++ .../without-main/index.js | 3 +++ .../without-main/index.test-d.ts | 6 +++++ .../without-main/package.json | 6 +++++ source/test/test.ts | 24 ++++++++++++++++--- 14 files changed, 72 insertions(+), 7 deletions(-) rename source/test/fixtures/{test-non-barrel-main-via-types/foo.d.ts => no-explicit-types-property/with-main-barrel/index.d.ts} (100%) rename source/test/fixtures/{test-non-barrel-main-via-types/foo.js => no-explicit-types-property/with-main-barrel/index.js} (100%) create mode 100644 source/test/fixtures/no-explicit-types-property/with-main-barrel/index.test-d.ts create mode 100644 source/test/fixtures/no-explicit-types-property/with-main-barrel/package.json create mode 100644 source/test/fixtures/no-explicit-types-property/with-main-other/foo.d.ts create mode 100644 source/test/fixtures/no-explicit-types-property/with-main-other/foo.js rename source/test/fixtures/{test-non-barrel-main-via-types => no-explicit-types-property/with-main-other}/foo.test-d.ts (55%) rename source/test/fixtures/{test-non-barrel-main-via-types => no-explicit-types-property/with-main-other}/package.json (66%) create mode 100644 source/test/fixtures/no-explicit-types-property/without-main/index.d.ts create mode 100644 source/test/fixtures/no-explicit-types-property/without-main/index.js create mode 100644 source/test/fixtures/no-explicit-types-property/without-main/index.test-d.ts create mode 100644 source/test/fixtures/no-explicit-types-property/without-main/package.json diff --git a/source/lib/index.ts b/source/lib/index.ts index 5be84b42..3c1580dc 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -14,7 +14,12 @@ export interface Options { } const findTypingsFile = async (pkg: any, options: Options) => { - const typings = options.typingsFile || pkg.types || pkg.typings || 'index.d.ts'; + const typings = + options.typingsFile || + pkg.types || + pkg.typings || + (pkg.main && path.parse(pkg.main).name + '.d.ts') || + 'index.d.ts'; const typingsExist = await pathExists(path.join(options.cwd, typings)); if (!typingsExist) { diff --git a/source/test/fixtures/test-non-barrel-main-via-types/foo.d.ts b/source/test/fixtures/no-explicit-types-property/with-main-barrel/index.d.ts similarity index 100% rename from source/test/fixtures/test-non-barrel-main-via-types/foo.d.ts rename to source/test/fixtures/no-explicit-types-property/with-main-barrel/index.d.ts diff --git a/source/test/fixtures/test-non-barrel-main-via-types/foo.js b/source/test/fixtures/no-explicit-types-property/with-main-barrel/index.js similarity index 100% rename from source/test/fixtures/test-non-barrel-main-via-types/foo.js rename to source/test/fixtures/no-explicit-types-property/with-main-barrel/index.js diff --git a/source/test/fixtures/no-explicit-types-property/with-main-barrel/index.test-d.ts b/source/test/fixtures/no-explicit-types-property/with-main-barrel/index.test-d.ts new file mode 100644 index 00000000..d06d20f9 --- /dev/null +++ b/source/test/fixtures/no-explicit-types-property/with-main-barrel/index.test-d.ts @@ -0,0 +1,6 @@ +import {expectType, expectError} from '../../../..'; +import one from './'; + +expectType(one('foo', 'bar')); +expectType(one(1, 2)); +expectError(one(1, 2)); diff --git a/source/test/fixtures/no-explicit-types-property/with-main-barrel/package.json b/source/test/fixtures/no-explicit-types-property/with-main-barrel/package.json new file mode 100644 index 00000000..c1152c5e --- /dev/null +++ b/source/test/fixtures/no-explicit-types-property/with-main-barrel/package.json @@ -0,0 +1,7 @@ +{ + "name": "foo", + "main": "index.js", + "files": [ + "index.d.ts" + ] +} diff --git a/source/test/fixtures/no-explicit-types-property/with-main-other/foo.d.ts b/source/test/fixtures/no-explicit-types-property/with-main-other/foo.d.ts new file mode 100644 index 00000000..0616ebaa --- /dev/null +++ b/source/test/fixtures/no-explicit-types-property/with-main-other/foo.d.ts @@ -0,0 +1,6 @@ +declare const one: { + (foo: string, bar: string): string; + (foo: number, bar: number): number; +}; + +export default one; diff --git a/source/test/fixtures/no-explicit-types-property/with-main-other/foo.js b/source/test/fixtures/no-explicit-types-property/with-main-other/foo.js new file mode 100644 index 00000000..f17717f5 --- /dev/null +++ b/source/test/fixtures/no-explicit-types-property/with-main-other/foo.js @@ -0,0 +1,3 @@ +module.exports.default = (foo, bar) => { + return foo + bar; +}; diff --git a/source/test/fixtures/test-non-barrel-main-via-types/foo.test-d.ts b/source/test/fixtures/no-explicit-types-property/with-main-other/foo.test-d.ts similarity index 55% rename from source/test/fixtures/test-non-barrel-main-via-types/foo.test-d.ts rename to source/test/fixtures/no-explicit-types-property/with-main-other/foo.test-d.ts index 91ea5821..60fe51a3 100644 --- a/source/test/fixtures/test-non-barrel-main-via-types/foo.test-d.ts +++ b/source/test/fixtures/no-explicit-types-property/with-main-other/foo.test-d.ts @@ -1,5 +1,6 @@ -import {expectType} from '../../..'; +import {expectType, expectError} from '../../../..'; import one from './foo'; expectType(one('foo', 'bar')); expectType(one(1, 2)); +expectError(one(1, 2)); diff --git a/source/test/fixtures/test-non-barrel-main-via-types/package.json b/source/test/fixtures/no-explicit-types-property/with-main-other/package.json similarity index 66% rename from source/test/fixtures/test-non-barrel-main-via-types/package.json rename to source/test/fixtures/no-explicit-types-property/with-main-other/package.json index e2b33494..ef89ee5f 100644 --- a/source/test/fixtures/test-non-barrel-main-via-types/package.json +++ b/source/test/fixtures/no-explicit-types-property/with-main-other/package.json @@ -1,9 +1,7 @@ { "name": "foo", "main": "foo.js", - "types": "foo.d.ts", "files": [ - "foo.js", "foo.d.ts" ] } diff --git a/source/test/fixtures/no-explicit-types-property/without-main/index.d.ts b/source/test/fixtures/no-explicit-types-property/without-main/index.d.ts new file mode 100644 index 00000000..0616ebaa --- /dev/null +++ b/source/test/fixtures/no-explicit-types-property/without-main/index.d.ts @@ -0,0 +1,6 @@ +declare const one: { + (foo: string, bar: string): string; + (foo: number, bar: number): number; +}; + +export default one; diff --git a/source/test/fixtures/no-explicit-types-property/without-main/index.js b/source/test/fixtures/no-explicit-types-property/without-main/index.js new file mode 100644 index 00000000..f17717f5 --- /dev/null +++ b/source/test/fixtures/no-explicit-types-property/without-main/index.js @@ -0,0 +1,3 @@ +module.exports.default = (foo, bar) => { + return foo + bar; +}; diff --git a/source/test/fixtures/no-explicit-types-property/without-main/index.test-d.ts b/source/test/fixtures/no-explicit-types-property/without-main/index.test-d.ts new file mode 100644 index 00000000..d06d20f9 --- /dev/null +++ b/source/test/fixtures/no-explicit-types-property/without-main/index.test-d.ts @@ -0,0 +1,6 @@ +import {expectType, expectError} from '../../../..'; +import one from './'; + +expectType(one('foo', 'bar')); +expectType(one(1, 2)); +expectError(one(1, 2)); diff --git a/source/test/fixtures/no-explicit-types-property/without-main/package.json b/source/test/fixtures/no-explicit-types-property/without-main/package.json new file mode 100644 index 00000000..e3e72041 --- /dev/null +++ b/source/test/fixtures/no-explicit-types-property/without-main/package.json @@ -0,0 +1,6 @@ +{ + "name": "foo", + "files": [ + "index.d.ts" + ] +} diff --git a/source/test/test.ts b/source/test/test.ts index af372ff0..b22b6bbb 100644 --- a/source/test/test.ts +++ b/source/test/test.ts @@ -146,10 +146,28 @@ test('support non-barrel main', async t => { verify(t, diagnostics, []); }); -test('support non-barrel main using `types` property', async t => { - const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/test-non-barrel-main-via-types')}); +test('allow omitting `types` property when `main` property is missing but main is a barrel (`index.js`) and .d.ts file matches main', async t => { + const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/no-explicit-types-property/without-main')}); - verify(t, diagnostics, []); + verify(t, diagnostics, [ + [6, 0, 'error', 'Expected an error, but found none.'] + ]); +}); + +test('allow omitting `types` property when `main` property is set to a barrel (`index.js`) and .d.ts file matches main', async t => { + const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/no-explicit-types-property/with-main-barrel')}); + + verify(t, diagnostics, [ + [6, 0, 'error', 'Expected an error, but found none.'] + ]); +}); + +test('allow omitting `types` property when `main` property is set to non-barrel (`foo.js`) and .d.ts file matches main', async t => { + const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/no-explicit-types-property/with-main-other')}); + + verify(t, diagnostics, [ + [6, 0, 'error', 'Expected an error, but found none.'] + ]); }); test('support testing in sub-directories', async t => {