From a6233f84bd103a867d90607ddd5942766bcc3b95 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Fri, 15 Mar 2019 10:27:00 +0000 Subject: [PATCH] Support `types` property and correctly test `files` for types in project sub-dir (#12) --- source/lib/index.ts | 2 +- source/lib/rules/files-property.ts | 12 +++++++++--- .../test/fixtures/test-in-subdir/package.json | 9 +++++++++ .../fixtures/test-in-subdir/src/index.d.ts | 6 ++++++ .../test/fixtures/test-in-subdir/src/index.js | 3 +++ .../test-in-subdir/src/index.test-d.ts | 5 +++++ .../test-non-barrel-main-via-types/foo.d.ts | 6 ++++++ .../test-non-barrel-main-via-types/foo.js | 3 +++ .../foo.test-d.ts | 5 +++++ .../package.json | 9 +++++++++ .../fixtures/test-non-barrel-main/foo.d.ts | 6 ++++++ .../test/fixtures/test-non-barrel-main/foo.js | 3 +++ .../test-non-barrel-main/foo.test-d.ts | 5 +++++ .../fixtures/test-non-barrel-main/package.json | 9 +++++++++ source/test/test.ts | 18 ++++++++++++++++++ 15 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 source/test/fixtures/test-in-subdir/package.json create mode 100644 source/test/fixtures/test-in-subdir/src/index.d.ts create mode 100644 source/test/fixtures/test-in-subdir/src/index.js create mode 100644 source/test/fixtures/test-in-subdir/src/index.test-d.ts create mode 100644 source/test/fixtures/test-non-barrel-main-via-types/foo.d.ts create mode 100644 source/test/fixtures/test-non-barrel-main-via-types/foo.js create mode 100644 source/test/fixtures/test-non-barrel-main-via-types/foo.test-d.ts create mode 100644 source/test/fixtures/test-non-barrel-main-via-types/package.json create mode 100644 source/test/fixtures/test-non-barrel-main/foo.d.ts create mode 100644 source/test/fixtures/test-non-barrel-main/foo.js create mode 100644 source/test/fixtures/test-non-barrel-main/foo.test-d.ts create mode 100644 source/test/fixtures/test-non-barrel-main/package.json diff --git a/source/lib/index.ts b/source/lib/index.ts index d1d6f90c..a38dd56a 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -10,7 +10,7 @@ interface Options { } const findTypingsFile = async (pkg: any, options: Options) => { - const typings = pkg.typings || 'index.d.ts'; + const typings = pkg.types || pkg.typings || 'index.d.ts'; const typingsExist = await pathExists(path.join(options.cwd, typings)); diff --git a/source/lib/rules/files-property.ts b/source/lib/rules/files-property.ts index 3f3999da..c0e9f9fd 100644 --- a/source/lib/rules/files-property.ts +++ b/source/lib/rules/files-property.ts @@ -11,9 +11,15 @@ import {getJSONPropertyPosition} from '../utils'; */ export default (context: Context): Diagnostic[] => { const {pkg, typingsFile} = context; - const typingsFileName = path.basename(typingsFile); - if (!Array.isArray(pkg.files) || pkg.files.indexOf(typingsFileName) !== -1) { + if (!Array.isArray(pkg.files)) { + return []; + } + + const normalizedTypingsFile = path.normalize(typingsFile); + const normalizedFiles = (pkg.files as string[]).map(path.normalize); + + if (normalizedFiles.includes(normalizedTypingsFile)) { return []; } @@ -22,7 +28,7 @@ export default (context: Context): Diagnostic[] => { return [ { fileName: 'package.json', - message: `TypeScript type definition \`${typingsFileName}\` is not part of the \`files\` list.`, + message: `TypeScript type definition \`${normalizedTypingsFile}\` is not part of the \`files\` list.`, severity: 'error', ...getJSONPropertyPosition(content, 'files') } diff --git a/source/test/fixtures/test-in-subdir/package.json b/source/test/fixtures/test-in-subdir/package.json new file mode 100644 index 00000000..496ecb3d --- /dev/null +++ b/source/test/fixtures/test-in-subdir/package.json @@ -0,0 +1,9 @@ +{ + "name": "foo", + "main": "src/index.js", + "typings": "src/index.d.ts", + "files": [ + "src/index.js", + "src/index.d.ts" + ] +} diff --git a/source/test/fixtures/test-in-subdir/src/index.d.ts b/source/test/fixtures/test-in-subdir/src/index.d.ts new file mode 100644 index 00000000..0616ebaa --- /dev/null +++ b/source/test/fixtures/test-in-subdir/src/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/test-in-subdir/src/index.js b/source/test/fixtures/test-in-subdir/src/index.js new file mode 100644 index 00000000..f17717f5 --- /dev/null +++ b/source/test/fixtures/test-in-subdir/src/index.js @@ -0,0 +1,3 @@ +module.exports.default = (foo, bar) => { + return foo + bar; +}; diff --git a/source/test/fixtures/test-in-subdir/src/index.test-d.ts b/source/test/fixtures/test-in-subdir/src/index.test-d.ts new file mode 100644 index 00000000..448f75cc --- /dev/null +++ b/source/test/fixtures/test-in-subdir/src/index.test-d.ts @@ -0,0 +1,5 @@ +import {expectType} from '../../../..'; +import one from '.'; + +expectType(one('foo', 'bar')); +expectType(one(1, 2)); diff --git a/source/test/fixtures/test-non-barrel-main-via-types/foo.d.ts b/source/test/fixtures/test-non-barrel-main-via-types/foo.d.ts new file mode 100644 index 00000000..0616ebaa --- /dev/null +++ b/source/test/fixtures/test-non-barrel-main-via-types/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/test-non-barrel-main-via-types/foo.js b/source/test/fixtures/test-non-barrel-main-via-types/foo.js new file mode 100644 index 00000000..f17717f5 --- /dev/null +++ b/source/test/fixtures/test-non-barrel-main-via-types/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/test-non-barrel-main-via-types/foo.test-d.ts new file mode 100644 index 00000000..91ea5821 --- /dev/null +++ b/source/test/fixtures/test-non-barrel-main-via-types/foo.test-d.ts @@ -0,0 +1,5 @@ +import {expectType} from '../../..'; +import one from './foo'; + +expectType(one('foo', 'bar')); +expectType(one(1, 2)); diff --git a/source/test/fixtures/test-non-barrel-main-via-types/package.json b/source/test/fixtures/test-non-barrel-main-via-types/package.json new file mode 100644 index 00000000..e2b33494 --- /dev/null +++ b/source/test/fixtures/test-non-barrel-main-via-types/package.json @@ -0,0 +1,9 @@ +{ + "name": "foo", + "main": "foo.js", + "types": "foo.d.ts", + "files": [ + "foo.js", + "foo.d.ts" + ] +} diff --git a/source/test/fixtures/test-non-barrel-main/foo.d.ts b/source/test/fixtures/test-non-barrel-main/foo.d.ts new file mode 100644 index 00000000..0616ebaa --- /dev/null +++ b/source/test/fixtures/test-non-barrel-main/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/test-non-barrel-main/foo.js b/source/test/fixtures/test-non-barrel-main/foo.js new file mode 100644 index 00000000..f17717f5 --- /dev/null +++ b/source/test/fixtures/test-non-barrel-main/foo.js @@ -0,0 +1,3 @@ +module.exports.default = (foo, bar) => { + return foo + bar; +}; diff --git a/source/test/fixtures/test-non-barrel-main/foo.test-d.ts b/source/test/fixtures/test-non-barrel-main/foo.test-d.ts new file mode 100644 index 00000000..91ea5821 --- /dev/null +++ b/source/test/fixtures/test-non-barrel-main/foo.test-d.ts @@ -0,0 +1,5 @@ +import {expectType} from '../../..'; +import one from './foo'; + +expectType(one('foo', 'bar')); +expectType(one(1, 2)); diff --git a/source/test/fixtures/test-non-barrel-main/package.json b/source/test/fixtures/test-non-barrel-main/package.json new file mode 100644 index 00000000..5170b354 --- /dev/null +++ b/source/test/fixtures/test-non-barrel-main/package.json @@ -0,0 +1,9 @@ +{ + "name": "foo", + "main": "foo.js", + "typings": "foo.d.ts", + "files": [ + "foo.js", + "foo.d.ts" + ] +} diff --git a/source/test/test.ts b/source/test/test.ts index a6d5a45a..cb567842 100644 --- a/source/test/test.ts +++ b/source/test/test.ts @@ -49,6 +49,24 @@ test('return no diagnostics', async t => { t.true(diagnostics.length === 0); }); +test('support non-barrel main', async t => { + const diagnostics = await m({cwd: path.join(__dirname, 'fixtures/test-non-barrel-main')}); + + t.true(diagnostics.length === 0); +}); + +test('support non-barrel main using `types` property', async t => { + const diagnostics = await m({cwd: path.join(__dirname, 'fixtures/test-non-barrel-main-via-types')}); + + t.true(diagnostics.length === 0); +}); + +test('support testing in sub-directories', async t => { + const diagnostics = await m({cwd: path.join(__dirname, 'fixtures/test-in-subdir')}); + + t.true(diagnostics.length === 0); +}); + test('support top-level await', async t => { const diagnostics = await m({cwd: path.join(__dirname, 'fixtures/top-level-await')});