Skip to content

Commit

Permalink
Ignore errors from libs in node_modules folders (#110)
Browse files Browse the repository at this point in the history
Fixes #45
Fixes #76
  • Loading branch information
BendingBender authored Jun 1, 2021
1 parent fc871a1 commit 3b61da7
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 8 deletions.
9 changes: 2 additions & 7 deletions source/lib/assertions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export enum Assertion {
}

// List of diagnostic handlers attached to the assertion
const assertionHandlers = new Map<string, Handler | Handler[]>([
const assertionHandlers = new Map<Assertion, Handler>([
[Assertion.EXPECT_TYPE, isIdentical],
[Assertion.EXPECT_NOT_TYPE, isNotIdentical],
[Assertion.EXPECT_NOT_ASSIGNABLE, isNotAssignable],
Expand All @@ -39,12 +39,7 @@ export const handle = (typeChecker: TypeChecker, assertions: Map<Assertion, Set<
continue;
}

const handlers = Array.isArray(handler) ? handler : [handler];

// Iterate over the handlers and invoke them
for (const fn of handlers) {
diagnostics.push(...fn(typeChecker, nodes));
}
diagnostics.push(...handler(typeChecker, nodes));
}

return diagnostics;
Expand Down
5 changes: 4 additions & 1 deletion source/lib/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ export const getDiagnostics = (context: Context): Diagnostic[] => {
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;
}

Expand Down
4 changes: 4 additions & 0 deletions source/test/fixtures/exclude-node-modules/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default function (foo: number): number | null;

export type Foo = Bar;

3 changes: 3 additions & 0 deletions source/test/fixtures/exclude-node-modules/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports.default = foo => {
return foo > 0 ? foo : null;
};
4 changes: 4 additions & 0 deletions source/test/fixtures/exclude-node-modules/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {expectType} from '../../..';
import aboveZero from '.';

expectType<number | null>(aboveZero(1));
8 changes: 8 additions & 0 deletions source/test/fixtures/exclude-node-modules/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "foo",
"tsd": {
"compilerOptions": {
"noLib": true
}
}
}
45 changes: 45 additions & 0 deletions source/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.'});
Expand Down Expand Up @@ -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\'.']
]);
});

0 comments on commit 3b61da7

Please sign in to comment.