From 3b61da70854697aa2e0098c5d580820baf5ee564 Mon Sep 17 00:00:00 2001 From: Dimitri B Date: Tue, 1 Jun 2021 10:13:55 +0200 Subject: [PATCH] Ignore errors from libs in `node_modules` folders (#110) Fixes #45 Fixes #76 --- source/lib/assertions/index.ts | 9 +--- source/lib/compiler.ts | 5 ++- .../fixtures/exclude-node-modules/index.d.ts | 4 ++ .../fixtures/exclude-node-modules/index.js | 3 ++ .../exclude-node-modules/index.test-d.ts | 4 ++ .../exclude-node-modules/package.json | 8 ++++ source/test/test.ts | 45 +++++++++++++++++++ 7 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 source/test/fixtures/exclude-node-modules/index.d.ts create mode 100644 source/test/fixtures/exclude-node-modules/index.js create mode 100644 source/test/fixtures/exclude-node-modules/index.test-d.ts create mode 100644 source/test/fixtures/exclude-node-modules/package.json diff --git a/source/lib/assertions/index.ts b/source/lib/assertions/index.ts index a90ae749..3c9d8239 100644 --- a/source/lib/assertions/index.ts +++ b/source/lib/assertions/index.ts @@ -13,7 +13,7 @@ export enum Assertion { } // List of diagnostic handlers attached to the assertion -const assertionHandlers = new Map([ +const assertionHandlers = new Map([ [Assertion.EXPECT_TYPE, isIdentical], [Assertion.EXPECT_NOT_TYPE, isNotIdentical], [Assertion.EXPECT_NOT_ASSIGNABLE, isNotAssignable], @@ -39,12 +39,7 @@ export const handle = (typeChecker: TypeChecker, assertions: Map { const expectedErrorsLocationsWithFoundDiagnostics: Location[] = []; for (const diagnostic of tsDiagnostics) { - if (!diagnostic.file) { + /* Filter out all diagnostic messages without a file or from node_modules directories, files under + * node_modules are most definitely not under test. + */ + if (!diagnostic.file || /[/\\]node_modules[/\\]/.test(diagnostic.file.fileName)) { continue; } diff --git a/source/test/fixtures/exclude-node-modules/index.d.ts b/source/test/fixtures/exclude-node-modules/index.d.ts new file mode 100644 index 00000000..98f2e199 --- /dev/null +++ b/source/test/fixtures/exclude-node-modules/index.d.ts @@ -0,0 +1,4 @@ +export default function (foo: number): number | null; + +export type Foo = Bar; + diff --git a/source/test/fixtures/exclude-node-modules/index.js b/source/test/fixtures/exclude-node-modules/index.js new file mode 100644 index 00000000..0d06f8dd --- /dev/null +++ b/source/test/fixtures/exclude-node-modules/index.js @@ -0,0 +1,3 @@ +module.exports.default = foo => { + return foo > 0 ? foo : null; +}; diff --git a/source/test/fixtures/exclude-node-modules/index.test-d.ts b/source/test/fixtures/exclude-node-modules/index.test-d.ts new file mode 100644 index 00000000..aadb8043 --- /dev/null +++ b/source/test/fixtures/exclude-node-modules/index.test-d.ts @@ -0,0 +1,4 @@ +import {expectType} from '../../..'; +import aboveZero from '.'; + +expectType(aboveZero(1)); diff --git a/source/test/fixtures/exclude-node-modules/package.json b/source/test/fixtures/exclude-node-modules/package.json new file mode 100644 index 00000000..132da0f4 --- /dev/null +++ b/source/test/fixtures/exclude-node-modules/package.json @@ -0,0 +1,8 @@ +{ + "name": "foo", + "tsd": { + "compilerOptions": { + "noLib": true + } + } +} diff --git a/source/test/test.ts b/source/test/test.ts index d949c146..af372ff0 100644 --- a/source/test/test.ts +++ b/source/test/test.ts @@ -2,6 +2,7 @@ import * as path from 'path'; import test from 'ava'; import {verify} from './fixtures/utils'; import tsd from '..'; +import {Diagnostic} from '../lib/interfaces'; test('throw if no type definition was found', async t => { await t.throwsAsync(tsd({cwd: path.join(__dirname, 'fixtures/no-tsd')}), {message: 'The type definition `index.d.ts` does not exist. Create one and try again.'}); @@ -313,3 +314,47 @@ test('includes extended config files along with found ones', async t => { [6, 64, 'error', 'Not all code paths return a value.'], ]); }); + +test('errors in libs from node_modules are not reported', async t => { + const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/exclude-node-modules')}); + + const [nodeModuleDiagnostics, testFileDiagnostics, otherDiagnostics] = diagnostics.reduce( + ([nodeModuleDiags, testFileDiags, otherDiags], diagnostic) => { + if (/[/\\]node_modules[/\\]/.test(diagnostic.fileName)) { + nodeModuleDiags.push(diagnostic); + } else if (/[/\\]fixtures[/\\]exclude-node-modules[/\\]/.test(diagnostic.fileName)) { + testFileDiags.push(diagnostic); + } else { + otherDiags.push(diagnostic); + } + return [nodeModuleDiags, testFileDiags, otherDiags]; + }, + [[], [], []] as Diagnostic[][] + ); + + t.deepEqual( + nodeModuleDiagnostics.length, + 0, + 'There must be no errors from node_modules folders when standard lib is not available (option `"noLib": true`).', + ); + + t.true( + otherDiagnostics.length > 0, + 'There must be errors from tsd lib when standard lib is not available (option `"noLib": true`).', + ); + + const alloweOtherFileFailures = [ + /[/\\]lib[/\\]index.d.ts$/, + /[/\\]lib[/\\]interfaces.d.ts$/, + ]; + otherDiagnostics.forEach(diagnostic => { + t.true( + alloweOtherFileFailures.some(allowedFileRe => allowedFileRe.test(diagnostic.fileName)), + `Found diagnostic from an unexpected file: ${diagnostic.fileName} - ${diagnostic.message}`, + ); + }); + + verify(t, testFileDiagnostics, [ + [3, 18, 'error', 'Cannot find name \'Bar\'.'] + ]); +});